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 | class ZambaVideoClassificationLightningModule(ZambaClassificationLightningModule):
def on_train_start(self):
metrics = {"val_macro_f1": {}}
if self.num_classes > 2:
metrics.update(
{f"val_top_{k}_accuracy": {} for k in DEFAULT_TOP_K if k < self.num_classes}
)
else:
metrics.update({"val_accuracy": {}})
# write hparams to hparams.yaml file, log metrics to tb hparams tab
self.logger.log_hyperparams(self.hparams, metrics)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.binary_cross_entropy_with_logits(y_hat, y)
self.log("train_loss", loss.detach())
self.training_step_outputs.append(loss)
return loss
def _val_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.binary_cross_entropy_with_logits(y_hat, y)
self.log("val_loss", loss.detach())
y_proba = torch.sigmoid(y_hat.cpu()).numpy()
return {
"y_true": y.cpu().numpy().astype(int),
"y_pred": y_proba.round().astype(int),
"y_proba": y_proba,
}
def validation_step(self, batch, batch_idx):
output = self._val_step(batch, batch_idx)
self.validation_step_outputs.append(output)
return output
def test_step(self, batch, batch_idx):
output = self._val_step(batch, batch_idx)
self.test_step_outputs.append(output)
return output
@staticmethod
def aggregate_step_outputs(
outputs: Dict[str, np.ndarray],
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
y_true = np.vstack([output["y_true"] for output in outputs])
y_pred = np.vstack([output["y_pred"] for output in outputs])
y_proba = np.vstack([output["y_proba"] for output in outputs])
return y_true, y_pred, y_proba
def compute_and_log_metrics(
self, y_true: np.ndarray, y_pred: np.ndarray, y_proba: np.ndarray, subset: str
):
self.log(
f"{subset}_macro_f1",
f1_score(y_true, y_pred, average="macro", zero_division=0),
)
# if only two classes, skip top_k accuracy since not enough classes
if self.num_classes > 2:
for k in DEFAULT_TOP_K:
if k < self.num_classes:
self.log(
f"{subset}_top_{k}_accuracy",
top_k_accuracy_score(
y_true.argmax(
axis=1
), # top k accuracy only supports single label case
y_proba,
labels=np.arange(y_proba.shape[1]),
k=k,
),
)
else:
self.log(f"{subset}_accuracy", accuracy_score(y_true, y_pred))
for metric_name, label, metric in compute_species_specific_metrics(
y_true, y_pred, self.species
):
self.log(f"species/{subset}_{metric_name}/{label}", metric)
def on_validation_epoch_end(self):
"""Aggregates validation_step outputs to compute and log the validation macro F1 and top K
metrics.
Args:
outputs (List[dict]): list of output dictionaries from each validation step
containing y_pred and y_true.
"""
y_true, y_pred, y_proba = self.aggregate_step_outputs(self.validation_step_outputs)
self.compute_and_log_metrics(y_true, y_pred, y_proba, subset="val")
self.validation_step_outputs.clear() # free memory
def on_test_epoch_end(self):
y_true, y_pred, y_proba = self.aggregate_step_outputs(self.test_step_outputs)
self.compute_and_log_metrics(y_true, y_pred, y_proba, subset="test")
self.test_step_outputs.clear() # free memory
def predict_step(self, batch, batch_idx, dataloader_idx: Optional[int] = None):
x, y = batch
y_hat = self(x)
pred = torch.sigmoid(y_hat).cpu().numpy()
return pred
|