Introduction

This document helps the user to build, train, test, and validate GAN Deep Learning model using Razorthink aiOS SDK. We can run the model either in Jupyter Notebooks, or the Razorthink robust engines.


The code is as follows.

import razor
import numpy as np
from razor import Pipeline, Block, inputs, outputs
from razor.dl import DlBlock
from rztdl.dl.components.layers import Input, Dense, Conv2DTranspose, Conv2D, MaxPool2D
from rztdl.dl.components.losses import MeanSquaredError
from rztdl.dl.components.operators import Add, Flatten, Reshape
from rztdl.dl.components.optimizers import Adam
from rztdl.dl.constants.string_constants import Padding
from rztdl.dl.groups import Group
from rztdl.dl.helpers.activations import Sigmoid
from rztdl.dl import RZTModel
from razor.dl import DlBlock
from razor import Pipeline
from razor.core.blocks.executors import SubProcessExecutor
from razor.core.blocks.transports import SocketTransport


Generator Block

generator = Group("generator", scopes=["A"])
generator.add(Dense(name="dense_1", units=5, inputs="dense_input"))
generator.add(Dense(name="dense_2", units=128 * 7 * 7))
generator.add(Reshape(name="r1", target_shape=[7, 7, 128]))
generator.add(Conv2DTranspose(name="conv2D_transpose", filters=3, kernel_size=[4, 4], strides=[2, 2]))


Discriminator Block

discriminator = Group("discriminator", scopes=['B'])
discriminator.add(Conv2D(name="conv1", kernel_size=2, filters=3, padding=Padding.SAME, inputs="conv1_in"))
discriminator.add(MaxPool2D(name="max_pool_1", pool_size=3, strides=2, padding=Padding.SAME))
discriminator.add(Flatten(name="flatten"))
discriminator.add(Dense(name="out", units=1, activation=Sigmoid()))


Input Layers

model = RZTModel("DC_GAN")
model.add(Input(name="noise", shape=[10]))
model.add(Input(name="images", shape=[16, 16, 3]))
model.add(Input(name="ones", shape=[1]))
model.add(Input(name="zeros", shape=[1]))


Connections

gen = generator("gen", inputs={"dense_input": 'noise'})
model.add(gen)
real_disc = discriminator("real_disc", inputs={"conv1_in": 'images'})
model.add(real_disc)
disc_fake = discriminator('disc_fake',inputs={"conv1_in": gen.get('conv2D_transpose')}, 
                                              shared="real_disc")
model.add(disc_fake)

model.add(MeanSquaredError(name="disc_mse_real", predictions=real_disc.get('out'), labels="ones"))

model.add(MeanSquaredError(name="disc_mse_fake", predictions=disc_fake.get('out'), labels="zeros"))

model.add(Add(name="discriminator_loss", inputs=['disc_mse_real', 'disc_mse_fake']))

model.add(MeanSquaredError(name="generator_loss", predictions=disc_fake.get('out'), labels='ones'))

model.add(Adam(name="generator_optimizer", inputs='generator_loss', scopes=['A']))

model.add(Adam(name="discriminator_optimizer", inputs='discriminator_loss', scopes=['B']))

model.plot()


Data Reader Block

@outputs.series.generic(name="data")
class DataProducer(Block):
    def run(self, data):
        num_records = 100
        for i in range(num_records):
            k = {
            "noise": np.random.random((10)),
            'images': np.random.random((16, 16, 3)),
            'ones': np.ones((1)),
            'zeros': np.zeros((1))
            }
            data.put(k)
data_producer = DataProducer()


Trainer Block

trainer = ( DlBlock("Gan_Trainer")
     .rzt_model(model)
     .mode("FIT")
     .data(data_producer.data)
     .test_data(data_producer.data)
     .epochs(1)
     .optimizers("discriminator_optimizer")
     .metrics(['discriminator_loss'])
     .batch_size(10)
     .model_save_path("gan")
     .log_frequency(5)
     .layer_output(transport=SocketTransport))


Persistent Trainer Block

persist_model = (DlBlock("DC_GAN_PERSIST")
     .rzt_model(model)
     .mode("PERSIST_TRAIN")
     .data(data_producer.data)
     .test_data(data_producer.data)
     .epochs(1)
     .optimizers("generator_optimizer")
     .metrics(['generator_loss'])
     .batch_size(10)
     .model_load_path(trainer.model_saved_path)
     .model_save_path("gan")
     .log_frequency(5)
     .layer_output(transport=SocketTransport))


Inference Block

infer_model = ( DlBlock(name="gan_infer")
    .rzt_model(model)
    .mode("PREDICT")
    .batch_size(10)
    .data(data_producer.data)
    .test_data(data_producer.data)
    .model_load_path(persist_model.model_saved_path)
    .layers(['generator_loss'])
    .layer_output(transport=SocketTransport))


The pipeline will look as follows -

pipeline = Pipeline(targets=[infer_model])
pipeline.show()



Executing the pipeline in the Jupyter Notebooks.

pipeline.execute(default_executor=SubProcessExecutor)


Executing the pipeline in the Razorthink aiOS Engine.

a=[n.name for n in razor.platform.engines()]
running_pipeline = razor.platform.engines(name=a[0]).execute(pipeline=pipeline)


We can view the status of the pipeline in the Engine using the below code.

running_pipeline.status().__dict__


We can view the logs of the pipeline in the Engine using the below code.

running_pipeline.logs(pretty=True)


We can view the model metrics, and Hardware metrics of the pipeline in the Engine using the below code.

running_pipeline.metrics(time_unit="milliseconds").plot()