Generating Cats with Tensorflow

Generating Cats with Tensorflow

Using science to generate images of cats with Tensorflow and serve this as a service on azure? ☁️🙀 To achieve this we’ll use Python 3.8 and Jupyter Notebook 🐍 Tensorflow 2.3.1 (cpu) 🤖 Flask 🌶 Docker & docker hub 🐳 Azure container service 📦

Using science to generate images of cats and serve this as a service on azure? ☁️🙀 Why not!To achieve this we’ll use the following technologies:

  • Python 3.8 and Jupyter Notebook 🐍Tensorflow 2.3.1 (cpu) 🤖Flask 🌶Docker & docker hub 🐳Azure container service 📦

What are Deep Convolutional Generative Adversarial Networks?Google says: “Generative Adversarial Networks_ (GANs) are one of the most interesting ideas in computer science today. Two models are trained simultaneously by an adversarial process. A generator (“the artist”) learns to create images that look real, while a discriminator (“the art critic”) learns to tell real images apart from fakes.”_

Image for post

Training of GANs is a process of training two neural nets simultaneously.

Image for post

Even though Google describes this approach on images of cats, jupyter notebook they provide uses boring dataset of MNIST handwritten digits.So let’s start by downloading and running notebook file from here.

Necessary steps

  • Swap the dataset,Reshape the data,Increase image resolution,Serve model from docker.

New DatasetFor this demo we’ll use dataset of cat faces from kaggle. It contains images of 15K cats at resolution of 64x64.

Image for post

Sample cat faces from dataset (

Google uses this line to initialise train_images and _train_labels _variables.

(train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()

We’ll take the lazy approach and replace its contents with our cats. To do so create data directory with the following structure. Our _data/catdata _folder contains one subfolder catface with images. If your dataset contains more classes place their images in a folder on the same level as catface. Folder names and class_names must match.You can read more about directory structure parameters here and here.

cat_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    image_size=(img_size, img_size), #img_size should be 28 for now

Data reshapingNext step is to iterate over the cat_dataset structure and transform it into the same structure as MNIST had.

cat_train_labels = []
cat_train_images = []

for images, labels in cat_dataset:
    for i in range(len(images)):
c_images = np.array(cat_train_images)
c_images = c_images.reshape(c_images.shape[0],img_size,img_size,)
c_labels = np.array(cat_train_labels)
c_labels = c_labels.reshape(c_labels.shape[0],)

Ensure that our data shape matches the original data shape (except for the count of items).


train_labels = c_labels
train_images = c_images

Without any further modifications, your training process should be able to start with the original resolution of 28x28.

Image for post

Training progress of 250 epochs with resolution of 56x56 (faulty run with only ~4K samples)

Before proceeding you may want to store your reshaped numpy arrays to files: (Source)

from numpy import save
save('./data/images.npy', c_images)
save('./data/labels.npy', c_labels)

Increasing the resolution28x28 may be enough for handwritten digits, but for pictures of cats it is not that much. To get a higher resolution predictions we need to change size of the discriminator and generator model layers.The golden rule:** Output size of the generator should match the input size of the discriminator. **I messed around with the numbers, but I ended up with the following:

Image for post

Last few lines of make_generator_model()

Image for post

First lines of discriminator model

Increasing values by two will grant us twice the original resolution.

tensorflow machine-learning artificial-intelligence python developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Machine Learning Developers in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.

AI(Artificial Intelligence): The Business Benefits of Machine Learning

Enroll now at CETPA, the best Institute in India for Artificial Intelligence Online Training Course and Certification for students & working professionals & avail 50% instant discount.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[]( "")**, our full-stack Python development services...

Learning in Artificial Intelligence - Great Learning

What is Artificial Intelligence (AI)? AI is the ability of a machine to think like human, learn and perform tasks like a human. Know the future of AI, Examples of AI and who provides the course of Artificial Intelligence?

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[]( "")**, we constantly endeavor to give you exactly what you need. If you need to...