首页 > 解决方案 > 在 keras 中微调 InceptionV3 模型

问题描述

我是 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_train 形状:(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 模型进行分类?

标签: tensorflowmachine-learningkerasconv-neural-network

解决方案


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

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

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

在将 inception 的输出传递给分类器之前,展平张量:

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')

推荐阅读