[英]Error when training my CNN: ValueError: Shape must be rank 2 but is rank 4
I am designing a CNN for image segmentation.我正在设计一个用于图像分割的 CNN。
The model looks as follows: model 如下所示:
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()
The 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
My Data Generator:我的数据生成器:
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)
So, if I do:所以,如果我这样做:
[X,y]=trainGenerator.__getitem__(0)
print(type(X))
print(X.shape)
print(type(y))
print(y.shape)
I get:我得到:
<class 'numpy.ndarray'>
(10, 64, 64, 3)
<class 'numpy.ndarray'>
(10, 64, 64, 3)
Then, I try to train the model with the Data Generator I created:然后,我尝试使用我创建的数据生成器训练 model:
train = theModel.fit(trainGenerator, epochs=10, validation_data=valGenerator)
And I get this error:我得到这个错误:
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], [?,?,?], [].
I don't understand why it complains about the shape.我不明白为什么它抱怨形状。 Since the last layer is the shape I think is the correct (None, 64, 64, 3).
由于最后一层是我认为正确的形状(无、64、64、3)。 The images are 64x64 pixels and the 3 are the 3 possible classes the pixels will have.
图像是 64x64 像素,3 是像素将具有的 3 个可能的类别。
Can someone explain this error please?有人可以解释这个错误吗?
Thanks谢谢
The issue was that I am running this code in Google Colab.问题是我在 Google Colab 中运行此代码。 And it is installed Tensorflow 2.5 by default.
默认安装 Tensorflow 2.5。
I had to downgrade the version of Tensorflow:我不得不降级 Tensorflow 的版本:
!pip install tensorflow==1.15.3
And then, one of the metrics was giving the error.然后,其中一个指标是给出错误。 So, I compile the model as follows:
所以,我编译 model 如下:
theModel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.