Python Keras图像大小错误

Python Keras图像大小错误,python,tensorflow,deep-learning,keras,convolution,Python,Tensorflow,Deep Learning,Keras,Convolution,我想测试CNN模型对测试图像的准确性。以下是将mha格式的地面实况图像转换为png格式的代码 def save_labels(fns): ''' INPUT list 'fns': filepaths to all labels ''' progress.currval = 0 for label_idx in progress(xrange(len(fns))): slices = io.imread(fns[label_idx], pl

我想测试CNN模型对
测试图像的准确性。以下是将mha格式的地面实况图像转换为png格式的代码

def save_labels(fns):
    '''
    INPUT list 'fns': filepaths to all labels
    '''
    progress.currval = 0
    for label_idx in progress(xrange(len(fns))):
        slices = io.imread(fns[label_idx], plugin = 'simpleitk')
        for slice_idx in xrange(len(slices)):
        '''
        commented code in order to reshape the image slices. I tried reshaping but it did not work 
        strip=slices[slice_idx].reshape(1200,240)
        if np.max(strip)!=0:
        strip /= np.max(strip)
            if np.min(strip)<=-1:
        strip/=abs(np.min(strip))
        '''
        io.imsave('Labels2/{}_{}L.png'.format(label_idx, slice_idx), slices[slice_idx])
我正在获取
ValueError:无法将172800大小的数组重塑为形状(5240240)
。我将5改为3,这样3X240X240=172800。但随后出现了新问题
ValueError:检查时出错:预期卷积2d\u input\u 1有4维,但得到了形状为(43264,33,33)的数组

我的模型如下所示:

        single = Sequential()
        single.add(Convolution2D(self.n_filters[0], self.k_dims[0], self.k_dims[0], border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg), input_shape=(self.n_chan,33,33)))
        single.add(Activation(self.activation))
        single.add(BatchNormalization(mode=0, axis=1))
        single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1)))
        single.add(Dropout(0.5))
        single.add(Convolution2D(self.n_filters[1], self.k_dims[1], self.k_dims[1], activation=self.activation, border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg)))
        single.add(BatchNormalization(mode=0, axis=1))
        single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1)))
        single.add(Dropout(0.5))
        single.add(Convolution2D(self.n_filters[2], self.k_dims[2], self.k_dims[2], activation=self.activation, border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg)))
        single.add(BatchNormalization(mode=0, axis=1))
        single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1)))
        single.add(Dropout(0.5))
        single.add(Convolution2D(self.n_filters[3], self.k_dims[3], self.k_dims[3], activation=self.activation, border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg)))
        single.add(Dropout(0.25))

        single.add(Flatten())
        single.add(Dense(5))
        single.add(Activation('softmax'))

        sgd = SGD(lr=0.001, decay=0.01, momentum=0.9)
        single.compile(loss='categorical_crossentropy', optimizer='sgd')
        print 'Done.'
        return single

我使用的是keras 1.2.2。请参考和(是因为上面代码中的完整预测发生了变化)了解我之前的文章的背景信息。请参考了解这些特定尺寸(如33,33)的原因

您应该检查面片阵列的形状。这应该有4个尺寸(NR批次、NR通道、宽度和高度)。根据您的错误消息,只有3个维度。因此,您似乎将渠道维度与批处理维度合并了

        single = Sequential()
        single.add(Convolution2D(self.n_filters[0], self.k_dims[0], self.k_dims[0], border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg), input_shape=(self.n_chan,33,33)))
        single.add(Activation(self.activation))
        single.add(BatchNormalization(mode=0, axis=1))
        single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1)))
        single.add(Dropout(0.5))
        single.add(Convolution2D(self.n_filters[1], self.k_dims[1], self.k_dims[1], activation=self.activation, border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg)))
        single.add(BatchNormalization(mode=0, axis=1))
        single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1)))
        single.add(Dropout(0.5))
        single.add(Convolution2D(self.n_filters[2], self.k_dims[2], self.k_dims[2], activation=self.activation, border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg)))
        single.add(BatchNormalization(mode=0, axis=1))
        single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1)))
        single.add(Dropout(0.5))
        single.add(Convolution2D(self.n_filters[3], self.k_dims[3], self.k_dims[3], activation=self.activation, border_mode='valid', W_regularizer=l1l2(l1=self.w_reg, l2=self.w_reg)))
        single.add(Dropout(0.25))

        single.add(Flatten())
        single.add(Dense(5))
        single.add(Activation('softmax'))

        sgd = SGD(lr=0.001, decay=0.01, momentum=0.9)
        single.compile(loss='categorical_crossentropy', optimizer='sgd')
        print 'Done.'
        return single