Keras Python多图像输入形状错误 [英] Keras Python Multi Image Input shape error

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

问题描述

我正在尝试教自己建立一个CNN,该CNN可以将多个图像作为输入.由于我创建的用于测试数据集的数据量很大,从长远来看,我希望解决涉及非常大的数据集的问题,因此,我正在使用生成器将图像读取到数组中,然后将其传递给Keras Model的fit_generator函数.

I am trying to teach myself to build a CNN that takes more than one image as an input. Since the dataset I created to test this is large and in the long run I hope to solve a problem involving a very large dataset, I am using a generator to read images into arrays which I am passing to Keras Model's fit_generator function.

当我独立运行发电机时,它可以正常工作,并产生适当形状的输出.它产生一个包含两个条目的元组,第一个条目的形状为(4, 100, 100, 1),第二个条目的形状为(4, ).

When I run my generator in isolation it works fine, and produces outputs of the appropriate shape. It yields a tuple containing two entries, the first of which has shape (4, 100, 100, 1) and the second of which has shape (4, ).

阅读有关多个输入Keras CNN的信息给我的印象是,这是用于4个输入CNN的生成器的正确格式,该生成器可以识别4个输入中的哪个包含图像.

Reading about multiple input Keras CNNs has given me the impression that this is the right format for a generator for a 4 input CNN that is identifying which of the 4 inputs contains an image.

但是,当我运行代码时,我得到了:

However, when I run the code I get:

"ValueError: Error when checking input: expected input_121 to have 4 dimensions, but got array with shape (100, 100, 1)"

我一直在寻找解决方案已有一段时间,我怀疑问题出在将我的(100, 100, 1)形状数组作为(None, 100, 100, 1)形状数组发送到输入.

I've been searching for a solution for some time now and I suspect that the problem lies in getting my (100, 100, 1) shape arrays to be sent to the Inputs as (None, 100, 100, 1) shape arrays.

但是当我尝试修改生成器的输出时,出现关于尺寸为5的错误,这是一个错误,因为生成器的输出应采用X, y = [X1, X2, X3, X4], [a, b, c, d]形式,其中Xn具有形状(100, 100, 1)和a/b/c/d是数字.

But when I tried to modify the output of my generator I get an error about having dimension 5, which makes sense as an error because the output of the generator should have the form X, y = [X1, X2, X3, X4], [a, b, c, d], where Xn has shape (100, 100, 1), and a/b/c/d are numbers.

这是代码:

https://gist.github.com/anonymous/d283494aee982fbc30f3b52f2a6f422c

提前谢谢!

推荐答案

您正在使用错误的尺寸在生成器中创建数组列表.

You are creating a list of arrays in your generator with the wrong dimensions.

如果您想要正确的形状,请将单个图像重塑为4个尺寸:(n_samplesx_sizey_sizen_bands),您的模型可以使用.在您的情况下,您应该将图像重塑为(1, 100, 100, 1).

If you want the correct shape, reshape individual images to have the 4 dimensions: (n_samples, x_size, y_size, n_bands) your model will work. In your case you should reshape your images to (1, 100, 100, 1).

最后,将它们与np.vstack堆叠在一起.生成器将生成形状为(4, 100, 100, 1)的数组.

At the end stack them with np.vstack. The generator will yield an array of shape (4, 100, 100, 1).

检查此修改后的代码是否有效

def input_generator(folder, directories):

    Streams = []
    for i in range(len(directories)):
        Streams.append(os.listdir(folder + "/" + directories[i]))
        for j in range(len(Streams[i])):
            Streams[i][j] = "Stream" + str(i + 1) + "/" + Streams[i][j]   
        Streams[i].sort()


    length = len(Streams[0])
    index = 0
    while True:
        X = []
        y = np.zeros(4)
        for Stream in Streams:
            image = load_img(folder + '/' + Stream[index], grayscale = True)
            array = img_to_array(image).reshape((1,100,100,1))
            X.append(array)
        y[int(Stream[index][15]) - 1] = 1
        index += 1
        index = index % length
        yield np.vstack(X), y

这篇关于Keras Python多图像输入形状错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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