def export_caffe2_tracing(cfg, torch_model, inputs): tracer = Caffe2Tracer(cfg, torch_model, inputs) if args.format == "caffe2": caffe2_model = tracer.export_caffe2() caffe2_model.save_protobuf(args.output) # draw the caffe2 graph caffe2_model.save_graph(os.path.join(args.output, "model.svg"), inputs=inputs) return caffe2_model elif args.format == "onnx": onnx_model = tracer.export_onnx() onnx.save(onnx_model, os.path.join(args.output, "model.onnx")) elif args.format == "torchscript": ts_model = tracer.export_torchscript() ts_model.save(os.path.join(args.output, "model.ts")) dump_torchscript_IR(ts_model, args.output)
def _test_model(self, config_path, device="cpu"): # requires extra dependencies from detectron2.export import Caffe2Model, add_export_config, Caffe2Tracer cfg = model_zoo.get_config(config_path) add_export_config(cfg) cfg.MODEL.DEVICE = device model = model_zoo.get(config_path, trained=True, device=device) inputs = [{"image": get_sample_coco_image()}] c2_model = Caffe2Tracer(cfg, model, copy.deepcopy(inputs)).export_caffe2() with tempfile.TemporaryDirectory(prefix="detectron2_unittest") as d: c2_model.save_protobuf(d) c2_model.save_graph(os.path.join(d, "test.svg"), inputs=copy.deepcopy(inputs)) c2_model = Caffe2Model.load_protobuf(d) c2_model(inputs)[0]["instances"]
def export_caffe2_tracing(cfg, torch_model, inputs): from detectron2.export import Caffe2Tracer tracer = Caffe2Tracer(cfg, torch_model, inputs) if args.format == "caffe2": caffe2_model = tracer.export_caffe2() caffe2_model.save_protobuf(args.output) # draw the caffe2 graph caffe2_model.save_graph(os.path.join(args.output, "model.svg"), inputs=inputs) return caffe2_model elif args.format == "onnx": import onnx onnx_model = tracer.export_onnx() onnx.save(onnx_model, os.path.join(args.output, "model.onnx")) elif args.format == "torchscript": ts_model = tracer.export_torchscript() with PathManager.open(os.path.join(args.output, "model.ts"), "wb") as f: torch.jit.save(ts_model, f) dump_torchscript_IR(ts_model, args.output)
def _test_model(self, config_path, device="cpu"): cfg = model_zoo.get_config(config_path) cfg.MODEL.DEVICE = device model = model_zoo.get(config_path, trained=True, device=device) inputs = [{"image": get_sample_coco_image()}] tracer = Caffe2Tracer(cfg, model, copy.deepcopy(inputs)) with tempfile.TemporaryDirectory(prefix="detectron2_unittest") as d: if not os.environ.get("CI"): # This requires onnx, which is not yet available on public CI c2_model = tracer.export_caffe2() c2_model.save_protobuf(d) c2_model.save_graph(os.path.join(d, "test.svg"), inputs=copy.deepcopy(inputs)) c2_model = Caffe2Model.load_protobuf(d) c2_model(inputs)[0]["instances"] ts_model = tracer.export_torchscript() ts_model.save(os.path.join(d, "model.ts"))
logger = setup_logger() logger.info("Command line arguments: " + str(args)) os.makedirs(args.output, exist_ok=True) cfg = setup_cfg(args) # create a torch model torch_model = build_model(cfg) DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS) # get a sample data data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0]) first_batch = next(iter(data_loader)) # convert and save caffe2 model tracer = Caffe2Tracer(cfg, torch_model, first_batch) if args.format == "caffe2": caffe2_model = tracer.export_caffe2() caffe2_model.save_protobuf(args.output) # draw the caffe2 graph caffe2_model.save_graph(os.path.join(args.output, "model.svg"), inputs=first_batch) elif args.format == "onnx": onnx_model = tracer.export_onnx() onnx.save(onnx_model, os.path.join(args.output, "model.onnx")) elif args.format == "torchscript": script_model = tracer.export_torchscript() script_model.save(os.path.join(args.output, "model.ts")) # Recursively print IR of all modules with open(os.path.join(args.output, "model_ts_IR.txt"), "w") as f: try: