-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpneumoniacnn.py
351 lines (273 loc) · 10.5 KB
/
pneumoniacnn.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
# -*- coding: utf-8 -*-
"""pneumoniaCNN.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1D_BbJ0BbZmgoJ6zIXTnNFheV6pb4AlCb
Importing packages
"""
import os
import torch
import torchvision
import tarfile
from torchvision.datasets.utils import download_url
from torch.utils.data import random_split
"""Naming project"""
project_name='normal-vs-pneumonia-cnn'
"""Downloading Dataset
"""
#installation du package kaggle
!pip install -q kaggle
#Création d'un dossier kaggle
!mkdir ~/.kaggle/
#Copier kaggle.json dans le dossier Kaggle
!cp '/content/drive/MyDrive/Kaggle/kaggle.json' ~/.kaggle/
#donner les droits à kaggle.json
!chmod 600 ~/.kaggle/kaggle.json
#télécharger le dataset
! kaggle datasets download -d paultimothymooney/chest-xray-pneumonia
#unzip dataset and move it to drive to avoid it being deleted
! unzip /content/chest-xray-pneumonia.zip -d pneumonia_data
#resizing images
import PIL
import os
from PIL import Image
directories=['/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/test/NORMAL','/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/test/PNEUMONIA',
'/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/train/NORMAL','/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/train/PNEUMONIA']
for d in directories:
for file in os.listdir(d):
d_img = d+"/"+file
img = Image.open(d_img)
img = img.resize((64,64))
img.save(d_img)
data_dir = '/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray'
print(os.listdir(data_dir))
classes = os.listdir(data_dir + "/train")
print(classes)
"""pneumonia_files = os.listdir(data_dir + "/train/PNEUMONIA")
print('No. of training examples for pneumonia cases:', len(pneumonia_files))
print(pneumonia_files[:5])
normal_train_files = os.listdir(data_dir + "/train/NORMAL")
print("No. of test examples for normal cases:", len(normal_train_files))
print(normal_train_files[:5])
"""
from torchvision.datasets import ImageFolder
from torchvision.transforms import ToTensor
dataset = ImageFolder(data_dir+'/train', transform=ToTensor())
#img, label = dataset[0]
#print(img.shape, label)
#img
#print(dataset.classes)
# Commented out IPython magic to ensure Python compatibility.
import matplotlib
import matplotlib.pyplot as plt
# %matplotlib inline
matplotlib.rcParams['figure.facecolor'] = '#ffffff'
import cv2 as cv
def Denoising (img):
return cv.fastNlMeansDenoising(img,None,3,7,21)
def show_example(img, label):
print('Label: ', dataset.classes[label], "("+str(label)+")")
plt.imshow(img.permute(1, 2, 0))
"""!pip install jovian --upgrade -q
import jovian
jovian.commit(project=project_name)"""
#test_ds,train_ds, val_ds = '/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/test','/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/train','/content/drive/MyDrive/Kaggle/pneumonia_data/chest_xray/chest_xray/val'
random_seed = 42
torch.manual_seed(random_seed);
val_size = len(dataset)//10
train_size = len(dataset) - val_size
train_ds, val_ds = random_split(dataset, [train_size, val_size])
len(train_ds), len(val_ds)
from torch.utils.data.dataloader import DataLoader
batch_size=10
train_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=4, pin_memory=True)
val_dl = DataLoader(val_ds, batch_size*2, num_workers=4, pin_memory=True)
def apply_kernel(image, kernel):
ri, ci = image.shape # image dimensions
rk, ck = kernel.shape # kernel dimensions
ro, co = ri-rk+1, ci-ck+1 # output dimensions
output = torch.zeros([ro, co])
for i in range(ro):
for j in range(co):
output[i,j] = torch.sum(image[i:i+rk,j:j+ck] * kernel)
return output
sample_image = torch.tensor([
[3, 3, 2, 1, 0],
[0, 0, 1, 3, 1],
[3, 1, 2, 2, 3],
[2, 0, 0, 2, 2],
[2, 0, 0, 0, 1]
], dtype=torch.float32)
sample_kernel = torch.tensor([
[0, 1, 2],
[2, 2, 0],
[0, 1, 2]
], dtype=torch.float32)
apply_kernel(sample_image, sample_kernel)
import torch.nn as nn
import torch.nn.functional as F
simple_model = nn.Sequential(
nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(2, 2)
)
for images, labels in train_dl:
print('images.shape:', images.shape)
out = simple_model(images)
print('out.shape:', out.shape)
break
"""defining the model by extending an** ImageClassificationBase **class which contains helper methods for training & validation."""
class ImageClassificationBase(nn.Module):
def training_step(self, batch):
images, labels = batch
out = self(images) # Generate predictions
loss = F.cross_entropy(out, labels) # Calculate loss
return loss
def validation_step(self, batch):
images, labels = batch
out = self(images) # Generate predictions
loss = F.cross_entropy(out, labels) # Calculate loss
acc = accuracy(out, labels) # Calculate accuracy
return {'val_loss': loss.detach(), 'val_acc': acc}
def validation_epoch_end(self, outputs):
batch_losses = [x['val_loss'] for x in outputs]
epoch_loss = torch.stack(batch_losses).mean() # Combine losses
batch_accs = [x['val_acc'] for x in outputs]
epoch_acc = torch.stack(batch_accs).mean() # Combine accuracies
return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}
def epoch_end(self, epoch, result):
print("Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}".format(
epoch, result['train_loss'], result['val_loss'], result['val_acc']))
def accuracy(outputs, labels):
_, preds = torch.max(outputs, dim=1)
return torch.tensor(torch.sum(preds == labels).item() / len(preds))
class PneumoniaCNN(ImageClassificationBase):
def __init__(self):
super().__init__()
self.network = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2), # output: 128 x 32 x 32
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2), # output: 256 x 16 x 16
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2), # output: 512 x 8 x 8
nn.Flatten(),
nn.Linear(512*8*8, 2048),
nn.ReLU(),
nn.Linear(2048, 1024),
nn.ReLU(),
nn.Linear(1024, 2))
def forward(self, xb):
return self.network(xb)
model = PneumoniaCNN()
model
for images, labels in train_dl:
print('images.shape:', images.shape)
out = model(images)
print('out.shape:', out.shape)
print('out[0]:', out[0])
break
def get_default_device():
"""Pick GPU if available, else CPU"""
if torch.cuda.is_available():
return torch.device('cuda')
else:
return torch.device('cpu')
def to_device(data, device):
"""Move tensor(s) to chosen device"""
if isinstance(data, (list,tuple)):
return [to_device(x, device) for x in data]
return data.to(device, non_blocking=True)
class DeviceDataLoader():
"""Wrap a dataloader to move data to a device"""
def __init__(self, dl, device):
self.dl = dl
self.device = device
def __iter__(self):
"""Yield a batch of data after moving it to device"""
for b in self.dl:
yield to_device(b, self.device)
def __len__(self):
"""Number of batches"""
return len(self.dl)
device = get_default_device()
device
train_dl = DeviceDataLoader(train_dl, device)
val_dl = DeviceDataLoader(val_dl, device)
to_device(model, device);
"""defining two functions: **fit** and **evaluate** to train the model using **gradient descent** and evaluate its performance on the validation set"""
@torch.no_grad()
def evaluate(model, val_loader):
model.eval()
outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs)
def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):
history = []
optimizer = opt_func(model.parameters(), lr)
for epoch in range(epochs):
# Training Phase
model.train()
train_losses = []
for batch in train_loader:
loss = model.training_step(batch)
train_losses.append(loss)
loss.backward()
optimizer.step()
optimizer.zero_grad()
# Validation phase
result = evaluate(model, val_loader)
result['train_loss'] = torch.stack(train_losses).mean().item()
model.epoch_end(epoch, result)
history.append(result)
return history
model = to_device(PneumoniaCNN(), device)
evaluate(model, val_dl)
num_epochs = 10
opt_func = torch.optim.Adam
lr = 0.001
history = fit(num_epochs, lr, model, train_dl, val_dl, opt_func)
def plot_accuracies(history):
accuracies = [x['val_acc'] for x in history]
plt.plot(accuracies, '-x')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.title('Accuracy vs. No. of epochs');
plot_accuracies(history)
def plot_losses(history):
train_losses = [x.get('train_loss') for x in history]
val_losses = [x['val_loss'] for x in history]
plt.plot(train_losses, '-bx')
plt.plot(val_losses, '-rx')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['Training', 'Validation'])
plt.title('Loss vs. No. of epochs');
plot_losses(history)
"""# Testing the model
"""
test_dataset = ImageFolder(data_dir+'/test', transform=ToTensor())
def predict_image(img, model):
# Convert to a batch of 1
xb = to_device(img.unsqueeze(0), device)
# Get predictions from model
yb = model(xb)
# Pick index with highest probability
_, preds = torch.max(yb, dim=1)
# Retrieve the class label
return dataset.classes[preds[0].item()]
img, label = test_dataset[75]
plt.imshow(img.permute(1, 2, 0))
print('Label:', dataset.classes[label], ', Predicted:', predict_image(img, model))
test_loader = DeviceDataLoader(DataLoader(test_dataset, batch_size*2), device)
result = evaluate(model, test_loader)
result
model2 = to_device(PneumoniaCNN(), device)
evaluate(model2, test_loader)