torchvision.transform.Colorjitter report Image has a wrong mode

dreamyun: <pre><code class="lang-auto">bb=PIL.Image.fromarray(np.squeeze(temp))
torchvision.transforms.ColorJitter(brightness=0.4,saturation=0.4,contrast=0.4,hue=0.4)(bb)

</code></pre>
<blockquote>
<p>the information of temp is as follows:<br>
temp.dtype,temp.shape<br>
(dtype(‘float32’), (65, 65, 1))</p>
</blockquote>
<p>ValueError                                Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 torchvision.transforms.ColorJitter(brightness=0.4,saturation=0.4,contrast=0.4,hue=0.4)(bb)</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py in <strong>call</strong>(self, img)<br>
754         transform = self.get_params(self.brightness, self.contrast,<br>
755                                     self.saturation, self.hue)<br>
 → 756         return transform(img)<br>
757<br>
758     def <strong>repr</strong>(self):</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py in <strong>call</strong>(self, img)<br>
47     def <strong>call</strong>(self, img):<br>
48         for t in self.transforms:<br>
—&gt; 49             img = t(img)<br>
50         return img<br>
51</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py in <strong>call</strong>(self, img)<br>
281<br>
282     def <strong>call</strong>(self, img):<br>
 → 283         return self.lambd(img)<br>
284<br>
285     def <strong>repr</strong>(self):</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py in (img)<br>
725         if brightness &gt; 0:<br>
726             brightness_factor = random.uniform(max(0, 1 - brightness), 1 + brightness)<br>
 → 727             transforms.append(Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))<br>
728<br>
729         if contrast &gt; 0:</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/functional.py in adjust_brightness(img, brightness_factor)<br>
448<br>
449     enhancer = ImageEnhance.Brightness(img)<br>
 → 450     img = enhancer.enhance(brightness_factor)<br>
451     return img<br>
452</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/PIL/ImageEnhance.py in enhance(self, factor)<br>
35         :rtype: :py:class:<code>~PIL.Image.Image</code><br>
36         “”"<br>
—&gt; 37         return Image.blend(self.degenerate, self.image, factor)<br>
38<br>
39</p>
<p>~/anaconda3/envs/pytorch4/lib/python3.6/site-packages/PIL/Image.py in blend(im1, im2, alpha)<br>
2629     im1.load()<br>
2630     im2.load()<br>
 → 2631     return im1._new(core.blend(im1.im, im2.im, alpha))<br>
2632<br>
2633</p>
<p>ValueError: image has wrong mode</p>

ptrblck: <p>Currently you are passing an image in ‘F’ mode, i.e. as <code>flaot32</code>.<br>
Unfortunately it seems <code>PIL.Image.blend</code> doesn’t work with this kind of image.<br>
Could you convert your image to another format, e.g. <code>L</code> or would this destroy your data?</p>

dreamyun: <p><code>ColorJitter</code> can process images with one channel, namely  <code>n*n*1</code>?  I guess that because the image has only one channel not three or four channel.</p>

ptrblck: <p>I thought the same, but apparently at least <code>PIL</code> has no problems with grayscale images.<br>
It seems images in <code>L</code> mode (grayscale in <code>uint8</code>) work fine, while <code>F</code> gives the error in <code>Image.blend</code>.</p>

dreamyun: <p>I am agree with you. So,  <code>colorjitter</code> is not applied to these images with <code>F</code> model.  The pixel value is decimal, not an integer. I can not convert it to the images with ‘L’ mode.</p>

ptrblck: <p>In that case you could implement the contrast and brightness manipulation manually. Saturation and hue manipulations don’t really make sense regarding a grayscale image.</p>
<p>The brightness values should be manipulated additively, while the contrast should be changed multiplicatively. Your new image should therefore be calculated as:</p>
<pre><code class="lang-python">image = image * contrast_factor + brighness_factor
</code></pre>

Saharkakavand: <p>is it possible to change the brightness of all images with one value?<br>
I want to augment the whole dataset in getitem of the dataloader, but colorjitter works randomly.</p>

ptrblck: <p>You could use the functional API and call <a href="https://github.com/pytorch/vision/blob/5b1716a2ce67359c1aa8831b06011067bdbce1e2/torchvision/transforms/functional.py#L556">torchvision.transforms.functional.adjust_brightness</a> on each <code>Image</code>.</p>

Saharkakavand: <p>Thank you. is there a way to apply augmentation on torch tensor it self?<br>
I used :<br>
im_orig_tosave = Image.fromarray(np.uint8(padding_data))<br>
enhancer = ImageEnhance.Brightness(im_orig_tosave)<br>
enhanced_im = enhancer.enhance(0.6)<br>
enhanced_im = np.minimum(enhanced_im, 255.)<br>
padding_data = enhanced_im<br>
# pil image to tensor!<br>
I need to convert it back to a tensor.<br>
so I asked if I can directly modify the tensor in order to change the brightness</p>

ptrblck: <p>If I recall correctly, the brightness is changed by adding a constant to the image (and clipping to e.g. <code>uint8</code>).<br>
If you have a tensor in the right range, <code>image_tensor + offset</code> should do it.<br>
(The contrast should be manipulated with a scalar multiplication.)</p>

malodhi: <p>I found that torvision.transforms.Colorjitter only accepts RGB image and not single channel image, L.<br>
Is there any other way to increase contrast of Graysclae Image ?<br>
Thanks.</p>

ptrblck: <p>If my previous assumption is correct, you could multiply the grayscale image with a scalar factor to change the brightness (and clip it afterwards, if needed).</p>