How to change an image which has dimensions (512, 512, 3) to a tensor of size ([1, 3, 224, 224])?

username: e-cockroach
cooked: <p>Hi guys,<br>
I was trying to implement a paper where the input dimensions are meant to be a tensor of size ([1, 3, 224, 224]). My current image size is (512, 512, 3).</p>
<p>How do I resize and convert in order to input to the model?</p>
<p>Any help will be much appreciated. Thanks!</p>

username: ptrblck
cooked: <p>First you would have to <code>permute</code> the dimensions to create a channels-first tensor from the channels-last input and could then use <code>torchvision.transforms.Resize</code> to resize the tensor to the desired <code>224x224</code> spatial size:</p>
<pre><code class="lang-python">input = torch.randn(512, 512, 3)
x = input.permute(2, 0, 1)
print(x.shape)
# torch.Size([3, 512, 512])

transform = transforms.Resize((224, 224))
out = transform(x)
print(out.shape)
# torch.Size([3, 224, 224])
</code></pre>

username: e-cockroach
cooked: <p>Thanks! It helps.<br>
I just had a follow up:<br>
This is what was mentioned in the paper implementation: make sure the dimension of image is 3 and 4th dimension is batch dim.<br>
So how do I add batch dimension to the dimension?<br>
(I am very sorry if question is lame, but I am new to pytorch and learning a bit by implementing)</p>

username: ptrblck
cooked: <p>I don’t know which paper you are reading, but</p>
<blockquote>
<p>make sure the dimension of image is 3 and 4th dimension is batch dim.</p>
</blockquote>
<p>sounds strange.<br>
The dimension of the image tensor would be 4 if you add a batch dimension, so I guess the first part targets the input image without the batch dimension?<br>
The batch dimension is added in <code>dim0</code> be default (in all frameworks if I’m not mistaken).<br>
For your tensor, you could use:</p>
<pre data-code-wrap="python"><code class="lang-python">out = out.unsqueeze(0)
</code></pre>
<p>to add the batch dimension which would then create <code>out</code> in the shape <code>[1, 3, 224, 224]</code>.<br>
I don’t know why one would add the batch dim into the 4th dim, but the paper might be working on a special use case where dimensions might be used in an unconventional way.</p>

username: e-cockroach
cooked: <p>Thanks I think it should do the job!<br>
Also while I was converting from image to tensor using this:</p>
<pre><code class="lang-auto">input = sample_f_img # sample_f_img is the input image
convert_tensor = transforms.ToTensor()

input=convert_tensor(input)
</code></pre>
<p>and then adding your solution to this,<br>
I am getting a weird size: <code>torch.Size([1, 512, 224, 224])</code><br>
whereas the expected is meant to be: torch.Size([1, 3, 224, 224])</p>
<p>I was wondering if you had any possible explanation for why this happened? Or is my image to tensor conversion not done the right way?</p>

username: ptrblck
cooked: <p>Could you post the <code>dtype</code> and shape of <code>input</code>?</p>

username: e-cockroach
cooked: <p>Sure,</p>
<pre><code class="lang-auto">dtype=uint8
shape=(512, 512, 3)
</code></pre>

username: ptrblck
cooked: <p>Thanks! It seems to work for me:</p>
<pre><code class="lang-python">input = np.random.randint(0, 256, (512, 512, 3), dtype=np.uint8)
out = transforms.ToTensor()(input)
print(out.shape)
# torch.Size([3, 512, 512])
</code></pre>