簡體   English   中英

Keras logits 和 label 必須具有相同的第一維,得到 logits 形狀 [10240,151] 和標簽形狀 [1],sparse_categorical_crossentropy

[英]Keras logits and labels must have the same first dimension, got logits shape [10240,151] and labels shape [1], sparse_categorical_crossentropy

我正在嘗試創建一個用於語義分割的Unet 。我一直在關注這個包含本文代碼的存儲庫。 我使用的是場景解析 150 數據集,而不是文章中使用的數據集。 我的數據不是一次性編碼的,所以我嘗試使用 sparse_categorical_crossentropy 進行丟失。

這是我的數據的形狀。 x 是 RGB 圖像,y 是類別的 1 個通道注釋(151 個類別)。 是的,我每個只使用 10 個樣本,僅用於測試,當我真正可以開始訓練時,這將改變。

x_train shape:  (10, 32, 32, 3)
y_train shape:  (10, 32, 32, 1)
x_val shape:  (10, 32, 32, 3)
y_val shape:  (10, 32, 32, 1)

這些是 Unet 的第一層和最后一層。

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            [(None, 32, 32, 3)]  0
__________________________________________________________________________________________________
... other layers ...
__________________________________________________________________________________________________
conv2d_23 (Conv2D)              (None, 32, 32, 151)  453         conv2d_22[0][0] 
==================================================================================================

或者從代碼中:

input_size = (IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS) # 32x32x3
inputs = Input(shape=input_size)
...
output = Conv2D(N_CLASSES, 1, activation='softmax')(conv_dec_4)

我得到它的確切錯誤:

tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [10240,151] and labels shape [1]
         [[{{node loss/dense_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]

我可以看到 logits 形狀是[32x32xSamples,Number_of_classes]但為什么 label 形狀只是[1]

我找不到 label 形狀為[1]的人的單個搜索結果,所以我在這里發布了一個新問題。 我做錯了什么,我需要做什么來解決這個問題? 請詢問您是否需要任何其他相關信息。

筆記:

我非常希望繼續使用 sparse_categorical_crossentropy 而不是 one-hot 編碼和使用 categorical_crossentropy。

這里還有我的 package 和 python 版本:

$ python -V
Python 3.6.7

$ pip list
Package                  Version
------------------------ -------------------
absl-py                  0.12.0
astor                    0.8.1
astunparse               1.6.3
attrs                    21.2.0
cached-property          1.5.2
cachetools               4.2.2
certifi                  2021.5.30
chardet                  4.0.0
cycler                   0.10.0
dataclasses              0.8
dill                     0.3.3
flatbuffers              1.12
future                   0.18.2
gast                     0.4.0
google-auth              1.30.1
google-auth-oauthlib     0.4.4
google-pasta             0.2.0
googleapis-common-protos 1.53.0
grpcio                   1.34.1
h5py                     2.9.0
idna                     2.10
importlib-metadata       4.4.0
Keras                    2.4.3
Keras-Applications       1.0.8
keras-nightly            2.5.0.dev2021032900
Keras-Preprocessing      1.1.2
kiwisolver               1.3.1
Markdown                 3.3.4
matplotlib               3.3.4
numpy                    1.19.5
oauthlib                 3.1.1
opencv-python            3.4.2.16
opt-einsum               3.3.0
Pillow                   8.2.0
pip                      21.1.2
promise                  2.3
protobuf                 3.17.2
pyasn1                   0.4.8
pyasn1-modules           0.2.8
pyparsing                2.4.7
python-dateutil          2.8.1
PyYAML                   5.4.1
requests                 2.25.1
requests-oauthlib        1.3.0
rsa                      4.7.2
scipy                    1.5.4
setuptools               57.0.0
six                      1.15.0
tensorboard              1.14.0
tensorboard-data-server  0.6.1
tensorboard-plugin-wit   1.8.0
tensorflow               1.14.0
tensorflow-datasets      1.3.2
tensorflow-estimator     1.14.0
tensorflow-metadata      1.0.0
termcolor                1.1.0
tqdm                     4.61.0
typing-extensions        3.7.4.3
urllib3                  1.26.5
Werkzeug                 2.0.1
wheel                    0.36.2
wrapt                    1.12.1
zipp                     3.4.1

最小可重現示例:

"""Minimal Example"""
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import *

IMAGE_HEIGHT = 4
IMAGE_WIDTH = 4
IMAGE_CHANNELS = 3
MASK_CHANNELS = 1
N_CLASSES = 151

x_train = np.array([[[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                   [[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                   [[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                   [[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                    ])
x_train = np.expand_dims(x_train, axis=0)

y_train = np.array([[[0], [9], [1], [1]],
                   [[2], [1], [3], [6]],
                   [[1], [4], [1], [1]],
                   [[1], [1], [1], [8]],
                    ])
y_train = np.expand_dims(y_train, axis=0)

x_val = np.array([[[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                 [[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                 [[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                 [[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
                  ])
x_val = np.expand_dims(x_val, axis=0)

y_val = np.array([[[0], [9], [1], [1]],
                 [[2], [1], [3], [6]],
                 [[1], [4], [1], [1]],
                 [[1], [1], [1], [8]],
                  ])
y_val = np.expand_dims(y_val, axis=0)

inputs = Input(shape=(IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS))
conv_enc_1 = Conv2D(4, 3)(inputs)
output = Conv2D(N_CLASSES, 1, activation='softmax')(conv_enc_1)

unet = tf.keras.Model(inputs=inputs, outputs=output)

unet.summary()

unet.compile(optimizer='sgd', loss=tf.keras.losses.SparseCategoricalCrossentropy())
unet.fit((x_train, y_train), epochs=10, batch_size=1, shuffle=True, verbose=1, validation_data=(x_val, y_val))

根據Dominik Ficek 的評論

unet.fit((x_train, y_train))

本來應該:

unet.fit(x_train, y_train)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM