Error when train model with 2 output classes in Image Classification Mushroom

I have a complaint during the model train that may be caused by my code syntax. I want to create a mushroom image classification model that will produce 2 outputs to appear on mobile apps (tflite deploy to mobile apps). I created 2 classes for the output where there are types of mushrooms, namely Edible and Non-Edible Mushroom and also the name of the mushroom type which has 20 names of mushroom type names. In the code below, I used mobilnetV2 transfer learning :

import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input
from tensorflow.keras.models import Model

# Load the MobileNetV2 model with pre-trained weights
IMG_SHAPE = (224, 224, 3)
base_model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False, input_shape=IMG_SHAPE)

# Freeze the base model
base_model.trainable = False

# Add custom layers on top of the base model
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)

# Output layer for classifying mushroom type (20 classes)
mushroom_class = Dense(20, activation='softmax', name='mushroom_class')(x)

# Output layer for classifying edibility (2 classes)
edibility_output = Dense(2, activation='softmax', name='edibility_output')(x)
# edibility_output = Dense(2, activation='softmax', name='edibility_output')(x)

# Create the full model with two outputs
model = Model(inputs=base_model.input, outputs=[edibility_output, mushroom_class])

# Summary of the model
model.summary()
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import sparse_categorical_crossentropy
# Compile the model
model.compile(optimizer=Adam(),
              loss={'edibility_output': 'binary_crossentropy', 'mushroom_class': 'categorical_crossentropy'},
              metrics={'mushroom_class': 'accuracy', 'edibility_output': 'accuracy'})

I also created a custom generator to classify the types of mushrooms and the names of the types of mushrooms.

def custom_generator(generator):
    while True:
        x, y = generator.next()
        # Split labels into two parts: 1-class edible and 20-class mushroom types
        edible_labels = y[:, :2]  # Assuming the first column is edible labels
        mushroom_labels = y[:, 0:20]  # The remaining columns are mushroom type labels
        yield x, {'edibility_output': edible_labels, 'mushroom_class': mushroom_labels}

train_generator_custom = custom_generator(train_generator)
validation_generator_custom = custom_generator(validation_generator)

For the folder directory place, more or less like the image below:

I also declared ImageDataGenerator :

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Path to your training and validation directories
train_dir = 'mushroom3/MO_95/mushroom_dataset/train'
validation_dir = 'mushroom3/MO_95/mushroom_dataset/test'

train_ediblemushroom_dir = os.path.join(train_dir, 'Edible')
train_inediblemushroom_dir = os.path.join(train_dir, 'Non-Edible')

validation_ediblemushroom_dir = os.path.join(validation_dir, 'Edible')
validation_inediblemushroom_dir = os.path.join(validation_dir, 'Non-Edible')

# Define ImageDataGenerator for training data
train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)

# validation_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)

# Generate batches of augmented data from the directories
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(224, 224),
    batch_size=20,
    class_mode='categorical'  # Because you have multiple classes
)

validation_generator = validation_datagen.flow_from_directory(
    validation_dir,
    target_size=(224, 224),
    batch_size=20,
    class_mode='categorical'  # Because you have multiple classes
)

This is the code syntax for train model :

history = model.fit(
    train_generator_custom,
    steps_per_epoch=len(train_generator),
    epochs=200,
    validation_data=validation_generator_custom,
    validation_steps=len(validation_generator)
)

When I want to train the model, the following error message appears :

InvalidArgumentError: Graph execution error:
........
........
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[20,20] labels_size=[20,2]
     [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_8124]

Can anyone help with the error message in my script code?

I want the mobile apps to produce output for mushroom types and names of mushroom types with tflite after I create tflite.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật