I can’t see any critical problem with this code, but some small changes you could try:

move the with torch.no_grad() out of for loop
if the oom happens exactly after 20 pics, you could set a breakpoint at that moment and have a closer look at the variables.
maybe upgrade PyTorch?