How to input image in a model without normalizing it?

bolt25: <p>I’m struggling on how to train my network on images without normalizing them first ? Because when I use ToTensor as a transform, it normalizes the images automatically.</p>
<p><strong>Can anyone PLEASE help me out?</strong></p>

Nikronic: <p>Hi,</p>
<p>I think it can help:</p>
<pre><code class="lang-auto">try:
    import accimage
except ImportError:
    accimage = None

def _is_pil_image(img):
    if accimage is not None:
        return isinstance(img, (Image.Image, accimage.Image))
    else:
        return isinstance(img, Image.Image)


def _is_tensor_image(img):
    return torch.is_tensor(img) and img.ndimension() == 3


def _is_numpy_image(img):
    return isinstance(img, np.ndarray) and (img.ndim in {2, 3})

def my_to_tensor(pic):
    """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
    See ``ToTensor`` for more details.
    Args:
        pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
    Returns:
        Tensor: Converted image.
    """
    if not(_is_pil_image(pic) or _is_numpy_image(pic)):
        raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))

    if isinstance(pic, np.ndarray):
        # handle numpy array
        if pic.ndim == 2:
            pic = pic[:, :, None]

        img = torch.from_numpy(pic.transpose((2, 0, 1)))
        # backward compatibility
        if isinstance(img, torch.ByteTensor):
            return img.float()  ###### removed .div(255)
        else:
            return img

    if accimage is not None and isinstance(pic, accimage.Image):
        nppic = np.zeros([pic.channels, pic.height, pic.width], dtype=np.float32)
        pic.copyto(nppic)
        return torch.from_numpy(nppic)

    # handle PIL Image
    if pic.mode == 'I':
        img = torch.from_numpy(np.array(pic, np.int32, copy=False))
    elif pic.mode == 'I;16':
        img = torch.from_numpy(np.array(pic, np.int16, copy=False))
    elif pic.mode == 'F':
        img = torch.from_numpy(np.array(pic, np.float32, copy=False))
    elif pic.mode == '1':
        img = 255 * torch.from_numpy(np.array(pic, np.uint8, copy=False))
    else:
        img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
    # PIL image mode: L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK
    if pic.mode == 'YCbCr':
        nchannel = 3
    elif pic.mode == 'I;16':
        nchannel = 1
    else:
        nchannel = len(pic.mode)
    img = img.view(pic.size[1], pic.size[0], nchannel)
    # put it from HWC to CHW format
    # yikes, this transpose takes 80% of the loading time/CPU
    img = img.transpose(0, 1).transpose(0, 2).contiguous()
    if isinstance(img, torch.ByteTensor):
        return img.float()  ###### removed .div(255)
    else:
        return img

### test
class MyToTensor(object):
    def __call__(self, pic):
        return my_to_tensor(pic)
</code></pre>
<p>Note that, I have just copy pasted source code of <a href="https://github.com/pytorch/vision/blob/71322cba652b4ba7bcaa7ae5ee86f539d1ae3a2b/torchvision/transforms/functional.py#L23-L94" rel="nofollow noopener">ToTensor</a> and removed the lines where images have been normalized using <code>.div(255)</code>.<br>
You can use this <code>MyToTensor</code> instead of default <code>ToTensor</code> in <code>transofrms</code>.</p>
<p>Or another way will be to unnormalize tensors after using <code>ToTensor</code> in transforms. Here it is:</p>
<pre><code class="lang-auto">def unnormalize(tensor):
    return tensor.mul(255)

class Unnormalize(object):
    def __call__(self, tensor):
        return unnormalize(tensor)

img = Image.open('img.jpg')
img_tensor_normalized = transforms.ToTensor()(img)
img_tensor = Unnormalize()(img_tensor_normalized)
</code></pre>
<p>As before, you can add <code>Unnormalize</code> in your transforms pipepline after <code>ToTensor()</code>.</p>
<p>Bests</p>