-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGG_cs_fc.py
138 lines (107 loc) · 4.73 KB
/
VGG_cs_fc.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
#%% -------------------------------------- Import Lib --------------------------------------------------------------------
import torch
import torch.nn as nn
import os
import random
import numpy as np
from Helper import train_baseline_model, DataAug, learning_rate_finder, evaluation, FocalLoss
from torchvision import models, transforms
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import f1_score
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
import torch.nn.functional as F
from PIL import Image
from sklearn.utils.class_weight import compute_class_weight
# %% --------------------------------------- Set-Up --------------------------------------------------------------------
SEED = 42
os.environ['PYTHONHASHSEED'] = str(SEED)
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# number of labels
n_classes = 3
# %% -------------------------------------- Data Prep ------------------------------------------------------------------
# load the data
x_train, y_train = np.load("train/x_train.npy"), np.load("train/y_train.npy")
x_valid, y_valid = np.load("train/x_valid.npy"), np.load("train/y_valid.npy")
x_test, y_test = np.load("train/x_test.npy"), np.load("train/y_test.npy")
x_train, y_train = shuffle(x_train, y_train) ## shuffle training set
#%%
weights = compute_class_weight(class_weight='balanced', classes=np.unique(y_train), y=y_train)
class_weights = torch.FloatTensor(weights)
#%% ------------------------------ DataLoader, Data Augmentation ----------------------------------------------------------
# convert to torch.Tensor
# transformation for oversampled training set
train_data_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop(120),
transforms.RandomHorizontalFlip(),
#transforms.RandomVerticalFlip(),
transforms.RandomRotation(degrees=15),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# transformation for original training, validation and testing set
test_data_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.CenterCrop(120),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# batch size
batch_train = 256
batch_test = 512
# apply transformation
trainset = DataAug(x_train, y_train, transform = train_data_transform ,length=5*len(x_train))
valset = DataAug(x_valid, y_valid, transform = test_data_transform, length=len(x_valid))
testset = DataAug(x_test, y_test, transform = test_data_transform, length=len(x_test))
# generate DataLoader
trainloader = DataLoader(trainset, batch_size=batch_train)
valloader = DataLoader(valset, batch_size=batch_test)
testloader = DataLoader(testset, batch_size=batch_test)
#%% --------------------------------- Preparation -----------------------------------------------------------------
model = models.vgg16(pretrained=True)
model.classifier[-1] = nn.Sequential(
nn.Linear(4096, 512),
nn.ReLU(),
nn.Dropout(0.1),
nn.Linear(512, n_classes)
)
for param in model.parameters():
param.requires_grad = False
for param in model.classifier[-1].parameters():
param.requires_grad = True
LR = 1e-4
criterion = nn.CrossEntropyLoss(weight=class_weights)
epochs = 1000
dir = os.path.dirname('Model/')
if not os.path.exists(dir):
os.makedirs(dir)
#loss, rate = learning_rate_finder(model, criterion, min_lr=1e-7, max_lr = 1e-3, iteration = 200, trainloader=trainloader)
path ="Model/vgg16_fc.pt"
train_losses, val_losses = train_baseline_model(model, criterion, LR, epochs, "train_val", trainloader, valloader, path)
#%% -------------------------- Evaluation ---------------------------------------------------------
# load best model weights
model.load_state_dict(torch.load(path))
TPR_val, FNR_val, score_val = evaluation(model, valloader)
TPR_test, FNR_test, score_test = evaluation(model, testloader)
#%% ---------------------------------------- Learning curve ------------------------------------------------------------
inds = np.arange(1,len(val_losses)+1)
plt.figure()
plt.plot(inds.astype(np.uint8), train_losses, label = "training loss")
plt.plot(inds.astype(np.uint8), val_losses, label = "validation loss")
plt.xlabel("Epoch")
plt.ylabel("Magnitude")
plt.title("VGG16 model learning curve")
plt.legend(loc='best')
plt.xticks(np.arange(0, max(inds)+2, 3))
plt.show()
#%%
print("Validation set: sensitivity = {:.4f}, specificity = {:.4f}, score = {:.4f}".format(TPR_val, FNR_val, score_val))
print("Testing set: sensitivity = {:.4f}, specificity = {:.4f}, score = {:.4f}".format(TPR_test, FNR_test, score_test))