【问题标题】:Keras wrong image sizeKeras 错误的图像大小
【发布时间】:2017-03-22 10:28:05
【问题描述】:

我想测试我的 CNN 模型对于 test-images 的准确性。以下是将 mha 格式的 Ground-truth 图像转换为 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])

此代码生成 png 格式的 240 X 240 图像。然而,它们中的大多数是低对比度或完全变黑的。继续前进,现在我将这些图像传递给计算知道标记图像类别的函数。

   def predict_image(self, test_img, show=False):
        '''
        predicts classes of input image
        INPUT   (1) str 'test_image': filepath to image to predict on
                (2) bool 'show': True to show the results of prediction, False to return prediction
        OUTPUT  (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels
                (2) if show == True: displays segmentation results
        '''
        imgs = io.imread(test_img,plugin='simpleitk').astype('float').reshape(5,240,240)
        plist = []

        # create patches from an entire slice
        for img in imgs[:-1]:
            if np.max(img) != 0:
                img /= np.max(img)
            p = extract_patches_2d(img, (33,33))
            plist.append(p)
        patches = np.array(zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3])))

        # predict classes of each pixel based on model
        full_pred = keras.utils.np_utils.probas_to_classes(self.model_comp.predict(patches))
        fp1 = full_pred.reshape(208,208)
        if show:
            io.imshow(fp1)
            plt.show
        else:
            return fp1

我收到ValueError: cannot reshape array of size 172800 into shape (5,240,240)。我将 5 更改为 3,以便 3X240X240=172800。但是然后ValueError: Error when checking : expected convolution2d_input_1 to have 4 dimensions, but got array with shape (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。有关背景信息,请参考herehere(是不是由于this 上面代码中的full_predict 发生了变化)我之前的帖子。请参考this 了解为什么这些特定尺寸像 33,33。

【问题讨论】:

    标签: python tensorflow deep-learning keras convolution


    【解决方案1】:

    您应该检查补丁数组的形状。这应该有 4 个维度(nrBatches、nrChannels、宽度、高度)。根据您的错误消息,只有 3 个维度。因此,您似乎将渠道维度与批次维度合并。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 2017-07-27
      • 1970-01-01
      • 2017-07-07
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多