【发布时间】:2021-12-31 17:43:26
【问题描述】:
这是我的代码:
import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
loss='binary_crossentropy',
optimizer=RMSprop(lr=0.001),
metrics=['accuracy']
)
train_datagen = ImageDataGenerator(rescale=1.0/255.)
train_generator = train_datagen.flow_from_directory('training_set',
batch_size=250,
class_mode='binary',
target_size=(150, 150))
validation_datagen = ImageDataGenerator(rescale=1.0/255.)
validation_generator = validation_datagen.flow_from_directory('test_set',
batch_size=456,
class_mode='binary',
target_size=(150, 150))
history = model.fit(
train_generator,
validation_data=validation_generator,
epochs=15,
steps_per_epoch=22,
validation_steps=22,
verbose=1
)
我正在尝试在这里对猫和狗进行分类。如果你想自己重现这个东西,这里是 Kaggle 上数据集的链接:https://www.kaggle.com/tongpython/cat-and-dog。
在model.fit() 函数中,我指定了epochs=15。但是当我运行它时,它会一直持续到它完成 1/15 个 epoch。看看:
Epoch 1/15
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x16882d280> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: unsupported operand type(s) for -: 'NoneType' and 'int'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING: AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x16882d280> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: unsupported operand type(s) for -: 'NoneType' and 'int'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
2021-11-21 19:10:51.086856: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-11-21 19:10:51.087052: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz
22/22 [==============================] - ETA: 0s - loss: 1.5458 - accuracy: 0.5119 WARNING:tensorflow:AutoGraph could not transform <function Model.make_test_function.<locals>.test_function at 0x1699b7670> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: unsupported operand type(s) for -: 'NoneType' and 'int'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING: AutoGraph could not transform <function Model.make_test_function.<locals>.test_function at 0x1699b7670> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: unsupported operand type(s) for -: 'NoneType' and 'int'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
您知道为什么会发生这种情况,以及我可以做些什么来获得 15 个 epoch 的准确度吗?
【问题讨论】:
-
尝试在数据生成器中设置批量大小以匹配。另外,你确定你的批量应该那么大吗?
-
数据集中有10032张图片; 456 完美地除以 10032。应该更小吗?
-
@Djinn 我认为批量大小 * 每个时期的步数应该等于或接近文件数,对吧?您认为合适的批量大小是多少?
-
这是首选,但我从未见过有人使用如此高的批量大小,我认为这就是导致问题的原因。试试32号什么的。您可能想阅读更多有关 Keras 模型创建的信息。
标签: python tensorflow keras deep-learning neural-network