-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_utils.py
67 lines (53 loc) · 1.78 KB
/
main_utils.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
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
import cv2
import torch
import torch.nn as nn
from models2D.unet import UNet
from models2D.unet_do import UNet_DO
import numpy as np
def parse(parser):
arg = parser.add_argument
arg('--image_path', type=str)
arg('--model_path', type=str)
arg('--result_path', type=str)
arg('--unc_method', type=str, choices=['TTA', 'Ensemble', 'MCDO'])
arg('--gpu_number', type=str)
args = parser.parse_args()
return args
def load_image(_path: str) -> np.ndarray:
im = cv2.imread(_path)
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
s = 96
im = cv2.resize(im, (s, s))
return im
def load_image_3D_CT(_path: str) -> np.ndarray:
im = cv2.imread(_path)
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
s = 96
im = cv2.resize(im, (s, s))
return im
def to_tensor(im: np.ndarray) -> torch.Tensor:
if len(im.shape) != 3:
im = im[:, :, None]
im = np.array([im.transpose((2, 0, 1))])
return torch.from_numpy(im).to(torch.float32)
def load_model(_path: str, _n_channels: int, _n_classes: int):
model = UNet(n_channels=_n_channels,
n_classes=_n_classes)
state = torch.load(str(_path))
epoch = state['epoch']
model.load_state_dict(state['model'])
print('Restored model, epoch {}'.format(epoch))
return model
def load_model_DO(_path: str, _n_channels: int, _n_classes: int):
model = UNet_DO(n_channels=_n_channels,
n_classes=_n_classes)
state = torch.load(str(_path))
epoch = state['epoch']
model.load_state_dict(state['model'])
print('Restored model, epoch {}'.format(epoch))
return model
def model_parallel(model):
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model)
return model