MCPcopy Create free account
hub / github.com/Topdu/OpenOCR / export_single_model

Function export_single_model

tools/export_rec.py:37–60  ·  view source on GitHub ↗
(model: torch.nn.Module, _cfg, export_dir,
                        export_config, logger, type)

Source from the content-addressed store, hash-verified

35
36
37def export_single_model(model: torch.nn.Module, _cfg, export_dir,
38 export_config, logger, type):
39 for layer in model.modules():
40 if hasattr(layer, 'rep') and not getattr(layer, 'is_repped'):
41 layer.rep()
42 os.makedirs(export_dir, exist_ok=True)
43
44 export_cfg = {'PostProcess': _cfg['PostProcess']}
45 export_cfg['Transforms'] = build_rec_process(_cfg)
46
47 cfg.save(os.path.join(export_dir, 'config.yaml'), export_cfg)
48
49 dummy_input = torch.randn(*export_config['export_shape'], device='cpu')
50 if type == 'script':
51 save_path = os.path.join(export_dir, 'model.pt')
52 trace_model = torch.jit.trace(model, dummy_input, strict=False)
53 torch.jit.save(trace_model, save_path)
54 elif type == 'onnx':
55 save_path = os.path.join(export_dir, 'model.onnx')
56 to_onnx(model, dummy_input, export_config.get('dynamic_axes', []),
57 save_path)
58 else:
59 raise NotImplementedError
60 logger.info(f'finish export model to {save_path}')
61
62
63def main(cfg, type):

Callers 1

mainFunction · 0.85

Calls 5

build_rec_processFunction · 0.90
repMethod · 0.80
saveMethod · 0.80
getMethod · 0.80
to_onnxFunction · 0.70

Tested by

no test coverage detected