-
Notifications
You must be signed in to change notification settings - Fork 0
/
chest2.py
505 lines (416 loc) · 16.4 KB
/
chest2.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# %%
try:
%load_ext autotime
except:
print("Console warning-- Autotime is jupyter platform specific")
# %%
import os
from tqdm import tqdm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from comet_ml import Experiment
import icecream as ic
from IPython.display import display
from torch.utils.data import DataLoader, Dataset, random_split
import torch
import random
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
from pydicom import dcmread
torch.cuda._initialized = True
# %%
experiment = Experiment(api_key="xleFjfKO3kcwc56tglgC1d3zU",
project_name="Chest Xray",log_code=True)
# %%
import pandas as pd
df = pd.read_csv('/media/gyasis/Drive 2/Data/vinbigdata/train.csv')
df.head(10)
# %%
df = df[['image_id', 'class_name','class_id']]
torch.cuda.empty_cache()
# %%
def build_path(x):
path_ = '/media/gyasis/Drive 2/Data/vinbigdata/train/'
filetype = '.dicom'
x = (path_+x+filetype)
return x
# %%
import os.path
# %%
df['imagepath'] = df['image_id'].apply(lambda x: build_path(x))
df = df[['imagepath','class_name','class_id']]
df.head()
# %%
pd.get_dummies(df['class_name'])
df1 = pd.get_dummies(df['class_id'].astype(str))
# %%
#mapping for later use
disease= ["Aortic enlargement"
,"Atelectasis"
,"Calcification"
,"Cardiomegaly"
,"Consolidation"
,"ILD"
,"Infiltration"
,"Lung Opacity"
,"Nodule/Mass"
,"Other lesion"
,"Pleural effusion"
,"Pleural thickening"
,"Pneumothorax"
,"Pulmonary fibrosis"
,"No_finding"]
#map df.class_id to disease
# df['class_id_test'] = df['class_id'].map(lambda x: disease[x])
df.head()
df1.columns = df1.columns.astype(int).map(lambda x: disease[x])
s_array = np.array(df1)
# %%
def get_class_frequencies():
positive_freq = s_array.sum(axis=0) / s_array.shape[0]
negative_freq = np.ones(positive_freq.shape) - positive_freq
return positive_freq, negative_freq
p,n = get_class_frequencies()
# %%
data = pd.DataFrame({"Class": df1.columns, "Label": "Positive", "Value": p})
data = data.append([{"Class": df1.columns[l], "Label": "Negative", "Value": v} for l, v in enumerate(n)], ignore_index=True)
plt.xticks(rotation=90)
f = sns.barplot(x="Class", y="Value",hue="Label", data=data)
plt.savefig("skewness.png")
experiment.log_image(image_data = 'skewness.png')
# %%
pos_weights = n
neg_weights = p
pos_contribution = p * pos_weights
neg_contribution = n * neg_weights
print(p)
print(n)
print("Weight to be added: ",pos_contribution)
# %%
data = pd.DataFrame({"Class": df1.columns, "Label": "Positive", "Value": pos_contribution})
data = data.append([{"Class": df1.columns[l], "Label": "Negative", "Value": v} for l, v in enumerate(neg_contribution)], ignore_index=True)
plt.xticks(rotation=90)
g = sns.barplot(x="Class", y="Value",hue="Label", data=data)
plt.savefig("Balanced.png")
experiment.log_image(image_data = "Balanced.png")
# %%
import torchvision
from torchvision import transforms
import albumentations as A
from albumentations.pytorch import ToTensorV2
c_transform = nn.Sequential(transforms.Resize([256,]),
transforms.CenterCrop(224),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)))
ten = torchvision.transforms.ToTensor()
scripted_transforms = torch.jit.script(c_transform)
# %%
transform = A.Compose(
[A.Resize(width=256,height=256, always_apply=True),
A.HorizontalFlip(p=0.5),
A.OneOf([
A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.25),
A.RandomBrightnessContrast(p=0.1, contrast_limit=0.005, brightness_limit=0.005,),
A.InvertImg(p=0.02),
]),
A.OneOf([
A.RandomCrop(width=224, height=224, p=0.5),
A.CenterCrop(width=224, height=224, p=0.5),
]),
A.Resize(width=224, height=224, always_apply=True),
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
ToTensorV2()
])
W_o_ten_transform = A.Compose(
[A.Resize(width=256,height=256, always_apply=True),
A.HorizontalFlip(p=0.5),
A.OneOf([
A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.25),
A.RandomBrightnessContrast(p=0.1, contrast_limit=0.05, brightness_limit=0.05,),
A.InvertImg(p=0.02),
]),
A.OneOf([
A.RandomCrop(width=224, height=224, p=0.5),
A.CenterCrop(width=224, height=224, p=0.5),
]),
A.Resize(width=224, height=224, always_apply=True),
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
# ToTensorV2()
])
# %%
class MyDataset(Dataset):
def __init__(self, dataset, transform=None):
print('creating Dataset')
self.df = dataset
self.imagearray = np.asarray(dataset.imagepath)
self.class_arr = np.asarray(dataset.class_id)
self.transform = transform
self.data_len = len(dataset.index)
def __getitem__(self, index):
ds = dcmread(self.imagearray[index])
arr = ds.pixel_array
arr = arr.astype('float')
class_id = self.df.loc[index, 'class_id']
arr = ten(arr)
arr = arr.expand(3, -1,-1)
arr = scripted_transforms(arr)
return arr, class_id
def __len__(self):
return self.data_len
class AlbumentationsDataset(Dataset):
def __init__(self, dataset, transform=transform):
self.df = dataset
self.imagearray = np.asarray(dataset.imagepath)
self.class_arr = np.asarray(dataset.class_id)
self.transform = transform
self.data_len = len(dataset.index)
def __len__(self):
return self.data_len
def __getitem__(self, index):
ds = dcmread(self.imagearray[index])
class_id = self.df.loc[index, 'class_id']
arr = ds.pixel_array
arr = np.stack((arr,)*3, axis=-1)
arr = transform(image = arr)["image"]
return arr, class_id
class VisualDataset(Dataset):
def __init__(self, dataset, transform=transform):
self.df = dataset
self.imagearray = np.asarray(dataset.imagepath)
self.class_arr = np.asarray(dataset.class_id)
self.transform = transform
self.data_len = len(dataset.index)
def __len__(self):
return self.data_len
def __getitem__(self, index):
ds = dcmread(self.imagearray[index])
class_id = self.df.loc[index, 'class_id']
arr = ds.pixel_array
arr = np.stack((arr,)*3, axis=-1)
arr = W_o_ten_transform(image = arr)["image"]
return arr, class_id
# %%
ChestData = MyDataset(df, transform=None)
# train_dataloader = DataLoader(train_ds, batch_size=128)
# %%
ChestData_Aug = AlbumentationsDataset(df, transform=transform)
ChestData_Visual = VisualDataset(df, transform=transform)
# %%
set_batchsize = 128
# %%
from torch.utils.data import DataLoader, Dataset, random_split
num_items = len(ChestData_Aug)
num_train = round(num_items * 0.7)
num_val = num_items - num_train
train_ds, val_ds = random_split(ChestData_Aug, [num_train, num_val])
train_dataloader = DataLoader(train_ds, batch_size=set_batchsize, num_workers=4, pin_memory=True, shuffle=True)
val_dataloader = DataLoader(val_ds,batch_size=set_batchsize, num_workers=4, pin_memory=True,shuffle=False)
# %%
from torchvision import models
import torch
model = models.resnet18(pretrained=True)
print(model)
# %%
from torch import nn as nn
num_classes = 15
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, num_classes)
device = torch.device("cuda:0")
model= model.to(device)
# %%
df.class_id.unique()
# %%
# torch.manual_seed(17)
# %%
import copy
def visualize_augmentations(dataset, idx=12,iterate='random', samples=9, cols=3):
dataset = copy.deepcopy(dataset)
dataset.transform = transform
rows = samples // cols
figure, ax = plt.subplots(nrows=rows, ncols=cols, figsize=(12, 12))
for i in range(samples):
rando = random.randint(0,len(dataset)-1)
if (iterate=='random'):
image, _ = dataset[rando]
ax.ravel()[i].imshow(image[:,:,0],cmap='gray')
ax.ravel()[i].set_axis_off()
else:
image, _ = dataset[i]
ax.ravel()[i].imshow(image[0,:,:],cmap='gray')
ax.ravel()[i].set_axis_off()
plt.tight_layout()
filename = 'augmented_images_' + str(rando) + '.png'
plt.savefig(filename)
experiment.log_image(image_data = filename)
plt.show()
# %%
for t in range(9):
visualize_augmentations(ChestData_Visual, idx=5, samples=9, cols=3)
# %%
for t in range(9):
visualize_augmentations(ChestData, idx=5, samples=9,iterate='iterate', cols=3)
# # %%
# random.seed(42)
# visualize_augmentations(ChestData)
# %%
def training(model, train_dataloader, num_epochs):
optimizer_name = torch.optim.SGD(model.parameters(), lr=0.01)
# criterion = nn.CrossEntropyLoss()
criterion = nn.CrossEntropyLoss(weight=torch.tensor(pos_contribution).type(torch.FloatTensor).to(device))
optimizer = optimizer_name
scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer,
max_lr=0.01,
steps_per_epoch=int(len(train_dataloader)),
epochs=num_epochs,
anneal_strategy='linear')
for epoch in tqdm(range(num_epochs)):
running_loss = 0.0
correct_prediction = 0
total_prediction = 0
for i, data, in enumerate(tqdm(train_dataloader)):
inputs = data[0].float().to(device)
labels = data[1].to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
scheduler.step()
running_loss += loss.item()
_, prediction = torch.max(outputs, 1)
correct_prediction += (prediction == labels).sum().item()
total_prediction += prediction.shape[0]
running_acc = correct_prediction/total_prediction
experiment.log_metric("Train/train_accuracy", running_acc, epoch)
try:
experiment.log_metric("Loss/train",running_loss/i, epoch)
except:
print('div by zero')
if i > 2:
if i % 20 == 0:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / i))
num_batches = len(train_dataloader)
avg_loss = running_loss / num_batches
acc = correct_prediction/total_prediction
print(f'Epoch: {epoch}, Loss: {avg_loss:.2f}, Accuracy: {acc:.2f}')
experiment.log_metric("Accuracy", acc, epoch)
num_epochs = 6
training(model, train_dataloader, num_epochs)
experiment.end()
# %%
print(model)
# %%
# ----------------------------
# Inference
# ----------------------------
def inference (model, x):
correct_prediction = 0
total_prediction = 0
# Disable gradient updates
with torch.no_grad():
for data in x:
# Get the input features and target labels, and put them on thresige GPU
inputs = data[0].float().to(device)
labels = data[1].to(device)
# Normalize the inputs
inputs_m, inputs_s = inputs.mean(), inputs.std()
inputs = (inputs - inputs_m) / inputs_s
# Get predictions
outputs = model(inputs)
# Get the predicted class with the highest score
_, prediction = torch.max(outputs,1)
# Count of predictions that matched the target label
correct_prediction += (prediction == labels).sum().item()
total_prediction += prediction.shape[0]
ic.ic(prediction)
acc = correct_prediction/total_prediction
print(f'Accuracy: {acc:.2f}, Total items: {total_prediction}')
# Run inference on trained model with the validation set
inference(model, val_dataloader)
# %%
import copy
def visualize_augmentations(dataset, idx=12, samples=9, cols=3):
dataset = copy.deepcopy(dataset)
dataset.transform = transform
rows = samples // cols
figure, ax = plt.subplots(nrows=rows, ncols=cols, figsize=(12, 12))
for i in range(samples):
image, _ = dataset[i]
ax.ravel()[i].imshow(image[0,:,:],cmap='gray')
ax.ravel()[i].set_axis_off()
plt.tight_layout()
plt.show()
# %%
random.seed(42)
visualize_augmentations(ChestData)
# %%
ds = dcmread(df.imagepath[5])
arr = ds.pixel_array
# arr = arr.astype('float')
arr = np.stack((arr,)*3, axis=-1)
print(arr.shape)
print(arr.dtype)
transformed = transform(image = arr)["image"]
def visualize(image):
plt.figure(figsize=(10, 10))
plt.axis('off')
plt.imshow(image)
visualize(transformed)
def training(model, train_dataloader, num_epochs):
optimizer_name = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()
optimizer = optimizer_name
scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer,
max_lr=0.01,
epochs=num_epochs,
anneal_strategy='linear')
for epoch in tqdm(range(num_epochs)):
running_loss = 0.0
correct_prediction = 0
total_prediction = 0
for i, data, in enumerate(tqdm(train_dataloader)):
inputs = data[0].float().to(device)
labels = data[1].to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
scheduler.step()
running_loss += loss.item()
_, prediction = torch.max(outputs, 1)
correct_prediction += (prediction == labels).sum().item()
total_prediction += prediction.shape[0]
running_acc = correct_prediction/total_prediction
num_batches = len(train_dataloader)
avg_loss = running_loss / num_batches
acc = correct_prediction/total_prediction
num_epochs = 6
training(model, train_dataloader, num_epochs)
# this function takes a datset, list of columns to augment, by a factor, and a savepath
# it will augment the dataset by the factor, and save the augmented images to the savepath
# then returns the augmented dataset as a dataframe
def augment_data(dataset, listoftoaugmentclass, numofaugmentation ,savepath):
path = []
class = []
for i in listoftoaugmentclass:
print(i)
for j in random.sample(dataset[j][1], len(dataset)):
if dataset[j][1] == i:
print(dataset[j][1])
ds = dcmread(dataset[j][0])
arr = ds.pixel_array
arr = arr.astype('float')
arr = np.stack((arr,)*3, axis=-1)
transformed = transform(image = arr)["image"]
savepath = savepath + str(i) + '/'
if not os.path.exists(savepath):
os.makedirs(savepath)
plt.imsave(savepath + str(j) + '.png', transformed)
path.append(savepath + str(j) + '.png')
class.append(i)
df = pd.DataFrame(list(zip(path,class)),columns=['imagepath','class'])
return df
# for loop in range of lenth of list but random interation
def loop_random(len(df1)):