|
6 | 6 | from torchvision import transforms |
7 | 7 | import graph_net |
8 | 8 |
|
9 | | -if __name__ == "__main__": |
| 9 | + |
| 10 | +def extract_visio_graph(model_name, model_path): |
10 | 11 | # Normalization parameters for ImageNet |
11 | 12 | normalize = transforms.Normalize( |
12 | 13 | mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] |
|
20 | 21 | normalized_input = normalize(random_input) |
21 | 22 |
|
22 | 23 | # Instantiate model |
23 | | - model = torchvision.models.get_model("resnet18", weights="DEFAULT") |
| 24 | + model = torchvision.models.get_model(model_path, weights="DEFAULT") |
24 | 25 | model.eval() |
25 | 26 |
|
26 | 27 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
27 | 28 | model.to(device) |
28 | 29 | normalized_input = normalized_input.to(device) |
29 | 30 |
|
30 | | - model = graph_net.torch.extract(name="resnet18", dynamic=True)(model) |
| 31 | + model = graph_net.torch.extract(name=model_name, dynamic=True)(model) |
31 | 32 |
|
32 | 33 | print("Running inference...") |
33 | 34 | print("Input shape:", normalized_input.shape) |
34 | 35 | output = model(normalized_input) |
35 | 36 | print("Inference finished. Output shape:", output.shape) |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + # get parameters from command line |
| 41 | + workspace_default = os.environ.get("GRAPH_NET_EXTRACT_WORKSPACE", "workspace") |
| 42 | + |
| 43 | + parser = argparse.ArgumentParser() |
| 44 | + parser.add_argument("--model_name", type=str, default="resnet18") |
| 45 | + parser.add_argument("--model_path", type=str, default="resnet18") # timm 模型名称 |
| 46 | + parser.add_argument("--workspace", type=str, default=workspace_default) |
| 47 | + parser.add_argument("--dynamic", type=bool, default=True) |
| 48 | + args = parser.parse_args() |
| 49 | + |
| 50 | + os.environ["GRAPH_NET_EXTRACT_WORKSPACE"] = args.workspace |
| 51 | + |
| 52 | + extract_visio_graph(args.model_name, args.model_path) |
0 commit comments