#NoPayJan Offer - Access all CloudxLab Courses for free between 1st to 31st JanEnroll Now >>
We shall now create a file named
In this file, we shall create a function named
get_classes inside which we:
import the pre-trained model
preprocess the input image as per requirements of the pre-trained model
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.
vi is used to:
If we want to create and/or edit the file named
myFile, we use
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.
ESCkey to go to command mode and type
:wqand hit on
wmeans save, and
If you are not comfortable with vi, you could
nano command line text editor or you could also use Jupyter text editor in CloudxLab.
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:
This command should output the path:
If the path displayed is not the same as the above, switch to the
cd ~ cd Image-Classification-App
Now, create a file named
app_helper.py using the
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) return predictions
Now, click on
esc button on your keyboard.
:wq! and then hit on
enter key on your keyboard. This returns you to your console.
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