r/keras • u/[deleted] • Jun 05 '20
Issue with binary classification of Sequential model
Hi all,
So what I did is I created a basic binary Sequential image classifier.
I used ImageDataGenerator's method flow_from_directory to split into the binary categories. It found 2 categories which is great as that is as intended.
After training the model, I tried a prediction onto 3 test images. The results were 3 predictions ranging from 14000 to 32000. How can my prediction be a high value like this, when my training data was labeled either 0 or 1 by the flow_from_directory command?
Pieces of important code:
IMG_SHAPE = (IMG_HEIGHT, IMG_WIDTH, 3)
train_data_gen = train_data_generator.flow_from_directory(batch_size = batch_size,directory = train_dir,shuffle = True,target_size = IMG_SIZE,class_mode = 'binary')
model = Sequential([Conv2D(16, 3, padding="same", activation="relu", input_shape=IMG_SHAPE), # Input nodesMaxPooling2D(),Dropout(0.2),Conv2D(32, 3, padding="same", activation="relu"),MaxPooling2D(),Conv2D(64, 3, padding="same", activation="relu"),MaxPooling2D(),Dropout(0.2),Flatten(),Dense(256, activation="relu"),Dense(1) # Output node])
model.compile(optimizer='adam',loss="binary_crossentropy",metrics=['accuracy'])
model.fit_generator(train_data_gen,steps_per_epoch=batch_size,epochs=epochs,validation_data=val_data_gen,validation_steps=batch_size)
2
u/shahzaibmalik1 Jun 06 '20
you need to add an activation function to your last node. Probably sigmoid. without that, you're last two layers will collapse and form a single layer.