-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_seg.py
192 lines (137 loc) · 4.66 KB
/
train_seg.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
#!/usr/bin/env python
# coding: utf-8
# Author: Akshaj Verma
import os
import pathlib
import argparse
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from torch.utils.data import DataLoader
## Util Imports
from utils.load_data import DatasetChestXrayMontShznJsrt, create_samplers
from utils.load_model import ModelDeconvSmall
from utils.load_train_loop import train_loop
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
set_seed(100)
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, help="Dataset")
parser.add_argument("--subset", type=str, help="Subset")
parser.add_argument("--model_name", type=str, help="Model Name")
parser.add_argument("--img_msk_size", type=int, help="Image and Mask Size")
parser.add_argument("--batch_size", type=int, help="Batch Size")
parser.add_argument("--epochs", type=int, help="Epochs")
parser.add_argument("--learning_rate", type=float, help="Learning Rate")
ap = parser.parse_args()
######################
## Config ##
######################
DATA_NAME = ap.dataset # montgomery, shenzhen, jsrt
DATA_SUBSET = ap.subset # full, normal, abnormal
MODEL_NAME = ap.model_name # "deconv_small_decodertransposeinit_sqex"
IMAGE_MASK_SIZE = ap.img_msk_size # 512
BATCH_SIZE = ap.batch_size # 16
EPOCHS = ap.epochs # 60
LEARNING_RATE = ap.learning_rate # 0.01
print(f"\nDataset Name: {DATA_NAME}")
print(f"Data Subset: {DATA_SUBSET}")
print(f"Model Name: {MODEL_NAME}")
print(f"Input size = {IMAGE_MASK_SIZE}")
print(f"Batch size = {BATCH_SIZE}")
print(f"Epochs = {EPOCHS}")
print(f"Learning Rate= {LEARNING_RATE}")
# Summary Writer
writer = SummaryWriter(f"runs/{DATA_NAME}_{DATA_SUBSET}_{MODEL_NAME}")
## Define Paths and Set GPU
images_path = pathlib.Path(f"../../data/{DATA_NAME}/train/images/")
masks_path = pathlib.Path(f"../../data/{DATA_NAME}/train/masks/")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("\nWe're using =>", device)
######################
## Load Data ##
######################
## Create Dataset
image_transforms = {
"train": transforms.Compose([transforms.ToTensor()]),
"test": transforms.Compose([transforms.ToTensor()]),
}
print("\nImages path: ", images_path)
print("Masks path: ", masks_path)
# Initialize Dataset
dataset = DatasetChestXrayMontShznJsrt(
images_path=images_path,
masks_path=masks_path,
image_transform=image_transforms["train"],
img_msk_size=IMAGE_MASK_SIZE,
dataset_name=DATA_NAME,
dsubset=DATA_SUBSET,
)
## Train - Val Split
train_sampler, val_sampler = create_samplers(dataset, 0.8)
## Create Dataloaders
train_loader = DataLoader(
dataset=dataset, shuffle=False, batch_size=BATCH_SIZE, sampler=train_sampler,
)
val_loader = DataLoader(
dataset=dataset, shuffle=False, batch_size=1, sampler=val_sampler
)
## Data Sanity Check
print(f"\nTrain loader = {next(iter(train_loader))[0].shape}")
print(f"Val loader = {next(iter(val_loader))[0].shape}")
print(f"Train loader length = {len(train_loader)}")
print(f"Val loader length = {len(val_loader)}")
#################################
## Load Model and Config ##
#################################
# init model
model = ModelDeconvSmall(num_class=2)
# Sample data loader examples
x_train_example, y_train_example = next(iter(train_loader))
y_pred_example = model(x_train_example)
print("\nShape of output mask = ", y_pred_example.shape)
# Add model graph to tensorboard
writer.add_graph(model, (next(iter(train_loader))[0]))
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(
model.parameters(), lr=LEARNING_RATE, momentum=0.9, weight_decay=0.0005
)
scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.0001, max_lr=0.001)
model_save_path = f"../../models/seg"
print(f"\nModels are saved here: {model_save_path}")
# Move model to GPU (if available)
model.to(device)
print("\n")
print("=" * 50)
######################
## Train Loop ##
######################
print("\nBegin training.")
train_loop(
epochs=EPOCHS,
model=model,
optimizer=optimizer,
scheduler=scheduler,
criterion=criterion,
writer=writer,
data_name=DATA_NAME,
data_subset=DATA_SUBSET,
model_name=MODEL_NAME,
train_loader=train_loader,
val_loader=val_loader,
model_save_path=model_save_path,
device=device,
)
# close tensorboard writer
writer.close()
print("\n\nTraining is over.")