Image classification tensor

TTTT: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>In fact, i want to concat image pixels (train_x) and their labels(train_y). Then I want to convert to tensor.<br>
I don’t know how to do ?</p>
<p>Thank You</p>

J_Johnson: <p>Some more details about your objective might help.</p>
<p>In order to concat 2 tensors, they need to be the same size, except in the dim being concatenated. You could do:</p>
<pre><code class="lang-auto">labels = torch.empty_like(train_x)
labels[:, 0, labels.size()[1], :] = train_y.view(-1, 1, labels.size()[1], 1)

data_labels=torch.cat([train_x, labels], dim=1)
</code></pre>
<p>But, in most cases, you might find it easier to just use:</p>
<pre><code class="lang-auto">data_labels = [train_x, train_y]
...
train_x, train_y = data_labels[0], data_labels[1]
</code></pre>

TTTT: <p>Thank you so much ! I will try it</p>

TTTT: <aside class="quote no-group" data-username="J_Johnson" data-post="2" data-topic="168423">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/j_johnson/48/55494_2.png" class="avatar"> J_Johnson:</div>
<blockquote>
<pre><code class="lang-auto">labels[:, 0, labels.size()[1], :] = train_y.view(-1, 1, labels.size()[1], 1)

data_labels=torch.cat([train_x, labels], dim=1)
</code></pre>
</blockquote>
</aside>
<p>But i have an error =<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/a/2a8131323bdad950ece09f3a3c47e9b1fcc3e6d9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/2a8131323bdad950ece09f3a3c47e9b1fcc3e6d9" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/a/2a8131323bdad950ece09f3a3c47e9b1fcc3e6d9_2_690x231.jpeg" alt="Capture" data-base62-sha1="640QuLhsFSTxTWtAu6uXan4RO9H" width="690" height="231" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/a/2a8131323bdad950ece09f3a3c47e9b1fcc3e6d9_2_690x231.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/a/2a8131323bdad950ece09f3a3c47e9b1fcc3e6d9_2_1035x346.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/a/2a8131323bdad950ece09f3a3c47e9b1fcc3e6d9_2_1380x462.jpeg 2x" data-dominant-color="F4F4F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture</span><span class="informations">1382×464 67 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Call <code>view</code> on a PyTorch tensor not a numpy array:</p>
<pre><code class="lang-python">x = torch.randn(2, 3, 4, 5)
x = x.view(-1, 1, 1, 1) # works

x = np.random.randn(2, 3, 4, 5)
x = x.view(-1, 1, 1, 1) # fails
# TypeError: view() takes from 0 to 2 positional arguments but 4 were given
</code></pre>

J_Johnson: <p>As <a class="mention" href="/u/ptrblck">@ptrblck</a> said, if it’s a NumPy object, you’ll need to convert it into a Pytorch tensor, via <code>torch.from_numpy()</code>:</p>
<pre><code class="lang-auto">labels[:, 0, labels.size()[1], :] = torch.from_numpy(train_y).view(-1, 1, labels.size()[1], 1)

data_labels=torch.cat([train_x, labels], dim=1)
</code></pre>
<p><a href="https://pytorch.org/docs/stable/generated/torch.from_numpy.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.from_numpy.html</a></p>

TTTT: <p>I have an error :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/8/6820889a7debcf5365e8b3f3247bf2d158ddb479.png" data-download-href="https://discuss.pytorch.org/uploads/default/6820889a7debcf5365e8b3f3247bf2d158ddb479" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/6820889a7debcf5365e8b3f3247bf2d158ddb479_2_690x340.png" alt="image" data-base62-sha1="eR9mjitwkvqeG5rApxDWnmepHQt" width="690" height="340" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/6820889a7debcf5365e8b3f3247bf2d158ddb479_2_690x340.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/6820889a7debcf5365e8b3f3247bf2d158ddb479_2_1035x510.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/6820889a7debcf5365e8b3f3247bf2d158ddb479_2_1380x680.png 2x" data-dominant-color="F6F5F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1403×692 32.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <p>You changed <code>train_y.view(-1,1,labels.size()[1], 1)</code> to <code>train_y.view(-1,1,labels.size()[0], 1)</code>. The <code>-1</code> is intended to capture the batch size, as this can be variable, while the labels.size()[1] captures the number of labels per item.  Change it back to dim 1 so the sizes match.</p>

J_Johnson: <p>Additionally, it seems your images are removing a dim. Images should have 4 dimensions: batch size, channels, height, width.</p>
<p>If you are getting your images/labels from a numpy dataloader, it could be squeezing the channels dim. You’ll need that back in to pass to a model for inference, if it has convolutional layers. i.e. <code>train_x=train_x.unsqueeze(1)</code></p>
<p>If the model is strictly linear layers, then you can resize the labels view accordingly.</p>

TTTT: <p>But in train_x there are pixels for each images.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/410aef9cbb20193f6ebc56520b094ae4217a011d.png" data-download-href="https://discuss.pytorch.org/uploads/default/410aef9cbb20193f6ebc56520b094ae4217a011d" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/410aef9cbb20193f6ebc56520b094ae4217a011d.png" alt="image" data-base62-sha1="9hosSBQDeWx75Q3miu9hCR9vCY5" width="690" height="485" data-dominant-color="F4F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1077×758 29.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

TTTT: <p>I changed because i got this error :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/f/6f9a5ea350191618ce295c67c133fdca74449cd6.png" data-download-href="https://discuss.pytorch.org/uploads/default/6f9a5ea350191618ce295c67c133fdca74449cd6" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/f/6f9a5ea350191618ce295c67c133fdca74449cd6_2_690x160.png" alt="image" data-base62-sha1="fVhJyjnUIPxNX9jcbLXegCIqaTI" width="690" height="160" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/f/6f9a5ea350191618ce295c67c133fdca74449cd6_2_690x160.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/f/6f9a5ea350191618ce295c67c133fdca74449cd6_2_1035x240.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/f/6f9a5ea350191618ce295c67c133fdca74449cd6_2_1380x320.png 2x" data-dominant-color="F5F4F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1450×338 17.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <p>As I suspected, you’re missing the channels dim. Does your model have Conv2D layers?</p>

TTTT: <p>I did not train the model. In fact, for the image classification, i have a folder with human faces and two csv files : train_csv contains the name of image (ex : 0001.jpg) and the label  (1 or -1). So  i start to collect then into datframe.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/7/1751456e8b8aca86a068414a07fec487e32bd622.png" data-download-href="https://discuss.pytorch.org/uploads/default/1751456e8b8aca86a068414a07fec487e32bd622" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/1751456e8b8aca86a068414a07fec487e32bd622_2_690x372.png" alt="image" data-base62-sha1="3kh6u5z7SYV6xpj4htnYj7pYBNw" width="690" height="372" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/1751456e8b8aca86a068414a07fec487e32bd622_2_690x372.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/1751456e8b8aca86a068414a07fec487e32bd622_2_1035x558.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/1751456e8b8aca86a068414a07fec487e32bd622_2_1380x744.png 2x" data-dominant-color="F9F9F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1383×746 27.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

TTTT: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/a/0a6525374e92ac52eec2dd2be74e4ea793497241.png" data-download-href="https://discuss.pytorch.org/uploads/default/0a6525374e92ac52eec2dd2be74e4ea793497241" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a6525374e92ac52eec2dd2be74e4ea793497241_2_690x185.png" alt="image" data-base62-sha1="1tXtCasxuJIpGzk4qyNqMZgmlhv" width="690" height="185" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a6525374e92ac52eec2dd2be74e4ea793497241_2_690x185.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a6525374e92ac52eec2dd2be74e4ea793497241_2_1035x277.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a6525374e92ac52eec2dd2be74e4ea793497241_2_1380x370.png 2x" data-dominant-color="EDEEED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1433×385 22.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <p>Then just remove the 2nd dim in the .view():</p>
<pre><code class="lang-auto">labels[:,0: labels.size()[1], 0:1] = train_y.view(-1, labels.size()[1], 1)
</code></pre>

TTTT: <p>It  did not change:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/12fdb37733eed961b7ea18d9eedb87259cc8abe0.png" data-download-href="https://discuss.pytorch.org/uploads/default/12fdb37733eed961b7ea18d9eedb87259cc8abe0" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/2/12fdb37733eed961b7ea18d9eedb87259cc8abe0.png" alt="image" data-base62-sha1="2I094TpOMAK0GzmGAxGMJ8DnbUI" width="690" height="155" data-dominant-color="F4F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1339×301 17.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

TTTT: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e6fd63217cf7563bde19a8d84d93e74cec21d43.png" data-download-href="https://discuss.pytorch.org/uploads/default/9e6fd63217cf7563bde19a8d84d93e74cec21d43" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e6fd63217cf7563bde19a8d84d93e74cec21d43.png" alt="image" data-base62-sha1="mBB33JMidv8XcdzRRbT7Phms02D" width="690" height="165" data-dominant-color="F5F5F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1320×317 16.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <p>Can you print the sizes of <code>train_x</code> and <code>train_y</code>?</p>

TTTT: <p>Yes :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/9/99477ed33a9622e4a6962e565795ff73122f7429.png" data-download-href="https://discuss.pytorch.org/uploads/default/99477ed33a9622e4a6962e565795ff73122f7429" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/9/99477ed33a9622e4a6962e565795ff73122f7429.png" alt="image" data-base62-sha1="lRYekGV6HCfOXbgcOYxyE1YI7s5" width="690" height="149" data-dominant-color="F7F7F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">821×178 2.54 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <pre><code class="lang-auto">import torch

train_x=torch.rand(200, 130, 130)
train_y=torch.rand(200)

labels = torch.empty_like(train_x)
labels[:,:1, :1] = train_y.view(-1, 1, 1)

data_labels=torch.cat([train_x, labels], dim=1)

print(data_labels.size())
</code></pre>