Training a CNN with TIFF images in pytorch

username: Vijay_Dubey
cooked: <p>I am working on a project of object detection in a Kinect depth image in the TIFF format. I have coded the neural network but now I am Stuck.<br>
I have no idea how to use the TIFF images stored on my computer to train the model and perform object detection. Please help.<br>
I also have to draw a bounding box around the particular object if it is detdcted in the image.Any help regarding that or some similar Project which I can refer to would be very useful</p>
<p>Thanks and regards<br>
Vijay</p>
<p><a class="mention" href="/u/chsasank">@chsasank</a></p>

username: jytug
cooked: <p>Working with <code>tiff</code> images shouldn’t be any different from working with <code>jpeg</code>. You can use some library for loading images, like <code>PIL</code>, and then use <code>torchvision.transforms</code> to transform it to a <code>torch.Tensor</code>:</p>
<pre><code class="lang-auto">from PIL import Image
from torchvision.transforms import ToTensor
image = Image.open('/path/to/image.tif')
image = ToTensor()(image)
</code></pre>
<p>To learn more about loading images, and piping them nicely to torch <code>model</code>s, read this tutorial:</p>
<p><a href="http://pytorch.org/tutorials/beginner/data_loading_tutorial.html" class="onebox" target="_blank" rel="nofollow noopener">http://pytorch.org/tutorials/beginner/data_loading_tutorial.html</a></p>

username: prasasnth
cooked: <p>If you have any luck working with tiff images can you post the code please…</p>

username: yassineAlouini
cooked: <p>Hello there, my first post here so hopefully something useful. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>I am currently working with TIFF images and so far found the <a href="https://rasterio.readthedocs.io/en/latest/" rel="noopener nofollow ugc">rasterio</a> library the best option in terms of installation ease, functionalities, speed, and so on.</p>
<p>Here is a short code snippet to expand on what <a class="mention" href="/u/jytug">@jytug</a> has already answered:</p>
<pre><code class="lang-auto">from torchvision.transforms import ToTensor
import rasterio
image = rasterio.open(f"path/to/image.tiff")
image = ToTensor()(image)
</code></pre>
<p>The only problem I had with <code>PIL</code> is that you need to change the max size otherwise it errors (preventing DOS bomb attack apparently).<br>
Finally, notice that you need some amount of RAM if the image is big of course.</p>

username: jsetty
cooked: <p>At the moment PIL cannot read multi-band floating-point images or int 16/32 images, see here: <a href="https://github.com/python-pillow/Pillow/issues/1888" rel="noopener nofollow ugc">https://github.com/python-pillow/Pillow/issues/1888</a>. It will throw “PIL.UnidentifiedImageError: cannot identify image file”.</p>
<p>You can use tifffile like this:</p>
<pre><code class="lang-auto">from tifffile import imread
from torchvision.transforms import ToTensor
image= imread(image_path)
image = ToTensor()(image)
</code></pre>

username: yassineAlouini
cooked: <p>I am trying to use this and I found it it was missing one command. <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">from torchvision.transforms import ToTensor
import rasterio
image = rasterio.open(f"path/to/image.tiff")
image = image.read()
image = ToTensor()(image)
</code></pre>
<p>Sorry for this!</p>

username: Hari_Priyanka_K
cooked: <p>I am working on CT images. However, one of the publicly available dataset contains .tif images. Before training the model i would like to view the images. How do i view a tif image on my system? Now they look like black image.</p>

username: amspsingh04
cooked: <p>To view the tif images you can simply use:</p>
<p>from PIL import Image<br>
img_path = r’path\to\image’<br>
im = Image.open(img_path)<br>
im.show()</p>
<p>(Image.MAX_IMAGE_PIXELS = None can be used if the tif file is to large to view, this is a safety guardrail tho so better not to use this command unless you know what youre doing.)</p>
<p>This should load and show your image, you can also convert it to a numpy array or a torch tensor as has been answered before in this thread.</p>
<p>You can use third party tools like asap to view these images if you have memory issues and cannot load the .tif images.</p>