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:
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.”_
Training of GANs is a process of training two neural nets simultaneously.
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.
Sample cat faces from dataset (https://www.kaggle.com/spandan2/cats-faces-64x64-for-generative-models)
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( "./data/catdata", labels="inferred", label_mode="int", class_names=['catface'], color_mode="grayscale", batch_size=32, image_size=(img_size, img_size), #img_size should be 28 for now shuffle=True, seed=None, validation_split=None, subset=None, interpolation="bilinear", follow_links=False, )
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)): cat_train_images.append(images[i]) cat_train_labels.append(labels[i]) c_images = np.array(cat_train_images) c_images = c_images.reshape(c_images.shape,img_size,img_size,) c_labels = np.array(cat_train_labels) c_labels = c_labels.reshape(c_labels.shape,)
Ensure that our data shape matches the original data shape (except for the count of items).
print(c_images.shape) print(train_images.shape) print(c_labels.shape) print(train_labels.shape) 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.
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:
Last few lines of make_generator_model()
First lines of discriminator model
Increasing values by two will grant us twice the original resolution.
We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.
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.
Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...
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?
Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...