-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswin_chexpert_v2.py
359 lines (273 loc) · 10.6 KB
/
swin_chexpert_v2.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
# %%
import torch
# %%
#Data Loader
import os
import csv
import random, copy
from torch.utils.data import Dataset
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
import timm # Pretrained models like Swin Transformer
import numpy as np
from PIL import Image
# %%
import csv
import pandas as pd
from sklearn.metrics import roc_auc_score
import time
print(torch.cuda.is_available())
class CheXpertDataset(Dataset):
def __init__(self, images_path, file_path, augment, num_class=14,
uncertain_label="LSR-Ones", unknown_label=0, annotation_percent=100):
self.img_list = []
self.img_label = []
self.augment = augment
assert uncertain_label in ["Ones", "Zeros", "LSR-Ones", "LSR-Zeros"]
self.uncertain_label = uncertain_label
with open(file_path, "r") as fileDescriptor:
csvReader = csv.reader(fileDescriptor)
next(csvReader, None)
for line in csvReader:
imagePath = os.path.join(images_path, line[0])
label = line[5:]
# print(label)
for i in range(num_class):
if label[i]:
a = float(label[i])
if a == 1:
label[i] = 1
elif a == 0:
label[i] = 0
elif a == -1: # uncertain label
label[i] = -1
else:
label[i] = unknown_label # unknown label
self.img_list.append(imagePath)
# print(label)
imageLabel = [int(i) for i in label]
self.img_label.append(imageLabel)
indexes = np.arange(len(self.img_list))
if annotation_percent < 100:
random.Random(99).shuffle(indexes)
num_data = int(indexes.shape[0] * annotation_percent / 100.0)
indexes = indexes[:num_data]
_img_list, _img_label = copy.deepcopy(self.img_list), copy.deepcopy(self.img_label)
self.img_list = []
self.img_label = []
for i in indexes:
self.img_list.append(_img_list[i])
self.img_label.append(_img_label[i])
def __getitem__(self, index):
imagePath = self.img_list[index]
imageData = Image.open(imagePath).convert('RGB')
label = []
for l in self.img_label[index]:
if l == -1:
if self.uncertain_label == "Ones":
label.append(1)
elif self.uncertain_label == "Zeros":
label.append(0)
elif self.uncertain_label == "LSR-Ones":
label.append(random.uniform(0.55, 0.85))
elif self.uncertain_label == "LSR-Zeros":
label.append(random.uniform(0, 0.3))
else:
label.append(l)
imageLabel = torch.FloatTensor(label)
if self.augment != None: imageData = self.augment(imageData)
return imageData, imageLabel
def __len__(self):
return len(self.img_list)
# %%
class CheXpertTestDataset(Dataset):
def __init__(self, images_path, file_path, augment, num_class=14,
uncertain_label="LSR-Ones", unknown_label=0, annotation_percent=100):
self.img_list = []
self.img_label = []
self.augment = augment
assert uncertain_label in ["Ones", "Zeros", "LSR-Ones", "LSR-Zeros"]
self.uncertain_label = uncertain_label
with open(file_path, "r") as fileDescriptor:
csvReader = csv.reader(fileDescriptor)
next(csvReader, None)
for line in csvReader:
imagePath = os.path.join(images_path, line[0])
label = line[1:]
# print(label)
for i in range(num_class):
if label[i]:
a = float(label[i])
if a == 1:
label[i] = 1
elif a == 0:
label[i] = 0
elif a == -1: # uncertain label
label[i] = -1
else:
label[i] = unknown_label # unknown label
self.img_list.append(imagePath)
# print(label)
imageLabel = [int(i) for i in label]
self.img_label.append(imageLabel)
indexes = np.arange(len(self.img_list))
if annotation_percent < 100:
random.Random(99).shuffle(indexes)
num_data = int(indexes.shape[0] * annotation_percent / 100.0)
indexes = indexes[:num_data]
_img_list, _img_label = copy.deepcopy(self.img_list), copy.deepcopy(self.img_label)
self.img_list = []
self.img_label = []
for i in indexes:
self.img_list.append(_img_list[i])
self.img_label.append(_img_label[i])
def __getitem__(self, index):
imagePath = self.img_list[index]
imageData = Image.open(imagePath).convert('RGB')
label = []
for l in self.img_label[index]:
if l == -1:
if self.uncertain_label == "Ones":
label.append(1)
elif self.uncertain_label == "Zeros":
label.append(0)
elif self.uncertain_label == "LSR-Ones":
label.append(random.uniform(0.55, 0.85))
elif self.uncertain_label == "LSR-Zeros":
label.append(random.uniform(0, 0.3))
else:
label.append(l)
imageLabel = torch.FloatTensor(label)
if self.augment != None: imageData = self.augment(imageData)
return imageData, imageLabel
def __len__(self):
return len(self.img_list)
# %%
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
# %%
train_dataset = CheXpertDataset(images_path='~/dataset/CheXpert-v1.0',file_path='~/dataset/CheXpert-v1.0/CheXpert-v1.0/train.csv', augment=transform, num_class=14)
test_dataset = CheXpertTestDataset(images_path='~/dataset/CheXpert-v1.0',file_path='~/CheXpert-v1.0/CheXpert-v1.0/test.csv', augment=transform, num_class=14)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=10)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=True, num_workers=10)
data = pd.read_csv('/data/jliang12/jpang12/dataset/CheXpert-v1.0/CheXpert-v1.0/test.csv')
print(data.head())
# Define basic device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Define the model (Swin Transformer - base version)
model = timm.create_model('swin_base_patch4_window7_224', pretrained=True, num_classes=14) # CheXpert is a 5-class problem
model = model.to(device)
# Define the loss function and optimizer
criterion = nn.BCEWithLogitsLoss() # CheXpert is a multi-label classification task
optimizer = optim.AdamW(model.parameters(), lr=0.005)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=3, verbose=True)
# %%
# Function for testing the model
def test_model(model, test_loader, device):
model.eval() # Set the model to evaluation mode
criterion = nn.BCEWithLogitsLoss() # Use appropriate loss function
running_loss = 0.0
all_labels = []
all_preds = []
with torch.no_grad(): # Disable gradient calculation for testing
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
running_loss += loss.item()
# Store predictions and true labels for AUC calculation
preds = torch.sigmoid(outputs).cpu().numpy()
all_preds.append(preds)
all_labels.append(labels.cpu().numpy())
# Compute average loss
avg_loss = running_loss / len(test_loader)
# Flatten lists for AUC calculation
all_labels = np.concatenate(all_labels)
all_preds = np.concatenate(all_preds)
# Calculate AUC-ROC for each class
aucs = []
for i in range(all_labels.shape[1]):
auc = roc_auc_score(all_labels[:, i], all_preds[:, i])
aucs.append(auc)
return avg_loss, aucs
# Example of using the testing function
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Assuming `test_dataset` and `model` are already defined
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=True)
# Test the model
test_loss, test_aucs = test_model(model, test_loader, device)
print(f"Test Loss: {test_loss:.4f}")
for idx, auc in enumerate(test_aucs):
print(f"Class {idx+1} AUC: {auc:.4f}")
# Training loop
num_epochs = 10
for epoch in range(num_epochs):
start = time.time()
# print(f"The time for 1 epoch is {start:.2f} seconds")
model.train()
running_loss = 0.0
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device).float()
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {running_loss/len(train_loader):.4f}")
final = time.time()
print(f"The time for {epoch} epoch is {final:.2f} seconds")
test_loss, test_aucs = test_model(model, test_loader, device)
print(f"Test Loss: {test_loss:.4f}")
# Update the scheduler
scheduler.step(test_loss)
print("Training completed!")
# %%
# Function for testing the model
def test_model(model, test_loader, device):
model.eval() # Set the model to evaluation mode
criterion = nn.BCEWithLogitsLoss() # Use appropriate loss function
running_loss = 0.0
all_labels = []
all_preds = []
with torch.no_grad(): # Disable gradient calculation for testing
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
running_loss += loss.item()
# Store predictions and true labels for AUC calculation
preds = torch.sigmoid(outputs).cpu().numpy()
all_preds.append(preds)
all_labels.append(labels.cpu().numpy())
# Compute average loss
avg_loss = running_loss / len(test_loader)
# Flatten lists for AUC calculation
all_labels = np.concatenate(all_labels)
all_preds = np.concatenate(all_preds)
# Calculate AUC-ROC for each class
aucs = []
for i in range(all_labels.shape[1]):
auc = roc_auc_score(all_labels[:, i], all_preds[:, i])
aucs.append(auc)
return avg_loss, aucs
# Example of using the testing function
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Assuming `test_dataset` and `model` are already defined
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=True)
# Test the model
test_loss, test_aucs = test_model(model, test_loader, device)
print(f"Test Loss: {test_loss:.4f}")
for idx, auc in enumerate(test_aucs):
print(f"Class {idx+1} AUC: {auc:.4f}")
# %%