gpt4 book ai didi

python - 无法从 flow_from_dataframe 进行训练 意外没有。类(class)数

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:04 25 4
gpt4 key购买 nike

我将在标签位于 csv 文件中的一组图像上训练模型。所以我用了flow_from_dataframe from tf.keras并指定了参数,但是当涉及到class_mode时它显示错误并显示 Found 3662 validated image filenames belonging to 1 classes. - 对于稀疏和分类。这是多类分类。”

“最初标签是 int,所以我将其转换为字符串,然后得到了这个输出。”

df_train=pd.read_csv(r"../input/train.csv",delimiter=',')
df_test=pd.read_csv(r"../input/test.csv",delimiter=',')
print(df_train.head())
print(df_test.head())
df_train['id_code']=df_train['id_code']+'.png'
df_train['diagnosis']=str(df_train['diagnosis'])
df_test['id_code']=df_test['id_code']+'.png'

""" output is
id_code diagnosis
0 000c1434d8d7 2
1 001639a390f0 4
2 0024cdab0c1e 1
3 002c21358ce6 0
4 005b95c28852 0
id_code
0 0005cfc8afb6
1 003f0afdcd15
2 006efc72b638
3 00836aaacf06
4 009245722fa4
"""

train_datagen = ImageDataGenerator(
rescale = 1./255,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

TRAINING_DIR='../input/train_images'

train_generator= train_datagen.flow_from_dataframe(
dataframe=df_train,
directory=TRAINING_DIR,
x_col='id_code',
y_col='diagnosis',
batch_size=20,
target_size=(1050,1050),
class_mode='categorical'#used also sparsed
)

""" output is
Found 3662 validated image filenames belonging to 1 classes.
"""

“我期望输出 "Found 3662 validated image filenames belonging to 5 classes" ,但实际输出是 "Found 3662 validated image filenames belonging to 1 classes"

最佳答案

“稀疏”类模式需要整数值,“分类”需要类列的一个热编码向量。所以我会尝试:

df['diagnosis'] = df['diagnosis'].astype(str)

然后使用“稀疏”类模式。

train_generator= train_datagen.flow_from_dataframe(
dataframe=df_train,
directory=TRAINING_DIR,
x_col='id_code',
y_col='diagnosis',
batch_size=20,
target_size=(1050,1050),
class_mode='sparse'
)

或者或者您可以使用一个热编码,如下所示:

pd.get_dummies(df,prefix=['diagnosis'], drop_first=True)

然后使用“categorical”class_mode:

train_generator= train_datagen.flow_from_dataframe(
dataframe=df_train,
directory=TRAINING_DIR,
x_col='id_code',
y_col=df.columns[1:],
batch_size=20,
target_size=(1050,1050),
class_mode='categorical'
)

关于python - 无法从 flow_from_dataframe 进行训练 意外没有。类(class)数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57167200/

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