转移学习中的可变图像大小(inception_resnet_v2)

时间:2019-11-14 23:16:14

标签: python conv-neural-network feature-extraction keras-layer tensorflow-hub

我正在使用Tensorflow Hub和Imagenet上训练的Inception-Resnet的特征向量(没有分类头)运行图像特征提取任务。从文档中,可以使用不同的图像尺寸作为输入。想知道一次将特征提取器初始化(不指定input_shape)然后以不同的图像尺寸进行输入是否正确? 例如,在单个图像上运行(带有/不带有input_shape初始化)时,输出特征向量是相同的:

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import matplotlib.pylab as plt
!pip install -q -U tf-hub-nightly --user
import tensorflow_hub as hub
from tensorflow.keras import layers

import numpy as np
import PIL.Image as Image
img=Image.open('image.png') # RGB image, (148, 299, 3)
img = np.array(img)/255.0

# extract features after specifying image_shape
feature_extractor_url = "https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4"
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,input_shape=img.shape)
feature_img_shape = feature_extractor_layer(img[np.newaxis, ...])

# extract features without specifying image_shape
feature_extractor_url = "https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4"
feature_extractor_layer = hub.KerasLayer(feature_extractor_url)
feature_img_noshape = feature_extractor_layer(img[np.newaxis, ...])

# Check if the feature vectors are the same
np.array_equal(feature_img_shape.numpy(),feature_img_noshape.numpy())
True

0 个答案:

没有答案