Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
168 lines (147 loc) · 5.49 KB
/
Copy pathmodels.py
File metadata and controls
168 lines (147 loc) · 5.49 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import torchmetrics.classification
import torchmetrics.segmentation
import torchmetrics.utilities
import torchmetrics.utilities.data
from typing import Any
import torch
import torch.nn.functional as F
import lightning as L
import torch.nn as nn
from torch import optim
from utils import config
import torchmetrics
from pathlib import Path
from net.PytorchUNet.unet.unet_model import UNet3D
from data_pretrain import generate_DA_transfroms
from net.MedNeXt.nnunet_mednext import create_mednext_v1, MedNeXt
from net.Med3DMaxVit import Med3DMaxViT
from net.SwinUNet import SwinUNETR
from net.SwinUNERT_monai import SwinTransformer as SwinViT
from net.pretrain.PCRLv2.MICCAI_LITS2017.get_model import setup_pcrlv2_model
from net.research_contributions.DAE.BTCV_Finetune.main_for_ngc import get_dae_model
from net.pretrain.GVSL.PyTorch.Downstream.models_finetune.UNet_GVSL import UNet3D_GVSL
from thop import profile, clever_format
def setup_model(weight_path: str = None, model_name: str = None):
"""如果模型越来越多考虑使用 improtlib 模块进行导入,现在懒得管
Returns:
_type_: _description_
"""
if model_name is not None:
config.model_name = model_name
print(f"load model {config.model_name}")
if config.model_name == "native_3dunet":
model = UNet3D()
elif config.model_name == "MedNeXt":
model = create_mednext_v1(
num_input_channels=1,
num_classes=config.num_classes,
model_id=config.MedNeXt_model_id, # S, B, M and L are valid model ids
kernel_size=3, # 3x3x3 and 5x5x5 were tested in publication
deep_supervision=False, # was used in publication
)
elif config.model_name == "Med3DMaxViT":
model = Med3DMaxViT(config.num_classes)
elif config.model_name == "SwinUNETR":
vit = SwinViT(
in_chans=config.vit_in_channels,
embed_dim=config.vit_embed_dim,
window_size=config.vit_window_size,
patch_size=config.vit_patch_size,
depths=config.vit_depths,
num_heads=config.vit_heads,
mlp_ratio=config.vit_mlp_ratio,
qkv_bias=config.vit_qkv_bias,
drop_rate=config.vit_drop_rate,
attn_drop_rate=config.vit_attn_drop_rate,
drop_path_rate=config.vit_dropout_path_rate,
use_checkpoint=config.vit_use_checkpoint,
spatial_dims=config.vit_spatial_dims,
use_v2=config.vit_use_v2,
)
model = SwinUNETR(
img_size=config.patch_shape,
in_channels=1,
out_channels=config.num_classes,
feature_size=config.vit_embed_dim,
vit=vit,
)
elif config.model_name == "SwinViT":
model = SwinViT(
in_chans=config.vit_in_channels,
embed_dim=config.vit_embed_dim,
window_size=config.vit_window_size,
patch_size=config.vit_patch_size,
depths=config.vit_depths,
num_heads=config.vit_heads,
mlp_ratio=config.vit_mlp_ratio,
qkv_bias=config.vit_qkv_bias,
drop_rate=config.vit_drop_rate,
attn_drop_rate=config.vit_attn_drop_rate,
drop_path_rate=config.vit_dropout_path_rate,
use_checkpoint=config.vit_use_checkpoint,
spatial_dims=config.vit_spatial_dims,
use_v2=config.vit_use_v2,
)
elif config.model_name == "PCRLv2":
model = setup_pcrlv2_model(weight_path=weight_path)
elif config.model_name == "DAE":
model = get_dae_model()
elif config.model_name == "GVSL":
model = UNet3D_GVSL(
n_channels=1,
n_classes=config.num_classes,
pretrain_weight_path=Path(config.tensorboard_dir)
/ "AMOS"
/ "pretrain_AMOS"
/ "pretrain_GVSL"
/ "GVSL_epoch_1000.pth",
)
else:
raise ValueError(f"model {config.model_name} not found")
print(
f"number of model parameters is {sum([i.numel() for i in model.parameters()])}"
)
print(f"loadding {config.model_name}")
return model
def mednext_test():
from net.MedNeXt.nnunet_mednext import create_mednext_v1
model = create_mednext_v1(
num_input_channels=1,
num_classes=16,
model_id="S", # S, B, M and L are valid model ids
kernel_size=3, # 3x3x3 and 5x5x5 were tested in publication
deep_supervision=False, # was used in publication
)
model.to(config.gpu_id)
input = torch.rand(size=(1, 1, 80, 80, 80)).to(config.gpu_id)
logit = model(input)
print(logit.max())
print(logit.min())
print(logit.shape)
predit = nn.Softmax(dim=1)(logit)
print(predit.shape)
print(predit.max())
print(predit.min())
def simple_test():
# from net.SwinUNERT_monai import SwinUNETR
model = setup_model()
print(model)
# model.to(config.gpu_id)
# input = torch.rand(size=(1, 1, 96, 96, 96)).to(config.gpu_id)
input = torch.rand(size=(2, 1, 64, 64, 64))
print(model(input).shape)
def print_param():
model: nn.Module = setup_model()
for n, m in model.named_modules():
print(n, sum(mm.numel() for mm in m.parameters()))
def thop_model():
model = setup_model()
input = torch.rand(size=(2, 1, 64, 64, 64))
macs, params = profile(model, inputs=(input,))
macs, params = clever_format([macs, params], "%.3f")
print(macs)
print(params)
if __name__ == "__main__":
# simple_test()
print_param()
# thop_model()