-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
154 lines (128 loc) · 5.47 KB
/
predict.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
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
import os
import time
import random
import argparse
import numpy as np
import pandas as pd
import cv2
import PIL.Image
from tqdm import tqdm
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
import torch
from torch.utils.data import DataLoader, Dataset
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data.sampler import RandomSampler, SequentialSampler
from torch.optim.lr_scheduler import CosineAnnealingLR
from util import GradualWarmupSchedulerV2
import apex
from apex import amp
from dataset import get_df, get_transforms, MelanomaDataset
from models import Effnet_Melanoma, Resnest_Melanoma, Seresnext_Melanoma
from train import get_trans
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--kernel-type', type=str, required=True)
parser.add_argument('--data-dir', type=str, default='/raid/')
parser.add_argument('--data-folder', type=int, required=True)
parser.add_argument('--image-size', type=int, required=True)
parser.add_argument('--enet-type', type=str, required=True)
parser.add_argument('--batch-size', type=int, default=64)
parser.add_argument('--num-workers', type=int, default=32)
parser.add_argument('--out-dim', type=int, default=9)
parser.add_argument('--use-amp', action='store_true')
parser.add_argument('--use-meta', action='store_true')
parser.add_argument('--DEBUG', action='store_true')
parser.add_argument('--model-dir', type=str, default='./weights')
parser.add_argument('--log-dir', type=str, default='./logs')
parser.add_argument('--sub-dir', type=str, default='./subs')
parser.add_argument('--eval', type=str, choices=['best', 'best_20', 'final'], default="best")
parser.add_argument('--n-test', type=int, default=8)
parser.add_argument('--CUDA_VISIBLE_DEVICES', type=str, default='0')
parser.add_argument('--n-meta-dim', type=str, default='512,128')
args, _ = parser.parse_known_args()
return args
def main():
df, df_test, meta_features, n_meta_features, mel_idx = get_df(
args.kernel_type,
args.out_dim,
args.data_dir,
args.data_folder,
args.use_meta
)
transforms_train, transforms_val = get_transforms(args.image_size)
if args.DEBUG:
df_test = df_test.sample(args.batch_size * 3)
dataset_test = MelanomaDataset(df_test, 'test', meta_features, transform=transforms_val)
test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, num_workers=args.num_workers)
# load model
models = []
for fold in range(5):
if args.eval == 'best':
model_file =
os.path.join(args.model_dir, f'{args.kernel_type}_best_fold{fold}.pth')
elif args.eval == 'best_20':
model_file = os.path.join(args.model_dir, f'{args.kernel_type}_best_20_fold{fold}.pth')
if args.eval == 'final':
model_file = os.path.join(args.model_dir, f'{args.kernel_type}_final_fold{fold}.pth')
model = ModelClass(
args.enet_type,
n_meta_features=n_meta_features,
n_meta_dim=[int(nd) for nd in args.n_meta_dim.split(',')],
out_dim=args.out_dim
)
model = model.to(device)
try: # single GPU model_file
model.load_state_dict(torch.load(model_file), strict=True)
except: # multi GPU model_file
state_dict = torch.load(model_file)
state_dict = {k[7:] if k.startswith('module.') else k: state_dict[k] for k in state_dict.keys()}
model.load_state_dict(state_dict, strict=True)
if len(os.environ['CUDA_VISIBLE_DEVICES']) > 1:
model = torch.nn.DataParallel(model)
model.eval()
models.append(model)
# predict
PROBS = []
with torch.no_grad():
for (data) in tqdm(test_loader):
if args.use_meta:
data, meta = data
data, meta = data.to(device), meta.to(device)
probs = torch.zeros((data.shape[0], args.out_dim)).to(device)
for model in models:
for I in range(args.n_test):
l = model(get_trans(data, I), meta)
probs += l.softmax(1)
else:
data = data.to(device)
probs = torch.zeros((data.shape[0], args.out_dim)).to(device)
for model in models:
for I in range(args.n_test):
l = model(get_trans(data, I))
probs += l.softmax(1)
probs /= args.n_test
probs /= len(models)
PROBS.append(probs.detach().cpu())
PROBS = torch.cat(PROBS).numpy()
# save cvs
df_test['target'] = PROBS[:, mel_idx]
df_test[['image_name', 'target']].to_csv(os.path.join(args.sub_dir, f'sub_{args.kernel_type}_{args.eval}.csv'), index=False)
if __name__ == '__main__':
args = parse_args()
os.makedirs(args.sub_dir, exist_ok=True)
os.environ['CUDA_VISIBLE_DEVICES'] = args.CUDA_VISIBLE_DEVICES
if args.enet_type == 'resnest101':
ModelClass = Resnest_Melanoma
elif args.enet_type == 'seresnext101':
ModelClass = Seresnext_Melanoma
elif 'efficientnet' in args.enet_type:
ModelClass = Effnet_Melanoma
else:
raise NotImplementedError()
DP = len(os.environ['CUDA_VISIBLE_DEVICES']) > 1
device = torch.device('cuda')
main()