我是ML的新人,想要重做keras tutorial(使用预训练的VGG16模型进行图像识别), 当我对受过训练的模型进行微调时,我的代码也是如此:
# path to the model weights files.
weights_path = 'vgg16_weights.h5'
top_model_weights_path = 'bottleneck_fc_model.h5'
# dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = 'train'
validation_data_dir = 'val'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16
input_tensor = Input(shape=(img_width,img_height,3))
# build the VGG16 network
base_model=applications.VGG16(weights='imagenet',include_top=False,input_tensor=input_tensor)
print('Model loaded.')
# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:])) #8192
top_model.add(Dense(units=256, activation='relu')) #256
top_model.add(Dropout(0.5)) #256
top_model.add(Dense(units=1, activation='sigmoid')) #1
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)
# add the model on top of the convolutional base
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))
# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:25]:
layer.trainable = False
# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
print(model.input_shape)
# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='binary')
validation_generator =test_datagen.flow_from_directory(validation_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='binary')
# fine-tune the model
model.fit_generator(generator=train_generator,steps_per_epoch=batch_size, epochs=epochs, validation_steps=nb_validation_samples)
但是我收到以下错误:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "fine-tune.py", line 126, in <module> validation_steps=nb_validation_samples)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper return func(*args, **kwargs)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 1840, in fit_generator class_weight=class_weight)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 1565, in train_on_batch outputs =self.train_function(ins)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\sitepackages\keras\backend\theano_backend.py", line 1197, in __call__ return self.function(*inputs)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\site-packages\theano\compile\function_module.py", line 898, in __call__ storage_map=getattr(self.fn, 'storage_map', None))
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\site-packages\theano\gof\link.py", line 325, in raise_with_op reraise(exc_type, exc_value, exc_trace)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\sitepackages\six.py", line 685, in reraise raise value.with_traceback(tb)
File "C:\Users\maxim.kukuskin\AppData\Local\Continuum\anaconda3\lib\site-packages\theano\compile\function_module.py", line 884, in __call__ self.fn() if output_subset is None else\
ValueError: Shape mismatch: x has 8192 cols (and 16 rows) but y has 25088 rows (and 64 cols) Apply node that caused the error: Dot22(Reshape{2}.0, dense_1/kernel)
Toposort index: 88
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(16, 8192), (25088, 64)]
Inputs strides: [(32768, 4), (256, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Elemwise{Composite{((i0 + i1) + Abs((i0 + i1)))}}[(0,0)](Dot 22.0, InplaceDimShuffle{x,0}.0)]]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
有人可以帮我解决这个问题吗?顺便说一句,我使用python 3.6,keras 2.06 + Theano作为后端,如果这可以帮助