![](/img/trans.png)
[英]logits and labels must have the same first dimension, got logits shape [1568,10] and labels shape [32]
[英]logits and labels must have the same first dimension, got logits shape [327680,7] and labels shape [983040]
我正在嘗試使用 U-Net 架構執行語義分割。 當我適合 model 時: history = model.fit(imgs_train, masks_train,
batch_size= 5, epochs = 5) 我不斷收到以下錯誤。 imgs_train.shape: (1500, 256, 256, 3) masks_train.shape: (1500, 256, 256, 3)
# Building Unet using encoder and decoder blocks
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, concatenate, Conv2DTranspose,
BatchNormalization, Dropout, Lambda
from keras.layers import Activation, MaxPool2D, Concatenate
def conv_block(input, num_filters=64):
# first conv layer
x = Conv2D(num_filters, kernel_size = (3,3), padding='same')(input)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# second conv layer
x = Conv2D(num_filters, kernel_size= (3,3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
return x
def encoder_block(input, num_filters=64):
# conv block
x = conv_block(input,num_filters)
# maxpooling
p = MaxPool2D(strides = (2,2))(x)
p = Dropout(0.4)(p)
return x,p
def decoder_block(input, skip_features, num_filters=64):
x = Conv2DTranspose(num_filters, (2,2), strides=2, padding='same')(input)
x = Concatenate()([x, skip_features])
x = conv_block(x, num_filters)
return x
num_classes=7
def unet_architect(input_shape=(256,256,3)):
""" Input Layer """
inputs = Input(input_shape)
""" Encoder """
s1,p1 = encoder_block(inputs, 64)
s2,p2 = encoder_block(p1,128)
s3,p3 = encoder_block(p2, 256)
s4,p4 = encoder_block(p3, 512)
""" Bridge """
b1 = conv_block(p4,1024)
""" Decoder """
d1 = decoder_block(b1, s4, 512)
d2 = decoder_block(d1, s3, 256)
d3 = decoder_block(d2, s2, 128)
d4 = decoder_block(d3, s1, 64)
""" Output Layer """
outputs = Conv2D(num_classes, (1,1), padding='same', activation = 'softmax')(d4)
model = Model(inputs, outputs, name='U-Net')
return model
model = unet_architect()
model.compile(optimizer = 'adam' ,
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
我試圖將 sparse_categorical_crossentropy 更改為 categorical_cross_entropy,出現另一個錯誤。
當我在擬合 model 時更改批量大小時,logits.shape 和 labels.shape 會相應更改。
錯誤
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-4c0704d8f65d> in <module>()
2 masks_train,
3 batch_size= 10,
----> 4 epochs = 5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1863, in sparse_categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5203, in sparse_categorical_crossentropy
labels=target, logits=output)
ValueError: `labels.shape` must equal `logits.shape` except for the last dimension. Received: labels.shape=(1966080,) and logits.shape=(655360, 7)
筆記本鏈接: https://github.com/Tamimi123600/Deep-Learning/blob/main/Image_Segmentation1.ipynb
提前致謝
為什么您的蒙版圖像(GT 目標)的形狀是1500, 256, 256, 3
而不是1500, 256, 256
? 你有num_classes=7
所以你的 GT 圖像應該有一個通道,其值{0...6}
代表每個像素的 class。
請檢查您如何加載和處理目標圖像——問題就在那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.