[英]Tensorflow with Keras: sparse_categorical_crossentropy
[英]Sparse Categorical CrossEntropy shape problem with Keras
我有一個多類問題,其中圖像可以是三個類之一(Masked、UnMasked、Hybrid)。
我正在使用來自 keras 預處理模塊的image_dataset_from_directory ,這使事情變得更容易。
def load_from_directory(shuffle=False):
train_ds = tfk.preprocessing.image_dataset_from_directory(
directory=TRAINING_PATH,
image_size=IMAGE_SIZE,
validation_split=VALIDATION_SPLIT,
batch_size=BATCH_SIZE,
seed=SEED,
subset='training',
label_mode='int',
shuffle=shuffle
)
val_ds = tfk.preprocessing.image_dataset_from_directory(
directory=TRAINING_PATH,
image_size=IMAGE_SIZE,
validation_split=VALIDATION_SPLIT,
batch_size=BATCH_SIZE,
seed=SEED,
subset='validation',
label_mode='int',
shuffle=False
)
test_ds = tfk.preprocessing.image_dataset_from_directory(
directory=TESTING_PATH,
labels=None,
image_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
seed=SEED,
label_mode='int',
shuffle=False
)
return train_ds, val_ds, test_ds
在 keras 文檔中,它為 label_model 聲明了以下內容: 'int':表示標簽被編碼為整數(例如,對於 sparse_categorical_crossentropy 損失)...... (這里是鏈接)。 由於我的文件夾具有以下結構:
對於最終層的模型如下:
x = tfkl.SeparableConv2D(1024, 3, padding='same')(x)
x = tfkl.BatchNormalization()(x)
x = tfkl.Activation('relu')(x)
# GlobalAveragePooling + Dropout
x = tfkl.GlobalAveragePooling2D()(x)
x = tfkl.Dropout(0.5)(x)
# Softmax
outputs = tfkl.Dense(units=len(CLASS_NAMES), activation='softmax')(x)
model = tfk.Model(inputs, outputs)
我得到以下抱怨:“ValueError:形狀(None,3)和(None,1)不兼容”
如果我將 label_mode 更改為“categorical”,在加載數據集並將損失函數從 SparseCategoricalCrossEntropy 更改為 CategoricalCrossEntropy 並將准確度從 SparseCategoricalAccuracy 更改為 CategoricalAccuracy 它有效,但我真的很想了解為什么我無法使用 SparseCrossEntropy 損失函數以及如何解決它。
def compile_model(model, plot=False):
model.compile(
optimizer=tf.optimizers.Adam(1e-3),
loss=tf.losses.SparseCategoricalCrossentropy(name='loss'),
metrics=[
tfk.metrics.SparseCategoricalAccuracy(name='accuracy'),
tfk.metrics.Precision(name='precision'),
tfk.metrics.Recall(name='recall'),
]
)
model.summary()
if plot: tfk.utils.plot_model(model, show_shapes=True)
def train_model(model, debug_mode=False):
callbacks = [tfk.callbacks.EarlyStopping(patience=5, monitor='val_loss', restore_best_weights=True)]
if debug_mode:
callbacks.append(tfk.callbacks.ModelCheckpoint(filepath='model.{epoch:02d}-{val_loss:.2f}.h5', save_best_only=True, monitor='val_loss'))
callbacks.append(tfk.callbacks.TensorBoard(log_dir='./tensorboard'))
history = model.fit(
x=train_ds,
validation_data=val_ds,
epochs=100,
callbacks=callbacks,
# steps_per_epoch=len(train_ds),
# validation_steps=len(val_ds),
)
return history
tf.keras.metrics.SparseCategoricalAccuracy() --> 用於 SparseCategorical (int) 類。 tf.keras.metrics.Recall() --> 用於分類(one-hot)類。
如果要使用沒有“稀疏”的任何度量命名,則必須使用 one-hot 類。
更新:
num_class=4
def get_img_and_onehot_class(img_path, class):
img = tf.io.read_file(img_path)
img = tf.io.decode_jpeg(img, channels=3)
""" Other preprocessing of image."""
return img, tf.one_hot(class, num_class)
當你上了一堂熱課時:
loss=tf.losses.CategoricalCrossentropy
METRICS=[tf.keras.metrics.CategoricalAccuracy(name='accuracy'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),]
model.compile(
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001),
loss=loss,
metrics= METRICS)
編號: #72217176
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.