我是靠谱客的博主 魁梧烤鸡,这篇文章主要介绍【ONNX】Faster-rcnn 转 ONNX,现在分享给大家,希望可以做个参考。

Faster RCNN代码可以参考下面链接:

https://github.com/facebookresearch/maskrcnn-benchmark

最后模型会保存为.pth格式,可以通过如下代码转为ONNX

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os import numpy from io import BytesIO from matplotlib import pyplot import requests import torch import timeit from PIL import Image from maskrcnn_benchmark.config import cfg from predictor import COCODemo from maskrcnn_benchmark.structures.image_list import ImageList import pytorch_export_patch if __name__ == "__main__": # load config from file and command-line arguments project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) cfg.merge_from_file( os.path.join(project_dir, "configs/caffe2/e2e_faster_rcnn_R_50_FPN_1x_caffe2.yaml")) cfg.merge_from_list(["MODEL.DEVICE", "cpu"]) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=0.7, show_mask_heatmaps=False, masks_per_dim=2, min_image_size=480, ) # prepare for onnx export coco_demo.model.prepare_onnx_export() def single_image_to_top_predictions(image): image_list = ImageList(image.unsqueeze(0), [(int(image.size(-2)), int(image.size(-1)))]) for param in coco_demo.model.parameters(): param.requires_grad = False result, = coco_demo.model(image_list) scores = result.get_field("scores") result = (result.bbox, result.get_field('labels'), scores) return result class FRCNNModel(torch.nn.Module): def forward(self, image): return single_image_to_top_predictions(image) def fetch_image(url): response = requests.get(url) return Image.open(BytesIO(response.content)).convert("RGB") if __name__ == '__main__': pil_image = fetch_image( url="http://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg") """ Preprocessing image. """ # convert to BGR format image = torch.from_numpy(numpy.array(pil_image)[:, :, [2, 1, 0]]) original_image = image image = torch.nn.functional.upsample(image.permute(2, 0, 1).unsqueeze(0).to(torch.float), size=(960, 1280)).to(torch.uint8).squeeze(0).permute(1, 2, 0).to('cpu') image = image.permute(2, 0, 1) if not cfg.INPUT.TO_BGR255: image = image.float() / 255.0 image = image[[2, 1, 0]] else: image = image.float() # we absolutely want fixed size (int) here (or we run into a tracing error (or bug?) # or we might later decide to make things work with variable size... image = image - torch.tensor(cfg.INPUT.PIXEL_MEAN)[:, None, None].to('cpu') model_path = 'faster_rcnn_R_50_FPN_1x.onnx' with torch.no_grad(): model = FRCNNModel() model.eval() torch.onnx.export(model, (image,), model_path, verbose=True, opset_version=10, strip_doc_string=True, do_constant_folding=True) pytorch_export_patch.postprocess_model(model_path)

 参考:https://github.com/BowenBao/maskrcnn-benchmark/blob/onnx_stage/demo/export_to_onnx.py

最后

以上就是魁梧烤鸡最近收集整理的关于【ONNX】Faster-rcnn 转 ONNX的全部内容,更多相关【ONNX】Faster-rcnn内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部