我正在尝试微调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将是非常有帮助的。
答案 0 :(得分:0)
我无法判断您是否已根据自己提供的内容正确预处理输入。但是,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)
然后,您应该能够解冻所有图层,或者您想要训练的少数图层。
希望有所帮助!