From eca332cbde12106451b76a74364a27e0079613a9 Mon Sep 17 00:00:00 2001 From: Pedro Larroy Date: Tue, 15 Jan 2019 16:26:57 +0100 Subject: [PATCH] Fixes for wine detection tutorial --- docs/tutorials/embedded/wine_detector.md | 53 +++++++++++++++--------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/docs/tutorials/embedded/wine_detector.md b/docs/tutorials/embedded/wine_detector.md index 65e6fbaa4d91..e31e9d882ca9 100644 --- a/docs/tutorials/embedded/wine_detector.md +++ b/docs/tutorials/embedded/wine_detector.md @@ -1,4 +1,4 @@ -# Real-time Object Detection with MXNet On The Raspberry Pi +# Real-time Object Detection with MXNet On The Raspberry Pi This tutorial shows developers who work with the Raspberry Pi or similar embedded ARM-based devices how to compile MXNet for those devices and run a pretrained deep network model. It also shows how to use AWS IoT to manage and monitor MXNet models running on your devices. @@ -74,6 +74,7 @@ The next step is to create a python script to load the model, and run inference import mxnet as mx import numpy as np +import time import cv2, os, urllib from collections import namedtuple Batch = namedtuple('Batch', ['data']) @@ -83,13 +84,14 @@ with open('synset.txt', 'r') as f: synsets = [l.rstrip() for l in f] # Load the network parameters -sym, arg_params, aux_params = mx.model.load_checkpoint('Inception_BN', 0) +sym, arg_params, aux_params = mx.model.load_checkpoint('Inception-BN', 126) + # Load the network into an MXNet module and bind the corresponding parameters mod = mx.mod.Module(symbol=sym, context=mx.cpu()) mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))]) mod.set_params(arg_params, aux_params) - + ''' Function to predict objects by giving the model a pointer to an image file and running a forward pass through the model. @@ -112,14 +114,14 @@ def predict(filename, mod, synsets, N=5): img = np.swapaxes(img, 1, 2) img = img[np.newaxis, :] print "pre-processed image in "+str(time.time()-tic) - + toc = time.time() mod.forward(Batch([mx.nd.array(img)])) prob = mod.get_outputs()[0].asnumpy() prob = np.squeeze(prob) print "forward pass in "+str(time.time()-toc) - - + + topN = [] a = np.argsort(prob)[::-1] for i in a[0:N]: @@ -139,7 +141,7 @@ def predict_from_url(url, N=5): return predict(filename, mod, synsets, N) # Code to predict on a local file -def predict_from_local_file(filename, N=5): +def predict_from_local_file(filename, N=5): return predict(filename, mod, synsets, N) ``` @@ -147,11 +149,24 @@ Now that we have defined inception_predict.py we can test that the model is runn ```bash python ->>> import inception_predict ->>> predict_from_url("http://imgur.com/HzafyBA") +>>> from inception_predict import * +>>> predict_from_url("https://i.imgur.com/HzafyBA.jpg") ``` -This should give a reasonable prediction for the fluffy cow in this [image](http://imgur.com/HzafyBA). +This should give a reasonable prediction for the fluffy cow in this [image](http://imgur.com/HzafyBA). + +``` +pre-processed image in 0.20366191864 +forward pass in 63.2164611816 +probability=0.718524, class=n02403003 ox +probability=0.176381, class=n02389026 sorrel +probability=0.095558, class=n03868242 oxcart +probability=0.002765, class=n02408429 water buffalo, water ox, Asiatic buffalo, Bubalus bubalis +probability=0.001262, class=n03935335 piggy bank, penny bank +[(0.71852392, 'n02403003 ox'), (0.17638102, 'n02389026 sorrel'), (0.09555836, 'n03868242 oxcart'), +(0.0027645244, 'n02408429 water buffalo, water ox, Asiatic buffalo, Bubalus bubalis'), +(0.0012616422, 'n03935335 piggy bank, penny bank')] +``` ## Running an Inception on Real-Time Video From PiCamera @@ -177,11 +192,11 @@ while True: camera.start_preview() camera.capture(filename) camera.stop_preview() - + # Run inception prediction on image print "Predicting" topn = inception_predict.predict_from_local_file(filename, N=5) - + # Print the top N most likely objects in image (default set to 5, change this in the function call above) print topn ``` @@ -192,7 +207,7 @@ You can then run this file by entering the following command: python camera_test.py ``` -If camera_test.py is working you should see a preview every few seconds of the image that is being captured and fed to the model, as well as predicted classes for objects in the image being written to the terminal. +If camera_test.py is working you should see a preview every few seconds of the image that is being captured and fed to the model, as well as predicted classes for objects in the image being written to the terminal. Try pointing the PiCamera at a few different objects and see what predictions the network comes out with. @@ -231,13 +246,13 @@ def customCallback(client, userdata, message): # Usage usageInfo = """Usage: - + Use certificate based mutual authentication: python wine_alerter.py -e -r -c -k - + Use MQTT over WebSocket: python wine_alerter.py -e -r -w - + Type "python wine_alerter.py -h" for available options. """ @@ -255,7 +270,7 @@ helpInfo = """-e, --endpoint -h, --help Help information """ - + # Read in command-line parameters useWebsocket = False host = "" @@ -350,10 +365,10 @@ while True: camera.capture(filename) camera.stop_preview() topn = inception_predict.predict_from_local_file(filename, N=5) - + # Check if either of the top two predictions are wine related and publish a message if it is # you can change 'wine' here to anything you want to alert the server about detecting - if 'wine' in topn[0][1] or 'wine' in topn[1][1]: + if 'wine' in topn[0][1] or 'wine' in topn[1][1]: myAWSIoTMQTTClient.publish("sdk/test/Python", "New Message: WINE DETECTED!", 0) ```