Keras预测对于测试集中的不同类别给出相同的结果

时间:2018-12-17 12:23:37

标签: python tensorflow keras deep-learning transfer-learning

全部。

我正在使用转移学习根据自己的样本构建新模型。 学习框架是Keras 2.0+。我修改了参考此页的代码: 在一组新的课程上微调InceptionV3 https://keras.io/applications/

在培训步骤中没有任何问题。当我使用测试集测试模型时,尽管每张图片都来自不同的类,但它们给出了相同的预测类。示例:

>>> print(preds)
[[0.0000000e+00 4.5558951e-38 0.0000000e+00 0.0000000e+00 6.3798614e-36
  8.4623914e-22 1.0000000e+00 1.0636564e-11]]
>>> print(pred_classes)
6

我测试了8类的10张图片,都给了6类。

有什么建议吗?

培训代码:

from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(8, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['accuracy'])

train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator=train_datagen.flow_from_directory('./TranningSet',
                                                 target_size=(224,224),
                                                 color_mode='rgb',
                                                 batch_size=32,
                                                 class_mode='categorical',
                                                 shuffle=True)

step_size_train=train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
                   steps_per_epoch=step_size_train,
                   epochs=100,
                   use_multiprocessing=True)

最终列车的精度较低,但大约为70%

50/50 [==============================] - 297s 6s/step - loss: 4.2306 - acc: 0.7040
Epoch 99/100
50/50 [==============================] - 303s 6s/step - loss: 3.7681 - acc: 0.7387
Epoch 100/100
50/50 [==============================] - 293s 6s/step - loss: 3.7569 - acc: 0.7443
<keras.callbacks.History object at 0x7fd931756bd0>
>>>

预测代码:

import keras
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
import numpy as np
from keras.models import load_model
from keras.applications.inception_v3 import preprocess_input

model = load_model('/root/AIdetection/Keras/V6.5/20181217_V6.5.h5')


from keras.preprocessing import image
img =image.load_img('/root/AIdetection/Keras/V6.5/TestSet/Healthy50/20181026.06.JPG', target_size=(224, 224))
x = image.img_to_array(img)
x *= (255.0/x.max())
image = np.expand_dims(x, axis = 0)
image = preprocess_input(image)
preds = model.predict(image)
pred_classes = np.argmax(preds)
print(preds)
print(pred_classes)

1 个答案:

答案 0 :(得分:0)

您的训练数据是否平衡,并且在训练之前进行了改组吗?换句话说,您的大多数训练数据是否可能属于6级,并且每次学习都只是简单地预测6级?

还要检查测试仪的格式是否与火车的格式相同。在将训练数据传递到模型之前,您是否要进行任何类型的图像处理?