gpt4 book ai didi

python - ValueError : logits and labels must have the same shape ((None, 14)与(无,1))

转载 作者:行者123 更新时间:2023-12-04 04:06:57 32 4
gpt4 key购买 nike

我正在创建一个 CNN,它使用 14 个可以共存的不同类别对 CXR 进行多标签分类。 (https://stanfordmlgroup.github.io/competitions/chexpert/)。我将 Python 与 Keras 和 Tensorflow 一起使用,现在我正在尝试让代码工作(使用小型测试 CNN),但我收到错误“ValueError:logits 和标签必须具有相同的形状((None,14)vs(无,1))”我使用了具有二元交叉熵损失的 sigmoid 激活函数。我认为创建训练和验证数据集可能会出现问题。如图所示,我将 ImageDataGenerator.flow_from_dataframe 函数与 pandas 数据框一起使用,所有 14 个标签(0 或 1)都有一列。 Pandas Dataframe Structure

我在 stackoverflow/github 上搜索过同样的问题,但他们大多不使用 ImageDataGenerator 并且必须调整 X 或 Y 的大小,但我不知道该怎么做。有谁知道出了什么问题?提前致谢!我的代码如下。

import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
import time
import h5py

df = pd.read_csv('D:\\Milou\\CheXpert-v1.0-small\\train.csv', usecols = [0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])
df = df.fillna(0) # Change NaN values to 0
df = df.convert_dtypes() # Change datatypes from float to integer if possible
df = df.replace({-1 : 0}) # Regard the uncertain labels '-1' as negative
print(df.head(5))

label_names = ["No Finding", "Enlarged Cardiomediastinum", "Cardiomegaly", "Lung Opacity", "Lung Lesion", "Edema", "Consolidation", "Pneumonia", "Atelectasis", "Pneumothorax", "Pleural Effusion", "Pleural Other", "Fracture", "Support Devices"]

datagen=ImageDataGenerator(rescale=1./255, validation_split=0.2)

train_generator=datagen.flow_from_dataframe(dataframe=df,
directory="D:\\Milou",
x_col="Path",
y_col=label_names,
subset="training",
class_mode="multi_output",
target_size=(100,100),
batch_size=64)
validation_generator=datagen.flow_from_dataframe(dataframe=df,
directory="D:\\Milou",
x_col="Path",
y_col=label_names,
subset="validation",
class_mode="multi_output",
target_size=(100,100),
batch_size=64)


# Creating model
NAME = "Test-{}".format(int(time.time()))

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=(100,100,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors

model.add(Dense(256))
model.add(Activation('relu'))

model.add(Dropout(0.5))

model.add(Dense(14))
model.add(Activation('sigmoid'))

tensorboard = TensorBoard(log_dir="logs_multilabel\\{}".format(NAME))

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

step_size_train = train_generator.n//train_generator.batch_size
model.fit(train_generator,
steps_per_epoch = step_size_train,
epochs=3,
validation_data= validation_generator,
callbacks=[tensorboard])

第一次在 stackoverflow 上提问,所以请不要犹豫,对缺失的信息等提供反馈!

最佳答案

因为你有 2 个不同的类,你应该在你的最终 dense 层中有 2 个神经元,而不是你拥有的观察数量。不应在您的神经网络架构中指定观察次数。

关于python - ValueError : logits and labels must have the same shape ((None, 14)与(无,1)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62307785/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com