Solved by jpeg729 in post #2 
              
              
                I suspect that some_var[:,:,i,j] = stuff counts as an inplace operation. 
If I have understood correctly what your code is doing then most of them are unnecessary, and the last one sample[:,:,i,j] = images[:,:,i,j] doesn’t need gradients. 
Would this work? 
for i in range(picSize):
    for j in rang…