简体   繁体   English

CNN 值错误输入形状,同时用作损失“categorical_crossentropy”。 此损失期望目标与输出具有相同的形状

[英]CNN value error input shape while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output

hiya been tryna make an image classifier for whale species with 9 categories im running python 3.6 and these are my codes你好,我一直在尝试为鲸鱼物种制作一个图像分类器,它有 9 个类别,我正在运行 python 3.6,这些是我的代码

import numpy as np
from tensorflow import keras
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from sklearn.metrics import confusion_matrix
import itertools
import matplotlib.pyplot as plt

train_path=r'C:\Users\Blessie Balais\whales\TRAIN'
valid_path=r'C:\Users\Blessie Balais\whales\VAL'
test_path=r'C:\Users\Blessie Balais\whales\TEST'

class_labels=['BLUE WHALES', 'BOWHEAD WHALES', 'BRYDES WHALES', 'FIN WHALES', 'GRAY WHALES', 'HUMPBACK WHALES', 'MINKE WHALES', 'NORTHERN RIGHT WHALES', 'OMURA WHALES', 'PYGMY RIGHT WHALES', 'SEI WHALES', 'SOUTHERN RIGHT WHALES']

train_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input)\
    .flow_from_directory(train_path, target_size=(299,299),classes=class_labels,batch_size=5)
valid_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input)\
    .flow_from_directory(valid_path, target_size=(299,299),classes=class_labels,batch_size=5)
test_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input)\
    .flow_from_directory(test_path, target_size=(299,299),classes=class_labels,batch_size=5, shuffle=False)

base_model=keras.applications.xception.Xception(include_top=False)

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024, activation='relu')(x)
x=Dense(9, activation='sigmoid')(x)
model=Model(inputs=base_model.input, outputs=x)


base_model.trainable = False

N=30

model.compile(Adam(lr=.0001),loss='categorical_crossentropy', metrics=['accuracy'])
history=model.fit_generator(train_batches, steps_per_epoch=40, validation_data=valid_batches,
validation_steps=90,epochs=N,verbose=1)

print("[INFO]evaluating model...")

test_labels=test_batches.classes
predictions=model.predict_generator(test_batches, steps=28, verbose=1)

and got this error并得到这个错误

Traceback (most recent call last):
  File "C:/Users/Blessie Balais/PycharmProjects/app/classifaying whales.py", line 39, in <module>
    validation_steps=90,epochs=N,verbose=1)
  File "C:\Users\Blessie Balais\Anaconda3\envs\bbssie\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1297, in fit_generator
    steps_name='steps_per_epoch')
  File "C:\Users\Blessie Balais\Anaconda3\envs\bbssie\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py", line 265, in model_iteration
    batch_outs = batch_function(*batch_data)
  File "C:\Users\Blessie Balais\Anaconda3\envs\bbssie\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 973, in train_on_batch
    class_weight=class_weight, reset_metrics=reset_metrics)
  File "C:\Users\Blessie Balais\Anaconda3\envs\bbssie\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 253, in train_on_batch
    extract_tensors_from_dataset=True)
  File "C:\Users\Blessie Balais\Anaconda3\envs\bbssie\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2538, in _standardize_user_data
    y, self._feed_loss_fns, feed_output_shapes)
  File "C:\Users\Blessie Balais\Anaconda3\envs\bbssie\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 743, in check_loss_and_target_compatibility
    ' while using as loss ' + loss_name + '. '
ValueError: A target array with shape (5, 12) was passed for an output of shape (None, 9) while using as loss categorical_crossentropy. This loss expects targets to have the same shape as the output.

Process finished with exit code 1

nvm 我得到了x=Dense(9, activation='sigmoid')(x)输出设置为 9 而不是 12

暂无
暂无

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

相关问题 形状为 (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` 一个形状为 (11203, 25) 的目标数组被传递给形状为 (None, 3) 的输出,同时用作损失`categorical_crossentropy` - A target array with shape (11203, 25) was passed for an output of shape (None, 3) while using as loss `categorical_crossentropy` 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:此损失期望目标具有与 output 相同的形状 - ValueError : This loss expects targets to have the same shape as the output `categorical_crossentropy` 损失中的 ValueError function:形状问题 - ValueError in `categorical_crossentropy` loss function: shape issue 损失为 NaN,使用激活 softmax 和损失 function categorical_crossentropy - Loss is NaN using activation softmax and loss function categorical_crossentropy 形状为 (6400, 1) 的目标数组被传递给形状为 (None, 2) 的 output,同时用作损失`binary_crossentropy` - A target array with shape (6400, 1) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy` 形状为 (64, 4) 的目标数组被传递给形状为 (None, 3) 的 output,同时用作损失`binary_crossentropy` - A target array with shape (64, 4) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy` 一个形状为 (687, 809) 的目标数组被传递给形状 (None, 25) 的输出,同时使用作为损失`binary_crossentropy - A target array with shape (687, 809) was passed for an output of shape (None, 25) while using as loss `binary_crossentropy keras自定义生成器categorical_crossentropy修复输出形状问题 - keras custom generator categorical_crossentropy fix output shape issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM