UnidentifiedImageError during Data Augmenatition

username: upasana_siva
cooked: <p>I am facing this UnidentifiedImageError when I try to display the augmented images. Kindly help in resolving this error:<br>
the code is as follows:</p>
<blockquote>
<p>class ImageDataset2(torch.utils.data.Dataset):<br>
def <strong>init</strong>(self, csv_file, transform = transform):<br>
# Input will be a csv file (self.reference) and optional transform<br>
self.reference = pd.read_csv(csv_file, header=None)<br>
# Will allow transforms to be defined in main, default is off (False)<br>
<span class="hashtag-raw">#self</span>.transform = transform<br>
self.transform = transform<br>
def <strong>len</strong>(self):<br>
# Finds size of data set for initializing training and validation<br>
return len(self.reference)<br>
def <strong>getitem</strong>(self, idx):<br>
# Load image from csv using imread function from imageio (renamed to io here)<br>
<span class="hashtag-raw">#image</span> = torch.tensor(io.imread(self.reference[0][idx])).permute(2, 1, 0)<br>
<span class="hashtag-raw">#label</span> = torch.tensor(self.reference[1][idx])-1<br>
label = self.reference[1][idx]<br>
label = torch.tensor(label)-1<br>
# Output image and label pair (Convert to torch friendly inputs)<br>
image = plt.imread(self.reference[0][idx])</p>
<pre><code>    image = Image.fromarray(image).convert('RGB')        
    image = np.asarray(image).astype(np.uint8)

    image = self.transforms(image)
    #image=torch.tensor(image).permute(2, 1, 0)
    #out = {
        #'image': image.type(torch.FloatTensor),
        #'label': label.type(torch.long)
    #}
    return torch.tensor(image, dtype=torch.float)
</code></pre>
<p>batchsize = 2<br>
trainset = ImageDataset2(csv_file=‘trainingSet.csv’, transform = True)<br>
trainLoader = torch.utils.data.DataLoader(dataset=trainset, shuffle=False, batch_size=batchsize)<br>
valset = ImageDataset2(csv_file=‘validationSet.csv’, transform = True)<br>
validLoader = torch.utils.data.DataLoader(dataset=valset, shuffle=False, batch_size=1)<br>
testset = ImageDataset2(csv_file=‘testingSet.csv’, transform=False)<br>
testLoader = torch.utils.data.DataLoader(dataset=testset, shuffle=False, batch_size=1)</p>
<p><span class="hashtag-raw">#display</span> images<br>
f = plt.figure(figsize=(15,15))<br>
n = range(1,11)<br>
i=0<br>
for batch in trainLoader:<br>
images = batch[‘image’]<br>
image = images[0]<br>
npimage = image.numpy()<br>
trans = transforms.ToPILImage()<br>
f.add_subplot(2,5,n[i])<br>
<span class="hashtag-raw">#plt</span>.imshow(trans(image))<br>
plt.imshow(np.transpose(npimg,(1,2,0)))<br>
plt.show()<br>
f.subplots_adjust(bottom=0.55)<br>
i=i+1<br>
if(i&gt;len(n)-1):<br>
break</p>
</blockquote>
<blockquote>
<p><strong>UnidentifiedImageError</strong> Traceback (most recent call last) <strong></strong> in  2 n <strong>=</strong> range**(** <strong>1</strong> <strong>,</strong> <strong>11</strong> <strong>)</strong> 3 i**=** <strong>0</strong> <strong>----&gt; 4 <strong><strong>for</strong></strong></strong> batch <strong>in</strong> trainLoader**:** 5 images <strong>=</strong> batch**[** <strong>‘image’</strong> <strong>]</strong> 6 image <strong>=</strong> images**[** <strong>0</strong> <strong>]</strong> <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py</strong> in <strong>next</strong><strong>(self)</strong> 515 <strong>if</strong> self**.** _sampler_iter <strong>is</strong> <strong>None</strong> <strong>:</strong> 516 self**.** _reset**(** <strong>)</strong> <strong> → 517</strong> data <strong>=</strong> self**.** _next_data**(** <strong>)</strong> 518 self**.** _num_yielded <strong>+=</strong> <strong>1</strong> 519 <strong>if</strong> self**.** _dataset_kind <strong>==</strong> _DatasetKind**.** Iterable <strong>and</strong> \ <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py</strong> in _next_data**(self)** 555 <strong>def</strong> _next_data**(** self**)** <strong>:</strong> 556 index <strong>=</strong> self**.** _next_index**(** <strong>)</strong> <strong># may raise StopIteration</strong> <strong> → 557</strong> data <strong>=</strong> self**.** _dataset_fetcher**.** fetch**(** index**)** <strong># may raise StopIteration</strong> 558 <strong>if</strong> self**.** _pin_memory**:** 559 data <strong>=</strong> _utils**.** pin_memory**.** pin_memory**(** data**)** <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data_utils\fetch.py</strong> in fetch**(self, possibly_batched_index)** 42 <strong>def</strong> fetch**(** self**,** possibly_batched_index**)** <strong>:</strong> 43 <strong>if</strong> self**.** auto_collation**:** <strong>—&gt; 44</strong> data <strong>=</strong> <strong>[</strong> self**.** dataset**[** idx**]** <strong>for</strong> idx <strong>in</strong> possibly_batched_index**]** 45 <strong>else</strong> <strong>:</strong> 46 data <strong>=</strong> self**.** dataset**[** possibly_batched_index**]** <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data_utils\fetch.py</strong> in <strong>(.0)</strong> 42 <strong>def</strong> fetch**(** self**,** possibly_batched_index**)** <strong>:</strong> 43 <strong>if</strong> self**.** auto_collation**:** <strong>—&gt; 44</strong> data <strong>=</strong> <strong>[</strong> self**.** dataset**[** idx**]** <strong>for</strong> idx <strong>in</strong> possibly_batched_index**]** 45 <strong>else</strong> <strong>:</strong> 46 data <strong>=</strong> self**.** dataset**[** possibly_batched_index**]** <strong></strong> in <strong>getitem</strong><strong>(self, idx)</strong> 22 label <strong>=</strong> torch**.** tensor**(** label**)** <strong>-</strong> <strong>1</strong> 23 <strong># Output image and label pair (Convert to torch friendly inputs)</strong> <strong>—&gt; 24</strong> image <strong>=</strong> plt**.** imread**(** self**.** reference**[** <strong>0</strong> <strong>]</strong> <strong>[</strong> idx**]** <strong>)</strong> 25 26 image <strong>=</strong> Image**.** fromarray**(** image**)** <strong>.</strong> convert**(** <strong>‘RGB’</strong> <strong>)</strong> <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py</strong> in imread**(fname, format)** 2059 <strong>@</strong> docstring**.** copy**(** matplotlib**.** image**.** imread**)** 2060 <strong>def</strong> imread**(** fname**,** format**=** <strong>None</strong> <strong>)</strong> <strong>:</strong> <strong> → 2061 <strong><strong>return</strong></strong></strong> matplotlib**.** image**.** imread**(** fname**,** format**)** 2062 2063 <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py</strong> in imread**(fname, format)** 1462 raise ValueError('Only know how to handle PNG; with Pillow ’ 1463 ‘installed, Matplotlib can handle more images’) <strong> → 1464 <strong><strong>with</strong></strong></strong> Image**.** open**(** fname**)** <strong>as</strong> image**:** 1465 <strong>return</strong> pil_to_array**(** image**)** 1466 <strong>from</strong> matplotlib <strong>import</strong> _png <strong>~\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py</strong> in open**(fp, mode)** 2929 warnings**.** warn**(** message**)** 2930 raise UnidentifiedImageError( <strong> → 2931 <strong><strong>“cannot identify image file %r”</strong></strong></strong> <strong>%</strong> <strong>(</strong> filename <strong>if</strong> filename <strong>else</strong> fp**)** 2932 ) 2933 <strong>UnidentifiedImageError</strong> : cannot identify image file ‘C:\Users\DELL\Documents\RGB Image Slices\Day 3\2303\WoundEdgeStack-002\Slice90.tiff’</p>
</blockquote>

username: ptrblck
cooked: <p>The error is raised by <code>PIL/Pillow</code>, which cannot create an image given the provided array. You can check supported formats <a href="https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html">here</a> and make sure to transform the image into a listed format. Additionally, you could check, if e.g. <code>matplotlib</code> would allow to display the currently provided array.</p>

username: upasana_siva
cooked: <p>I am getting the same when I change the format. I think that there is something wrong in the class ‘imagedataset2’ because the error is in the line</p>
<p>----&gt; 4 for batch in trainLoader:</p>
<p>but I can’t spot that out. I am actually a beginner in pytorch. Please let me know your thoughts here.</p>