ESRGAN Implementation-: RuntimeError: number of dims don't match in permute

Darsh_S: <p>I was working on ESRGAN implementation and while running the code, the following error occured:-</p>
<blockquote>
<p>RuntimeError: number of dims don’t match in permute</p>
</blockquote>
<p>The code is given below:- `import torch<br>
from torchvision import transforms<br>
from PIL import Image<br>
from esrgan_model import ESRGAN<br>
print(“imports done”)<br>
<span class="hashtag-raw">#load</span> the pretrained ESRGAN model<br>
model = ESRGAN()<br>
print(‘model imported’)<br>
<span class="hashtag-raw">#define</span> the image preprocessing steps<br>
transform = transforms.Compose([<br>
transforms.Resize((224, 224)),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br>
])<br>
print(“preprocessing steps defined”)<br>
<span class="hashtag-raw">#load</span> the image<br>
img = Image.open(“example.jpg”)<br>
print(“loading the image”)</p>
<p><span class="hashtag-raw">#preprocess</span> the image<br>
img = transform(img).unsqueeze(0)<br>
print(‘Preprocessing the image…’)</p>
<p><span class="hashtag-raw">#run</span> the image through the model<br>
output = model(img)<br>
print(‘Running the image through the model…’)</p>
<p><span class="hashtag-raw">#save</span> the output image<br>
print(output.shape)<br>
output = output.squeeze()<br>
print(output.shape)<br>
output = output.permute(1, 2, 0)<br>
output = (output * 0.5) + 0.5<br>
output = transforms.ToPILImage()(output)<br>
output.save(“output.jpg”)<br>
`<br>
The shape before and after squeeze is</p>
<blockquote>
<p>torch.Size([1, 1, 28, 28])<br>
torch.Size([28, 28])</p>
</blockquote>
<p>If I change the dims, it gives the following error:-</p>
<blockquote>
<p>IndexError: Dimension out of range (expected to be in range of [-2, 1],</p>
</blockquote>
<p>How can i resolve this error?</p>

ptrblck: <p>The <code>squeeze</code> call removes <code>dim0</code> and <code>dim1</code> since you are not specifying any dimension in this call. The next <code>permute(1, 2, 0)</code> call expects to use 3 dimensions and thus fails.<br>
In case you want to <code>squeeze</code> <code>dim1</code> only, you should use <code>output = output.squeeze(1)</code> instead.</p>

Darsh_S: <p>On doing so, I get the following error:-</p>
<blockquote>
<p>Traceback (most recent call last):<br>
File “D:/pythonProject/main.py”, line 34, in <br>
output = transforms.ToPILImage()(output)<br>
File “D:\Venvs\lib\site-packages\torchvision\transforms\transforms.py”, line 179, in <strong>call</strong><br>
return F.to_pil_image(pic, self.mode)<br>
File “D:\Venvs\lib\site-packages\torchvision\transforms\functional.py”, line 227, in to_pil_image<br>
raise ValueError(‘pic should not have &gt; 4 channels. Got {} channels.’.format(pic.shape[-3]))<br>
ValueError: pic should not have &gt; 4 channels. Got 28 channels.</p>
</blockquote>
<p>Although the dims reduced to 3 values as follows:-</p>
<blockquote>
<p>torch.Size([1, 28, 28])</p>
</blockquote>
<p>But the error above is causing problem in the code.</p>

ptrblck: <p><code>ToPILImage</code> does not expect a channel dimension for grayscale images, so you would need to <code>squeeze</code> the channel dimension after the <code>permute</code> call or you could keep squeezing both dimensions and fix the permute call instead:</p>
<pre><code class="lang-python">output = transforms.ToPILImage()(output.squeeze(2))
</code></pre>