Export Pytorch model to TorchScript model and verify the outputs are same between Pytorch and TorchScript. Args: model (nn.Module): Pytorch model we want to export. input_shape (tuple): Use this input shape to construct the corresponding dummy input and execute t
(model,
input_shape,
show=False,
output_file='tmp.pt',
verify=False)
| 87 | |
| 88 | |
| 89 | def pytorch2libtorch(model, |
| 90 | input_shape, |
| 91 | show=False, |
| 92 | output_file='tmp.pt', |
| 93 | verify=False): |
| 94 | """Export Pytorch model to TorchScript model and verify the outputs are |
| 95 | same between Pytorch and TorchScript. |
| 96 | |
| 97 | Args: |
| 98 | model (nn.Module): Pytorch model we want to export. |
| 99 | input_shape (tuple): Use this input shape to construct |
| 100 | the corresponding dummy input and execute the model. |
| 101 | show (bool): Whether print the computation graph. Default: False. |
| 102 | output_file (string): The path to where we store the |
| 103 | output TorchScript model. Default: `tmp.pt`. |
| 104 | verify (bool): Whether compare the outputs between |
| 105 | Pytorch and TorchScript. Default: False. |
| 106 | """ |
| 107 | if isinstance(model.decode_head, nn.ModuleList): |
| 108 | num_classes = model.decode_head[-1].num_classes |
| 109 | else: |
| 110 | num_classes = model.decode_head.num_classes |
| 111 | |
| 112 | mm_inputs = _demo_mm_inputs(input_shape, num_classes) |
| 113 | |
| 114 | imgs = mm_inputs.pop('imgs') |
| 115 | |
| 116 | # replace the orginal forword with forward_dummy |
| 117 | model.forward = model.forward_dummy |
| 118 | model.eval() |
| 119 | traced_model = torch.jit.trace( |
| 120 | model, |
| 121 | example_inputs=imgs, |
| 122 | check_trace=verify, |
| 123 | ) |
| 124 | |
| 125 | if show: |
| 126 | print(traced_model.graph) |
| 127 | |
| 128 | traced_model.save(output_file) |
| 129 | print('Successfully exported TorchScript model: {}'.format(output_file)) |
| 130 | |
| 131 | |
| 132 | def parse_args(): |
no test coverage detected