Project - How to Deploy an Image Classification Model using Flask

6 / 15

Creating the Helper Function File

  • We shall now create a file named app_helper.py.

  • In this file, we shall create a function named get_classes inside which we:

  1. import the pre-trained model

  2. preprocess the input image as per requirements of the pre-trained model

  3. feed this preprocessed image as the input of the model and get the top 3 predictions as classified by the model.

For a detailed explanation of the code, it is highly recommended to complete working on the Image Classification with Pre-trained Keras models project as cautioned earlier.

Note:

  • vi is used to:

    • create and edit the file, if the file doesn't exist.
    • edit the file, if the file already exists.

    If we want to create and/or edit the file named myFile, we use vi myFile.

  • vi has multiple modes: command mode, insert, append mode. In the begining, it is in command mode. you can type 'a' to change the mode to append mode. Once in append mode, you can type text content.

  • We pass the commands such as quit file, delete line, search etc in command mode. If you want to change the mode to command mode, press 'ESC' key
  • To exit from the file, we press ESC key to go to command mode and type :wq and hit on enter key. In :wq, w means save, and q means quit.

If you are not comfortable with vi, you could nano command line text editor or you could also use Jupyter text editor in CloudxLab.

INSTRUCTIONS
  • This app_helper.py file should be created inside the Image-Classification-App directory. So make sure you are in the directory Image-Classification-App. You could check your present working directory using the command:

    pwd
    

    This command should output the path:

    /home/$USER/Image-Classification-App
    

    If the path displayed is not the same as the above, switch to the Image-Classification-App using

    cd ~
    cd Image-Classification-App
    
  • Now, create a file named app_helper.py using the vi command.

  • Press i key on your keyboard, to switch to insert mode in the file.

  • Copy-paste the below code in the file. Please take a moment to read the code and comments.

    # Import the model and other libraries
    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
    
    def get_classes(file_path):
        # Create an instance of 'myModel' imported above
        model = myModel(weights="imagenet")
    
        # Load image and preprocess it
        img = image.load_img(file_path, target_size=(224, 224))
        x = image.img_to_array(img)
        x= np.array([x])
        x = preprocess_input(x)
    
        # This is the inference time. Given an instance, it produces the predictions.
        preds = model.predict(x)
        predictions = decode_predictions(preds, top=3)[0]
        return predictions
    
  • Now, click on esc button on your keyboard.

  • Then, type :wq! and then hit on enter key on your keyboard. This returns you to your console.

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

Get Hint See Answer


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

Loading comments...