-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInception3.py
More file actions
282 lines (237 loc) · 10.8 KB
/
Inception3.py
File metadata and controls
282 lines (237 loc) · 10.8 KB
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
import torch
import torch.nn as nn
import torch.optim as optim
import deeplake
from matplotlib import pyplot as plt
from torchvision import models
from torchvision import transforms
from sklearn.metrics import f1_score, precision_score, recall_score, hamming_loss, accuracy_score, roc_auc_score
import numpy as np
def custom_collate_fn(batch):
"""
DeepLake's default collation method attempts to stack tensors, which causes errors when the
samples vary in size. This function returns a dictionary where each key maps to a list of values
from the batch, preventing the stacking error.
"""
collated = {}
for key in batch[0].keys():
collated[key] = [sample[key] for sample in batch]
return collated
# Data Loading Function
def custom_get_data():
print("Data Loading......")
print("Loading training dataset from DeepLake...")
ds_train = deeplake.load('hub://activeloop/nih-chest-xray-train')
print("Training dataset loaded successfully!")
print("Loading test dataset from DeepLake...")
ds_test = deeplake.load('hub://activeloop/nih-chest-xray-test')
print("Test dataset loaded successfully!")
print(f"Total training samples: {len(ds_train)}")
print(f"Total test samples: {len(ds_test)}")
print("Creating training DataLoader with custom collate function...")
# Using our custom_collate_fn to prevent errors due to variable-sized labels
train_loader = ds_train.pytorch(
num_workers=4,
batch_size=64,
shuffle=True,
decode_method={"images": "pil"},
collate_fn=custom_collate_fn
)
test_loader = ds_test.pytorch(
batch_size=96,
shuffle=False,
decode_method={"images": "pil"},
collate_fn=custom_collate_fn
)
print("Testing DataLoader created successfully!")
return train_loader, test_loader, ds_train, ds_test
# Preprocessing
def get_transforms():
print("Preprocessing Pipeline........")
# We first convert grayscale images to 3-channel images because our CNN model
# expects 3-channel (RGB) input, then resize, convert to tensor, and normalize
transform_pipeline = transforms.Compose([
# Convert grayscale images to 3-channel images.
transforms.Grayscale(num_output_channels=3),
transforms.Resize((299, 299)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], # mean
[0.229, 0.224, 0.225])
])
print("Preprocessing pipeline is ready.")
return transform_pipeline
# Label Conversion Helper
def convert_labels_to_multihot(raw_labels, num_classes=14):
"""
Many sample have labels of variable lengths, which causes errors when stacking,
Tjis function converts each samples's labels into a fixed-length multi-hot vector.
Each index in the vector corresponds to one of the 14 classes.
"""
processed_labels = []
for label in raw_labels:
multi_hot = torch.zeros(num_classes, dtype=torch.float)
# if isinstance(label, Int64):
for item in label:
try:
idx = int(item)
except Exception:
continue
if 0 <= idx < num_classes:
multi_hot[idx] = 1.0
# else:
# try:
# idx = int(label)
# if 0 <= idx < num_classes:
# multi_hot[idx] = 1.0
# except Exception:
# pass
processed_labels.append(multi_hot)
return torch.stack(processed_labels)
# Training Loop
def train_model(model, train_loader, transform_pipeline, device, num_epochs=5):
epoch_losses = []
epochs = []
print("Starting Training Loop........")
model.to(device)
print(device)
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001,weight_decay=1e-5)
model.train()
for epoch in range(num_epochs):
print("\n---------------------------------------------")
print(f"Epoch {epoch + 1}/{num_epochs}")
epochs.append(epoch + 1)
running_loss = 0.0
batch_count = 0
epoch_preds = []
epoch_targets = []
for batch in train_loader:
batch_count += 1
print(f"\nProcessing batch {batch_count}...")
# Process images (assumes DataLoader returns a dict with key "images" as PIL images)
pil_images = batch.get("images")
if pil_images is None:
raise KeyError("The batch does not contain the key 'images'. Check your data loader.")
processed_images = [transform_pipeline(img) for img in pil_images]
images = torch.stack(processed_images).to(device)
# Process labels: check for "labels" or "findings"
if "labels" in batch:
raw_labels = batch["labels"]
elif "findings" in batch:
raw_labels = batch["findings"]
else:
raise KeyError("No label key found in batch (expected 'labels' or 'findings').")
# Convert raw labels into fixed-length multi-hot vectors.
labels = convert_labels_to_multihot(raw_labels, num_classes=15).to(device)
optimizer.zero_grad()
outputs, aux_outputs = model(images)
loss1 = criterion(outputs, labels)
loss2 = criterion(aux_outputs, labels)
loss = loss1 + .4 * loss2
loss.backward()
optimizer.step()
running_loss += loss.item() * images.size(0)
print(f" Batch {batch_count} Loss: {loss.item():.4f}")
# Accumulate predictions and targets for metric calculation.
preds = (torch.sigmoid(outputs) > 0.5).float()
epoch_preds.append(preds.detach().cpu())
epoch_targets.append(labels.detach().cpu())
epoch_loss = running_loss / len(train_loader.dataset)
epoch_losses.append(epoch_loss)
epoch_preds = torch.cat(epoch_preds, dim=0).numpy()
epoch_targets = torch.cat(epoch_targets, dim=0).numpy()
plt.plot(epoch_losses, label='loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.savefig("Loss Graph")
# Calculating metrics
accuracy = accuracy_score(epoch_targets, epoch_preds)
f1_micro = f1_score(epoch_targets, epoch_preds, average='micro', zero_division=0)
f1_macro = f1_score(epoch_targets, epoch_preds, average='macro', zero_division=0)
precision = precision_score(epoch_targets, epoch_preds, average='micro', zero_division=0)
recall = recall_score(epoch_targets, epoch_preds, average='micro', zero_division=0)
hamming = hamming_loss(epoch_targets, epoch_preds)
auc = roc_auc_score(epoch_targets, epoch_preds)
print("\n=============================================")
print(f"Recall (micro): {recall:.4f}")
print(f"F1 Score (micro): {f1_micro:.4f}")
print(f"F1 Score (macro): {f1_macro:.4f}")
print(f"Hamming Loss: {hamming:.4f}")
print(f"Accuracy: {accuracy:.4f}")
print(f'AUC: {auc:.4f}')
print("=============================================\n")
print("========== Training Loop Finished ==========")
print("Saving model as 'model.pth'")
torch.save(model.state_dict(), "model.pth")
def test(model, test_loader, transform_pipeline, device, saved_model = ""):
if (saved_model != ""):
model.load_state_dict(torch.load(saved_model))
model.eval()
model.eval()
print("Starting Testing Loop")
model.to(device)
print(device)
ground_truth = []
predictions = []
with torch.no_grad():
for i, batch in enumerate(test_loader):
print(f"Testing Batch #{i} of {len(test_loader)}")
# Process images (assumes DataLoader returns a dict with key "images" as PIL images)
pil_images = batch.get("images")
if pil_images is None:
raise KeyError("The batch does not contain the key 'images'. Check your data loader.")
processed_images = [transform_pipeline(img) for img in pil_images]
images = torch.stack(processed_images).to(device)
# Process labels: check for "labels" or "findings"
if "labels" in batch:
raw_labels = batch["labels"]
elif "findings" in batch:
raw_labels = batch["findings"]
else:
raise KeyError("No label key found in batch (expected 'labels' or 'findings').")
# Convert raw labels into fixed-length multi-hot vectors.
labels = convert_labels_to_multihot(raw_labels, num_classes=15).to(device)
outputs = model(images)
outputs = torch.sigmoid(outputs)
predicted = (outputs > 0.5).float()
ground_truth.extend(labels.detach().cpu().numpy())
predictions.extend(predicted.detach().cpu().numpy())
# Calculating metrics
accuracy = accuracy_score(ground_truth, predictions)
f1_micro = f1_score(ground_truth, predictions, average='micro', zero_division=0)
f1_macro = f1_score(ground_truth, predictions, average='macro', zero_division=0)
precision = precision_score(ground_truth, predictions, average='micro', zero_division=0)
recall = recall_score(ground_truth, predictions, average='micro', zero_division=0)
hamming = hamming_loss(ground_truth, predictions)
auc = roc_auc_score(ground_truth, predictions, average='macro')
print("\n=============================================")
print(f"Testing Finished")
print(f"Precision (micro): {precision:.4f}")
print(f"Recall (micro): {recall:.4f}")
print(f"F1 Score (micro): {f1_micro:.4f}")
print(f"F1 Score (macro): {f1_macro:.4f}")
print(f"Hamming Loss: {hamming:.4f}")
print(f"Accuracy: {accuracy:.4f}")
print(f"Area Under Curve (AUC): {auc:.4f}")
print("=============================================\n")
def main():
print("========== Starting Model Training Process ==========")
train_loader, test_loader, ds_train, ds_test = custom_get_data()
transform_pipeline = get_transforms()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
num_classes = 15
model = models.inception_v3(pretrained=True, aux_logits=True)
freeze = True
for name, param in model.named_parameters():
if "Mixed_7a" in name:
freeze = False # start unfreezing here
param.requires_grad = not freeze
model.fc = nn.Linear(model.fc.in_features, num_classes)
model.AuxLogits.fc = nn.Linear(model.AuxLogits.fc.in_features, num_classes)
print(model)
train_model(model, train_loader, transform_pipeline, device, num_epochs=20)
print("========== Model Training is Complete ==========")
test(model, test_loader, transform_pipeline, device, saved_model = "model.pth")
if __name__ == "__main__":
main()