Trouble with ToPILImage

Wesley_Neill: <p>Why does this not work?</p>
<pre><code class="lang-auto">import torchvision.transforms.functional as tf
from torchvision import transforms
pic = np.random.randint(0, 255+1, size=28*28).reshape(28, 28)
pic = pic.astype(int)
plt.imshow(pic)
t = transforms.ToPILImage()
t(pic.reshape(28, 28, 1))
# tf.to_pil_image(pic.reshape(28, 28, 1))
</code></pre>
<p>A beautiful random picture is plotted by matplotlib, but no matter what datatype I chose for my NumPy ndarray, neither <code>to_pil_image</code> or <code>ToPILImage</code> work as expected.</p>
<p>The docs have this to say:</p>
<blockquote>
<p>Converts a tensor … <em><strong>or a numpy ndarray of shape H x W x C</strong></em> to a PIL Image while preserving the value range.<br>
…<br>
If the input has 1 channel, the  <code>mode</code>  is determined by the data type <strong>(i.e  <code>int</code> ,  <code>float</code> ,  <code>short</code> )</strong>.</p>
</blockquote>
<p>None of these datatypes work except for “short”.</p>
<p>Everything else results in:</p>
<pre><code class="lang-auto">TypeError: Input type int64/float64 is not supported
</code></pre>
<p>thrown from <code>torchvision/transforms/functional.py</code> in <code>to_pil_image()</code>.</p>
<p>Further, even though the <code>short</code> datatype will work for the stand alone code snippet I provided first, it breaks down when used inside a <code>transform.Compose()</code> called from a <code>Dataset</code> object’s <code>__getitem__</code>:</p>
<pre><code class="lang-auto">choices = transforms.RandomChoice([transforms.RandomAffine(30),
                                   transforms.RandomPerspective()])

transform = transforms.Compose([transforms.ToPILImage(),
                                transforms.RandomApply([choices], 0.5),
                                transforms.ToTensor(),
                                transforms.Normalize((0.5,), (0.5,))])

trainset = MNIST('data/train.csv', transform=transform)
trainloader = DataLoader(trainset, batch_size=32, shuffle=True, num_workers=4)


</code></pre>
<pre><code class="lang-auto">RuntimeError: DataLoader worker (pid 12917) is killed by signal: Floating point exception.
RuntimeError: DataLoader worker (pid(s) 12917) exited unexpectedly
</code></pre>

Nikronic: <p>Hi,</p>
<aside class="quote no-group" data-username="Wesley_Neill" data-post="1" data-topic="87152">
<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/wesley_neill/48/25709_2.png" class="avatar"> Wesley_Neill:</div>
<blockquote>
<p><code>pic = pic.astype(int)</code></p>
</blockquote>
</aside>
<p>Solution:</p>
<pre><code class="lang-auto">pic = pic.astype(np.uint8)
</code></pre>
<p>Reason:<br>
Python <code>int</code> dtype is 64bits so it does not work on PIL images. If you scroll down the documentation of <code>ToPILImage</code>, you will find a link to all modes that PIL supports. <a href="https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes" class="inline-onebox" rel="noopener nofollow ugc">Concepts - Pillow (PIL Fork) 10.2.0.dev0 documentation</a></p>
<p>In this page, you can find that different dtypes correspond to different modes, so if you have a 0-255 image you need to use 8bit dtypes.</p>
<p>I think based on first issue, second one is also can be considered as solved, right?</p>
<p>Bests</p>

Wesley_Neill: <p>Yessir, that indeed solves my problem.</p>