-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_strides.py
37 lines (34 loc) · 1.58 KB
/
calculate_strides.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def get_kernels_strides():
"""
This function is only used for decathlon datasets with the provided patch sizes.
When refering this method for other tasks, please ensure that the patch size for each spatial dimension should
be divisible by the product of all strides in the corresponding dimension.
In addition, the minimal spatial size should have at least one dimension that has twice the size of
the product of all strides. For patch sizes that cannot find suitable strides, an error will be raised.
"""
sizes, spacings = [224, 224, 96], [0.7958985, 0.7958985, 2.5]
input_size = sizes
strides, kernels = [], []
while True:
spacing_ratio = [sp / min(spacings) for sp in spacings]
stride = [
2 if ratio <= 2 and size >= 8 else 1
for (ratio, size) in zip(spacing_ratio, sizes)
]
kernel = [3 if ratio <= 2 else 1 for ratio in spacing_ratio]
if all(s == 1 for s in stride):
break
for idx, (i, j) in enumerate(zip(sizes, stride)):
if i % j != 0:
raise ValueError(
f"Patch size is not supported, please try to modify the size {input_size[idx]} in the spatial dimension {idx}."
)
sizes = [i / j for i, j in zip(sizes, stride)]
spacings = [i * j for i, j in zip(spacings, stride)]
kernels.append(kernel)
strides.append(stride)
strides.insert(0, len(spacings) * [1])
kernels.append(len(spacings) * [3])
return kernels, strides
if __name__=="__main__":
print(get_kernels_strides())