首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用预先训练的Inceptionv3提取瓶颈特征--Keras实现与本地Tensorflow实现的区别

利用预先训练的Inceptionv3提取瓶颈特征--Keras实现与本地Tensorflow实现的区别
EN

Stack Overflow用户
提问于 2017-11-07 19:41:53
回答 2查看 3K关注 0票数 8

(对这一长职表示歉意)

全,

我希望使用预先训练过的Inceptionv3模型中的瓶颈特性来预测输入图像的分类。在训练模型和预测分类之前,我尝试了3种不同的方法来提取瓶颈特征。

我的3种方法产生了不同的瓶颈特性(不仅在值上,甚至在大小上也不同)。

  1. 方法1和方法2中瓶颈特性的大小:(输入图像的数量)x3x3x2048 方法3中瓶颈特性的大小:(输入图像的数量)x 2048 为什么基于Keras的Inceptionv3模型和本地Tensorflow模型的大小不同?我的猜测是,当我在Keras中说include_top=False时,我并不是提取池3/_ layer :0层。这是正确的吗?如果是,我如何提取Keras中的池_3/_Keras:0层?如果我的猜测是错误的,我遗漏了什么?
  2. 我比较了方法1和方法2中的瓶颈特征值,它们有显着性差异。我想我给它的输入图像是相同的,因为在我把它作为脚本的输入阅读之前,我调整了图像的大小和大小。在方法1中,我没有ImageDataGenerator的选项,根据该函数的文档,所有默认值都不会更改我的输入映像。我已经将洗牌设置为false,因此我假设predict_generator和predict正在以相同的顺序读取图像。我错过了什么?

请注意:

我的输入图像采用RGB格式(因此通道数量= 3),我将所有输入图像的大小调整为150×150。我在preprocess_input中使用inceptionv3.py函数对所有图像进行预处理。

代码语言:javascript
复制
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函数重新排列,保存调整大小和重新缩放的图像。

代码语言:javascript
复制
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函数重新排列,保存调整大小和重新缩放的图像。

代码语言:javascript
复制
# 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。

非常感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-13 12:51:26

1和2之间的不同结果

由于您没有显示代码,所以我(可能是错误的)认为问题在于您在声明preprocess_input时可能没有使用ImageDataGenerator

代码语言:javascript
复制
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中没有最终的池层。然后输出仍将具有空间维数。

简单地通过在末尾添加一个池来解决这个问题。其中之一是:

代码语言:javascript
复制
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'之前。

票数 4
EN

Stack Overflow用户

发布于 2018-03-13 05:44:50

您可以在这里查看inceptionv3的Keras实现:v3.py

因此,默认参数是:

代码语言:javascript
复制
def InceptionV3(include_top=True,
                weights='imagenet',
                input_tensor=None,
                input_shape=None,
                pooling=None,
                classes=1000):

请注意pooling=None的默认设置,然后在构建模型时,代码是:

代码语言:javascript
复制
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')

因此,如果不指定池,则在没有任何池的情况下提取瓶颈特性,则需要指定是否要在这些特性的基础上获得平均池或最大池。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47166191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档