That’s really interesting, as I was looking into a “functional” issue in your code.
Based on your update and the initial output image my current guess was that maybe a reshaping operation might be failing somehow (although I didn’t have an idea where and why it should be used).