【问题标题】:Keras Inception-v3 fine-tuning workaraoundKeras Inception-v3 微调解决方法
【发布时间】:2018-04-06 09:01:00
【问题描述】:

我正在尝试微调 Inception-v3,但无论我选择冻结哪一层,我都会得到随机预测。我发现其他人也有同样的问题:https://github.com/keras-team/keras/issues/9214。似乎问题出在将 BN 层设置为不可训练。 现在我正在尝试获取我想要冻结的最后一层的输出并将其用作以下层的输入,然后我将对其进行训练:

train_generator = train_datagen.flow_from_directory(
                os.path.join(directory, "train_data"),
                target_size=size,
                interpolation="bilinear",
                classes=["a", "b", "c","d"],
                batch_size=1,
                shuffle=False) base_model = InceptionV3(weights='imagenet', include_top=True, input_shape=(299, 299, 3))

model_features = Model(inputs=base_model.input, outputs=base_model.get_layer(
                self.Inception_Fine_Tune_Layers[layer_freeze]).output)

#I want to use this as input 
values_train = model_features.predict_generator(train_generator, verbose=1)

但是,虽然我有 12Gb,但我得到了这样的内存错误,这超出了我的需要:

....
 I tensorflow/core/common_runtime/bfc_allocator.cc:696] 1 Chunks of size 3268864 totalling 3.12MiB
 I tensorflow/core/common_runtime/bfc_allocator.cc:696] 1 Chunks of size 3489024 totalling 3.33MiB
 I tensorflow/core/common_runtime/bfc_allocator.cc:696] 1 Chunks of size 4211968 totalling 4.02MiB
 I tensorflow/core/common_runtime/bfc_allocator.cc:696] 1 Chunks of size 5129472 totalling 4.89MiB
 I tensorflow/core/common_runtime/bfc_allocator.cc:700] Sum Total of in-use chunks: 3.62GiB
 I tensorflow/core/common_runtime/bfc_allocator.cc:702] Stats: 
Limit:                 68719476736
InUse:                  3886957312
MaxInUse:               3889054464
NumAllocs:                    3709
MaxAllocSize:              8388608

任何关于如何解决该问题或其他解决方法以微调 Inception 的建议都会非常有帮助。

【问题讨论】:

    标签: performance tensorflow keras


    【解决方案1】:

    我不知道您是否根据您提供的内容正确地预处理了您的输入。但是,Keras 提供了特定于预训练网络的预处理功能,在本例中为 Inception V3。

    from keras.applications.inception_v3 import preprocess_input
    

    尝试将它作为预处理函数添加到您的数据生成器中......

    train_generator = train_datagen.flow_from_directory(
                os.path.join(directory, "train_data"),
                preprocessing_function=preprocess_input,  # <---
                target_size=size,
                interpolation="bilinear",
                classes=["a", "b", "c","d"],
                batch_size=1,
                shuffle=False) 
    

    然后,您应该能够解冻所有层,或者您想要训练的少数层。

    希望有帮助!

    【讨论】:

    • 我已经在使用正确的预处理:train_datagen = ImageDataGenerator(data_format='channels_last', preprocessing_function=preprocess_inception) 我已经对 VGG-19 进行了微调,它工作正常。问题在于,通过在 BN 层(存在于 Inception-v3 中)上调用 layer.istrainable = False,一些变量会不断更新,或者至少这是假设。如果我在 Inception-v3 中微调所有层,则没有问题 - 验证准确度会提高,但当我冻结层时,验证准确度保持不变
    • 你能提供你用来冻结图层的代码吗?
    猜你喜欢
    • 2017-06-12
    • 2018-02-01
    • 2019-02-16
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多