batch_size = x.shape [0] AttributeError:“元组"对象没有属性"shape" [英] batch_size = x.shape[0] AttributeError: 'tuple' object has no attribute 'shape'

查看:587
本文介绍了batch_size = x.shape [0] AttributeError:“元组"对象没有属性"shape"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码结合了图像和遮罩以进行图像检测吗?
我该如何纠正该错误?

This code combine image and mask for image detection?
How can i correct that error?

batch_size = x.shape [0] AttributeError:元组"对象没有属性"shape"

batch_size = x.shape[0] AttributeError: 'tuple' object has no attribute 'shape'

这是用于训练的代码:

train_datagen = ImageDataGenerator(
            rescale=1. / 255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)
train_datagen_1 = ImageDataGenerator(
            rescale=1. / 255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)


train_generator = train_datagen.flow_from_directory(
            train_data_dir,
            target_size=(200, 150),
            batch_size=1
          )
train_generator_1= train_datagen_1.flow_from_directory(
            train_data_dir_1,
            target_size=(200, 150),
            batch_size=1)
train_generator_2 = zip( train_generator, train_generator_1)

model.fit_generator(
            train_generator_2,
            steps_per_epoch=nb_train_samples // batch_size,
            epochs=50)

这是我正在使用的模型:

This is the model I'm using:

model = Sequential() 
model.add(Conv2D(32, (3, 3), input_shape=(200, 150, 3))) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Flatten()) 
model.add(Dense(20)) model.add(Activation('relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(90000)) 
model.add(Activation('sigmoid')) 
model.compile(loss='mse', optimizer='rmsprop', metrics=['accuracy'])

推荐答案

因此,由于您的模型只有一个输出,因此无法像这样加入两个生成器.

So, since your model has only one output, you cannot join two generators like that.

  • 生成器必须在元组中输出类似(input,output)之类的东西.
  • 您正在输出((input1,output1), (input2,output2))元组中的元组.
  • A generator must output things like (input,output) in a tuple.
  • Yours is outputting ((input1,output1), (input2,output2)), tuples inside a tuple.

当模型从生成器中获取批处理时,它会尝试获取其认为是input的形状,但会找到(input,output).

When your model gets a batch from the generator, it's trying to get the shape of what it thinks is the input, but it finds (input,output) instead.

使用生成器:

您可能可以像这样创建自己的生成器:

You can probably create your own generator like this:

def myGenerator(train_generator,train_generator1):

    while True:

        xy = train_generator.next() #or next(train_generator)
        xy1 = train_generator1.next() #or next(train_generator1)
        yield (xy[0],xy1[0])

实例化为:

train_generator2 = myGenerator(train_generator,train_generator1)


现在,您将在输出形状方面遇到真正的麻烦.如果要处理图像之间的图像,建议您使用纯卷积模型.


Now, you're going to have real trouble with the output shapes. If you're working from image to image, I recommend you work with a purely convolutional model.

卷积层输出(Batch, Side1, Side2, channels),这是您在图像中使用的形状.

A convolutional layer outputs (Batch, Side1, Side2, channels), which is the shape you are working with in your images.

但是密集层输出(Batch, size).仅当您稍后使用Reshape((200,150,3))重塑形状以匹配真实图像"时,此方法才有效.

But a dense layer outputs (Batch, size). This can only work if you reshape it later with Reshape((200,150,3)) to match your "true images".

提示:模型中间的密集20可能太小而无法代表整个图像. (当然,这取决于您的任务).

Hint: a Dense 20 in the middle of the model may be too little to represent an entire image. (But of course it depends on your task).

此任务可能的模型是:

Conv
... Maybe more convs
MaxPooling
Conv
... Maybe more convs
MaxPooling
Conv

......

UpSampling
Conv
...
UpSampling
Conv
....

padding='same'进行每次卷积,使您的生活更轻松. (但是,由于一维为150,因此必须在某个点上对值进行填充,因为当达到75时,MaxPooling将删除/添加一个像素(75不能除以2).

Every convolution with padding='same' to make your life easier. (But since you have one dimension being 150, you will have to manage padding the values at some point, because when you reach 75, the MaxPooling will remove/add one pixel (75 cannot be divided by two).

这篇关于batch_size = x.shape [0] AttributeError:“元组"对象没有属性"shape"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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