【问题标题】:expected conv2d input to have shape (5665,445,3) but got aray with shape (1,445,3)预期 conv2d 输入具有形状 (5665,445,3) 但得到形状为 (1,445,3) 的数组
【发布时间】:2020-09-15 00:40:44
【问题描述】:

我有输入形状的数据 (5665,445,3),但是当我运行我的代码时,我得到了这个错误 expected conv2d input to have shape (5665,445,3) but got aaray with shape (1,445,3) 我不知道为什么。我知道为什么会出现此错误以及如何解决它吗??

代码

def generate_arrays_for_training(indexPat, paths, start=0, end=100):     
    while True:
        from_=int(len(paths)/100*start)
        to_=int(len(paths)/100*end)
        for i in range(from_, int(to_)):
            f=paths[i]
            x = np.load(PathSpectogramFolder+f)
            x=x[:,:,:-1] #3channels
            x=np.array([x])
            x=x.swapaxes(0,1)
            if('P' in f):
                y = np.repeat([[0,1]],x.shape[0], axis=0)
            else:
                y =np.repeat([[1,0]],x.shape[0], axis=0)
            yield(x,y)
def createModel():
    input_shape=(5665, 445, 3)
    model = Sequential()
    model.add(Conv2D(16, ( 5, 5), strides=( 2, 2), padding='same',activation='relu',data_format= "channels_last", input_shape=input_shape))
    model.add(keras.layers.MaxPooling2D(pool_size=( 2, 2),data_format= "channels_last",  padding='same'))
    model.add(BatchNormalization())
    model.add(Conv2D(32, ( 3, 3), strides=( 1,1), padding='same',data_format= "channels_last",  activation='relu'))
    model.add(keras.layers.MaxPooling2D(pool_size=(2, 2),data_format= "channels_last",padding='same' ))
    model.add(BatchNormalization())
    model.add(Conv2D(64, (3, 3), strides=(1,1), padding='same',data_format= "channels_last",  activation='relu'))
    model.add(keras.layers.MaxPooling2D(pool_size=(2, 2),data_format= "channels_last",padding='same' ))
    model.add(BatchNormalization())
    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(256, activation='sigmoid'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))
return model

【问题讨论】:

  • 这是您的数据处理问题。检查您的输入数据形状。
  • @ashraful 怎么可能是处理问题?什么意思??
  • 在训练时,您将赋予图像 (1,445,3) 形状

标签: python keras deep-learning conv-neural-network convolution


【解决方案1】:

为什么形状为 (1,445,3) 的错误数组

检查您的代码后,我发现您的函数generate_arrays_for_training 将返回x.shape is (5665, 1, 445, 3) 的形状。这看起来像是发生在x=x.swapaxes(0,1) 行,它交换了第一维和第二维的空间。

注释掉该行会返回更好的响应 x.shape is (1, 5665, 445, 3)

工作代码

我已经重写并简化了您的生成器,并提供了测试它的支持代码

import numpy as np

def make_training():
    for i in range(10):
        name = f'data/item{i}'
        np.save(name, np.zeros([5665,445,4]))
        yield name+'.npy'

paths = [ _ for _ in make_training() ]
PathSpectogramFolder ='./'

def generate_arrays_for_training(paths):     
    while True:
        for path in paths:
            x = np.load(PathSpectogramFolder+path)
            x=x[:,:,:-1] #3channels
            x=np.array([x])
            if('P' in path):
                y = np.repeat([[0,1]],x.shape[0], axis=0)
            else:
                y = np.repeat([[1,0]],x.shape[0], axis=0)
            yield(x,y)

gen = generate_arrays_for_training(paths)
x,y = next(gen)
print('x.shape is',x.shape)
print('y.shape is',y.shape)

输出是这样的:

x.shape is (1, 5665, 445, 3)
y.shape is (1, 2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2021-02-26
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多