Data Lab 3: Tensors

1. Setup

Open the google colab notebook Tensors.ipynb provided to you in your personal Block C Github repository.

2. Assignment on Tensors in Tensorflow

Start with the assignment in the python notebook regarding Tensors. There are some 25 easy questions (all the comments in the notebook). Complete the assignment and upload copy of the notebook regularly to Github.

3. Convert Image to Tensorflow Tensor

In this section, you will learn to implement image to tensor conversion code for Tensorflow framework. As you have known, tensors represents the building blocks for your machine learning project. They are are immutable and can only be created and not updated.

You might assume that when an image is converted into a tensor, that resulting tensor will be rank two. It's easy to picture a 2D image as a 2D tensor, except that pixel color generally can't be stored as a single number. A rank-two tensor works only with grayscale images. The most common practice for a colored pixel is to represent it as three separate values. The red, green, blue (RGB) system. Now will be a good time to revisit your Block A Github pages in case you forgot about RGB system in images. Link1 Link2

For your information, the typical axis order for an image tensor in Tensorflow is as follows:

shape=(N, H, W, C)

- N — batch size (number of images)
- H — height of the image
- W — width of the image
- C — number of channels (usually uses 3 channels for RGB)

You can easily load an image file (PNG, JPEG, etc.) with the following code:

import tensorflow as tf

# load image via tf.io
img = tf.io.read_file("image_to_load.png")

# convert to tensor (specify 3 channels explicitly since png files contains additional alpha channel)
# set the dtypes to align with tensorflow for comparison since it will use uint8 by default
tensor = tf.io.decode_image(img, channels=3, dtype=tf.dtypes.float32)
# (384, 470, 3)

# resize tensor to 224 x 224
tensor = tf.image.resize(tensor, [224, 224])
# (224, 224, 3)

# add another dimension at the front to get NHWC shape
input_tensor = tf.expand_dims(tensor, axis=0)
# (1, 224, 224, 3)

4. Change Tensorflow Tensors Dimension

Furthermore, Tensorflow does provide an useful function called tf.transpose to permutes the dimensions of tensor according to the value of perm. By default, perm is set to [n-1…0] where n represent the number of dimension.

Let's say that you would like to change the shape of tensor from

(224, 224, 3) -> (3, 224, 224)

Simply call the tf.transpose function as follows:

tensor = tf.transpose(tensor, perm=[2, 0, 1])

5. Assignment: (to be done in Tensors.ipynb)

A very simple assignment to end the day and recap why tensor data structure is important to store big image data sets (which we will need to do image classification using deep learning)

Follow tensorflow tutorial for the assignment.

5a load any image in your program

5b Use some features like resizing of the image, converting image to grey scale image, etc. to observe and comment on the corresponding changes it does to tensors.

5c make a 500x500 checkered board structure in tensor (revisit how you made heart using numpy in Block A)

5d Download Flower datasets.

Resize any 3000 images, by

1) Looping over the collection of images and resizing them

2) Batching a group of images as a rank-four tensor and resizing the entire tensor

Also, track processing time and answer - Which option is faster? and why ?

Helpful resource - https://www.tensorflow.org/tutorials/load_data/images

Resources

https://www.oreilly.com/library/view/learning-tensorflowjs/9781492090786/ch04.html