-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeopolyp.py
196 lines (161 loc) · 6.03 KB
/
Neopolyp.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import albumentations as A
import pandas as pd
import numpy as np
import cv2
import time
import imageio
import matplotlib.pyplot as plt
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import segmentation_models_pytorch as smp
from torch.optim import lr_scheduler
from torch import Tensor
from UNetDataCLass import UNetDataClass
from SegDataClass import SegDataClass
from CEDiceLoss import CEDiceLoss
from PIL import Image
from torch.utils.data import Dataset, DataLoader, random_split
from torchvision.transforms import Resize, PILToTensor, ToPILImage, Compose, InterpolationMode
from collections import OrderedDict
from torchsummary import summary
from torchgeometry.losses import one_hot
from torch.utils.data import ConcatDataset
def weights_init(model):
if isinstance(model, nn.Linear):
torch.nn.init.xavier_uniform_(model.weight)
def save_model(model, optimizer, path):
checkpoint = {
"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
}
torch.save(checkpoint, path)
def load_model(model, optimizer, path):
checkpoint = torch.load(path)
model.load_state_dict(checkpoint["model"])
optimizer.load_state_dict(checkpoint['optimizer'])
return model, optimizer
# Train function for each epoch
def train(train_dataloader, valid_dataloader,learing_rate_scheduler, epoch, display_step):
print(f"Start epoch #{epoch+1}, learning rate for this epoch: {learing_rate_scheduler.get_last_lr()}")
start_time = time.time()
train_loss_epoch = 0
test_loss_epoch = 0
last_loss = 999999999
model.train()
for i, (data,targets) in enumerate(train_dataloader):
# Load data into GPU
data, targets = data.to(device), targets.to(device)
optimizer.zero_grad()
outputs = model(data)
# Backpropagation, compute gradients
loss = loss_function(outputs, targets.long())
loss.backward()
# Apply gradients
optimizer.step()
# Save loss
train_loss_epoch += loss.item()
if (i+1) % display_step == 0:
# accuracy = float(test(test_loader))
print('Train Epoch: {} [{}/{} ({}%)]\tLoss: {:.4f}'.format(
epoch + 1, (i+1) * len(data), len(train_dataloader.dataset), 100 * (i+1) * len(data) / len(train_dataloader.dataset),
loss.item()))
print(f"Done epoch #{epoch+1}, time for this epoch: {time.time()-start_time}s")
train_loss_epoch/= (i + 1)
# Evaluate the validation set
model.eval()
with torch.no_grad():
for data, target in valid_dataloader:
data, target = data.to(device), target.to(device)
test_output = model(data)
test_loss = loss_function(test_output, target)
test_loss_epoch += test_loss.item()
test_loss_epoch/= (i+1)
return train_loss_epoch , test_loss_epoch
device = torch.device("cuda" if torch.cuda.is_available () else "cpu")
print(device)
model = smp.UnetPlusPlus(
encoder_name="efficientnet-b4",
encoder_weights="imagenet",
in_channels=3,
classes=3
)
# Hyper params
num_classes = 3
epochs = 50
learning_rate = 1e-3
batch_size = 8
display_step = 50
# Model path
checkpoint_path = '/teamspace/studios/this_studio/NeoPolyps/checkpoints'
pretrained_path = "/teamspace/studios/this_studio/NeoPolyps/checkpoints"
images_path = "/teamspace/studios/this_studio/NeoPolyps/data/train/"
masks_path = "/teamspace/studios/this_studio/NeoPolyps/data/train_gt/"
# Initialize lists to keep track of loss and accuracy
loss_epoch_array = []
train_accuracy = []
test_accuracy = []
valid_accuracy = []
transform = Compose([Resize((512, 512), interpolation=InterpolationMode.BILINEAR), PILToTensor()])
unet_dataset = UNetDataClass(images_path, masks_path, transform)
augmentation = A.Compose([
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.2),
A.ShiftScaleRotate(scale_limit=0.2, rotate_limit=20, shift_limit=0.2, p=0.5)
])
# transform = transforms.ToTensor()
aug_dataset = SegDataClass(images_path, masks_path, transform=transform, augmentation=augmentation)
combined_dataset = ConcatDataset([aug_dataset , unet_dataset])
train_size = 0.8
valid_size = 0.2
torch.manual_seed(42)
train_set, valid_set = random_split(combined_dataset,
[int(train_size * len(combined_dataset)) ,
int(valid_size * len(combined_dataset))])
train_dataloader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
valid_dataloader = DataLoader(valid_set, batch_size=batch_size, shuffle=True)
try:
checkpoint = torch.load(pretrained_path)
new_state_dict = OrderedDict()
for k, v in checkpoint['model'].items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
# load params
model.load_state_dict(new_state_dict)
model = nn.DataParallel(model)
model.to(device)
except:
model.apply(weights_init)
model = nn.DataParallel(model)
model.to(device)
weights = torch.Tensor([[0.4, 0.55, 0.05]]).cuda()
loss_function = CEDiceLoss(weights)
# Define the optimizer (Adam optimizer)
optimizer = optim.Adam(params=model.parameters(), lr=learning_rate)
try:
optimizer.load_state_dict(checkpoint['optimizer'])
except:
pass
# Learning rate scheduler
learing_rate_scheduler = lr_scheduler.StepLR(optimizer, step_size=4, gamma=0.8)
save_model(model, optimizer, checkpoint_path)
#Train
train_loss_array = []
test_loss_array = []
last_loss = 9999999999999
for epoch in range(epochs):
train_loss_epoch = 0
test_loss_epoch = 0
(train_loss_epoch, test_loss_epoch) = train(train_dataloader,
valid_dataloader,
learing_rate_scheduler, epoch, display_step)
if test_loss_epoch < last_loss:
save_model(model, optimizer, checkpoint_path)
last_loss = test_loss_epoch
learing_rate_scheduler.step()
train_loss_array.append(train_loss_epoch)
test_loss_array.append(test_loss_epoch)