【问题标题】:Mismatch in shape size of conv2d layer within autoencoder自编码器中 conv2d 层的形状大小不匹配
【发布时间】:2019-05-03 23:52:24
【问题描述】:

我正在构建(重用)一个卷积自动编码器,但我遇到了最后一个 conv2D 层的问题,因为它预期的形状为 (180,116,1) 但接收的是 (184,120,1)[这是我的图像的形状] .

我做了一些研究,但我没有解决这个问题,有没有人有任何解决方案?

# process the images into data

    import glob
    import pandas as pd
    import numpy as np
    from encoder_utils import prep_data
    import sys
    from sklearn.model_selection import train_test_split

    np.set_printoptions(threshold=np.nan)

    # create a list of XML files within the raw data folder
    image_list = glob.glob("Images/dpi60/*.jpeg")

    # set size of tensor_scope
    tensor_scope = 500

    # Process the images into numpy arrays and return a tensor
    T = prep_data(image_list, all_items = False, less_items = tensor_scope)

    # split into training and testing sets
    labels = image_list[0:tensor_scope]
    data_train, data_test, labels_train, labels_test = train_test_split(T, labels, test_size=0.20, random_state=42)

    # convert to 0-1 floats (reconversion by * 255)
    data_train = data_train.astype('float32') / 255.
    data_test = data_test.astype('float32') / 255.

    # reshape from channels first to channels last
    data_train = np.rollaxis(data_train, 0, 3)
    data_test = np.rollaxis(data_test, 0, 3)

    from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
    from keras.models import Model
    from keras import backend as K

    input_img = Input(shape=(184, 120, 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')

    data_train_dimensions =  data_train.shape
    data_test_dimensions =  data_test.shape

    data_train = np.reshape(data_train, (data_train_dimensions[2], 184, 120, 1))  # adapt this if using `channels_first` image data format
    data_test = np.reshape(data_test, (data_test_dimensions[2], 184, 120, 1))

    from keras.callbacks import TensorBoard
    autoencoder.fit(data_train, data_test,
                    epochs=50,
                    batch_size=128,
                    shuffle=True,
                    validation_data=(data_train, data_test),
                    callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

【问题讨论】:

    标签: python python-3.x tensorflow keras autoencoder


    【解决方案1】:

    您忘记了以下行中的 padding="same"

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

    添加填充=“相同”

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

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      相关资源
      最近更新 更多