Computer Vision News - May 2019
24 Computer Vision News Focus on The variable resulting img on the second line is a Keras image object of size (224,224). Next, we would like to change the dimension of the image. To this end, we will add one channel to gray images and three channels to RGB images. Since the network gets as input batches, we will also create a single image batch from our input image. These two operations can be done efficiently by using these two lines (respectively): Finally, we will normalize our input. As we all know, input normalization is essential to training a deep network. Normalization can take different forms: some to a range of 0 to 1, some to a range of -1 to 1, some subtract the mean and divide by the variance and some are just centering by subtracting the mean. Here, ResNet50 requires that we will subtract the mean of ImageNet, so we use the following utility function of the form: And now we are ready to start using the model. We will use the following command to import the ResNet50 model and to define our ResNet50 model object : In our case we chose the pre-trained weights trained on ImageNet; however, users who want to use the architecture and train the model themselves can set the 'weights' argument to None. This, will provide a random initialization to the model. For example, we would use this option if we wanted to train a model on different data set. Note that it is also possible to initialize the weights using the ImageNet pre-trained weights, and using this initialization to further optimize the weights for a different task. Finally, we produce a prediction from our model by simply writing: We Tried for You: x = image.img_to_array(img) x = np.expand_dims(x, axis= 0 ) x = preprocess_input(x) from keras.applications.resnet50 import ResNet50 model = ResNet50(weights= 'imagenet' ) preds = model.predict(x) finalPrediction = decode_predictions(preds, top= 3 )[ 0 ]
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=