训练我的CNN时出错:值错误:形状必须为等级2,但为等级4

2024-04-20 12:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设计一个用于图像分割的CNN

模型如下所示:

def create_and_compile_model():
    theModel=models.Sequential([
        Conv2D(8, (3, 3), activation='relu', padding='same',input_shape=(64,64,3)),
        MaxPooling2D((2, 2), padding='same'),            
        Conv2D(16, (3, 3), activation='relu', padding='same'),
        Conv2D(32, (3, 3), activation='relu', padding='same'),
        Conv2D(64, (3, 3), activation='relu', padding='same'),
        UpSampling2D((2, 2)),
        Conv2D(3, (3, 3), activation='softmax', padding='same')
    ])

    theModel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy','top_k_categorical_accuracy'])
    
    return theModel

# Now create and compile it
theModel=create_and_compile_model()

# Print the summary
theModel.summary()

摘要:

Model: "sequential_26"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_104 (Conv2D)          (None, 64, 64, 8)         224       
_________________________________________________________________
max_pooling2d_30 (MaxPooling (None, 32, 32, 8)         0         
_________________________________________________________________
conv2d_105 (Conv2D)          (None, 32, 32, 16)        1168      
_________________________________________________________________
conv2d_106 (Conv2D)          (None, 32, 32, 32)        4640      
_________________________________________________________________
conv2d_107 (Conv2D)          (None, 32, 32, 64)        18496     
_________________________________________________________________
up_sampling2d_27 (UpSampling (None, 64, 64, 64)        0         
_________________________________________________________________
conv2d_108 (Conv2D)          (None, 64, 64, 3)         1731      
=================================================================
Total params: 26,259
Trainable params: 26,259
Non-trainable params: 0

我的数据生成器:

class DataGenerator(Sequence):
    def __init__(self,fileNames,doRandomize=False,imgPath='DATA/IMG',gtPath='DATA/GT',batchSize=10):
        # Store parameters
        self.imgPath=imgPath
        self.gtPath=gtPath
        self.fileNames=fileNames
        self.batchSize=batchSize
        self.doRandomize=doRandomize
        self.numImages=len(self.fileNames)
        self.on_epoch_end()

    def on_epoch_end(self):
        if self.doRandomize:
            random.shuffle(self.fileNames)
    
    def _load_image_pair_(self,imageIndex):
        # Place your code here
        
        img_IMG = skio.imread(os.path.join(self.imgPath, self.fileNames[imageIndex]))
        img_GT = skio.imread(os.path.join(self.gtPath, self.fileNames[imageIndex]))

        #convert to range [0,1]
        theImage = img_as_float(img_IMG)

        #convert to categorical
        gtImage = to_categorical(img_GT)


        # The method returns the modified sample image (within the interval [0,1]) (theImage)
        # and the categorical version of the ground truth (gtImage)
        return theImage,gtImage
            
    # Returns the number ot batches
    def __len__(self):
        return int(np.ceil(float(self.numImages)/float(self.batchSize)))

    # Provides the "theIndex-th" batch
    # Batch format:
    # - X : The data. Numpy array of shape (bs,nr,nc,3)
    # - y : The ground truth. Numpy array of shape (bs,nr,nc,3)
    # Where nb=batch size, nr=num rows, nc=num cols (in this case, nr=nc=64)
    # Since "y" is provided in categorical format the last dimension (3) is
    # the number of classes.
    def __getitem__(self,theIndex):
        X=[]
        y=[]
        bStart=max(theIndex*self.batchSize,0)
        bEnd=min((theIndex+1)*self.batchSize,self.numImages)

        for i in range(bStart,bEnd):
            [curImage,curGT]=self._load_image_pair_(i)
            X.append(curImage)
            y.append(curGT)
        return np.array(X),np.array(y)

因此,如果我这样做:

[X,y]=trainGenerator.__getitem__(0)
print(type(X))
print(X.shape)
print(type(y))
print(y.shape)

我得到:

<class 'numpy.ndarray'>
(10, 64, 64, 3)
<class 'numpy.ndarray'>
(10, 64, 64, 3)

然后,我尝试使用我创建的数据生成器来训练模型:

train = theModel.fit(trainGenerator, epochs=10, validation_data=valGenerator)

我得到了这个错误:

ValueError: Shape must be rank 2 but is rank 4 for '{{node in_top_k/InTopKV2}} = InTopKV2[T=DT_INT64](sequential_26/conv2d_108/Softmax, ArgMax_2, in_top_k/InTopKV2/k)' with input shapes: [?,64,64,3], [?,?,?], [].

我不明白它为什么抱怨形状。因为最后一层是我认为正确的形状(无、64、64、3)。图像是64x64像素,3是像素将具有的3个可能类别

有人能解释一下这个错误吗

谢谢


Tags: theselfnoneimgdefactivationsameshape
1条回答
网友
1楼 · 发布于 2024-04-20 12:26:16

问题是,我在GoogleColab中运行这段代码。默认安装为Tensorflow 2.5

我不得不降级Tensorflow的版本:

!pip install tensorflow==1.15.3

然后,其中一个指标给出了错误。 因此,我将模型编译如下:

theModel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])

相关问题 更多 >