openvino系列 2. 修改模型的输入尺寸

openvino系列 2. 修改模型的输入尺寸

环境描述:

  • 本案例运行环境:Win10
  • IDE:VSCode
  • openvino版本:2022.1
  • 代码链接1-openvino-basicworkflow文件夹


1 修改模型的输入尺寸

除了修改图像大小以适应模型,我们还可以修改模型输入尺寸以适应图像。 请注意,并非所有模型都支持reshape,并且支持reshape的模型可能不支持所有输入尺寸。 如果我们reshape模型输入尺寸,模型精度也可能会受到影响。

我们首先检查模型的输入尺寸,然后重塑为新的输入尺寸。

分割网络的输入形状是 [1,3,512,512],图像尺寸为 N、C、H、W 格式:网络的输出宽度和高度为512,批为1,3通道。这里我们reshape网络为[1,1,544,544]。使用IENetwork.reshape()方法,宽度和高度为544。这个分割网络总是返回与输入宽度和高度相同的数组,因此将输入尺寸设置为 544x544 也会修改输出尺寸。尺寸调整完之后,再次编译网络(ie.compile_model)。

我们也可以修改网络输入的批大小(Batch)。我们依旧使用.reshape()函数。道理和调整网络输入大小是一样的。输出显示,通过将批大小(Batch size)设置为 2,输入和输出形状的第一个元素 (N) 现在的值为 2。我们尝试输入一个尺寸为[2,3,544,544]的数组,模型推理之后(segmentation_compiled_model([input_data]))得到的尺寸就是[2,1,544,544]。

from openvino.runtime import Core, PartialShape

print("1 load network. Here we use a segmentation IR model.")
ie = Core()
segmentation_model_xml = "model/segmentation.xml"
segmentation_model = ie.read_model(model=segmentation_model_xml)
segmentation_input_layer = segmentation_model.input(0)
segmentation_output_layer = segmentation_model.output(0)

print("~~~~ ORIGINAL MODEL ~~~~")
print("- model input shape: {}".format(segmentation_input_layer.shape))
print("- model output shape: {}".format(segmentation_output_layer.shape))

print("2 Change the model input shape ")
new_shape = PartialShape([1, 3, 544, 544])
segmentation_model.reshape({segmentation_input_layer.any_name: new_shape})
segmentation_compiled_model = ie.compile_model(model=segmentation_model, device_name="CPU")
# help(segmentation_compiled_model)
print("~~~~ RESHAPED MODEL Size ~~~~")
print("- model input shape: {}".format(segmentation_input_layer.shape))
print("- compiled model input shape: {}".format(segmentation_compiled_model.input(index=0).shape))
print("- compiled model output shape: {}".format(segmentation_output_layer.shape))

print("3 Change the model input batch size ")
# Model reload
segmentation_model_xml = "model/segmentation.xml"
segmentation_model = ie.read_model(model=segmentation_model_xml)
segmentation_input_layer = segmentation_model.input(0)
segmentation_output_layer = segmentation_model.output(0)
new_shape = PartialShape([2, 3, 544, 544])
segmentation_model.reshape({segmentation_input_layer.any_name: new_shape})
segmentation_compiled_model = ie.compile_model(model=segmentation_model, device_name="CPU")
print("~~~~ RESHAPED MODEL Batch ~~~~")
print("- compiled model input shape: {}".format(segmentation_input_layer.shape))
print("- compiled model output shape: {}".format(segmentation_output_layer.shape))
print("- now let's try to input data into this network.")
input_data = np.random.rand(2, 3, 544, 544)
output = segmentation_compiled_model([input_data])
print("- input data shape: {}".format(input_data.shape))
print("- result data data shape: {}".format(segmentation_output_layer.shape))

结果如下:

1 load network. Here we use a segmentation IR model.
~~~~ ORIGINAL MODEL ~~~~
- model input shape: {1, 3, 512, 512}
- model output shape: {1, 1, 512, 512}
2 Change the model input shape 
~~~~ RESHAPED MODEL Size ~~~~
- model input shape: {1, 3, 544, 544}
- compiled model input shape: {1, 3, 544, 544}
- compiled model output shape: {1, 1, 544, 544}
3 Change the model input batch size 
~~~~ RESHAPED MODEL Batch ~~~~
- compiled model input shape: {2, 3, 544, 544}
- compiled model output shape: {2, 1, 544, 544}
- now let's try to input data into this network.
- input data shape: (2, 3, 544, 544)
- result data data shape: {2, 1, 544, 544}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破浪会有时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值