-
Notifications
You must be signed in to change notification settings - Fork 3
/
chexpert.sex.py
293 lines (226 loc) · 9.64 KB
/
chexpert.sex.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
import pandas as pd
import numpy as np
import torchvision
import torchvision.transforms as T
from torchvision import models
import pytorch_lightning as pl
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.callbacks import ModelCheckpoint
from skimage.io import imread
from skimage.io import imsave
from tqdm import tqdm
from argparse import ArgumentParser
image_size = (224, 224)
num_classes = 2
batch_size = 150
epochs = 50
num_workers = 4
img_data_dir = '<path_to_data>/CheXpert-v1.0/'
class CheXpertDataset(Dataset):
def __init__(self, csv_file_img, image_size, augmentation=False, pseudo_rgb = True):
self.data = pd.read_csv(csv_file_img)
self.image_size = image_size
self.do_augment = augmentation
self.pseudo_rgb = pseudo_rgb
self.augment = T.Compose([
T.RandomHorizontalFlip(p=0.5),
T.RandomApply(transforms=[T.RandomAffine(degrees=15, scale=(0.9, 1.1))], p=0.5),
])
self.samples = []
for idx, _ in enumerate(tqdm(range(len(self.data)), desc='Loading Data')):
img_path = img_data_dir + self.data.loc[idx, 'path_preproc']
img_label = np.array(self.data.loc[idx, 'sex_label'], dtype='int64')
sample = {'image_path': img_path, 'label': img_label}
self.samples.append(sample)
def __len__(self):
return len(self.data)
def __getitem__(self, item):
sample = self.get_sample(item)
image = torch.from_numpy(sample['image']).unsqueeze(0)
label = torch.from_numpy(sample['label'])
if self.do_augment:
image = self.augment(image)
if self.pseudo_rgb:
image = image.repeat(3, 1, 1)
return {'image': image, 'label': label}
def get_sample(self, item):
sample = self.samples[item]
image = imread(sample['image_path']).astype(np.float32)
return {'image': image, 'label': sample['label']}
class CheXpertDataModule(pl.LightningDataModule):
def __init__(self, csv_train_img, csv_val_img, csv_test_img, image_size, pseudo_rgb, batch_size, num_workers):
super().__init__()
self.csv_train_img = csv_train_img
self.csv_val_img = csv_val_img
self.csv_test_img = csv_test_img
self.image_size = image_size
self.batch_size = batch_size
self.num_workers = num_workers
self.train_set = CheXpertDataset(self.csv_train_img, self.image_size, augmentation=True, pseudo_rgb=pseudo_rgb)
self.val_set = CheXpertDataset(self.csv_val_img, self.image_size, augmentation=False, pseudo_rgb=pseudo_rgb)
self.test_set = CheXpertDataset(self.csv_test_img, self.image_size, augmentation=False, pseudo_rgb=pseudo_rgb)
print('#train: ', len(self.train_set))
print('#val: ', len(self.val_set))
print('#test: ', len(self.test_set))
def train_dataloader(self):
return DataLoader(self.train_set, self.batch_size, shuffle=True, num_workers=self.num_workers)
def val_dataloader(self):
return DataLoader(self.val_set, self.batch_size, shuffle=False, num_workers=self.num_workers)
def test_dataloader(self):
return DataLoader(self.test_set, self.batch_size, shuffle=False, num_workers=self.num_workers)
class ResNet(pl.LightningModule):
def __init__(self, num_classes):
super().__init__()
self.num_classes = num_classes
self.model = models.resnet34(pretrained=True)
# freeze_model(self.model)
num_features = self.model.fc.in_features
self.model.fc = nn.Linear(num_features, self.num_classes)
def forward(self, x):
return self.model.forward(x)
def configure_optimizers(self):
params_to_update = []
for param in self.parameters():
if param.requires_grad == True:
params_to_update.append(param)
optimizer = torch.optim.Adam(params_to_update, lr=0.001)
return optimizer
def unpack_batch(self, batch):
return batch['image'], batch['label']
def process_batch(self, batch):
img, lab = self.unpack_batch(batch)
out = self.forward(img)
loss = F.cross_entropy(out, lab)
return loss
def training_step(self, batch, batch_idx):
loss = self.process_batch(batch)
self.log('train_loss', loss)
grid = torchvision.utils.make_grid(batch['image'][0:4, ...], nrow=2, normalize=True)
self.logger.experiment.add_image('images', grid, self.global_step)
return loss
def validation_step(self, batch, batch_idx):
loss = self.process_batch(batch)
self.log('val_loss', loss)
def test_step(self, batch, batch_idx):
loss = self.process_batch(batch)
self.log('test_loss', loss)
class DenseNet(pl.LightningModule):
def __init__(self, num_classes):
super().__init__()
self.num_classes = num_classes
self.model = models.densenet121(pretrained=True)
# freeze_model(self.model)
num_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(num_features, self.num_classes)
def forward(self, x):
return self.model.forward(x)
def configure_optimizers(self):
params_to_update = []
for param in self.parameters():
if param.requires_grad == True:
params_to_update.append(param)
optimizer = torch.optim.Adam(params_to_update, lr=0.001)
return optimizer
def unpack_batch(self, batch):
return batch['image'], batch['label']
def process_batch(self, batch):
img, lab = self.unpack_batch(batch)
out = self.forward(img)
loss = F.cross_entropy(out, lab)
return loss
def training_step(self, batch, batch_idx):
loss = self.process_batch(batch)
self.log('train_loss', loss)
grid = torchvision.utils.make_grid(batch['image'][0:4, ...], nrow=2, normalize=True)
self.logger.experiment.add_image('images', grid, self.global_step)
return loss
def validation_step(self, batch, batch_idx):
loss = self.process_batch(batch)
self.log('val_loss', loss)
def test_step(self, batch, batch_idx):
loss = self.process_batch(batch)
self.log('test_loss', loss)
def freeze_model(model):
for param in model.parameters():
param.requires_grad = False
def test(model, data_loader, device):
model.eval()
preds = []
targets = []
with torch.no_grad():
for index, batch in enumerate(tqdm(data_loader, desc='Test-loop')):
img, lab = batch['image'].to(device), batch['label'].to(device)
pred = torch.softmax(model(img), dim=1)
preds.append(pred)
targets.append(lab)
preds = torch.cat(preds, dim=0)
targets = torch.cat(targets, dim=0)
counts = []
for i in range(0,num_classes):
t = targets == i
c = torch.sum(t)
counts.append(c)
print(counts)
return preds.cpu().numpy(), targets.cpu().numpy()
def main(hparams):
# sets seeds for numpy, torch, python.random and PYTHONHASHSEED.
pl.seed_everything(42, workers=True)
# data
data = CheXpertDataModule(csv_train_img='../datafiles/chexpert/chexpert.sample.train.csv',
csv_val_img='../datafiles/chexpert/chexpert.sample.val.csv',
csv_test_img='../datafiles/chexpert/chexpert.sample.test.csv',
image_size=image_size,
pseudo_rgb=True,
batch_size=batch_size,
num_workers=num_workers)
# model
model_type = DenseNet
model = model_type(num_classes=num_classes)
# Create output directory
out_name = 'densenet-all'
out_dir = 'chexpert/sex/' + out_name
if not os.path.exists(out_dir):
os.makedirs(out_dir)
temp_dir = os.path.join(out_dir, 'temp')
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
for idx in range(0,5):
sample = data.train_set.get_sample(idx)
imsave(os.path.join(temp_dir, 'sample_' + str(idx) + '.jpg'), sample['image'].astype(np.uint8))
checkpoint_callback = ModelCheckpoint(monitor="val_loss", mode='min')
# train
trainer = pl.Trainer(
callbacks=[checkpoint_callback],
log_every_n_steps = 5,
max_epochs=epochs,
gpus=hparams.gpus,
logger=TensorBoardLogger('chexpert/sex', name=out_name),
)
trainer.logger._default_hp_metric = False
trainer.fit(model, data)
model = model_type.load_from_checkpoint(trainer.checkpoint_callback.best_model_path, num_classes=num_classes)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:" + str(hparams.dev) if use_cuda else "cpu")
model.to(device)
cols_names = ['class_' + str(i) for i in range(0,num_classes)]
print('VALIDATION')
preds_val, targets_val = test(model, data.val_dataloader(), device)
df = pd.DataFrame(data=preds_val, columns=cols_names)
df['target'] = targets_val
df.to_csv(os.path.join(out_dir, 'predictions.val.csv'), index=False)
print('TESTING')
preds_test, targets_test = test(model, data.test_dataloader(), device)
df = pd.DataFrame(data=preds_test, columns=cols_names)
df['target'] = targets_test
df.to_csv(os.path.join(out_dir, 'predictions.test.csv'), index=False)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--gpus', default=1)
parser.add_argument('--dev', default=0)
args = parser.parse_args()
main(args)