Keras:检查输入时出错 [英] Keras: Error when checking input

查看:80
本文介绍了Keras:检查输入时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Theano后端上使用Keras自动编码.并希望对720x1080 RGB图像进行自动编码. 这是我的代码

I am using Keras autoencodes with Theano backend. And want to make autoencode for 720x1080 RGB images. This is my code

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具有形状(无,720、1080、3)但具有形状(995、720、1280、3)的数组

ValueError: Error when checking input: expected input_7 to have shape (None, 720, 1080, 3) but got array with shape (995, 720, 1280, 3)

推荐答案

输入错误:

很简单:

  • 您将输入定义为(720,1080,3)
  • 您正尝试使用(720,1280,3)格式的数据对模型进行尝试

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

One of them is wrong, and I think it's a typo in the input:

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

输出错误(目标):

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

Now, your target data is shaped like (720,1280,3), and your last layer outputs (720,1280,1)

一个简单的解决方法是:

A simple fix is:

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

使用编码器:

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

After training that model, you can create submodels for using only the encoder or the decoder:

encoderModel = Model(input_img, decoded)    

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

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

These two models will share the exact same weights of the entire model, training one model will affect all three models.

要在未经培训的情况下使用它们,可以使用model.predict(data),这将为您提供未经培训的结果.

For using them without training, you can use model.predict(data), which will give you the results without training.

这篇关于Keras:检查输入时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆