【问题标题】:Convolutional Auto Encoder Decoder error in KerasKeras中的卷积自动编码器解码器错误
【发布时间】:2020-06-11 01:06:04
【问题描述】:

我正在尝试训练卷积自动编码器来重新创建 80X130 的图像

我已经添加了所有必要的导入,我正在用 python 3.7 编写它

这是我得到的错误:

Traceback(最近一次调用最后一次):

文件“CAED_Keras.py”,第 52 行,在“,第 52 行,在 validation_data=(x_train, x_train)) ,适合

文件“C:\Python37\lib\site-packages\keras\engine\training.py”,第 1154 行,适合 _standardize_user_data batch_size=batch_size)

文件“C:\Python37\lib\site-packages\keras\engine\training.py”,第 621 行,e 145,在 _standardize_user_data 中的 standardize_input_data exception_prefix='target') , 76, 1) 但得到了形状为 (1, 80, 130) 的数组

standardize_input_data 中的文件“C:\Python37\lib\site-packages\keras\engine\training_utils.py”,第 145 行 str(data_shape))

ValueError: 检查目标时出错:预期 conv2d_7 的形状为 (4, 76, 1) 但得到的数组为 形状 (1, 80, 130)

这是我的代码:

input_img = Input(shape=(80, 130, 1)) 

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

#Load Data
x_train = np.copy(lt.mel_spect_out[:int(len(lt.mel_spect_out)/10*9)])
x_test = np.copy(lt.mel_spect_out[int(len(lt.mel_spect_out)/10*9):])

#normalize
x_train = x_train / 255.
x_test = x_test / 255.


x_train = np.reshape(x_train, (len(x_train), 80, 130, 1))  
x_test = np.reshape(x_test, (len(x_test), 80, 130, 1))  

autoencoder.fit(x_train, x_train,
            epochs=50,
            batch_size=30,
            shuffle=True,
            validation_data=(x_train, x_train))

【问题讨论】:

    标签: python-3.x machine-learning keras autoencoder


    【解决方案1】:

    你有几个问题:

    网络架构

    我改变了一些东西:首先,我在编码后添加了一个上采样层,并删除了最后一个以保持对称性。然后,我将padding='same' 添加到最后的 16-filters conv 层,以防止您的数据被“裁剪”。

    这是生成的代码:

    x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    encoded = MaxPooling2D((2, 2), padding='same')(x)
    
    x = UpSampling2D((2, 2))(encoded)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
    decoded = Conv2D(1, (3, 3), activation='relu', padding='same')(x)
    

    输入形状

    这里的问题是您想对数据进行 3 次下采样。

    第一个维度没问题:80 % 2^3 = 0

    但是对于第二个:130 % 2^3 = 2,这是有问题的。

    你能做什么?

    • 您可以将图片的高度裁剪为 128
    • 我暂时没有其他想法

    最后一点

    你用过

    validation_data=(x_train, x_train)
    

    【讨论】:

    • 将图像尺寸调整为 80x128 后出现错误 -> “检查目标时出错:预期 conv2d_7 的形状为 (76, 124, 1) 但数组的形状为 (80, 128, 1) )"
    • 你是否也将padding='same' 设置为最后的 16-filters conv 层?我在我的机器上测试过,效果很好。
    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多