python - How to use Keras Conv2D layer with variably shaped input -
i've got np array called x_train following properties:
x_train.shape = (139,) x_train[0].shape = (210, 224, 3) x_train[1].shape = (220,180, 3) in other words, there 139 observations. each image has different width , height, have 3 channels. dimension should (139, none, none, 3) none = variable.
since don't include dimension number of observations in layer, conv2d layer used input_shape=(none,none,3). gives me error:
expected conv2d_1_input have 4 dimensions, got array shape (139, 1)
my guess problem input shape (139,) instead of (139, none, none, 3). i'm not sure how convert however.
one possible solution problem fill arrays zeros have similar size. afterwards, input shape (139, max_x_dimension, max_y_dimension, 3).
the following functions job:
import numpy np def fillwithzeros(inputarray, outputshape): """ fills input array dtype 'object' arrays have same shape 'outputshape' inputarray: input numpy array outputshape: max dimensions in inputarray (obtained function 'findmaxshape') output: inputarray filled zeros """ length = len(inputarray) output = np.zeros((length,)+outputshape, dtype=np.uint8) in range(length): output[i][:inputarray[i].shape[0],:inputarray[i].shape[1],:] = inputarray[i] return output def findmaxshape(inputarray): """ finds maximum x , y in inputarray dtype 'object' , 3 dimensions inputarray: input numpy array output: detected maximum shape """ max_x, max_y, max_z = 0, 0, 0 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 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 images have same shape new_shape = findmaxshape(x_train) new_x_train = fillwithzeros(x_train, new_shape)
Comments
Post a Comment