如何将数据转换为适当的Numpy维格式?

时间:2018-09-01 17:52:18

标签: python arrays numpy neural-network deep-learning

我正在尝试训练用于图像分类的神经网络。我无法将数据转换为正确的numpy数组格式。为了馈入网络,我的数组必须具有尺寸(9068,184,184,1)。问题是如果我检查数组的长度,它只会返回(9068,)。如果我检查数组中单个元素的长度,它将返回(184,184,1)。如何使整个数组的长度为四维(9068,184,184,1),以便我的神经网络可以将其作为输入?

下面是我的代码。我有一个(9068,2)数据框,其中包含文件名。我正在抓取文件名,将其作为像素信息读入一个数组,然后将其存储到另一个数组中。

path = '/home/vivek/Downloads/kaggle_ndsb2-master/data_segmenter_trainset/'

for ii in pairing_table['image']:    
    new_path = os.path.join(path,ii)
    img = Image.open(new_path)
    print type(ii)

for ii in range(0,len(image_table['image'])):
    new_path = os.path.join(path,image_table['image'][ii])
    img = Image.open(new_path)
    img2 = np.array(img.getdata()).reshape(184, 184, -1)
    #print(type(img))
    image_table['image'][ii] = img2
    img.close()


for ii in range(0,len(image_table['mask'])):
    new_path = os.path.join(path,image_table['mask'][ii])
    img = Image.open(new_path)
    img2 = np.array(img.getdata()).reshape(184, 184, -1)
    image_table['mask'][ii] = img2
    img.close()

print(image_table['image'][0].shape)  #this is returning (184,184,1)
print(image_table['image'].shape)   #this is returning (9068,)   should be (9068,184,184,1)
print(image_table['mask'][0].shape)  #this is returning (184,184,1)
print(image_table['mask'].shape)   #this is returning (9068,)   should be (9068,184,184,1)

0 个答案:

没有答案