[英]How to Proceed with Fine-Tuning and Testing InceptionV3 in Keras?
我正在阅读有关Keras的文档以及此处关于StackOverFlow的先前问题和答案。 目前,这是我到目前为止所拥有的:
#Creating base pre-trained model (Default Input Size for ResNet50 is (299, 299))
base_model = InceptionV3(weights = 'imagenet', include_top = False, input_shape = (299, 299, 3))
#Adding a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
#Adding a fully-connected dense layer
x = Dense(1024, activation = 'relu')(x)
#Adding a logistic layer - We have 2 classes: Cats and Dogs
predictions = Dense(2, activation = 'softmax')(x)
#Model to be trained
model = Model(inputs = base_model.input, outputs = predictions)
#First trains only the top layers
#Freeze all convolutional ResNet50 layers
for layer in base_model.layers:
layer.trainable = False
#Compile the model
model.compile(optimizer = 'rmsprop', loss = 'sparse_categorical_crossentropy')
#Train the model on the new data
train_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_directory("./data/train",
target_size = (299, 299),
batch_size = 25,
class_mode = 'binary')
model.fit_generator(train_generator, steps_per_epoch = 10)
#Train top 2 inception blocks by freezing first 249 layers and unfreezing the rest
for layer in model.layers[:249]:
layer.trainable = False
for layer in model.layers[249:]:
layer.trainable = True
#Now to recompile the model for modifications to take effect, use SGD
model.compile(optimizer = SGD(lr = 0.0001, momentum = 0.9), loss = 'sparse_categorical_crossentropy')
#Train model again, fine-tuning top 2 inception blocks alongside top Dense layers
model.fit_generator(train_generator, steps_per_epoch = 10)
#Test model
imges = glob.glob('./test/*.jpg')
for fname in imges:
img_path = fname
img = image.load_img(img_path, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print('Predicted:', decode_predictions(pred, top=3)[0])
我目前收到一个错误消息,指出“ decode_predictions中的第82行,值错误:'decode_predictions'需要一批预测(例如,二维形状的数组(样本,1000个)。找到的形状为(1、2]的数组) ”
有什么我忘了做的事吗? 我目前机智。 任何和所有帮助将不胜感激。 先感谢您
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.