2017-07-26 82 views
2

我有一个NP阵列称为X_train具有以下属性的使用Keras Conv2D层:如何使用可变形输入

X_train.shape = (139,) 
X_train[0].shape = (210, 224, 3) 
X_train[1].shape = (220,180, 3) 

换句话说,有139个观测。每张图片都有不同的宽度和高度,但都有3个通道。所以维度应该是(139, None, None, 3)其中None =变量。

由于您没有在图层中包含观察值数量的维数,因此我使用的Conv2D图层使用了input_shape=(None,None,3)。但是,这给我的错误:

expected conv2d_1_input to have 4 dimensions, but got array with shape (139, 1)

我的猜测是,这个问题是输入形状(139,),而不是(139, None, None, 3)。但是我不确定如何转换到那个。

+0

这就是问题所在,我想你会需要一个输入/培训目标在同一时间(批次大小为1),否则您将无法创建具有一致尺寸的阵列 – gionni

+0

为什么不用零填充图像以使它们都具有相似的尺寸? –

+0

@WilmarvanOmmeren好主意 - 有没有这样的功能? – megashigger

回答

3

你的问题的一个可能的解决方案是用零填充数组,使它们都具有相似的大小。之后,您的输入形状将如(139, max_x_dimension, max_y_dimension, 3)

以下功能将做的工作:

import numpy as np 

def fillwithzeros(inputarray, outputshape): 
    """ 
    Fills input array with dtype 'object' so that all arrays have the same shape as 'outputshape' 
    inputarray: input numpy array 
    outputshape: max dimensions in inputarray (obtained with the function 'findmaxshape') 

    output: inputarray filled with zeros 
    """ 
    length = len(inputarray) 
    output = np.zeros((length,)+outputshape, dtype=np.uint8) 
    for i in range(length): 
     output[i][:inputarray[i].shape[0],:inputarray[i].shape[1],:] = inputarray[i] 
    return output 

def findmaxshape(inputarray): 
    """ 
    Finds maximum x and y in an inputarray with dtype 'object' and 3 dimensions 
    inputarray: input numpy array 

    output: detected maximum shape 
    """ 
    max_x, max_y, max_z = 0, 0, 0 
    for array in inputarray: 
     x, y, z = array.shape 
     if x > max_x: 
      max_x = x 
     if y > max_y: 
      max_y = y 
     if z > max_z: 
      max_z = z 
    return(max_x, max_y, max_z) 

#Create random data similar to your data 
random_data1 = np.random.randint(0,255, 210*224*3).reshape((210, 224, 3)) 
random_data2 = np.random.randint(0,255, 220*180*3).reshape((220, 180, 3)) 
X_train = np.array([random_data1, random_data2]) 

#Convert X_train so that all images have the same shape 
new_shape = findmaxshape(X_train) 
new_X_train = fillwithzeros(X_train, new_shape) 
+0

惊人的答案ty – megashigger