在Keras上微调InceptionV3模型

时间:2018-12-28 20:09:50

标签: tensorflow machine-learning keras conv-neural-network

我是DL的新手。 我正在尝试使用InceptionV3模型并对其进行微调,以将其用作二进制分类器。 我的代码如下:

models=keras.applications.inception_v3.InceptionV3(weights='imagenet',include_top= False)


# add a global spatial average pooling layer
x = models.output
#x = GlobalAveragePooling2D()(x)
# add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(2, activation='softmax')(x)

# this is the model we will train
model = Model(input=models.input, output=predictions)

for layer in model.layers[:len(model.layers)-2]:
    layer.trainable = False
for layer in model.layers[-2:]:
    layer.trainable = True

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, epochs=nb_epoch,
          verbose=1,
          validation_split=0.25,
          class_weight='auto')

X火车形状:(80,299,299,3)

X_test形状:(20,299,299,3)

y_train形状:(80,2)

y_test形状:(20,2)

但是我收到一个值错误。

    ValueError                                Traceback (most recent call last)
<ipython-input-9-c06b0b388969> in <module>
    217 
    218     model = cnn_model(X_train, y_train, kernel_size, nb_filters, channels, nb_epoch, batch_size,
--> 219                       nb_classes)
    220 
    221     print("Predicting")

<ipython-input-9-c06b0b388969> in cnn_model(X_train, y_train, kernel_size, nb_filters, channels, nb_epoch, batch_size, nb_classes)
    152               verbose=1,
    153               validation_split=0.25,
--> 154               class_weight='auto')
    155 
    156     return model

~\Anaconda3\envs\tf_gpu\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

~\Anaconda3\envs\tf_gpu\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    787                 feed_output_shapes,
    788                 check_batch_axis=False,  # Don't enforce the batch size.
--> 789                 exception_prefix='target')
    790 
    791             # Generate sample-wise weight values given the `sample_weight` and

~\Anaconda3\envs\tf_gpu\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    126                         ': expected ' + names[i] + ' to have ' +
    127                         str(len(shape)) + ' dimensions, but got array '
--> 128                         'with shape ' + str(data_shape))
    129                 if not check_batch_axis:
    130                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_7 to have 4 dimensions, but got array with shape (80, 2)

我遇到了这个答案https://stackoverflow.com/a/36842553,其中OP提到必须更改3个分类层才能实现。在Keras中有什么方法可以做同样的事情?

是否有更好的方法使用InceptionV3模型进行分类?

1 个答案:

答案 0 :(得分:0)

您没有Flatten进行预测的张量会导致抛出此类异常。您模型中的输出张量的形状为:

Tensor("dense_1/truediv:0", shape=(?, ?, ?, 2), dtype=float32)

虽然标签的形状为[80,2]如何解决此问题?

在将初始输出传递到分类器之前,先展平张量:

import tensorflow as tf
from tensorflow.python.keras import Model
from tensorflow.python.keras.layers import Dense, Flatten, Input

inps = Input(shape=(299, 299, 3), name='image_input')
m = tf.keras.applications.inception_v3.InceptionV3(weights='imagenet', include_top=False)(inps)
x = Flatten()(m)
x = Dense(1024, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)

model = Model(inputs=inps, outputs=predictions)
model.compile('adam', 'mse')