@@ -15,38 +15,49 @@ def _evaluate_model(model, val_loader, criterion, epoch, num_epochs, writer, cur
15
15
16
16
# Set to eval mode
17
17
model .eval ()
18
-
18
+ # List of probabilities obtained from the model
19
19
y_probs = []
20
+ # List of groundtruth labels
20
21
y_gt = []
22
+ # List of losses obtained
21
23
losses = []
22
24
25
+ # Iterate over the validation dataset
23
26
for i , (images , label ) in enumerate (val_loader ):
24
-
27
+ # If GPU is available, load the images and label
28
+ # on GPU
25
29
if torch .cuda .is_available ():
26
30
images = [image .cuda () for image in images ]
27
31
label = label .cuda ()
28
32
33
+ # Obtain the model output by passing the images as input
29
34
output = model (images )
30
-
35
+ # Evaluate the loss by comparing the output and groundtruth label
31
36
loss = criterion (output , label )
32
-
37
+ # Add loss to the list of losses
33
38
loss_value = loss .item ()
34
39
losses .append (loss_value )
35
-
40
+ # Find probability for each class by applying
41
+ # sigmoid function on model output
36
42
probas = torch .sigmoid (output )
37
-
43
+ # Add the groundtruth to the list of groundtruths
38
44
y_gt .append (int (label .item ()))
45
+ # Add predicted probability to the list
39
46
y_probs .append (probas .item ())
40
47
41
48
try :
49
+ # Evaluate area under ROC curve based on the groundtruth label
50
+ # and predicted probability
42
51
auc = metrics .roc_auc_score (y_gt , y_probs )
43
52
except :
53
+ # Default area under ROC curve
44
54
auc = 0.5
45
-
55
+ # Add information to the writer about validation loss and Area under ROC curve
46
56
writer .add_scalar ('Val/Loss' , loss_value , epoch * len (val_loader ) + i )
47
57
writer .add_scalar ('Val/AUC' , auc , epoch * len (val_loader ) + i )
48
58
49
59
if (i % log_every == 0 ) & (i > 0 ):
60
+ # Display the information about average validation loss and area under ROC curve
50
61
print ('''[Epoch: {0} / {1} | Batch : {2} / {3} ]| Avg Val Loss {4} | Val AUC : {5} | lr : {6}''' .
51
62
format (
52
63
epoch + 1 ,
@@ -58,9 +69,9 @@ def _evaluate_model(model, val_loader, criterion, epoch, num_epochs, writer, cur
58
69
current_lr
59
70
)
60
71
)
61
-
72
+ # Add information to the writer about total epochs and Area under ROC curve
62
73
writer .add_scalar ('Val/AUC_epoch' , auc , epoch + i )
63
-
74
+ # Find mean area under ROC curve and validation loss
64
75
val_loss_epoch = np .round (np .mean (losses ), 4 )
65
76
val_auc_epoch = np .round (auc , 4 )
66
77
@@ -71,41 +82,62 @@ def _train_model(model, train_loader, epoch, num_epochs, optimizer, criterion, w
71
82
# Set to train mode
72
83
model .train ()
73
84
85
+ # Initialize the predicted probabilities
74
86
y_probs = []
87
+ # Initialize the groundtruth labels
75
88
y_gt = []
89
+ # Initialize the loss between the groundtruth label
90
+ # and the predicted probability
76
91
losses = []
77
92
93
+ # Iterate over the training dataset
78
94
for i , (images , label ) in enumerate (train_loader ):
95
+ # Reset the gradient by zeroing it
79
96
optimizer .zero_grad ()
80
-
97
+
98
+ # If GPU is available, transfer the images and label
99
+ # to the GPU
81
100
if torch .cuda .is_available ():
82
101
images = [image .cuda () for image in images ]
83
102
label = label .cuda ()
84
103
104
+ # Obtain the prediction using the model
85
105
output = model (images )
86
106
107
+ # Evaluate the loss by comparing the prediction
108
+ # and groundtruth label
87
109
loss = criterion (output , label )
110
+ # Perform a backward propagation
88
111
loss .backward ()
112
+ # Modify the weights based on the error gradient
89
113
optimizer .step ()
90
114
115
+ # Add current loss to the list of losses
91
116
loss_value = loss .item ()
92
117
losses .append (loss_value )
93
118
119
+ # Find probabilities from output using sigmoid function
94
120
probas = torch .sigmoid (output )
95
121
122
+ # Add current groundtruth label to the list of groundtruths
96
123
y_gt .append (int (label .item ()))
124
+ # Add current probabilities to the list of probabilities
97
125
y_probs .append (probas .item ())
98
126
99
127
try :
128
+ # Try finding the area under ROC curve
100
129
auc = metrics .roc_auc_score (y_gt , y_probs )
101
130
except :
131
+ # Use default value of area under ROC curve as 0.5
102
132
auc = 0.5
103
-
133
+
134
+ # Add information to the writer about training loss and Area under ROC curve
104
135
writer .add_scalar ('Train/Loss' , loss_value ,
105
136
epoch * len (train_loader ) + i )
106
137
writer .add_scalar ('Train/AUC' , auc , epoch * len (train_loader ) + i )
107
138
108
139
if (i % log_every == 0 ) & (i > 0 ):
140
+ # Display the information about average training loss and area under ROC curve
109
141
print ('''[Epoch: {0} / {1} | Batch : {2} / {3} ]| Avg Train Loss {4} | Train AUC : {5} | lr : {6}''' .
110
142
format (
111
143
epoch + 1 ,
@@ -117,9 +149,10 @@ def _train_model(model, train_loader, epoch, num_epochs, optimizer, criterion, w
117
149
current_lr
118
150
)
119
151
)
120
-
152
+ # Add information to the writer about total epochs and Area under ROC curve
121
153
writer .add_scalar ('Train/AUC_epoch' , auc , epoch + i )
122
154
155
+ # Find mean area under ROC curve and training loss
123
156
train_loss_epoch = np .round (np .mean (losses ), 4 )
124
157
train_auc_epoch = np .round (auc , 4 )
125
158
0 commit comments