erickerfoot commited on
Commit
f69094e
·
verified ·
1 Parent(s): f47f263

Uploading contents of MONAI-extra-test-data

Browse files
create_data.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import torch
4
+ from typing import Any, Dict, Sequence
5
+
6
+ import monai.networks.nets as nets
7
+
8
+
9
+ def create_model_test_data(
10
+ model_name: str,
11
+ model_params: Dict[str, Any],
12
+ input_shape: Sequence[int],
13
+ ) -> None:
14
+ """
15
+ Create test data to check model consistency
16
+
17
+ Args:
18
+ model_class: Name of model to be tested.
19
+ model_params: Dictionary of parameters to construct object.
20
+ input_shape: Tuple of dimensions (B, C, H, W, [D]).
21
+
22
+ .. code-block:: python
23
+
24
+ # network params
25
+ unet_params = {
26
+ "dimensions" : 3,
27
+ "in_channels" : 4,
28
+ "out_channels" : 2,
29
+ "channels": (4, 8, 16, 32),
30
+ "strides": (2, 4, 1),
31
+ "kernel_size" : 5,
32
+ "up_kernel_size" : 3,
33
+ "num_res_units": 2,
34
+ "act": "relu",
35
+ "dropout": 0.1,
36
+ }
37
+ # in shape
38
+ input_shape = (1, unet_params["in_channels"], 64, 64, 64)
39
+ # create data
40
+ create_model_test_data("UNet", unet_params, input_shape)
41
+ """
42
+ model_name = model_name.lower()
43
+ base_folder = os.path.dirname(os.path.abspath(__file__))
44
+
45
+ # get next unused folder
46
+ i=0
47
+ while True:
48
+ out_folder = os.path.join(base_folder, f"{model_name}_{i}")
49
+ if not os.path.isdir(out_folder):
50
+ print("\n\nCreating output folder: " + out_folder)
51
+ os.mkdir(out_folder)
52
+ break
53
+ i += 1
54
+ out_path_no_ext = os.path.join(out_folder, f"{model_name}_{i}")
55
+
56
+ # Create model
57
+ model = nets.__dict__[model_name](**model_params)
58
+ model.eval()
59
+
60
+ # Create input data
61
+ num_elements = int(torch.Tensor(input_shape).prod())
62
+ in_data = torch.arange(num_elements).reshape(input_shape).float()
63
+
64
+ # Forward pass data
65
+ out_data = model(in_data)
66
+
67
+ # Save in data, out data and model
68
+ data_path = out_path_no_ext + ".pt"
69
+ to_save = {"in_data": in_data, "out_data": out_data, "model": model.state_dict()}
70
+ print("Writing data output to .pt: " + data_path)
71
+ torch.save(to_save, data_path)
72
+
73
+ # Save parameters
74
+ json_params = out_path_no_ext + ".json"
75
+ with open(json_params, "w+") as f:
76
+ print("Writing network parameters to .json: " + json_params)
77
+ json.dump(model_params, f)
78
+
79
+
80
+
81
+ # default
82
+ if __name__ == "__main__":
83
+
84
+ # network params
85
+ unet_params = {
86
+ "dimensions" : 3,
87
+ "in_channels" : 4,
88
+ "out_channels" : 2,
89
+ "channels": (4, 8, 16, 32),
90
+ "strides": (2, 4, 1),
91
+ "kernel_size" : 5,
92
+ "up_kernel_size" : 3,
93
+ "num_res_units": 2,
94
+ "act": "relu",
95
+ "dropout": 0.1,
96
+ }
97
+ # in shape
98
+ input_shape = (1, unet_params["in_channels"], 64, 64, 64)
99
+ # create data
100
+ create_model_test_data("UNet", unet_params, input_shape)
dynunet_0/Dynunet_1.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"spatial_dims": 3, "in_channels": 4, "out_channels": 2, "kernel_size": [3, 3, 3, 1], "strides": [2, 2, 2, 2], "upsample_kernel_size": [2, 2, 2]}
dynunet_0/Dynunet_1.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:662e895f07075f3ab6ca8b5bf7473ba26b57a3525ef6c7b3d4ff85ff4907d63c
3
+ size 11569537
dynunet_0/dynunet_0.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from create_data import create_model_test_data
2
+ strides = (2, 2, 2, 2)
3
+
4
+ # network params
5
+ params = {
6
+ "spatial_dims": 3,
7
+ "in_channels": 4,
8
+ "out_channels": 2,
9
+ "kernel_size": (3, 3, 3, 1),
10
+ "strides": strides,
11
+ "upsample_kernel_size": strides[1:],
12
+ }
13
+ # in shape
14
+ input_shape = (1, params["in_channels"], 64, 64, 64)
15
+ # create data
16
+ create_model_test_data("DynUNet", params, input_shape)
unet_0/Unet_0.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"spatial_dims": 3, "in_channels": 4, "out_channels": 2, "channels": [4, 8, 16, 32], "strides": [2, 4, 1], "kernel_size": 5, "up_kernel_size": 3, "num_res_units": 2, "act": "relu", "dropout": 0.1}
unet_0/Unet_0.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:09573859799471a60debc13635cb79a5cf6c7351c443399a65491a89294545d3
3
+ size 7453957