Keras fit_generator产生异常:generator的输出应为元组(x,y,sample_weight)或(x,y).发现:[[[[0.86666673 [英] Keras fit_generator producing exception: output of generator should be a tuple(x, y, sample_weight) or (x, y). Found: [[[[ 0.86666673

查看:96
本文介绍了Keras fit_generator产生异常:generator的输出应为元组(x,y,sample_weight)或(x,y).发现:[[[[0.86666673的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为非MNIST,非Imagenet数据构建自动编码器.使用 https://blog.keras.io/building-autoencoders-in-keras. html 作为我的基础.但是,出现以下错误.

I am trying to build an autoencoder for non MNIST, non Imagenet data. Using https://blog.keras.io/building-autoencoders-in-keras.html as my base. However, am getting the following error.

**Exception: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: [[[[ 0.86666673  0.86666673  0.86666673 ...,  0.62352943  0.627451
     0.63137257]
   [ 0.86666673  0.86666673  0.86666673 ...,  0.63137257  0.627451
     0.627451  ]
   [ 0.86666673  0.86666673  0.86666673 ...,  0.63137257  0.627451
     0.62352943]
   ...,**

由于这是一个自动编码器,因此在我的数据生成器中,使用的类模式为无".我的代码如下.

Since this is an autoencoder, in my datagenerator, used class mode=None. My code is as follows.

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D,   UpSampling2D,Activation, Dropout, Flatten
from keras.models import Model,Sequential
from keras.preprocessing.image import ImageDataGenerator 
import numpy as np
import os
import h5py


img_width=140 
img_height=140
train_data_dir=r'SitePhotos\train'
valid_data_dir=r'SitePhotos\validation'
input_img = Input(batch_shape=(32,3, img_width, img_width))

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

# at this point the representation is (8, 4, 4) i.e. 128-dimensional

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

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



valid_datagen = ImageDataGenerator(rescale=1./255)
train_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=32,
        class_mode=None,
        shuffle=True)


valid_generator = valid_datagen.flow_from_directory(
        valid_data_dir,
        target_size=(img_width, img_height),
        batch_size=32,
        class_mode=None,
        shuffle=True)

autoencoder.fit_generator(train_generator,
                nb_epoch=50,                
                validation_data=valid_generator,
                samples_per_epoch=113,
                nb_val_samples=32
                )

推荐答案

真正的解决方案显然是@skottapa提出的Keras问题.

The real solution lies in this Keras issue apparently by @skottapa.

https://github.com/fchollet/keras/issues/4260

rodgzilla提供了更新的ImageDataGenerator,它添加了解决该问题的class_mode ='input'.

rodgzilla provided an updated ImageDataGenerator that adds a class_mode='input' that solves the problem.

令人高兴的是,您可以将所做的修改反向移植到较旧的Keras版本.稍作修改的图像模块可以在这里下载:

The nice thing is that you can backport the modification to older Keras versions. A slightly modified image module can be downloaded here:

https://gist.github.com/gsdefender/293db0987a800cf1b103b7777966f8af

这篇关于Keras fit_generator产生异常:generator的输出应为元组(x,y,sample_weight)或(x,y).发现:[[[[0.86666673的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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