Reputation: 65
I have implemented an EfficientNet in Keras for a binary problem using image generator. In the test case when i predict the output it return an array with a set of probability but referred to only one class, here the code and the output:
test_image_generator = ImageDataGenerator(
rescale=1./255
)
real_test=test_image_generator.flow_from_directory(
directory='/content/real_test',
target_size=(224, 224),
color_mode="rgb",
batch_size=1,
class_mode=None,
shuffle=False,
#seed=42
)
The output is:
real_test.reset()
from keras.models import load_model
efficient_net_custom_model = load_model('model_efficientnet4.h5',compile=False)
pred = efficient_net_custom_model.predict_(real_test, steps = len(real_test), verbose = 1)
print (pred)
Now when printing the prediction for 4 different images it returns:
[[0.45415235]
[0.52390164]
[0.9999932 ]
[0.99946016]]
Basically only one output probability (I think) for each image, and it is impossible to say which is the actual class. isn't it? How can I do to solve that issue?
Thank you
Edit:
Including model code
def output_custom_model(prebuilt_model):
print(f"Processing {prebuilt_model}")
prebuilt = prebuilt_model(include_top=False,
input_shape=(224, 224, 3),
weights='imagenet')
output = prebuilt.output
output = GlobalMaxPooling2D()(output)
output = Dense(128, activation='relu')(output)
output = Dropout(0.2)(output)
output = Dense(1, activation='sigmoid')(output)
model = Model(inputs=prebuilt.input, outputs=output)
model.compile(optimizer='sgd', loss='binary_crossentropy',
metrics=METRICS)
return model
efficient_net_custom_model = output_custom_model(EfficientNetB4)
filepath='model_efficientnet4.h5'
efficient_net_history =
efficient_net_custom_model.fit_generator(train_generator,
epochs=20,
validation_data=validation_generator,
)
Upvotes: 3
Views: 1368
Reputation: 8117
Your line output = Dense(1, activation='sigmoid')(output)
specifies only a single probability of output. You will need two neurons like a softmax to indicate what you wanted to do.
Upvotes: 0
Reputation: 365
In some type of networks, the binary output is just one and it represents the first class of the training data. We assume that your training data is like this:
img1data, class1
img2data, class1
..
imgNdata, class2
Your network has accepted the class1 as default class and the given results are the scores of that class. So these results show the scores of class1.
[[0.45415235]
[0.52390164]
[0.9999932 ]
[0.99946016]]
Due to binary classification, the first result shows that the score of class1 is 0.45, so the class2 must be 0.55, the image is belongs to the class2. The last result shows that the score of class1 is 0.999, so the class2 must be 0.0006, the image is belong to the class1. And so on...
You can write a method to make these operations and to find the class which the image is belong to.
def find_class(result):
if result >= 0.5:
return "class1"
else:
return "class2"
find_class(result[0])
Upvotes: 2