【问题标题】:ValueError: expected ndim=3, found ndim=2 after replacing BatchNormalizationValueError: 预期 ndim=3,替换 BatchNormalization 后发现 ndim=2
【发布时间】:2019-11-05 15:37:02
【问题描述】:

我正在使用 keras 和 TensorFlow 1.13.1 在 python 3.7.5 中编程

我想从下面编码的模型中删除批量标准化层:

from keras import backend as K
from keras.callbacks import *
from keras.layers import *
from keras.models import *
from keras.utils import *
from keras.optimizers import Adadelta, RMSprop, Adam, SGD
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard

from config import *


def ctc_lambda_func(args):
    iy_pred, ilabels, iinput_length, ilabel_length = args
    # the 2 is critical here since the first couple outputs of the RNN
    # tend to be garbage:
    iy_pred = iy_pred[:, 2:, :]  # no such influence
    return K.ctc_batch_cost(ilabels, iy_pred, iinput_length, ilabel_length)


def CRNN_model(is_training=True):
    inputShape = Input((width, height, 1), name='input')  # base on         Tensorflow backend
    conv_1 = Conv2D(64, (3, 3), activation='relu', padding='same')(inputShape)
    conv_2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv_1)
    #batchnorm_2 = BatchNormalization()(conv_2)
    pool_2 = MaxPooling2D(pool_size=(2, 2))(conv_2)

    conv_3 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool_2)
    conv_4 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv_3)
    #batchnorm_4 = BatchNormalization()(conv_4)
    pool_4 = MaxPooling2D(pool_size=(2, 2))(conv_4)

    conv_5 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool_4)
    conv_6 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv_5)
    pool_5 = MaxPool2D(pool_size=(2, 2))(conv_6)
    #batchnorm_6 = BatchNormalization()(conv_6)

    #bn_shape = batchnorm_6.get_shape()


    #print(bn_shape)

    #x_reshape = Reshape(target_shape=(int(bn_shape[1]), int(bn_shape[2] * bn_shape[3])))(batchnorm_6)
    #drop_reshape = Dropout(0.25, name='d1')(x_reshape)
    fl_1 = Flatten()(pool_5)
    fc_1 = Dense(256, activation='relu')(fl_1)

    #print(x_reshape.get_shape())
    #print(fc_1.get_shape())

    bi_LSTM_1 = Bidirectional(LSTM(256, return_sequences=True, kernel_initializer='he_normal'), merge_mode='sum')(fc_1)
    bi_LSTM_2 = Bidirectional(LSTM(128, return_sequences=True, kernel_initializer='he_normal'), merge_mode='concat')(bi_LSTM_1)

    #drop_rnn = Dropout(0.3, name='d2')(bi_LSTM_2)

    fc_2 = Dense(label_classes, kernel_initializer='he_normal', activation='softmax')(bi_LSTM_2)

    base_model = Model(inputs=[inputShape], outputs=fc_2) 

    labels = Input(name='the_labels', shape=[label_len], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([fc_2, labels, input_length, label_length])

    if is_training:
        return Model(inputs=[inputShape, labels, input_length, label_length], outputs=[loss_out]), base_model
    else:
        return base_model

但我收到此错误:

Traceback (most recent call last):
  File "C:/Users/Babak/PycharmProjects/CRNN-OCR/captcha-recognition-master1/captcha-recognition-master/training.py", line 79, in <module>
    model, base_model = CRNN_model(is_training=True)
  File "C:\Users\Babak\PycharmProjects\CRNN-OCR\captcha-recognition-master1\captcha-recognition-master\model.py", line 51, in CRNN_model
    bi_LSTM_1 = Bidirectional(LSTM(256, return_sequences=True, kernel_initializer='he_normal'), merge_mode='sum')(fc_1)
  File "C:\Program Files\Python37\lib\site-packages\keras\layers\wrappers.py", line 437, in __call__
    return super(Bidirectional, self).__call__(inputs, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\keras\engine\base_layer.py", line 446, in __call__
    self.assert_input_compatibility(inputs)
  File "C:\Program Files\Python37\lib\site-packages\keras\engine\base_layer.py", line 342, in assert_input_compatibility
    str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer bidirectional_1: expected ndim=3, found ndim=2

Process finished with exit code 1

如何删除已注释的批处理规范层。我注意到我手动删除了丢弃层。所以假设 dropout 被删除了。我毫无问题地删除了辍学层。但是我在删除批量标准化层时遇到了问题

【问题讨论】:

    标签: python tensorflow keras deep-learning batch-normalization


    【解决方案1】:

    根据错误代码,LSTM 层需要 3D 输入张量,但 Dense 仅输出 2D。存在许多可能的修复,但并非所有修复都同样有效:

    • Conv2D 输出 4D 张量,形状为 (samples, height, width, channels)
    • LSTM 期望输入形状为 (samples, timesteps, channels)
    • 因此,您需要以某种方式将(height, width) 维度转换为timesteps

    在现有研究中,图像数据被展平并按顺序处理 - 但是,channels 保持不变。因此,一种可行的方法是使用Reshape 来产生一个 3D 张量形状的(samples, height*width, channels)。最后,由于 Dense 无法处理 3D 数据,您需要 TimeDistributed 包装器,它将应用相同的 Dense 权重来暗淡输入的 1 - 即到 timesteps

    pool_shapes = K.int_shape(pool_5)
    fl_1 = Reshape((pool_shapes[1] * pool_shapes[2], pool_shapes[3]))(pool_5)
    fc_1 = TimeDistributed(Dense(256, activation='relu'))(fl_1)
    

    最后,return_sequences=True 输出一个 3D 张量,而您的输出 Dense 无法处理 - 所以要么使用 return_sequences=False 输出 2D,要么在 Dense 之前插入一个 Flatten

    【讨论】:

    • 我使用前面提到的 3 行代码编辑了我的代码,但得到了这个错误:fl_1 = Reshape(pool_shapes[1] * pool_shapes[2], pool_shapes[3]) # batch axis unaffected TypeError: __init__( ) 接受 2 个位置参数,但给出了 3 个
    • @Babak.Abad 对,忘了多余的括号 - 请参阅更新
    • 感谢您的回答。现在我得到这个错误:str(inputs) + '.层“ValueError: Layer time_distributed_1”的所有输入均使用非符号张量的输入调用。接收类型:。完整输入:[]。该层的所有输入都应该是张量。
    • @Babak.Abad 猜我有点太快了:请参阅更新。如果有任何其他错误,请告诉我
    • @Babak.Abad 不客气。如果问题现在解决了,也可以考虑投票。
    猜你喜欢
    • 2021-04-05
    • 2019-06-22
    • 2021-02-14
    • 2021-04-08
    • 2019-06-04
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多