ignite: ValueError: Targets should be binary (0 or 1).
I am facing this issue.
ValueError: For binary cases, y must be comprised of 0's and 1's.
The task is multilabel, and I am converting to binary with:
def custom_prepare_batch(batch, device, non_blocking):
x, y = batch["img"], batch["lab"]
return (
convert_tensor(x, device=device, non_blocking=non_blocking),
convert_tensor(y, device=device, non_blocking=non_blocking),
)
### model update function
def process_function(engine, batch):
model.train()
images, targets = custom_prepare_batch(batch, device=device, non_blocking=True)
optimizer.zero_grad()
outputs = model(images)
for task in range(targets.shape[1]):
task_output = outputs[:,task]
task_target = targets[:,task]
mask = ~torch.isnan(task_target)
task_output = task_output[mask]
task_target = task_target[mask]
if len(task_target) > 0:
if agreement_threshold > 0.0:
mean_loss, masks = and_mask_utils.get_grads(
agreement_threshold=agreement_threshold,
batch_size=1,
loss_fn=criterion,
n_agreement_envs=batch_size,
params=optimizer.param_groups[0]['params'],
output=task_output,
target=task_target,
method="and_mask",
scale_grad_inverse_sparsity=scale_grad_inverse_sparsity,
)
else:
mean_loss = criterion(y_pred, y)
mean_loss.backward()
optimizer.step()
return {
# "batchloss": mean_loss.item()
}
I used this from the ignite docs:
def activated_output_transform(output):
y_pred, y = output
y_pred = torch.sigmoid(y_pred)
return y_pred, y
metrics = {
"roc_auc": ROC_AUC(activated_output_transform),
}
And, now I am getting
ValueError: Targets should be binary (0 or 1).
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16
Thank you so much. This is exactly what my task looks like, and I can work with this.
Right, in this case, you have to split the target with
output_transform
function and create 16 metrics. Maybe, something like that could work in your case:HTH