当我输入的图像尺寸不是299 x 299像素时,我无法实现以下推理模型代码。当我测试一张240 x 800像素(1,2,240,800,3)的图像时,出现与tf.squeeze有关变量“ aux_logits”的dim [2]的错误,该变量的形状为(1,1 ,11,11,24),其中24 =类数。当我从具有5个类别的不同数据集中测试299 x 299像素的图像(1、299、299、3)时,变量“ aux_logits”的形状为(1、1、1、1、5)。在这种情况下,tf.squeeze成功,因为dim [2] = 1。请说明为什么我不能运行以下代码以获取非标准/ ImageNet图像大小。
import tensorflow as tf
from nets import inception_v3
from preprocessing import inception_preprocessing
import scipy.misc
from scipy.misc import imshow, imread, imresize
import numpy as np
slim = tf.contrib.slim
batch_size = 1
image_size1 = 240
image_size2 = 800
with tf.Graph().as_default():
with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
imgPath = r'C:\Users\atunick\Tensorflow\models\research\slim\tmp\imagenet\20130412_043218_54895.jpg'
testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
testImage = tf.image.decode_jpeg(testImage_string, channels=3)
processed_image = inception_preprocessing.preprocess_image(testImage, image_size1, image_size2, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
print(processed_images.get_shape()) #(1, 240, 800, 3)
logits, _ = inception_v3.inception_v3(processed_images, num_classes=24, is_training=False)
probabilities = tf.nn.softmax(logits)
checkpoint_path = tf.train.latest_checkpoint(r'C:\tmp\mardct-models\inception_v3\all')
init_fn = slim.assign_from_checkpoint_fn(
checkpoint_path, slim.get_model_variables('InceptionV3'))
with tf.Session() as sess:
init_fn(sess)
np_image, probabilities = sess.run([processed_images, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]
names = ['Alilaguna', 'Ambulanza', 'Barchino', 'Cacciapesca', 'Caorlina', 'Gondola', 'Lanciafino10m',
'Lanciafino10mBianca', 'Lanciafino10mMarrone', 'Lanciamaggioredi10mBianca', 'Lanciamaggioredi10mMarrone',
'Motobarca', 'Motopontonerettangolare', 'MotoscafoACTV', 'Mototopo', 'Patanella', 'Polizia', 'Raccoltarifiuti',
'Sandoloaremi', 'Sanpierota', 'Topa', 'VaporettoACTV', 'VigilidelFuoco', 'Water']
for i in range(24):
index = sorted_inds[i]
print((probabilities[index], names[index]))
#imshow(imread(imgPath))
以下是错误消息:
(tensorflow) C:\Users\atunick\Tensorflow\models\research\slim>python test_image_v3_mardct.py
(1, 240, 800, 3) #image shape
(1, 1, 11, 24) #aux_logits shape
Traceback (most recent call last):
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\framework\ops.py", line 1626, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Can not squeeze dim[2], expected a dimension of 1, got 11 for 'InceptionV3/AuxLogits/SpatialSqueeze' (op: 'Squeeze') with input shapes: [1,1,11,24].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test_image_v3_mardct.py", line 24, in <module>
logits, _ = inception_v3.inception_v3(processed_images, num_classes=24, is_training=False)
File "C:\Users\atunick\Tensorflow\models\research\slim\nets\inception_v3.py", line 518, in inception_v3
aux_logits = tf.squeeze(aux_logits, [1,2], name='SpatialSqueeze')
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\ops\array_ops.py", line 2567, in squeeze
return gen_array_ops.squeeze(input, axis, name)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 9895, in squeeze
"Squeeze", input=input, squeeze_dims=axis, name=name)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\framework\ops.py", line 3272, in create_op
op_def=op_def)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\framework\ops.py", line 1790, in __init__
control_input_ops)
File "C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\framework\ops.py", line 1629, in _create_c_op
raise ValueError(str(e))
ValueError: Can not squeeze dim[2], expected a dimension of 1, got 11 for 'InceptionV3/AuxLogits/SpatialSqueeze' (op: 'Squeeze') with input shapes: [1,1,11,24].