-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
181 lines (129 loc) · 5.08 KB
/
resnet.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
# train and run resnet on CXR images and guess continuous CTR
import os;
import glob;
import time;
import torch;
import argparse;
from PIL import Image;
from torch.utils.data import Dataset;
from torch.utils.data import DataLoader;
from torch import nn;
from torch.nn.functional import mse_loss as mean_square_error;
from torchvision import models;
#from torchvision.datasets import MNIST;
from torchvision import transforms;
#from torchvision.io import write_png;
#from sklearn.metrics import accuracy_score;
from matplotlib import pyplot as plt;
BATCH_SIZE = 4;
WORKERS = 4;
LEARNING_RATE = 0.0001;
EPOCHS = 2;
TRANSFORM = transforms.Compose([
transforms.ToTensor(),
# transforms.Grayscale() # PIL reads as grayscale
transforms.ConvertImageDtype(torch.float),
# Normalization?
]);
parser = argparse.ArgumentParser(description="Train and test RESNET18 on CXRs to predict CTR (with MSE loss and Adam optimizer).");
parser.add_argument("--test-only", action="store_true", help="skip training, load model, and test");
class ToFloat():
def __init__(self):
self.target_dtype = torch.float
def __call__(self, scalar):
return torch.tensor(scalar, dtype=self.target_dtype);
class CTRData(Dataset):
def __init__(self, cxr_dir, ctr_file, transform = None, target_transform = None):
super(CTRData, self).__init__();
self.imgs = glob.glob(os.path.join(cxr_dir, "*"));
f = open(ctr_file, "r");
lines = f.read().split("\n");
f.close();
self.ctr_map = {};
for line in lines:
tokens = line.split();
if len(tokens) > 0:
self.ctr_map[tokens[0]] = float(tokens[2]);
self.transform = transform;
self.target_transform = target_transform;
def __getitem__(self, i):
x = Image.open(self.imgs[i]);
y = self.ctr_map[ os.path.basename(self.imgs[i]) ];
if self.transform:
x = self.transform(x);
if self.target_transform:
y = self.target_transform(y);
return x, y
def __len__(self):
return len(self.imgs);
def debug(self):
print(self.imgs);
print(self.ctr_map);
class Resnet(nn.Module):
"Resnet18 adapted for CXR and CTR prediction"
def __init__(self):
super(Resnet, self).__init__();
self.resnet18 = models.resnet18(); # random weights
self.resnet18.conv1 = nn.Conv2d(1, 64, 7, stride = 2, padding = 3); # first layer, grayscale input
self.resnet18.fc = nn.Linear(512, 1, bias=True); # last computation layer
self.flatten = nn.Flatten(0); # make output scalar (1D array for batch)
def forward(self, x):
return self.flatten( self.resnet18(x) );
def train(loader, model, loss_func, optim, epochs):
steps = len(loader);
model.train();
for epoch in range(epochs):
for i, (x_batch, y_batch) in enumerate(loader):
#print(x_batch);
#print("---");
#print(y_batch);
y_pred = model(x_batch);
#print("---");
#print(y_pred);
loss = loss_func(y_pred, y_batch);
optim.zero_grad();
loss.backward();
optim.step();
if( True or (i+1)%100 == 0 ): # print everything atm
print(f"Epoch {epoch+1}/{epochs}, Step {i+1}/{steps}, Loss {loss.item()}");
def test(loader, model):
model.eval();
scores = [];
with torch.no_grad():
#correct = 0;
#total = 0;
for images, y_batch in loader:
y_pred = model(images);
mse = mean_square_error(y_batch, y_pred);
scores.append(mse);
print(f"Micro-averaged Mean Squared Error {sum(scores)/len(scores)}")
if __name__ == "__main__":
args = parser.parse_args();
print("===");
print("Loading & Initializing Data");
print("===");
train_cxr_folder = os.path.join('data', 'split', 'preprocessed', 'train');
test_cxr_folder = os.path.join('data', 'split', 'preprocessed', 'test');
labels_file = os.path.join('data', 'CTR_Logs.txt');
train_set = CTRData(train_cxr_folder, labels_file, transform=TRANSFORM, target_transform=ToFloat());
test_set = CTRData(test_cxr_folder, labels_file, transform=TRANSFORM, target_transform=ToFloat());
train_loader = DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=WORKERS);
test_loader = DataLoader(test_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=WORKERS);
resnet = Resnet();
mse_loss = nn.MSELoss();
adam = torch.optim.Adam(resnet.parameters(), lr = LEARNING_RATE);
print(resnet);
if not args.test_only:
print("===");
print("Training Model");
print("===");
start = time.time();
train(train_loader, resnet, mse_loss, adam, EPOCHS);
print(f"Finished in {(time.time() - start) / 60} minutes");
torch.save(resnet.state_dict(), "resnet.pt"); # give better name?
print("===");
print("Testing");
print("===");
if args.test_only:
resnet.load_state_dict(torch.load("resnet.pt"));
test(test_loader, resnet);