Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为适应自动编码器上的发电机工作,需要进行哪些更改_Python_Keras_Autoencoder - Fatal编程技术网

Python 为适应自动编码器上的发电机工作,需要进行哪些更改

Python 为适应自动编码器上的发电机工作,需要进行哪些更改,python,keras,autoencoder,Python,Keras,Autoencoder,我目前使用的生成器在卷积网络上运行良好。但是,当我使用相同的生成器安装自动编码器时,会出现以下错误 **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.866666

我目前使用的生成器在卷积网络上运行良好。但是,当我使用相同的生成器安装自动编码器时,会出现以下错误

**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]
   ...,**
我的代码如下

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D,       
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
                )
我对生成器所做的唯一更改是将class模式设置为None。将类模式保持为“二进制”也没有帮助。因为fit生成器需要一个元组,所以我尝试将(train_生成器,train_生成器)和(valid_生成器,valid_生成器)作为参数传递给fit_生成器

在这种情况下,我们得到了以下异常

检查模型输入时出错:数据应为Numpy数组,或Numpy数组的列表/目录。发现: 但似乎什么都不管用。不知道我错过了什么。作为keras的新手,任何帮助都将不胜感激

谢谢
SK

看起来,第一个问题是,训练数据也包含要预测的目标值,是吗

无论如何,这就是我编写生成器代码的方式:

class threadsafe_iter:
    """Takes an iterator/generator and makes it thread-safe by
    serializing call to the `next` method of given iterator/generator.
    """
    def __init__(self, it):
        self.it = it
        self.lock = threading.Lock()

    def __iter__(self):
        return self

    def next(self):
        with self.lock:
            return self.it.next()


def threadsafe_generator(f):
    """A decorator that takes a generator function and makes it thread-safe.
    """
    def g(*a, **kw):
        return threadsafe_iter(f(*a, **kw))
    return g

@threadsafe_generator
def myGenerator(batch_size,num_batches,pRandomShifts,autoenc=false):  # write the definition of your data generator
    #(X_train, y_train), (X_test, y_test) = mnist.load_data()
    #y_train = np_utils.to_categorical(y_train,10)
    #X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
    #X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
    #X_train = X_train.astype('float32')
    #X_test = X_test.astype('float32')
    #X_train /= 255
    #X_test /= 255
    #while 1:
    #    for i in range(1875):
    #        yield X_train[i*32:(i+1)*32], y_train[i*32:(i+1)*32]
    #    # print("Came here")
    self.read_hdf5_file = tables.open_file('nli-wordy0.hdf5', mode='r')
    self.X_test = read_hdf5_file.root.xtest[:] #this can make it to the memory
    if (!autoenc):
      self.Y_test = read_hdf5_file.root.ytest[:]
    self.num_rows = read_hdf5_file.root.xtrain.shape()[0]
    print('X_train shape:', read_hdf5_file.root.xtrain.shape)
    if (!autoenc):
      print('Y_train shape:', read_hdf5_file.root.ytrain.shape)
    print('X_test shape:', self.X_test.shape)
    if (!autoenc):
      print('Y_test shape:', self.Y_test.shape)
    print(self.X_test.shape[0], 'test samples')
    #varianta 1
    #while 1:
    #    for i in range(1875):
    #        yield read_hdf5_file.root.xtrain[i*32:(i+1)*32,:,:], y_train[i*32:(i+1)*32,:,:]
    #    # print("Came here")

    xbatchshape=read_hdf5_file.root.xtrain.shape
    xbatchshape[0]=batch_size
    if (!autoenc):
      ybatchshape=read_hdf5_file.root.ytrain.shape
      ybatchshape[0]=batch_size

    xtrbatch = np.empty(xbatchshape,'float32')
    if (!autoenc):
      ytrbatch = np.empty(xbatchshape,'float32')    

    i = 0
    while 1:    
        if (!autoenc):
          yield read_hdf5_file.root.xtrain[i:i+batch_size,:,:],read_hdf5_file.root.ytrain[i:i+batch_size,:,:]
        else:
          yield read_hdf5_file.root.xtrain[i:i+batch_size,:,:],read_hdf5_file.root.xtrain[i:i+batch_size,:,:]
      if i + batch_size > num_batches:
          i = 0
      else:
          i += batch_size

将课堂教学模式更改为:

 class_mode = input
输入
:与输入图像相同的图像(主要用于与自动编码器一起工作)。

可能重复的图像