【问题标题】:Keras: Error when checking inputKeras:检查输入时出错
【发布时间】:2017-05-12 19:14:15
【问题描述】:

我正在使用带有 Theano 后端的 Keras 自动编码。并想为 720x1080 RGB 图像进行自动编码。 这是我的代码

from keras.datasets import mnist
import numpy as np
from keras.layers import Input, LSTM, RepeatVector, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model

from PIL import Image


x_train = []
x_train_noisy = []

for i in range(5,1000):
    image = Image.open('data/trailerframes/frame' + str(i) + '.jpg', 'r')
    x_train.append(np.array(image))
    image = Image.open('data/trailerframes_avg/frame' + str(i) + '.jpg', 'r')
    x_train_noisy.append(np.array(image))


x_train = np.array(x_train)
x_train = x_train.astype('float32') / 255.
x_train_noisy = np.array(x_train_noisy)
x_train_noisy = x_train_noisy.astype('float32') / 255.


input_img = Input(shape=(720, 1080, 3))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(32, (3, 3), data_format="channels_last", activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), data_format="channels_last", activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)

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

autoencoder.fit(x_train_noisy, x_train,
            epochs=10,
            batch_size=128,
            shuffle=True,
            validation_data=(x_train_noisy, x_train))

但它给了我一个错误

ValueError: 检查输入时出错:预期 input_7 的形状为 (None, 720, 1080, 3) 但得到的数组的形状为 (995, 720, 1280, 3)

【问题讨论】:

    标签: keras theano autoencoder


    【解决方案1】:

    输入错误:

    就这么简单:

    • 您将输入定义为 (720,1080,3)
    • 您正在尝试使用 (720,1280,3) 形式的数据来试用您的模型

    其中一个是错误的,我认为是输入中的错字:

    #change 1080 for 1280
    input_img = Input(shape=(720, 1280, 3))
    

    输出错误(目标):

    现在,您的目标数据形状类似于 (720,1280,3),最后一层输出 (720,1280,1)

    一个简单的解决方法是:

    decoded = Conv2D(3, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)
    

    使用编码器:

    训练该模型后,您可以创建仅使用编码器或解码器的子模型:

    encoderModel = Model(input_img, decoded)    
    
    decoderInput = Input((shape of the encoder output))    
    decoderModel = Model(decoderInput,decoded))
    

    这两个模型将共享整个模型的完全相同的权重,训练一个模型将影响所有三个模型。

    对于未经培训使用它们,您可以使用model.predict(data),它将为您提供未经培训的结果。

    【讨论】:

    • ValueError: 检查目标时出错:预期 conv2d_78 的形状为 (None, 720, 1280, 1) 但得到的数组形状为 (995, 720, 1280, 3)
    • 现在您已将 3 更改为 1.... 您必须为该消息的两侧设置相同的形状。
    • 我在哪里将 3 更改为 1 ?我只是将输入形状的 1080 更改为 1280
    • 阅读您评论中的信息。它说(无、720、1280、1),应该是3
    • 好的。我明白:) 但我的代码中没有使用任何 1 。也许它来自 Conv2D ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多