[英]My ResNet transfer learning model is always stays 0.5 accuracy when training - What's wrong?
我是一名開始學習 ML 的學生。 我正在做的是使用 ResNet50 遷移學習與 Keras 進行簡單分類。 我想要的結果輸出只是“臉與否(像這樣)”。 我真的不在乎模型的表現。 但訓練時我的准確率始終保持在 0.5。 我的代碼有什么問題? 請幫忙。
數據是這樣的:
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)
我的預訓練模型是:
# 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()
和轉移模型:
# 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()
並編譯:
model.compile(loss='mse', optimizer='adam',metrics=['accuracy'])
print("model compile completed.")
歷史結果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
我解決了它:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.