Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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)

Output

 

Flipping the Image

flip = tf.image.flip_left_right(image)
plt.imshow(flip)

Output

 

Adjusting saturation in the Image

saturate = tf.image.adjust_saturation(image, 10)
plt.imshow(saturate)

Output

 

Rotating the Image by 90 degree

rotate = tf.image.rot90(image)
plt.imshow(rotate)

Output

 

Cropping the Image

crop = tf.image.central_crop(image, central_fraction=0.5)
plt.imshow(crop)

Output

 

Brightening the Image

brighten = tf.image.adjust_brightness(image, delta=0.6)
plt.imshow(brighten)

Output

 

Accessing various datasets in the TensorFlow library

 

Install and import all TensorFlow datasets

pip install tensorflow-datasets

import tensorflow_datasets as tfds

 

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"]

 

print(len(mnist_train))
print(len(mnist_test))

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'])
   

Output

 

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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()

 

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

if checkpoint_manager.latest_checkpoint:
   checkpoint.restore(checkpoint_manager.latest_checkpoint)
   

 

 

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]]

 

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