代码之家  ›  专栏  ›  技术社区  ›  mad

用异常cnn训练自定义图像的奇怪错误

  •  1
  • mad  · 技术社区  · 5 年前

    我有一系列的小图片,我想运行一个例外CNN的培训使用它们。训练集和验证集分别具有以下形状:

    >(63787, 72, 72, 3) 
    
    >(155, 72, 72, 3)
    

    换句话说,我的图像满足71,71,3最小形状的异常输入要求。

    这就是我构建模型的方式

        base_model=xception.Xception(include_top=False, weights=None, input_shape=(72, 72, 3))
        x = base_model.output
        x = Dense(256, activation='relu')(x)
        predictions = Dense(classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
        opt= SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
    

    我就是这样训练模特的

    checkpoint = ModelCheckpoint(filename, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='max', period=self.__model_history_period)
    lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1), cooldown=0, patience=1000, min_lr=0.5e-6)
    early_stopper = EarlyStopping(min_delta=0.0001, patience=10000)
    callbacks_list = [checkpoint, lr_reducer, early_stopper]
    
    datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, vertical_flip=True) 
    datagen.fit(x_train)
     # Fit the model on the batches generated by datagen.flow().
    H=model.fit_generator(datagen.flow(X_train, y_train,batch_size=self.__bs), steps_per_epoch=X_train.shape[0] // self.__bs, validation_data=(x_val, y_val), epochs=self.__epochs, verbose=1, max_q_size=100, callbacks=[checkpoint, lr_reducer, early_stopper])   
    

    但是,当我运行CNN培训时,我有以下错误:

    > Traceback (most recent call last):
      File "esperimento_paper.py", line 86, in <module>
        vgg.run_2D()
      File "Desktop/PhD-Market-Nets/src/classes/VggHandler.py", line 662, in run_2D
        model, H, n_epochs = self.__train_2D(x_train=x_train, y_train=y_train, x_val=x_val, y_val=y_val, index_net=index_net, index_walk=index_walk)
      File "Desktop/PhD-Market-Nets/src/classes/VggHandler.py", line 267, in __train_2D
        callbacks=[checkpoint, lr_reducer, early_stopper])  
      File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
        return func(*args, **kwargs)
      File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator
        initial_epoch=initial_epoch)
      File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training_generator.py", line 144, in fit_generator
        val_x, val_y, val_sample_weight)
      File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
        exception_prefix='target')
      File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training_utils.py", line 128, in standardize_input_data
        'with shape ' + str(data_shape))
    ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (155, 1)
    

    我错过了什么?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Marcin Możejko    5 年前

    问题就在这一行:

    x = base_model.output
    x = Dense(256, activation='relu')(x)
    

    正如你所设定的 include_top=False -然后返回的是形状的特征映射 [number of examples, h, w, number of features] 是的。当你申请 Dense 对于这个特性图-您只将它应用于最后一个维度(它类似于1x1卷积)。这就是为什么输出是 4D 是的。为了克服这种尝试:

    x = base_model.output
    x = Flatten()(x)
    x = Dense(256, activation='relu')(x)
    

    Flatten 会压扁 h 我是说, w number of features 维度到单个维度。多亏了这一点,你的网络应该运行良好。

    另外,你也可以尝试使用 GlobalMaxPooling2D 或者它的平均版本。这将跳过过滤器的空间位置,但将显著降低模型的内存占用率。