Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AutoML Vision如何处理尺寸小于224x224像素的图像?_Python_Tensorflow_Google Cloud Automl - Fatal编程技术网

Python AutoML Vision如何处理尺寸小于224x224像素的图像?

Python AutoML Vision如何处理尺寸小于224x224像素的图像?,python,tensorflow,google-cloud-automl,Python,Tensorflow,Google Cloud Automl,我用80x80x3图像样本训练了AutoML视觉。培训已成功完成,我下载了edge tflite模型。在python中实现tflite模型时,根据这一点,我意识到tflite模型的输入大小是224x224x3 我的问题是: 在培训期间,AutoML Vision如何处理尺寸小于224x224的图像?AutoML Vision是否通过插值将图像调整为224x224 为了获得更好的预测性能,我希望按照AutoML Vision在训练期间处理图像的方式来处理新图像 将带有输入形状(1、80、80、

我用80x80x3图像样本训练了AutoML视觉。培训已成功完成,我下载了edge tflite模型。在python中实现tflite模型时,根据这一点,我意识到tflite模型的输入大小是224x224x3

我的问题是:

  • 在培训期间,AutoML Vision如何处理尺寸小于224x224的图像?AutoML Vision是否通过插值将图像调整为224x224
为了获得更好的预测性能,我希望按照AutoML Vision在训练期间处理图像的方式来处理新图像

将带有输入形状(1、80、80、3)的80x80图像馈送到模型时,出现异常“无法设置张量:尺寸不匹配”,请参见下面的代码

馈送224x224图像无异常。然而,我想使用80x80x3的图像,就像我用于培训一样。或者像在AutoML Vision中训练时一样预处理80x80x3图像,例如,将其大小调整为224x224x3或AutoML Vision处理的方式

test_sample.shape

Out: (80, 80, 3)

test_sample = test_sample.reshape(1, 80, 80, 3)

Out: (1, 80, 80, 3)    

# Load TFLite model and allocate tensors. 
interpreter = tf.lite.Interpreter(model_path=model_path) 
interpreter.allocate_tensors() 

# Get input and output tensors. 
input_details = interpreter.get_input_details()

print(interpreter.get_input_details())

Out: [{'name': 'image', 'index': 0, 'shape': array([  1, 224, 224,   3], dtype=int32), 'dtype': <class 'numpy.uint8'>, 'quantization': (0.007874015718698502, 128)}]

output_details = interpreter.get_output_details() 

# Test model on input data. 
input_data = np.array(test_sample, dtype=np.uint8) 
interpreter.set_tensor(input_details[0]['index'], input_data) 

interpreter.invoke() 


Out: ValueError: Cannot set tensor: Dimension mismatch 
ValueError                                Traceback (most recent call last)
in engine
----> 1 interpreter.set_tensor(input_details[0]['index'], input_data)

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/lite/python/interpreter.py in set_tensor(self, tensor_index, value)
   173       ValueError: If the interpreter could not set the tensor.
   174     """
--> 175     self._interpreter.SetTensor(tensor_index, value)
   176 
   177   def resize_tensor_input(self, input_index, tensor_size):

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py in SetTensor(self, i, value)
   134 
   135     def SetTensor(self, i, value):
--> 136         return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_SetTensor(self, i, value)
   137 
   138     def GetTensor(self, i):

ValueError: Cannot set tensor: Dimension mismatch
test\u sample.shape
输出:(80,80,3)
测试样本=测试样本。重塑(1,80,80,3)
输出:(1,80,80,3)
#加载TFLite模型并分配张量。
解释器=tf.lite.解释器(模型路径=model路径)
解释器。分配_张量()
#获取输入和输出张量。
input\u details=解释器。获取\u input\u details()
打印(解释器。获取输入详细信息()
Out:[{'name':'image','index':0,'shape':数组([1224224,3],dtype=int32),'dtype':,'quantization':(0.007874015718698502,128)}]
output\u details=解释器。获取\u output\u details()
#输入数据上的测试模型。
输入数据=np.array(测试样本,数据类型=np.uint8)
解释器。设置解释器张量(输入解释器详细信息[0]['index'],输入解释器数据)
invoke()解释器
Out:ValueError:无法设置张量:尺寸不匹配
ValueError回溯(最近一次调用上次)
在引擎中
---->1.设置张量(输入详细信息[0]['index'],输入数据)
/set_tensor(self,tensor_index,value)中的home/cdsw/.local/lib/python3.6/site-packages/tensorflow/lite/python/explorer.py
173 ValueError:如果解释器无法设置张量。
174     """
-->175自解释设置器(张量索引,值)
176
177 def RESIGH_tensor_input(自我、输入索引、张量大小):
/setensor中的home/cdsw/.local/lib/python3.6/site-packages/tensorflow/lite/python/explorer\u wrapper/tensorflow\u wrapper\u wrapper\u wrapper.py(self,i,value)
134
135 def设置器(自身、i、值):
-->136返回\u tensorflow\u wrap\u解释器\u wrapper.translatorwrapper\u settersor(self,i,value)
137
138 def GetTensor(自我,i):
ValueError:无法设置张量:维度不匹配

我建议您使用此实现

设置数据类型

# check the type of the input tensor
floating_model = input_details[0]['dtype'] == np.float32
根据所需的模型高度和宽度调整图像大小

height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
img = Image.open(args.image).resize((width, height))
设置输入数据

input_data = np.expand_dims(img, axis=0)
下面是完整的例子
我建议您使用此实现

设置数据类型

# check the type of the input tensor
floating_model = input_details[0]['dtype'] == np.float32
根据所需的模型高度和宽度调整图像大小

height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
img = Image.open(args.image).resize((width, height))
设置输入数据

input_data = np.expand_dims(img, axis=0)
下面是完整的例子

它为您调整图像大小。我使用netron检查tflite和tensorflow模型时观察到了这一点。查找输入并跟踪输出,通过解码器进行调整大小操作。

它为您调整图像大小。我使用netron检查tflite和tensorflow模型时观察到了这一点。查找输入并跟踪输出将解码器重新调整大小操作。

也许我应该提出一个新问题,但是如果输入大小不是正方形怎么办?我从AutoML生成了一个tflite文件,输入大小是224x224。但是我的输入是矩形的。如何解决这个问题呢?也许我应该提出一个新问题,但是如果输入大小不是正方形怎么办?我想ted是AutoML中的一个tflite文件,输入大小为224x224。但是我的输入是矩形的。如何处理这个问题?