Table of contents
1.
Image Augmentation
1.1.
Output
1.2.
Flipping the Image
1.3.
Output
1.4.
 
1.5.
Adjusting saturation in the Image
1.6.
Output
1.7.
Rotating the Image by 90 degree
1.8.
Output
1.9.
Cropping the Image
1.10.
Output
1.11.
Brightening the Image
1.12.
Output
2.
Accessing various datasets in the TensorFlow library
2.1.
Install and import all TensorFlow datasets
2.2.
MNIST Digits Dataset
2.3.
Output
2.4.
Titanic Dataset
2.5.
Output
3.
Saving Checkpoints of a Model
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

TensorFlow Functions to make Deep Learning Easier

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Image Augmentation

 

import tensorflow as tf
import matplotlib.pyplot as plt

image = tf.io.read_file("tf_CN.png")
image = tf.io.decode_jpeg(image)
plt.figure()
plt.imshow(image)
You can also try this code with Online Python Compiler
Run Code

Output

 

Flipping the Image

flip = tf.image.flip_left_right(image)
plt.imshow(flip)
You can also try this code with Online Python Compiler
Run Code

Output

 

Adjusting saturation in the Image

saturate = tf.image.adjust_saturation(image, 10)
plt.imshow(saturate)
You can also try this code with Online Python Compiler
Run Code

Output

 

Rotating the Image by 90 degree

rotate = tf.image.rot90(image)
plt.imshow(rotate)
You can also try this code with Online Python Compiler
Run Code

Output

 

Cropping the Image

crop = tf.image.central_crop(image, central_fraction=0.5)
plt.imshow(crop)
You can also try this code with Online Python Compiler
Run Code

Output

 

Brightening the Image

brighten = tf.image.adjust_brightness(image, delta=0.6)
plt.imshow(brighten)
You can also try this code with Online Python Compiler
Run Code

Output

 

Accessing various datasets in the TensorFlow library

 

Install and import all TensorFlow datasets

pip install tensorflow-datasets

import tensorflow_datasets as tfds
You can also try this code with Online Python Compiler
Run Code

 

There are different widely used datasets available in this library.

 

MNIST Digits Dataset

mnist_data = tfds.load("mnist")
mnist_train, mnist_test = mnist_data["train"], mnist_data["test"]
You can also try this code with Online Python Compiler
Run Code

 

print(len(mnist_train))
print(len(mnist_test))
You can also try this code with Online Python Compiler
Run Code

Output

 

Titanic Dataset

data = tfds.load('titanic', split='train', shuffle_files=True)

data = data.shuffle(1024).batch(32).prefetch(tf.data.experimental.AUTOTUNE)
for i in data.take(1):
   print("Features")
   print(i['features'])
   
   print("Labels")
   print(i['survived'])
   
You can also try this code with Online Python Compiler
Run Code

Output

 

 

Saving Checkpoints of a Model

 

We use checkpoints to capture the exact parameters of a model; when source code to use the parameters is available, we use the checkpoints.

checkpoint_path = “random_save_path”

# Define a Checkpoint
checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)

# Create a CheckpointManager Object
checkpoint_manager = tf.train.CheckpointManager(checkpoint, checkpoint_path, max_to_keep=5)

# Save the Model
checkpoint_manager.save()
You can also try this code with Online Python Compiler
Run Code

 

TensorFlow uses the following directed graph to load from the latest Checkpoint.

if checkpoint_manager.latest_checkpoint:
   checkpoint.restore(checkpoint_manager.latest_checkpoint)
   
You can also try this code with Online Python Compiler
Run Code

 

 

Source: https://towardsdatascience.com/

 

Frequently Asked Questions

 

Q1. How can you create a Black Image with the same shape as the input image?

Ans. A Black image contains all zeros; hence we can use tf.zeros_like. The tf.zeros_like takes a tensor (input_image) as input & creates a tensor of zeros of the same shape and size.

tensor = tf.constant([[1, 2, 3], [7, 8, 9], [4, 5, 6])

tf.zeros_like(tensor) #[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
You can also try this code with Online Python Compiler
Run Code

 

Q2. What is ImageDataGenerator?

Ans. ImageDataGenerator is one of the features of tensorflow.keras API. We use it to perform data augmentation, such that we can train our model with different new combinations of data. With ImageDataGenerator, we do not generate new images directly, but the dataset images are transformed dynamically.

 

Q3. What are the different types of datasets available in the TensorFlow library?

Ans. There are many types of datasets that are available in TensorFlow, such as:

  • Audio Datasets
  • Graphs type
  • Image Datasets
  • Image Classification
  • Object Detection
  • Text

 

To know more, visit the official website of TensorFlow.

 

Key Takeaways

Check out this link if you are a Machine Learning enthusiast or want to brush up your knowledge with ML blogs.

 

If you are preparing for the upcoming Campus Placements, don't worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.

Live masterclass