Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

Project- How to build low-latency deep-learning-based flask app

2 / 17

Checking the Runtime

Note:

  • Upon running the command time python filename.py in the console, we could see the time of execution for the file filename.py.
INSTRUCTIONS
  • Switch to the Image-Classification-App folder using

    cd ~/Image-Classification-App/
    
  • Activate the virtual environment using

    source Img-Class-Env/bin/activate
    
  • Create a file named test_client_without_zmq.py. If you have not deleted the environment from How to Deploy an Image Classification Model using Flask project, this file will already be there. You can delete it first using rm test_client_without_zmq.py

    vi test_client_without_zmq.py
    

And change the vi to insert mode by pressing 'a' or 'i'

  • Copy-paste the following code in test_client_without_zmq.py and save it using ESC followed by :wq.

    from tensorflow.keras.applications.resnet50 import ResNet50 as myModel
    from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
    
    from tensorflow.keras.preprocessing import image
    import numpy as np
    
    model = myModel(weights="imagenet")
    
    def get_classes(file_path):
    
    
        img = image.load_img(file_path, target_size=(224, 224))
        x = image.img_to_array(img)
        x= np.array([x])
        x = preprocess_input(x)
    
        preds = model.predict(x)
        predictions = decode_predictions(preds, top=3)[0]
        print(predictions)
        return predictions
    
    
    if __name__ == "__main__":
        name = '/cxldata/projects/image-class/dog.png'
        get_classes(name)
    

    We are doing this to understand how much execution time it is taking for the model to load and predict the classes. In the above code, we are importing the model and feeding an image as input to the model to get its predictions.

  • Run the test_client_without_zmq.py file with the time command as follows:

    time python test_client_without_zmq.py
    

You can run any program with time to measure how much time the command is taking. Here we are running "python test_client_without_zmq.py" with "time". It displays something like this:

real    0m9.606s
user    0m10.621s
sys     0m2.090s

Observe the time displayed against "real" that is the time we are going focus on. In our case, the time is 9.606 seconds.

Run the same file for several times with different images. Some images are already in the image-class folder. You may view the file names using ls /cxldata/projects/image-class command. Observe the amount of time taken to execute the program for different images.

  • Also run the app.py :

    python app.py
    

    Now go to your favorite browser (preferably Google Chrome), and go to http://f.cloudxlab.com:4100/.

    We could observe that, for classification, the amount of time taken is at least around 10-21 seconds.

Note- If you face Unable to open file error while loading the model, refer to Input/Output Error(Error no. 5).


No hints are availble for this assesment

Answer is not availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...