简体   繁体   English

使用 categorical_crossentropy 在 TensorFlow 中训练图像分类时出现“ValueError: Shapes (None, 1) and (None, 32) are incompatible”

[英]"ValueError: Shapes (None, 1) and (None, 32) are incompatible" when training image classification network in TensorFlow using categorical_crossentropy

I am trying to train a machine learning model to classify images, but I am getting some issues when I attempt to use the categorical_crossentropy loss function.我正在尝试训练机器学习 model 来对图像进行分类,但是当我尝试使用 categorical_crossentropy 损失 function 时遇到了一些问题。

Here is the code that I am using to generate my model.这是我用来生成 model 的代码。

import numpy as np
import os
import PIL
import PIL.Image
import tensorflow as tf
import pathlib
import glob
import matplotlib.pyplot as plt
from tensorflow.keras import layers
from tensorflow.keras import callbacks 
from tensorflow import keras
from datetime import datetime
import tensorboard

if __name__ == "__main__":
    #This first section mostly follows the tutorial at https://www.tensorflow.org/tutorials/images/classification
    data_dir = "img_directories"
    image_count = len(list(glob.glob(f'{data_dir}/*/*.png')))
    print(image_count)

    batch_size = 128
    img_height = 100 
    img_width = 100

    #Set up training data
    val_split = 0.2

    train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=val_split,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

    #Set up testing data
    val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=val_split,
    subset="validation",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size, 
    color_mode='rgb')

    class_names = train_ds.class_names
    print(class_names)

    num_classes = len(train_ds.class_names)

    #   Normalize data
    normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
    normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
    image_batch, labels_batch = next(iter(normalized_ds))

    #Set up model

    model = tf.keras.Sequential()
    # model.add(layers.experimental.preprocessing.Rescaling((1./255),input_shape=(100, 100, 3)))
    model.add(layers.Conv2D(64, (3,3), activation='relu',input_shape=(img_height, img_width, 3)))
    model.add(layers.MaxPooling2D(pool_size=(2,2)))
    model.add(layers.Dropout(0.2))
    model.add(layers.Conv2D(64, (5,5)))
    model.add(layers.MaxPooling2D(pool_size=(3,3)))
    model.add(layers.Dense(64))
    model.add(layers.Flatten())
    model.add(layers.Dense(num_classes, activation='softmax'))
    
    model.compile(optimizer='adam',
                loss='categorical_crossentropy',
                metrics=['accuracy'])


    model.summary()

    earlystopping = callbacks.EarlyStopping(monitor ="val_loss",  
                                        mode ="min", patience = 7,  
                                        restore_best_weights = True) 


    history=model.fit(
    normalized_ds,
    validation_data=val_ds,
    epochs=100,
    callbacks=[earlystopping]
    )

Setting the loss function to categorical_crossentropy gives me the following errors:将损失 function 设置为categorical_crossentropy会给我以下错误:

    ValueError: Shapes (None, 1) and (None, 32) are incompatible

Where 32 is the number of classes in my dataset that I have, therefore it is having issues with my output layer.其中 32 是我拥有的数据集中的类数,因此我的 output 层存在问题。

However, it does not appear to have issues when I try to run it with sparse_categorical_crossentropy但是,当我尝试使用sparse_categorical_crossentropy运行它时,它似乎没有问题

How do I make it work with categorical_crossentropy because I have so few classes?因为我的课程太少,我该如何让它与categorical_crossentropy一起工作?

Edit:编辑:

I have tried something like this, but I am still getting errors similar to the original.我已经尝试过类似的方法,但我仍然遇到与原始错误类似的错误。

    val_imgs, val_labels = next(iter(val_ds))
    val_labels_one_hot=tf.one_hot(labels_batch,num_classes)

    model.compile(optimizer='adam',
                loss='categorical_crossentropy',
                metrics=['accuracy'])


    model.summary()

    earlystopping = callbacks.EarlyStopping(monitor ="val_loss",  
                                        mode ="min", patience = 7,  
                                        restore_best_weights = True) 

    history=model.fit(
    train_ds,
    validation_data=[val_imgs,val_labels_one_hot],
    epochs=100,
    callbacks=[earlystopping]
    )

sparse_categorical_crossentropy ( documentation ) assumes integers whereas categorical_crossentropy ( documentation ) assumes one-hot encoding vectors. sparse_categorical_crossentropy文档)假设整数,而categorical_crossentropy文档)假设单热编码向量。 You can use both but sparse_categorical_crossentropy works because you're providing each label with shape (None, 1) .您可以同时使用两者,但sparse_categorical_crossentropy有效,因为您为每个 label 提供形状(None, 1)

In summary, if you want to use categorical_crossentropy , you'll need to convert your current target tensor to one-hot encodings (which will then be used by the final softmax layer).总之,如果您想使用categorical_crossentropy ,您需要将当前目标张量转换为单热编码(然后由最终的 softmax 层使用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TensorFlow 'categorical_crossentropy' 中的 ValueError - ValueError in TensorFlow 'categorical_crossentropy' 形状为 (32, 3) 的目标数组被传递给形状为 (None, 15, 15, 3) 的 output,同时用作损失`categorical_crossentropy` - A target array with shape (32, 3) was passed for an output of shape (None, 15, 15, 3) while using as loss `categorical_crossentropy` Tensorflow:ValueError:形状(None,1)和(None,2)不兼容 - Tensorflow: ValueError: Shapes (None, 1) and (None, 2) are incompatible TensorFlow - ValueError:形状(无,1)和(无,10)不兼容 - TensorFlow - ValueError: Shapes (None, 1) and (None, 10) are incompatible Tensorflow ValueError:形状(无,1)和(无,10)不兼容 - Tensorflow ValueError: Shapes (None, 1) and (None, 10) are incompatible ValueError: 形状为 (72148, 23) 的目标数组被传递给形状为 (None, 826, 23) 的 output,同时用作损失`categorical_crossentropy` - ValueError: A target array with shape (72148, 23) was passed for an output of shape (None, 826, 23) while using as loss `categorical_crossentropy` ValueError: Shapes (None, 3, 2) 和 (None, 2) 不兼容使用 tfrecord - ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible using tfrecord Tensorflow 尺寸问题:ValueError:形状 (3, 1) 和 (None, 3) 不兼容 - Tensorflow dimension issue: ValueError: Shapes (3, 1) and (None, 3) are incompatible ValueError:形状(无,2)和(无,3)不兼容 - ValueError: Shapes (None, 2) and (None, 3) are incompatible “ValueError:形状 (None, 1) 和 (None, 6) 不兼容” - “ValueError: Shapes (None, 1) and (None, 6) are incompatible”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM