Fred Malack
unpack
Published in
3 min readApr 19, 2021

--

TENSORS IMAGES REPRESENTATION

IMAGES REPRESENTATION

The representation of an image can take many forms. Most of the time, it refers to the way that the conveyed information, such as color, is coded digitally and how the image is stored, i.e., how is structured an image file. Several open or patented standards were proposed to create, manipulate, store and exchange digital images. They describe the format of image files, the algorithms of image encoding such as compression as well as the format of additional information often called metadata.

TENSORS IMAGES

Tensors can be understood as nested lists of objects of the previous order all with the same size. For example, an order three tensor can be thought of as a list of matrices all of which have the same number of rows and columns. These matrices are tensors of order two and since they have all the same number of rows and columns, the tensor of order three is actually like a cuboid of numbers and we can find numbers by going along any of the three-axis. Each number is identified by the row, the column, and the depth at which it’s stored. We can formalize this idea in the concept of shape.

IMAGE REPRESENTATION IN FASTAI

In fastai we use tensors to represent images, find the following piece of code which illustrates how to create a tensor flow. For example, Let’s create a tensor containing 3s and 7s stacked together.

To view the image which is represented in tensor one can do the following.

Finally, we can compute what the ideal 3 looks like. We calculate the mean of all the image tensors by taking the mean along dimension 0 of our stacked, rank-3 tensor. This is the dimension that indexes over all the images.

In other words, for every pixel position, this will compute the average of that pixel overall images. The result will be one value for every pixel position or a single image. Here it is:

BROADCASTING WITH TENSORS

Broadcasting is an important capability that makes tensor code much easier to write. The magic trick is that PyTorch when it tries to perform a simple subtraction operation between two tensors of different ranks will use broadcasting. That is, it will automatically expand the tensor with the smaller rank to have the same size as the one with the larger rank.

After broadcasting so the two-argument tensors have the same rank, PyTorch applies its usual logic for two tensors of the same rank: it performs the operation on each corresponding element of the two tensors, and returns the tensor result. For instance:

Citations:-

  1. https://cloudacademy.com/course/convolutional-neural-networks/images-as-tensors/
  2. https://link.springer.com/referenceworkentry/10.1007%2F978-0-387-39940-9_1438

--

--