Ошибка при обучении моего CNN: Ошибка значения: Форма должна быть 2-го ранга, но 4-го ранга

#python #tensorflow #machine-learning #keras #conv-neural-network

Вопрос:

Я разрабатываю 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). Изображения имеют размер 64×64 пикселя, а 3 — это 3 возможных класса, которые будут иметь пиксели.

Может кто-нибудь объяснить эту ошибку, пожалуйста?

Спасибо

Комментарии:

1. где находится тренингенератор defiend?

2. Похоже, у вас неверная форма данных ваших меток.

3. Я отредактировал, чтобы добавить класс генератора данных

Ответ №1:

Проблема заключалась в том, что я запускаю этот код в Google Colab. И по умолчанию установлен Tensorflow 2.5.

Мне пришлось понизить версию Tensorflow:

 !pip install tensorflow==1.15.3
 

И затем, одна из метрик давала ошибку.
Итак, я компилирую модель следующим образом:

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