I am a student beginning study ML. what i'm doing was using ResNet50 transfer-learning for simple classification with Keras. the result output i want is just "face or not ( like this )". i really don't care about model's performance. but my accuracy always stays 0.5 when training. what's wrong with my code? please help.
data is like:
X = face_images + animal_images # 10 face_images and 10 animal_images
# if image is face :1, else: 0
y = [[1]]* len(face_images) + [[0]] * len(animal_images)
X = np.array(X)
y = np.array(y)
print(X.shape, y.shape)
(30, 75, 75, 3) (30, 1)
my pre-trained model is:
# pre-trained model
input_layer = layers.Input(shape=(75,75,3))
base_model = ResNet50(weights='imagenet',
input_tensor=input_layer,
include_top=False)
base_model.summary()
and transfer-model:
# transfer
# flatten last layer, and added dense layer
last_layer = base_model.output
flatten = layers.Flatten()(last_layer)
my_layer = layers.Dense(256, activation='relu')(flatten)
my_layer = layers.Dense(256, activation='relu')(my_layer)
output_layer = layers.Dense(1,activation='sigmoid')(my_layer)
# input_layer = layers.Input(shape="75,75,3")
model = Model(inputs=input_layer, outputs=output_layer)
model.summary()
and compiled with:
model.compile(loss='mse', optimizer='adam',metrics=['accuracy'])
print("model compile completed.")
result of history = model.fit(X_p, y, epochs=100, shuffle=True)
Epoch 1/100
1/1 [==============================] - 4s 4s/step - loss: 0.2589 - accuracy: 0.5000
Epoch 2/100
1/1 [==============================] - 1s 613ms/step - loss: 0.2570 - accuracy: 0.5000
Epoch 3/100
1/1 [==============================] - 1s 611ms/step - loss: 0.2553 - accuracy: 0.5000
Epoch 4/100
1/1 [==============================] - 1s 612ms/step - loss: 0.2538 - accuracy: 0.5000
Epoch 5/100
1/1 [==============================] - 1s 620ms/step - loss: 0.2526 - accuracy: 0.5000
Epoch 6/100
1/1 [==============================] - 1s 610ms/step - loss: 0.2516 - accuracy: 0.5000
.
.
.
Epoch 96/100
1/1 [==============================] - 1s 608ms/step - loss: 0.2500 - accuracy: 0.5000
Epoch 97/100
1/1 [==============================] - 1s 607ms/step - loss: 0.2500 - accuracy: 0.5000
Epoch 98/100
1/1 [==============================] - 1s 925ms/step - loss: 0.2500 - accuracy: 0.5000
Epoch 99/100
1/1 [==============================] - 1s 916ms/step - loss: 0.2500 - accuracy: 0.5000
Epoch 100/100
1/1 [==============================] - 1s 913ms/step - loss: 0.2500 - accuracy: 0.3333
I solved it:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.