| 54 | |
| 55 | @torch.no_grad() |
| 56 | def inference(pairs, model, device, batch_size=8, verbose=True): |
| 57 | if verbose: |
| 58 | print(f'>> Inference with model on {len(pairs)} image pairs') |
| 59 | result = [] |
| 60 | |
| 61 | # first, check if all images have the same size |
| 62 | multiple_shapes = not (check_if_same_size(pairs)) |
| 63 | if multiple_shapes: # force bs=1 |
| 64 | batch_size = 1 |
| 65 | |
| 66 | for i in tqdm.trange(0, len(pairs), batch_size, disable=not verbose): |
| 67 | res = loss_of_one_batch(collate_with_cat(pairs[i:i + batch_size]), model, None, device) |
| 68 | result.append(to_cpu(res)) |
| 69 | |
| 70 | result = collate_with_cat(result, lists=multiple_shapes) |
| 71 | |
| 72 | return result |
| 73 | |
| 74 | |
| 75 | def check_if_same_size(pairs): |