<jupyter_start><jupyter_text>IT'S TIME TO GENERATE NEW CARS IMAGES ![Inference.png](https://drive.google.com/uc?export=view&id=12JP9twsCjRrOuWBL_uws4Wt0ySUK8EsF)Measure the GAN performance by:* Images Quality* Images Diversity  Mount Drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Mounted at /content/drive<jupyter_text>Imports<jupyter_code>from glob import glob
import tensorflow as tf
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import Input
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers import (Activation, BatchNormalization, Concatenate, Dense,
                          Embedding, Flatten, Input, Multiply, Reshape)
from keras.layers.advanced_activations import LeakyReLU, ReLU
from keras.models import Model, Sequential
from keras.optimizers import Adam
from sklearn.preprocessing import OrdinalEncoder<jupyter_output><empty_output><jupyter_text>Configs<jupyter_code>CHECKPOINT_DIR = '/content/drive/My Drive/lecture hands on lab/cGAN/results/inference_checkpoint'
# Data path
DATA_PATH = "/content/drive/My Drive/lecture hands on lab/datasets/cars/cars_images/"
LR = 1e-4
LATENT_DEPTH = 100<jupyter_output><empty_output><jupyter_text>Data<jupyter_code># Extract cars models
def unique(list1): 
    list_set = set(list1) 
    unique_list = (list(list_set)) 
    return unique_list
cars_images_path = list(glob(str(DATA_PATH + '*.jpg')))
images_name = [i.split(DATA_PATH) for i in cars_images_path]
images_name = [x[:][1] for x in images_name]
cars_model = [i.split('_')[0] for i in images_name]
unique_cars = unique(cars_model)
cars_array = np.reshape(unique_cars,(-1, 1))
encoder = OrdinalEncoder()
encoder.fit(cars_array)
label_encoded = encoder.transform(cars_array)<jupyter_output><empty_output><jupyter_text>Loading Checkpoint<jupyter_code>def make_generator_model(latent_dim, n_classes=20):
    # label input
    # embedding for categorical input
    # linear multiplication
    # reshape to additional channel

    label = Input(shape=(1,))
    label_embedding = Embedding(n_classes, 50)(label)
    label_embedding = Dense(25 * 25)(label_embedding)
    label_embedding = Reshape((25, 25, 1))(label_embedding)

    input_noise = Input(shape=(latent_dim,))
    layer = Dense(128 * 25 * 25)(input_noise)
    layer = ReLU()(layer)
    layer = Reshape((25, 25, 128))(layer)
    merge = Concatenate()([layer, label_embedding])


    layer = Conv2DTranspose(128, (5,5), strides=(1,1), padding='same', use_bias=False)(merge)
    layer = ReLU()(layer)

    layer = Conv2DTranspose(64, (5,5), strides=(2,2), padding='same', use_bias=False)(layer)
    layer = ReLU()(layer)

    output_layer = Conv2DTranspose(3, (5,5), strides=(2,2), activation='sigmoid', padding='same', use_bias=False)(layer)

    model = Model([input_noise, label], output_layer)
    model.summary()
    return model
generator = make_generator_model(latent_dim=100)
generator_optimizer = tf.keras.optimizers.Adam(lr=LR)
checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer, generator=generator)
checkpoint.restore(tf.train.latest_checkpoint(CHECKPOINT_DIR))<jupyter_output><empty_output><jupyter_text>Inference After using the inference method we can tell if there is model collapse or no<jupyter_code>def generate_images(model, label):
    
    plt.figure(figsize=(15,10))

    for i in range(4):
        noise = tf.random.normal([1,100])
        images = model([noise, label], training=False)
        image = images[0, :, :, :]
        image = np.reshape(image, [100, 100, 3])

        plt.subplot(1, 4, i+1)
        plt.imshow(image, cmap='gray')
        plt.axis('off')
        plt.title("Randomly Generated Images_")

    plt.tight_layout()  
    plt.show()<jupyter_output><empty_output><jupyter_text>* 0: 'Chrysler'* 1: 'Audi'* 2: '<NAME>'* 3: 'Tesla'* 4: 'Volvo'* 5: 'Rolls-Royce'* 6: 'Bentley',* 7: 'Chevrolet'* 8: 'Buick'* 9: '<NAME>'* 10: 'Nissan'* 11: 'Ram'* 12: 'Cadillac'* 13: 'Porsche'* 14: 'Toyota'* 15: 'Volkswagen'* 16: 'smart'* 17: 'Acura'* 18: 'Subaru'* 19: 'BMW'<jupyter_code>x = label_encoded[7] 
generate_and_save_images(generator, x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13174077578051088, 'fraction_numerical': 0.028855250709555344, 'mean_word_length': 5.51617873651772, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '469584', 'license': 'MIT', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1495, 'n_words': 310, 'path': 'CGAN/Inference.ipynb', 'repo_name': 'parksurk/gmnd', 'size': '4238'}