-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_stage_2.py
404 lines (312 loc) · 14.4 KB
/
train_stage_2.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
#!/usr/bin/env python
# coding: utf-8
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
import torch
import os
# Automatically reload modules
get_ipython().run_line_magic('load_ext', 'autoreload')
get_ipython().run_line_magic('autoreload', '2')
#data_dir = "/Users/peter/repos/qu/results/2024-06-13_17-02-36_masked-image-text/output"
#session_label = "2024-11-21_15-42-26_with-cm-logging6"
session_label = "2024-12-05_16-34-39_output-test-predictions"
output_epoch = "epoch_18"
data_dir = f"/Users/peter/repos/qu/results/{session_label}/output"
if not output_epoch == "":
data_dir = os.path.join(data_dir, output_epoch)
# Read and concatenate the data sets (just use all of them for now)
df1 = pd.read_csv(os.path.join(data_dir, "df_train_predictions.csv"))
df2 = pd.read_csv(os.path.join(data_dir, "df_validation_predictions.csv"))
df3 = pd.read_csv(os.path.join(data_dir, "df_test_predictions.csv"))
df_combined = pd.concat([df1, df2])
print(df_combined.shape)
print(df1.shape, df2.shape)
print(df_combined.columns.values)
print(df_combined['h_score'].value_counts())
print('patient count:', df_combined['anon_id'].nunique())
print('exam count:', df_combined['exam_id'].nunique())
# Remove diagnosis 99 (ignored in 2nd stage model)
print(df_combined['diagnosis'].value_counts())
df_combined = df_combined[df_combined['diagnosis'] != 99]
print(df_combined.shape)
# Drop duplicate entries based on 'patient_id'
unique_patients = df_combined.drop_duplicates(subset='anon_id')
total_count = unique_patients.shape[0]
# Count the number of healthy (0) and diseased (non-0) patients
diagnosis_counts = unique_patients['diagnosis'].apply(lambda x: 'Healthy' if x == 0 else 'Diseased').value_counts()
print(diagnosis_counts)
healthy_count = diagnosis_counts['Healthy']
diseased_count = diagnosis_counts['Diseased']
# percentages
print(f"Total count: {total_count}")
print(f"Healthy: {healthy_count} ({100*healthy_count/total_count:.2f}%)")
print(f"Diseased: {diseased_count} ({100*diseased_count/total_count:.2f}%)")
# Input
input_column = 'muscle'
input_value_column = 'prediction'
print(df_combined[input_column].value_counts())
#input_values = df[input_column].unique() # all muscles
#input_values = input_values[input_values != 'Extensors'] # remove Extensors
input_values = ['Biceps','RF','TA','GM','VL','Deltoid'] # six most common muscles
value_to_index = {value: idx for idx, value in enumerate(input_values)}
print(value_to_index)
# Output
use_diagnosis = False
# Aggregate the h_scores for each muscle
df_agg_train = df1.groupby(['exam_id', input_column]).agg({'prediction': 'max', 'h_score': 'max', 'diagnosis': 'first'}).reset_index()
df_agg_val = df2.groupby(['exam_id', input_column]).agg({'prediction': 'max', 'h_score': 'max', 'diagnosis': 'first'}).reset_index()
df_agg_test = df3.groupby(['exam_id', input_column]).agg({'prediction': 'max', 'h_score': 'max', 'diagnosis': 'first'}).reset_index()
# df_agg['h_score'] = np.random.normal(2, 1.0, df_agg.shape[0])
# df_agg['h_score'] = 1.0
print(df_agg_train.head(5))
print(df_agg_val.head(5))
print(df_agg_test.head(5))
def create_vectors(df):
input_vectors = {id: np.full(len(input_values), np.NaN) for id in df['exam_id'].unique()}
label_vectors = {id: False for id in df['exam_id'].unique()}
# Populate the vectors
for _, row in df.iterrows():
exam_id = row['exam_id']
muscle = row[input_column]
if muscle in input_values:
value_index = value_to_index[muscle]
input_vectors[exam_id][value_index] = row[input_value_column]
#diagnosis_index = diagnosis_to_index[row['diagnosis']]
if use_diagnosis:
label_vectors[exam_id] = row['diagnosis']
else:
label_vectors[exam_id] = row['diagnosis'] != 0
# Convert the dictionary to a DataFrame for easy handling/viewing
input_df = pd.DataFrame.from_dict(input_vectors, orient='index', columns=input_values)
label_df = pd.DataFrame.from_dict(label_vectors, orient='index', columns=['diagnosis'])
return (input_df, label_df)
input_train_df, label_train_df = create_vectors(df_agg_train)
input_val_df, label_val_df = create_vectors(df_agg_val)
input_test_df, label_test_df = create_vectors(df_agg_test)
# Handling missing values in 'input_df' (simple imputation with mean of each column)
# input_df = input_df.apply(lambda x: x.fillna(x.mean()), axis=1)
# input_df = input_df.fillna(0)
#input_df_filled = pd.DataFrame(0, index=np.arange(input_df.shape[0]), columns=input_df.columns)
#print(input_df_filled.head(10))
print(input_train_df.shape, label_train_df.shape)
print(input_val_df.shape, label_val_df.shape)
print(input_test_df.shape, label_test_df.shape)
# display(input_df.head(20))
# display(label_df.head(20))
label_values = label_train_df['diagnosis'].unique()
label_values = [str(x) for x in label_values]
print(input_values, label_values)
def df_to_masked_array(df):
# Convert the dataframe to a numpy array
array = df.to_numpy()
# Create a mask array, 1.0 where the original array is not NaN, and 0.0 where it is NaN
mask = np.where(np.isnan(array), 0.0, 1.0)
# Stack the original array and the mask array to form a new array with 2 channels
two_channel_array = np.stack((array, mask), axis=-1)
# Replace NaN values in the original data with 0.0 (or another value of your choice)
two_channel_array[np.isnan(two_channel_array[..., 0]), 0] = 0.0
return two_channel_array
X_train_masked = df_to_masked_array(input_train_df)
print(X_train_masked.shape)
X_val_masked = df_to_masked_array(input_val_df)
print(X_val_masked.shape)
X_test_masked = df_to_masked_array(input_test_df)
print(X_test_masked.shape)
def boolean_df_to_int_array(df):
return df.astype(int).to_numpy()
if use_diagnosis:
y_train_int = label_train_df.astype(int).to_numpy()
y_val_int = label_val_df.astype(int).to_numpy()
y_test_int = label_test_df.astype(int).to_numpy()
else:
y_train_int = boolean_df_to_int_array(label_train_df)
y_val_int = boolean_df_to_int_array(label_val_df)
y_test_int = boolean_df_to_int_array(label_test_df)
from torch.utils.data import Dataset, DataLoader
X_train_tensor = torch.tensor(X_train_masked, dtype=torch.float).permute(0, 2, 1) # Swap the last two dimensions
y_train_tensor = torch.tensor(y_train_int, dtype=torch.long)
X_val_tensor = torch.tensor(X_val_masked, dtype=torch.float).permute(0, 2, 1) # Swap the last two dimensions
y_val_tensor = torch.tensor(y_val_int, dtype=torch.long)
X_test_tensor = torch.tensor(X_test_masked, dtype=torch.float).permute(0, 2, 1) # Swap the last two dimensions
y_test_tensor = torch.tensor(y_test_int, dtype=torch.long)
# Create DataLoader for batching
train_data = torch.utils.data.TensorDataset(X_train_tensor, y_train_tensor)
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)
val_data = torch.utils.data.TensorDataset(X_val_tensor, y_val_tensor)
val_loader = DataLoader(val_data, batch_size=32, shuffle=False)
test_data = torch.utils.data.TensorDataset(X_test_tensor, y_test_tensor)
test_loader = DataLoader(test_data, batch_size=32, shuffle=False)
import torch
import torch.nn as nn
import torch.optim as optim
from qumia_model2 import MaskedClassifier
from torchsummary import summary
# Define the model, loss function, and optimizer
vector_length = len(input_values)
num_classes = len(label_values) if use_diagnosis else 1
model = MaskedClassifier(vector_length, num_classes)
criterion = nn.CrossEntropyLoss() if use_diagnosis else nn.BCEWithLogitsLoss(pos_weight=torch.tensor([0.45]))
optimizer = optim.Adam(model.parameters(), lr=0.005)
# print model summary
summary(model, (2, vector_length))
# Calculate losses
total_loss = 0.0
for inputs, labels in train_loader:
outputs = model(inputs)
target = labels.squeeze() if use_diagnosis else labels.float()
loss = criterion(outputs, target)
total_loss += loss.item()
print(f'Initial loss: {total_loss/len(train_loader)}')
# Training the model
num_epochs = 20 # Set the number of epochs
for epoch in range(num_epochs):
for inputs, labels in train_loader:
# Forward pass
outputs = model(inputs)
target = labels.squeeze() if use_diagnosis else labels.float()
loss = criterion(outputs, target)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Calculate loss on validation set
with torch.no_grad():
val_loss = 0.0
for inputs, labels in val_loader:
outputs = model(inputs)
target = labels.squeeze() if use_diagnosis else labels.float()
loss = criterion(outputs, target)
val_loss += loss.item()
val_loss /= len(val_loader)
# Save the checkpoint
torch.save(model.state_dict(), f'checkpoint_{epoch+1}.pth')
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Val Loss: {val_loss:.4f}')
# Evaluate the model
# model.eval() # Set the model to evaluation mode
# with torch.no_grad():
# test_preds = model(X_test_tensor)
# _, predicted_classes = torch.max(test_preds, 1)
# matches = (predicted_classes == y_test_tensor.squeeze())
# accuracy = matches.sum().float().item() / y_test_tensor.size(0)
# print(f'Accuracy: {accuracy:.4f}')
import torch
from sklearn.metrics import f1_score
# Load the model at a specific checkpoint
model.load_state_dict(torch.load('checkpoint_4.pth'))
# Set the model to evaluation mode
model.eval()
# Store all the predictions and true labels
test_predictions = []
test_labels = []
correct_predictions = 0
total_predictions = 0
with torch.no_grad():
for inputs, labels in test_loader:
outputs = model(inputs)
if use_diagnosis:
# Get the predicted classes by finding the max logit value
_, predicted_classes = torch.max(outputs, 1)
# Collect the predictions and true labels
test_predictions.extend(predicted_classes.tolist())
test_labels.extend(labels.tolist())
correct_predictions += (predicted_classes == labels.squeeze()).sum().item()
total_predictions += labels.size(0)
else:
# Apply sigmoid to the outputs to get the probabilities
probs = torch.sigmoid(outputs.squeeze()) # Squeeze to remove unnecessary dimensions
# Convert probabilities to binary predictions using a threshold of 0.5
#predictions = (probs >= 0.5).long() # Convert to long to match labels type
# Collect the predictions and true labels (for f1 score)
test_predictions.extend(probs.tolist())
test_labels.extend(labels.tolist())
# Compare predictions to true labels (for accuracy)
#correct_predictions += (predictions == labels.squeeze()).sum().item()
#total_predictions += labels.size(0)
if use_diagnosis:
# Calculate the F1 score (choose 'macro', 'micro', or 'weighted' as per your requirement)
f1_macro = f1_score(test_labels, test_predictions, average='macro')
f1_micro = f1_score(test_labels, test_predictions, average='micro')
f1_weighted = f1_score(test_labels, test_predictions, average='weighted')
print(f'F1 Score (Macro): {f1_macro:.4f}')
print(f'F1 Score (Micro): {f1_micro:.4f}')
print(f'F1 Score (Weighted): {f1_weighted:.4f}')
from sklearn.metrics import classification_report
# all_classes = np.array(label_values)
# unique_labels = np.unique(np.concatenate((all_classes, all_predictions)))
# print(unique_labels)
report = classification_report(test_labels, test_predictions)
print(report)
else:
pass
# Calculate the F1 score
#f1 = f1_score(all_labels, all_predictions, average='binary') # 'binary' for binary classification tasks
#print(f'F1 Score: {f1:.4f}')
#print(f'Accuracy: {correct_predictions / total_predictions:.4f}')
from sklearn.metrics import confusion_matrix
# Assuming all_predictions and all_labels are your predicted and true labels, respectively
# Generate the confusion matrix
# cm = confusion_matrix(all_labels, all_predictions)
# # Print the confusion matrix
# print("Confusion Matrix:")
# col_labels = ['', 'Pred 0', 'Pred 1']
# pred_labels = ['True 0', 'True 1']
# for i, label in enumerate(col_labels):
# print(f"{label:>8}", end=" ")
# print()
# for i, row in enumerate(cm):
# print(f"{pred_labels[i]:>8}", end=" ")
# for val in row:
# print(f"{val:>8}", end=" ")
# print()
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
import matplotlib.pyplot as plt
# Compute Precision-Recall and plot curve, based on test_predictions and test_labels
y_test = np.array(test_labels)
y_score = np.array(test_predictions)
precision, recall, _ = precision_recall_curve(y_test, y_score)
average_precision = average_precision_score(y_test, y_score)
# Plot
plt.clf()
plt.plot(recall, precision, label='Precision-Recall curve')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall, AUC={0:0.2f}'.format(average_precision))
plt.legend(loc="lower left")
# Save the plot
fig_path = f"{session_label}_{output_epoch}_prec-recall.png"
plt.savefig(fig_path)
# AFTER saving
plt.show()
# from sklearn.metrics import roc_curve, auc
# from sklearn.model_selection import train_test_split
# from sklearn.preprocessing import label_binarize
# import matplotlib.pyplot as plt
# # # Assuming you have your features in X and target in y
# # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# # Binarize the output
# y_test = label_binarize(y_test, classes=[0, 1])
# # Train your classifier and predict probabilities
# # clf.fit(X_train, y_train)
# y_score = clf.predict_proba(X_test)
# # Compute ROC curve and ROC area for each class
# fpr, tpr, _ = roc_curve(y_test, y_score[:, 1])
# roc_auc = auc(fpr, tpr)
# # Plot
# plt.figure()
# lw = 2
# plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
# plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.05])
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')
# plt.title('Receiver Operating Characteristic example')
# plt.legend(loc="lower right")
# plt.show()