gpt4 book ai didi

python - 使用 tf.keras 和 Inception-v3 进行迁移学习 : No training is happening

转载 作者:行者123 更新时间:2023-12-01 07:44:15 28 4
gpt4 key购买 nike

我正在尝试基于卡住的 Inception_v3 模型训练一个模型,并以 3 个类作为输出。当我进行训练时,训练准确率上升,但验证准确率却没有上升,验证准确率大约为 33.33%,即显示完全随机的预测。我无法弄清楚我的代码和/或方法中的错误在哪里

我在 Inception v3 核心之后尝试了各种形式的输出,没有任何差异。

# Model definition
# InceptionV3 frozen, flatten, dense 1024, dropout 50%, dense 1024, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, flatten, dense 1024, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, flatten, dense 1024, dense 3, lr 0.005 --> does not train
# InceptionV3 frozen, GlobalAvgPooling, dense 1024, dense 1024, dense 512, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process, batch=32 --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process, batch=32, rebalance train/val sets --> does not train

IMAGE_SIZE = 150
BATCH_SIZE = 32

def build_model(image_size):
input_tensor = tf.keras.layers.Input(shape=(image_size, image_size, 3))

inception_base = InceptionV3(include_top=False, weights='imagenet', input_tensor=input_tensor)
for layer in inception_base.layers:
layer.trainable = False

x = inception_base.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dropout(0.2)(x)
output_tensor = tf.keras.layers.Dense(3, activation="softmax")(x)

model = tf.keras.Model(inputs=input_tensor, outputs=output_tensor)

return model

model = build_model(IMAGE_SIZE)
model.compile(optimizer=RMSprop(lr=0.002), loss='categorical_crossentropy', metrics=['acc'])

# Data generators with Image augmentations
train_datagen = ImageDataGenerator(
rescale=1./255,
preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

# Do not augment validation!
validation_datagen = ImageDataGenerator(
rescale=1./255,
preprocessing_function=tf.keras.applications.inception_v3.preprocess_input)

train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='categorical')

validation_generator = validation_datagen.flow_from_directory(
valid_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='categorical')

该单元的输出是:

找到属于 3 个类别的 1697 个图像。找到属于 3 个类别的 712 张图像。

最后两个时期的训练输出:

纪元 19/20
23/23 [================================] - 6s 257ms/步 - 损耗:1.1930 - 加速器:0.3174
54/54 [================================] - 20s 363ms/步 - 损失:0.7870 - acc:0.6912 - val_loss :1.1930 - val_acc:0.3174
20/20纪元
23/23 [================================] - 6s 255ms/步 - 损耗:1.1985 - 加速器:0.3160
54/54 [================================] - 20s 362ms/步 - 损耗:0.7819 - acc:0.7018 - val_loss :1.1985 - val_acc:0.3160

最佳答案

唯一让我惊讶的是放弃 rescale=1./255 ImageDataGenerators,因为这也是由 tf. keras.applications.inception_v3.preprocess_input,将 -1 缩放到 1;网络的预期输入。

关于python - 使用 tf.keras 和 Inception-v3 进行迁移学习 : No training is happening,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56546772/

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