简体   繁体   English

遇到错误“ValueError: Shapes (None, 5) and (None, 4) are incompatible”

[英]Meet an error " ValueError: Shapes (None, 5) and (None, 4) are incompatible"

Can anyboday help me on this error?有人可以帮我解决这个错误吗? the total files are 2204 to 5 classes.总文件为2204至5类。 and 1764 files for training.和 1764 个文件用于训练。 Thanks advanced.感谢先进。

this is my code:这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.python.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import pathlib
data_dir = r"/root/data_Camera"
data_dir = pathlib.Path(data_dir)
rock = list(data_dir.glob('rock/*'))
print(rock[0])
PIL.Image.open(str(rock[0]))
img_height, img_width = 400,2000
batch_size = 32
trains_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split = 0.2,
  subset = "training",
  seed = 123,
  label_mode = 'categorical',
  image_size = (img_height, img_width),
  batch_size = batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    seed=123,
    label_mode = 'categorical',
    image_size=(img_height, img_width),
    batch_size=batch_size)
class_names = trains_ds.class_names
print(class_names)
resnet_model = Sequential()
pretrained_model = tf.keras.applications.ResNet50(include_top=False, 
                                                  input_shape=(400,2000,3),
                                                  pooling='avg', 
                                                  classes = 5, 
                                                  weights = 'imagenet')
for layer in pretrained_model.layers: 
    layer.trainable=False
resnet_model.add(pretrained_model)
resnet_model.add(Flatten())
resnet_model.add(Dense(512, activation='relu'))
resnet_model.add(Dense(4,activation='softmax'))
resnet_model.summary()
resnet_model.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
epochs = 10
history= resnet_model.fit(
    trains_ds,
    validation_data=val_ds,
    epochs=epochs)

and I meet the error is: ValueError: Shapes (None, 5) and (None, 4) are incompatible I also add the file code to here.我遇到的错误是:ValueError: Shapes (None, 5) and (None, 4) are incompatible 我还将文件代码添加到此处。 https://github.com/CallaDai/Tensorflow.git you can check it out. https://github.com/CallaDai/Tensorflow.git你可以看看。 thank you!谢谢你!

Having recently faced a similar problem consider using loss='sparse_categorical_crossentropy instead of loss='categorical_crossentropy .最近遇到类似的问题考虑使用loss='sparse_categorical_crossentropy而不是loss='categorical_crossentropy The error may occur because 'categorical_crossentropy' works on a one-hot encoded target, while 'sparse_categorical_crossentropy' works on an integer target.发生错误的原因可能是“categorical_crossentropy”适用于单热编码目标,而“sparse_categorical_crossentropy”适用于 integer 目标。

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

相关问题 遇到 ValueError:形状 (None, 2204) 和 (None, 5) 不兼容 - Meet an ValueError: Shapes (None, 2204) and (None, 5) 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” ValueError:形状 (None, 2) 和 (None, 1) 不兼容 - ValueError: Shapes (None, 2) and (None, 1) are incompatible ValueError:形状(无,)和(无,1)不兼容 - ValueError: Shapes (None,) and (None, 1) are incompatible ValueError:形状 (None, 1) 和 (None, 3) 不兼容 - ValueError: Shapes (None, 1) and (None, 3) are incompatible ValueError:形状 (None, 0, 5) 和 (None, 5) 不兼容 - ValueError: Shapes (None, 0, 5) and (None, 5) are incompatible ValueError:形状(无,无)和(无,无,无,43)不兼容 - ValueError: Shapes (None, None) and (None, None, None, 43) are incompatible ValueError:形状(无,无)和(无,无,无,3)不兼容 - ValueError: Shapes (None, None) and (None, None, None, 3) are incompatible Colab 中的 Tensorflow 错误 - ValueError: Shapes (None, 1) 和 (None, 10) 不兼容 - Tensorflow error in Colab - ValueError: Shapes (None, 1) and (None, 10) are incompatible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM