Introduction:

In this guide, we are demonstrating a Deep Learning model-based image classification problem using the popular Fashion-MNIST dataset. The deep learning model will be trained to classify images of clothing, like sneakers and t-shirts, a total of 10 labels

Although the dataset is relatively simple, it can be used as the basis for learning and practicing how to develop, train, and use neural networks on RZT aiOS from scratch.


Data Overview:

This dataset has 60,000 small squares, and 28×28 pixel grayscale images of 10 different types of clothing, such as shoes, t-shirts, dresses, and more. The mapping of all 0-9 integers to class labels and sample data is depicted in the image below.

The following tutorial will mainly walk you through:

  • Create a Block to read the data file for DL model training and DL model inference 
  • Create a Block to write the Deep Learning model prediction results to a file
  • How to visually create a Deep Learning model using RZT aiOS
  • How  to use a DL model for training or prediction
  • How to build a Pipeline with Blocks and DL model


Preparing the dataset:

We are getting the dataset from Keras APIs. Basically there are 2 files with image data, ‘data_train.npz’ will be used for training the model and ‘data_test.npz’ will be used for prediction.



How to build this use case on RZT aiOS:

We will be building the model for this project using RZT aiOS DL model designer and will create two custom blocks, one to read the file and another one to write the output to a file. In total, we will be 


  • Create a project from the RZT aiOS homepage, we'll name it ‘Fashion MNIST classification”. You can provide it with a description you would like.


  • Click on the Project you just created and once you are inside, click on the “data” icon on the left side panel. Here you’ll see the “Open Jupyter Lab” button on the top right corner. Click on that. It will open an instance of Jupyter Lab for you. Now, create a Python Jupyter Notebook.
  • Let's start with the import statements

    import numpy as np

    import pandas as pd

    import matplotlib.pyplot as plt


    from razor.dl import DlBlock

    from razor import Block, inputs, outputs, Pipeline

    from razor.api import project_space_path

  • Setting up the data:        
                1. You can upload the data in your project-space directly if you have that in your local computer or you can use Keras API like we have, to get the dataset.

from tensorflow.keras.datasets.fashion_mnist import load_data


(x1, y1), (x2, y2)= load_data()

                            

                    2. And then you can save it in your project-space using the following command.

from razor.api import project_space_path

np.savez(project_space_path('data_train.npz'), images=x1, labels=y1)

np.savez(project_space_path('data_test.npz'), images=x2, labels=y2)



  • We have two levels of hierarchy for blocks, org and project. The blocks which are published as “org” blocks will be available for all the projects in your organisation account, and “project” blocks will be available only inside that particular project.

  • We will be creating a project level block in this tutorial. We go inside the project folder and create our Python bundle (bundle is basically how we are grouping our blocks in a category to have a proper hierarchy). 
  • We will create an “ImageClassificationV1” bundle for now.


  • Then we’ll go inside our bundle folder and create a python file to write our block code. Here, the python file name is “fashionmnist.py”.

  • Then import RZT aiOS SDK libraries to create blocks.

from razor import Block, inputs, outputs, Pipeline


  • We can now write our first custom block which we will use to read input files from Project Space. You can use this Block to read any numpy files from Project Space as well. This block takes two inputs, one is the path URL of string type and other is for no of examples/samples to read from the numpy file which is of integer type and has one output data of generic type.

@inputs.atomic.string("path", required=True)

@inputs.atomic.integer("no_of_examples", default=1000)

@outputs.series.generic("data")

 

  • We will now define a class “LoadImagesV1” which we will use to load our images data file from Project Space. Add necessary details for the block, and write to your code inside the run function.

class LoadImagesV1(Block):

    __publish__ = True

    __label__ = "LoadImagesV1"

    __description__ = ""

    __tags__ = []

    __category__ = "Image Classification"

    

    def run(self, path, no_of_examples, data):

        import numpy as np

        import pandas as pd

        from razor.platform import project_space_path

        import logging

        logging.basicConfig(level=logging.DEBUG, format='%(name)-15s - %(levelname)-8s - %(message)s')

        

        npy = np.load(project_space_path(path))

        

        logger = logging.getLogger()

        logger.debug('Running Load Images')

            

        for image, label in zip(npy['images'][:no_of_examples], npy['labels'][:no_of_examples]):

            image = image.astype('float32') / 255.0

            label = np.eye(10)[[label]].reshape(-1)

            image = np.reshape(image, (image.shape[0], image.shape[1], 1))

            

            data.put({"images":image, "labels":label})

            

        logger = logging.getLogger()

        logger.debug('Completed Load Images')



  • Similarly we will create another block to write the output data as a CSV file, configure its input and output, add details and then add our code inside run function. Again, you can reuse this Block to write any CSV file into Project Space.

@inputs.series.generic("preds")

@inputs.atomic.string("path")

class SavePredsV2(Block):

    __publish__ = True

    __label__ = "SavePredsV2"

    __description__ = ""

    __tags__ = []

    __category__ = "Image Classification"

    def run(self, preds, path):

        import os

        import numpy as np

        import pandas as pd

        from razor.platform import project_space_path 

        

        y_true = []

        y_hats = []

        

        if os.path.exists(project_space_path(path)):

            os.remove(project_space_path(path))

            

        for batch in preds:

            y_true.extend(list(np.argmax(batch['labels'], axis=1)))

            y_hats.extend(list(batch['argmax']))

            df = pd.DataFrame({'y_true':y_true, 'y_hats':y_hats}).to_csv(project_space_path(path), index=False,mode='a')



 

  • Then we go inside our “__init__.py” file inside the bundle folder and import our blocks.

from .fashionmnist import LoadImagesV1, SavePredsV2


from razor.platform.setuptools import block_setup


__metadata__ = block_setup(version="0.0.1")


 

  • Then you can publish your bundle anywhere inside your project space in jupyter lab.

import razor

razor.platform.publish_project_blocks(bundle='ImageClassificationV1', overwrite=True)


 

  • Now that we have our read and write blocks published, we can create our model. Click on the “DL model designer” in the right panel, and add a model name. For example we’re naming it “new_model”.

  • Open “new_model” and start building your model with existing components like layers, metrics, optimizers et al and build your model.

  • Now that we have our model we can build our train pipeline. Click on pipelines on left side panel and create you pipeline. We’ll name it “Classification model train”. 

  • Drag out your custom block “LoadImagesV1” to load the file, then drag the block “DLTrainBlock” for training. Add the input path and other parameters add your model in DLTrainBlock. Click on play icon to start training the model after adding an engine you want to run it on.

  • Now that you made the run, you can see progress on in pipeline runs, to get more details on the run you can click on that particular run and see that.

  • Once we’re done training your model we can run inference on it. Go back to the pipelines and create a new pipeline to run inference. We’ll name it “classification prediction”.


  • Again drag out your custom block “LoadImagesV1” to load the images along with “SavePredsV2” to save the prediction, drag out “DLInferBlock” and add your trained model.
  • Similarly for this run also you can get block wise details in the pipeline runs.


Cheers! You have successfully trained a model and have a pipeline to run predictions on the trained model using RZT aiOS.