Create export_to_torchscript.py
Browse files- export_to_torchscript.py +93 -0
export_to_torchscript.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Save CTransPath model in TorchScript format.
|
2 |
+
|
3 |
+
Adapted from https://github.com/Xiyue-Wang/TransPath
|
4 |
+
|
5 |
+
Licensed GPL 3.0.
|
6 |
+
"""
|
7 |
+
|
8 |
+
import sys
|
9 |
+
|
10 |
+
# Use the TIMM library with modifications by the CTransPath authors.
|
11 |
+
sys.path.append("timm-0.5.4/")
|
12 |
+
|
13 |
+
import timm
|
14 |
+
from timm.models.layers.helpers import to_2tuple
|
15 |
+
import torch
|
16 |
+
import torch.nn as nn
|
17 |
+
|
18 |
+
assert timm.__version__ == "0.5.4"
|
19 |
+
|
20 |
+
|
21 |
+
class ConvStem(nn.Module):
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
img_size=224,
|
25 |
+
patch_size=4,
|
26 |
+
in_chans=3,
|
27 |
+
embed_dim=768,
|
28 |
+
norm_layer=None,
|
29 |
+
flatten=True,
|
30 |
+
):
|
31 |
+
super().__init__()
|
32 |
+
|
33 |
+
assert patch_size == 4
|
34 |
+
assert embed_dim % 8 == 0
|
35 |
+
|
36 |
+
img_size = to_2tuple(img_size)
|
37 |
+
patch_size = to_2tuple(patch_size)
|
38 |
+
self.img_size = img_size
|
39 |
+
self.patch_size = patch_size
|
40 |
+
self.grid_size = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
|
41 |
+
self.num_patches = self.grid_size[0] * self.grid_size[1]
|
42 |
+
self.flatten = flatten
|
43 |
+
|
44 |
+
stem = []
|
45 |
+
input_dim, output_dim = 3, embed_dim // 8
|
46 |
+
for l in range(2):
|
47 |
+
stem.append(
|
48 |
+
nn.Conv2d(
|
49 |
+
input_dim,
|
50 |
+
output_dim,
|
51 |
+
kernel_size=3,
|
52 |
+
stride=2,
|
53 |
+
padding=1,
|
54 |
+
bias=False,
|
55 |
+
)
|
56 |
+
)
|
57 |
+
stem.append(nn.BatchNorm2d(output_dim))
|
58 |
+
stem.append(nn.ReLU(inplace=True))
|
59 |
+
input_dim = output_dim
|
60 |
+
output_dim *= 2
|
61 |
+
stem.append(nn.Conv2d(input_dim, embed_dim, kernel_size=1))
|
62 |
+
self.proj = nn.Sequential(*stem)
|
63 |
+
|
64 |
+
self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
|
65 |
+
|
66 |
+
def forward(self, x):
|
67 |
+
B, C, H, W = x.shape
|
68 |
+
assert (
|
69 |
+
H == self.img_size[0] and W == self.img_size[1]
|
70 |
+
), f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
|
71 |
+
x = self.proj(x)
|
72 |
+
if self.flatten:
|
73 |
+
x = x.flatten(2).transpose(1, 2) # BCHW -> BNC
|
74 |
+
x = self.norm(x)
|
75 |
+
return x
|
76 |
+
|
77 |
+
|
78 |
+
def ctranspath():
|
79 |
+
model = timm.create_model(
|
80 |
+
"swin_tiny_patch4_window7_224", embed_layer=ConvStem, pretrained=False
|
81 |
+
)
|
82 |
+
return model
|
83 |
+
|
84 |
+
|
85 |
+
model = ctranspath()
|
86 |
+
model.head = torch.nn.Identity()
|
87 |
+
td = torch.load(r"./ctranspath.pth")
|
88 |
+
model.load_state_dict(td["model"], strict=True)
|
89 |
+
|
90 |
+
|
91 |
+
jitted = torch.jit.script(model)
|
92 |
+
|
93 |
+
torch.jit.save(jitted, "torchscript_model.pt")
|