I trained my pre-trained model(densenet-121) with my dataset for binary image classification. When I use test_generator results of test_generator look good but when I run my code for prediction I get the output of [0. 1.]
How can I solve this problem?
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
img =
load_image('C:/Users/yurtt/Desktop/orkun/a/b/dataset/test2/not/159.png')
# predict the class
result = model.predict(img)
print(result[0])
output:
[0. 1.]
my model:
from keras.applications.densenet import DenseNet121
base_model = DenseNet121(weights='imagenet', include_top=False, input_tensor=Input(shape=input_shape))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, kernel_regularizer=l2(0.0001), bias_regularizer=l2(0.0001))(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, kernel_regularizer=l2(0.0001), bias_regularizer=l2(0.0001))(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dropout(0.5)(x)
x = Dense(512, kernel_regularizer=l2(0.0001), bias_regularizer=l2(0.0001))(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dropout(0.3)(x)
prediction = Dense(output_classes, activation=tf.nn.softmax)(x)
model = Model(inputs=base_model.input,outputs=prediction)
CodePudding user response:
You defined output_classes = 2
. On the other hand, the prediction as an output layer is defined to prediction = Dense(output_classes, activation=tf.nn.softmax)(x)
. Hence, the output of the prediction has dimension 2. You can keep it as it is, but you need to transform target values to [0 1]
and [1 0]
that mean it is in the class 2
and class 1
, respectively.
Note that, you can also, set output_classes
to 1
to keep target values as 0
and 1
.