类型错误:float()参数必须是一串或一个数字,而不是'BatchDataset'当的数据增加使用fit_generator()

0

的问题

我有麻烦申请的数据,扩充当培训的模式。 具体关于使用 fit_generator() 方法。

我最初运行模式成功地在不增加使用 适合() 方法,然而根据 其他人 ,它是建议使用 fit_generator(). 这似乎是这两种方法需要同样的输入时涉及到的图像和标签,但是我得到的以下 错误 时运行的代码如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_35/139227558.py in <module>
    105 
    106 # train the network
--> 107 model.fit_generator(aug.flow(train_ds,  batch_size=batch_size),
    108         validation_data=val_ds, steps_per_epoch=len(train_ds[0]) // batch_size,
    109     epochs=epochs)

/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in flow(self, x, y, batch_size, shuffle, sample_weight, seed, save_to_dir, save_prefix, save_format, subset)
    894         save_prefix=save_prefix,
    895         save_format=save_format,
--> 896         subset=subset)
    897 
    898   def flow_from_directory(self,

/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
    472         save_format=save_format,
    473         subset=subset,
--> 474         **kwargs)
    475 
    476 

/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
    119                     y = y[split_idx:]
    120 
--> 121         self.x = np.asarray(x, dtype=self.dtype)
    122         self.x_misc = x_misc
    123         if self.x.ndim != 4:

/opt/conda/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     81 
     82     """
---> 83     return array(a, dtype, copy=False, order=order)
     84 
     85 

TypeError: float() argument must be a string or a number, not 'BatchDataset'

我已经完成了谷歌在试图解决的 类型错误:float()参数必须是一串或一个数字,而不是'BatchDataset' 错误,但无济于事。 有人建议,为向前迈进?

import pathlib
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt

# Set data directory
data_dir = pathlib.Path("../input/validatedweaponsv6/images/")

# Set image size
img_height = 120
img_width = 120

# Hyperparameters
batch_size = 128
epochs = 50
learning_rate = 0.001

# Create the training dataset
train_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    label_mode='categorical',
    validation_split=0.2,
    subset="training",
    shuffle=True,
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

# Create the validation dataset
val_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    label_mode='categorical',
    validation_split=0.2,
    subset="validation",
    shuffle=True,
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

# Create sequential model
model = Sequential([

    # Preprocessing
    layers.Rescaling(1./127.5, offset=-1,
                     input_shape=(img_height, img_width, 3)),

    # Encoder
    layers.Conv2D(8, 3, activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(16, 3, activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(32, 3, activation='relu'),
    # layers.Conv2D(2, 3, activation='relu'), ???
    layers.Flatten(),

    # Decoder
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(2, activation='softmax')
])

# Print the model to see the different output shapes
print(model.summary())

# Compile model
model.compile(loss='categorical_crossentropy',
              optimizer=keras.optimizers.SGD(learning_rate=learning_rate), metrics=['accuracy'])

# construct the training image generator for data augmentation
aug = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=20, zoom_range=0.15,
    width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
    horizontal_flip=True, fill_mode="nearest")

# train the network
model.fit_generator(aug.flow(train_ds,  batch_size=batch_size),
validation_data=val_ds, steps_per_epoch=len(train_ds[0]) // batch_size,
epochs=epochs)

# Print scores
score = model.evaluate(train_ds, verbose=0)
print('Validation loss:', score[0])
print('Validation accuracy:', score[1])

# Show loss and accuracy models
show_history(history)

谢谢你看我的岗位! :)

deep-learning keras tensorflow
2021-11-19 13:25:05
1

最好的答案

0

第一、这篇文章你称是3岁,并且是一位已经过时。 从tensorflow2.1.0,包括,本 .适应 方法接受发电机过, 目前它完全替换。fit_generator. 我建议你来更新你的tensorflow如果可能的。

第二,错误似乎不在fit_generator方法,但在方式定义的数据集。 他们只是第一呼吁在fit_generator,这就是为什么错误消息跟踪你回到那里。

作为的错误本身,我不要了解的一部分,筑巢的发电机,并且我认为这可能会造成问题在这里。 你在试图通过批处理的数据集得到了从 tf。keras.工具.image_dataset_from_directory 到另一个发电机,这似乎是不可能的。

如果我理解正确,你只有一个标签上的每一个图像和图像的各类储存在单独的文件夹,因此,我建议您使用的 flow_from_directory 方法的 tf。keras.进行预处理。图像。ImageDataGenerator 。 这个发电机会和增强的图像,因此可以删除tf。keras.工具.image_dataset_from_directory的一部分。

使用这个发电机,你需要有图像形式:

  • root_directory
    • 1级的文件夹
    • 2级的文件夹
    • 等等

和你的代码会是什么东西这样的:

gen = tf.keras.preprocessing.image.ImageDataGenerator( #desired augmentation, ...) 
train_generator = gen.flow_from_directory(directory = root_directory,
target_size=(256, 256), classes= *list of class names*,
class_mode='categorical', batch_size=32, shuffle=True, ...)
model.fit(train_generator, ...)

你可以通过"validation_split"的说法也得到独立数据集,用于培训和验证。 了解更多关于该ImageDataGenerator和flow_from_directory方法 ,在正式文件.

2021-11-19 18:51:15

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................