I start the training script with
if __name__=='__main__':
    run()

So I don’t think it can be that. I did try the method I outlined above though:
    labels = []
    for idx, (batch, target) in enumerate(train_loader)
        labels.append(target)

…which has solved the problem!
So it looks like it just couldn’t cope with iterating through so many images without loading them into batches first.