3

我正在尝试使用迁移学习在一组新颖的图像上训练 InceptionV3。我遇到了这个问题 - 这显然与输入和输出维度的不匹配有关(我认为),但我似乎无法确定问题)。之前关于 SO 的所有相关帖子都与 VGG16(我已经开始工作)有关。这是我的代码:

 from keras.applications.inception_v3 import InceptionV3
 from keras.models import Model
 from keras.layers import Dense, GlobalAveragePooling2D
 from keras.callbacks import ModelCheckpoint, TensorBoard, CSVLogger, Callback
 from keras.optimizers import SGD
 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(3, activation='softmax')(x)
 model = Model(inputs=base_model.input, output=predictions)

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

 model.compile(optimizer=SGD(lr=0.001, momentum=0.9), loss='sparse_categorical_crossentropy')

 train_dir = 'hrct_data/ExtractedHRCTs/Train'
 validation_dir = 'hrct_data/ExtractedHRCTs/Validation'
 nb_train_samples = 21903
 nb_validation_samples = 6000
 epochs = 30
 batch_size = 256

 train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

 validation_datagen = ImageDataGenerator(
    rescale=1./255)

 train_generator = train_datagen.flow_from_directory(
    train_dir, 
    target_size=(512, 512), 
    batch_size=batch_size,
    class_mode="categorical")

 validation_generator = validation_datagen.flow_from_directory(
    validation_dir,
    target_size=(512, 512), 
    batch_size=batch_size,
    class_mode="categorical")


 model.fit_generator(
    train_generator,
    steps_per_epoch=21903 // batch_size,
    epochs=30,
    validation_data=validation_generator,
    validation_steps=6000 // batch_size)

 model.save_weights('hrct_inception.h5')

这是错误:

---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call last)
 <ipython-input-89-f79a107413cd> in <module>()
     4         epochs=30,
     5         validation_data=validation_generator,
     6         validation_steps=6000 // batch_size)
     7 model.save_weights('hrct_inception.h5')

 /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     86                 warnings.warn('Update your `' + object_name +
     87                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
     88             return func(*args, **kwargs)
     89         wrapper._legacy_support_signature = inspect.getargspec(func)
     90         return wrapper

 /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_q_size, workers, pickle_safe, initial_epoch)
     1888                     outs = self.train_on_batch(x, y,
     1889                                                
     sample_weight=sample_weight,
     1890                                                class_weight=class_weight)
     1891 
     1892                     if not isinstance(outs, list):

 /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight)
     1625             sample_weight=sample_weight,
     1626             class_weight=class_weight,
     1627             check_batch_axis=True)
     1628         if self.uses_learning_phase and not 
                isinstance(K.learning_phase(), int):
     1629             ins = x + y + sample_weights + [1.]

  /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
     1307                                     output_shapes,
     1308                                     check_batch_axis=False,
     1309                                     exception_prefix='target')
     1310         sample_weights = _standardize_sample_weights(sample_weight,
     1311                                                      self._feed_output_names)

  /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
      137                             ' to have shape ' + str(shapes[i]) +
      138                             ' but got array with shape ' +
      139                             str(array.shape))
      140     return arrays
      141 

      ValueError: Error when checking target: expected dense_12 to have shape (None, 1) but got array with shape (256, 3)

任何帮助——即使是让我朝着正确的方向前进,都会有所帮助。

4

1 回答 1

2

我相信错误来自您使用sparse_categorical_crossentropy.

这种损失将您在训练期间提供的目标(“y”)自动编码为单热编码目标。因此,它期望(256,1)您只提供索引的形状目标。

您使用数据生成器提供的内容已经是编码类。所以你作为目标饲料(256,3)......因此错误:

ValueError: Error when checking target: expected dense_12 to have shape (None, 1) but got array with shape (256, 3)

要修复它,请尝试使用 ' categorical_crossentropy' 作为损失函数。这个期望生成器给出的 one-hot 编码向量。

我希望这有帮助 :-)

于 2017-07-14T06:31:52.083 回答