我在重塑CNN LSTM模型的数据集时遇到麻烦。假设我的X.shape =(450,224,224,3)和Y.shape =(450,224,224,12),对通道进行了一些解释:3是图像的通道数,而12是语义分割的类数。
现在,我想对其进行重塑,使其变为X.shape =(150,3,224,224,3)和Y.shape =(150,3,224,224,12),以便馈入CNN + LSTM模型。
我已经尝试过使用np.reshape,但似乎无法正常工作。以下是我的尝试。这是我与Keras,Python共同完成的第一个项目,一切对我来说都是新的。希望能收到你们的有用建议。
def getImageArr( path , width , height ):
img = cv2.imread(path, 1)
img = np.float32(cv2.resize(img, ( width , height ))) / 127.5 - 1
return img
def getSegmentationArr( path , nClasses , width , height ):
seg_labels = np.zeros(( height , width , nClasses ))
img = cv2.imread(path, 1)
img = cv2.resize(img, ( width , height ))
img = img[:, : , 0]
for c in range(nClasses):
seg_labels[: , : , c ] = (img == c ).astype(int)
##seg_labels = np.reshape(seg_labels, ( width*height,nClasses ))
return seg_labels
images = os.listdir(dir_img)
images.sort()
segmentations = os.listdir(dir_seg)
segmentations.sort()
X = []
Y = []
for im , seg in zip(images,segmentations) :
if im != '.DS_Store' and seg != '.DS_Store':
X.append( getImageArr(dir_img + im , input_width , input_height ) )
Y.append( getSegmentationArr( dir_seg + seg , n_classes , output_width , output_height ) )
X, Y = np.array(X), np.array(Y)
np.reshape(X, (150,3,224,224,3))
np.reshape(Y, (150,3,224,224,12))
print(X.shape,Y.shape)