(对这一长职表示歉意)
全,
我希望使用预先训练过的Inceptionv3模型中的瓶颈特性来预测输入图像的分类。在训练模型和预测分类之前,我尝试了3种不同的方法来提取瓶颈特征。
我的3种方法产生了不同的瓶颈特性(不仅在值上,甚至在大小上也不同)。
请注意:
我的输入图像采用RGB格式(因此通道数量= 3),我将所有输入图像的大小调整为150×150。我在preprocess_input中使用inceptionv3.py函数对所有图像进行预处理。
def preprocess_input(image):
image /= 255.
image -= 0.5
image *= 2.
return image方法1:使用带有tensorflow的Keras作为后端,使用ImageDataGenerator读取数据,使用model.predict_generator计算瓶颈特性
我跟踪了Keras博客上的示例 (使用预先训练过的网络的瓶颈特性: 90%的准确性)。这里没有列出VGG模型,而是使用了Inceptionv3。下面是我使用的代码片段
(此处未显示代码,但我在下面代码之前所做的操作):读取所有输入图像,将大小调整为150x150x3,根据上述preprocessing_input函数重新排列,保存调整大小和重新缩放的图像。
train_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_directory(my_input_dir, target_size=(150,150),shuffle=False, batch_size=16)
# get bottleneck features
# use pre-trained model and exclude top layer - which is used for classification
pretrained_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(150,150,3))
bottleneck_features_train_v1 = pretrained_model.predict_generator(train_generator,len(train_generator.filenames)//16)方法2:使用带有tensorflow的Keras作为后端、我自己的阅读器和model.predict来计算瓶颈特性
这种方法与早期方法的唯一区别是,我使用自己的阅读器读取输入图像。(此处未显示代码,但我在下面代码之前所做的操作):读取所有输入图像,将大小调整为150x150x3,根据上述preprocessing_input函数重新排列,保存调整大小和重新缩放的图像。
# inputImages is a numpy array of size <number of input images x 150 x 150 x 3>
inputImages = readAllJPEGsInFolderAndMergeAsRGB(my_input_dir)
# get bottleneck features
# use pre-trained model and exclude top layer - which is used for classification
pretrained_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3))
bottleneck_features_train_v2 = pretrained_model.predict(trainData.images,batch_size=16)方法3:使用tensorflow (无KERAS)计算瓶颈特性
我遵循retrain.py提取输入图像的瓶颈特性。请注意,来自该脚本的权重可以从(http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz)获得。
正如在这个例子中所提到的,我使用bottleneck_tensor_name = 'pool_3/_reshape:0‘作为层来提取和计算瓶颈特性。与前两种方法类似,我使用调整大小和重新缩放的图像作为脚本的输入,并将此功能列表称为bottleneck_features_train_v3。
非常感谢
发布于 2018-03-13 12:51:26
1和2之间的不同结果
由于您没有显示代码,所以我(可能是错误的)认为问题在于您在声明preprocess_input时可能没有使用ImageDataGenerator?
from keras.applications.inception_v3 import preprocess_input
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input) 不过,请确保保存的图像文件范围从0到255。(钻头深度24)。
1和3之间的不同形状
在这种情况下,有三种可能的模型:
include_top = True ->这将返回类include_top = False (仅) -> --这意味着pooling = None (没有最终池层)include_top = False, pooling='avg'或='max' ->有一个池层。因此,在没有显式pooling=something的情况下,声明的模型在keras中没有最终的池层。然后输出仍将具有空间维数。
简单地通过在末尾添加一个池来解决这个问题。其中之一是:
pretrained_model = InceptionV3(include_top=False, pooling = 'avg', weights='imagenet', input_shape=(img_width, img_height, 3))
pretrained_model = InceptionV3(include_top=False, pooling = 'max', weights='imagenet', input_shape=(img_width, img_height, 3))不确定tgz文件中的模型使用的是哪一个。
作为另一种选择,您还可以从Tensorflow模型中获得另一层,该层就在'pool_3'之前。
发布于 2018-03-13 05:44:50
您可以在这里查看inceptionv3的Keras实现:v3.py
因此,默认参数是:
def InceptionV3(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000):请注意pooling=None的默认设置,然后在构建模型时,代码是:
if include_top:
# Classification block
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dense(classes, activation='softmax', name='predictions')(x)
else:
if pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = GlobalMaxPooling2D()(x)
# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
model = Model(inputs, x, name='inception_v3')因此,如果不指定池,则在没有任何池的情况下提取瓶颈特性,则需要指定是否要在这些特性的基础上获得平均池或最大池。
https://stackoverflow.com/questions/47166191
复制相似问题