keras 图像增强出错

swvgeqrz  于 2023-02-08  发布在  其他
关注(0)|答案(1)|浏览(158)

我试图使图像增强,看看它将如何影响模型,但由于某种原因,我得到了这个错误

TypeError: '>' not supported between instances of 'int' and 'ImageDataGenerator'

我使用efficientNetb4并添加了自己的分类器层。

augment = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=30, validation_split=0.15) 
train = augment.flow_from_directory(path, target_size=(380,380), batch_size=35, subset='training')
valid = augment.flow_from_directory(path, target_size=(380,380), batch_size=35, subset='validation')

base_model = keras.applications.EfficientNetB4(weights="imagenet",include_top=False, input_shape=(380, 380,3))

for layer in base_model.layers:
 layer.trainable = False

avg = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(3, activation="softmax")(avg)

model = keras.Model(inputs=base_model.input, outputs=output)

earlystopping = keras.callbacks.EarlyStopping(monitor='loss', patience=3)
optimizer = keras.optimizers.SGD(learning_rate=0.001, momentum=0.9, decay=0.0001)

model.compile(loss="sparse_categorical_crossentropy",optimizer=optimizer,metrics=["accuracy"])
history = model.fit_generator(train, augment, validation_data=valid, epochs=25, verbose=2, callbacks=[earlystopping])

我认为问题出在我指定的batch_size上,但无法理解它是否导致了此错误

1qczuiv0

1qczuiv01#

请检查fit_generator(https://faroit.com/keras-docs/1.2.0/models/model/)的文档:

fit_generator(self, generator, samples_per_epoch, nb_epoch, verbose=1, callbacks=[], validation_data=None, nb_val_samples=None, class_weight={}, max_q_size=10, nb_worker=1, pickle_safe=False, initial_epoch=0)

第二个参数是'samples_per_epoch',它是int,但是您传递的是ImageDataGenerator。因此出现了错误消息。我不明白为什么您需要在这里传递'augment'。下面的代码应该可以工作:

history = model.fit_generator(train, validation_data=valid, epochs=25, verbose=2, callbacks=[earlystopping])

相关问题