How to compute the IOU between two images "frame" and "templateimgw"

frame is an input image, and templateimgw is obtained by warping the template with a homography.
I have wrote the programm to compute the IOU between two images, but there are some errors in these codes below.
ZOOM_OUT_SCALE = 4
batch_size = frame.shape[0]
fake_template = torch.ones([batch_size, 1, 74 * ZOOM_OUT_SCALE, 115 * ZOOM_OUT_SCALE], device=frame.device)
scaling_mat = torch.eye(3, device=frame.device).repeat(batch_size, 1, 1)
scaling_mat[:, 0, 0] = scaling_mat[:, 1, 1] = ZOOM_OUT_SCALE
target_mask = warp.warp_image(fake_template, scaling_mat, out_shape=fake_template.shape[-2:])
mapping_mat = torch.matmul(frame, scaling_mat)
mapping_mat = torch.matmul(scaling_mat, frame)
mapping_mat = torch.matmul(mapping_mat, templateimgw.inverse())
output_mask = warp.warp_image(fake_template, mapping_mat, out_shape=fake_template.shape[-2:])
output_mask = (output_mask >= 0.5).float()
target_mask = (target_mask >= 0.5).float()

I have rewrote the code as this below, but there are still some errors.
ZOOM_OUT_SCALE = 4
batch_size = frame.shape[0]
fake_template = torch.ones([batch_size, 1, 74 * ZOOM_OUT_SCALE, 115 * ZOOM_OUT_SCALE], device=frame.device)
scaling_mat = torch.eye(3, device=frame.device).repeat(batch_size, 1, 1)
scaling_mat[:, 0, 0] = scaling_mat[:, 1, 1] = ZOOM_OUT_SCALE
target_mask = warp.warp_image(fake_template, scaling_mat, out_shape=fake_template.shape[-2:])
mapping_mat = torch.matmul(frame, scaling_mat)
mapping_mat = torch.matmul(templateimgw.inverse(), mapping_mat)output_mask = warp.warp_image(fake_template, mapping_mat, out_shape=fake_template.shape[-2:])
output_mask = (output_mask >= 0.5).float()
target_mask = (target_mask >= 0.5).float()

What kind of errors are you getting? Could you post the error message with the complete stack trace, please?

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. :wink:

Thanks for your reply.

The error is as follow.

Traceback (most recent call last):
File “ioucompute.py”, line 199, in mapping_mat = torch.matmul(frame, scaling_mat)
RuntimeError: Expected tensor to have size 256 at dimension 1, but got size 3 for argument #2 ‘batch2’ (while checking arguments for bmm)

Thanks for the error message. It seems to be a double post from here so lets keep the discussion in the other thread.

OK. The question is more specific from there.