Showing posts with label custom-processor. Show all posts
Showing posts with label custom-processor. Show all posts

Easy Deep Learning in Apache NiFi with DJL


Custom Processor for Deep Learning



 Happy Mm.. FLaNK Day!


I have been experimenting with the awesome new Apache 2.0 licensed Java Deep Learning Library, DJL.   In NiFi I was trying to figure out a quick use case and demo.   So I use my Web Camera processor to grab a still shot from my Powerbook webcam and send it to the processor.   The results are sent to slack.

Since it's the holidays I think of my favorite holiday movies:   The Matrix and Blade Runner.   So I thought a Voight-Kampf test would be fun.   Since I don't have a Deep Learning QA piece built yet, let's start by seeing if you look human.  We'll call them 'person'.   I am testing to see if I am a replicant.  Sometimes hard to tell.   Let's see if DJL thinks I am human.

See:   http://nautil.us/blog/-the-science-behind-blade-runners-voight_kampff-test



Okay, so it least it thinks I am a person.   The classification of a Christmas tree is vaguely accurate.






It did not identify my giant french bread.

Building A New Custom Processor for Deep Learning




The hardest part of was a good NiFi Integration test.   The DJL team provide some great examples and it's really easy to plug into their models.

ZooModel<BufferedImage, DetectedObjects> model =
                     MxModelZoo.SSD.loadModel(criteria, new ProgressBar())
Predictor<BufferedImage, DetectedObjects> predictor = model.newPredictor()
DetectedObjects detection = predictor.predict(img);

All the source is in github and references the below DJL sites and repos.

Using a New Custom Processor as part of a Real-time Holiday Flow

We first add the DeepLearningProcessor to our canvas.



An example flow:
  • GetWebCameraProcessor:  grab an image from an attached webcamera
  • UpdateAttribute:  Add media type for image
  • DeepLearningProcessor:   Run our DJL deep learning model from a zoo
  • PutSlack:   Put DJL results in a text window in slack
  • PostSlack:  Send our DJL altered image to slack
  • Funnel:   Send all failures to Valhalla



If we example the provenance we can see how long it took to run and some other interesting attributes.


We place the results of our image analysis in attributes while we return a new image that has a bounding box on the found object(s).




 We now a full classification workflow for real-time deep learning analysis on images, could be used for Santa watching, Security, Memes and other important business purposes.


The initial release is available here:   https://github.com/tspannhw/nifi-djl-processor/releases/tag/v1.0
Using library and example code from the Deep Java Library (https://djl.ai/).



Source Code:   https://github.com/tspannhw/nifi-djl-processor/

And now for something completely different, Christmas Cats:












Using Raspberry Pi 3B+ with Apache NiFi MiNiFi and Google Coral Accelerator and Pimoroni Inky Phat


Using Raspberry Pi 3B+ with Apache NiFi MiNiFi and Google Coral Accelerator and Pimoroni Inky Phat

Architecture



Introduction

First we need to unbox our new goodies.   The Inky Phat is an awesome E-Ink display with low power usage that stays displayed after shutdown! 

Next I added a new Google Coral Edge TPU ML Accelerator USB Coprocessor to a new Raspberry Pi 3B+.    This was so easy to integrate and get up and running.

Let's unbox this beautiful device (but be careful when it runs it can get really hot and there is a warning in the instructions).   So I run this on top of an aluminum case and with a big fan on it.







Pimoroni Inky Phat

It is pretty easy to set this up and it provides a robust Python library to write to our E-Ink display.   You can see an example screen here.

https://github.com/pimoroni/inky
Pimoroni Inky pHAT ePaper eInk Display in Red


Pimoroni Inky Phat (Red)


https://shop.pimoroni.com/products/inky-phat
https://github.com/pimoroni/inky
https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html
https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-inky-phat


Install Some Python Libraries and Debian Install for Inky PHAT and Coral

pip3 install font_fredoka_one
pip3 install geocoder
pip3 install fswebcam
sudo apt-get install fe
pip3 install psutil
pip3 install font_hanken_grotesk
pip3 install font_intuitive
wget http://storage.googleapis.com/cloud-iot-edge-pretrained-models/edgetpu_api.tar.gz
These libraries are for the Inky, it needs fonts to write.   The last TAR is for the Edge device and is a fast install documented well by Google.

Download Apache NiFi - MiNiFi Java Agent

https://nifi.apache.org/minifi/download.html

Next up, the most important piece.  You will need to have JDK 8 installed on your device if you are using the Java agent.   You can also use the MiniFi C++ Agent but that may require building it for your OS/Platform.   That has some interesting Python running abilities.


Google Coral Documentation - Google Edge TPU
  • Google Edge TPU ML accelerator coprocessor
  • USB 3.0 Type-C socket
  • Supports Debian Linux on host CPU
  • ASIC designed by Google that provides high performance ML inferencing for TensorFlow Lite models


Using Pretrained Tensorflow Lite Model:

Inception V4 (ImageNet)
Recognizes 1,000 types of objects
Dataset: ImageNet
Input size: 299x299

Let's run a flow!

I can run this Python3 script every 10 seconds without issues that includes capturing the picture, running it through classification with the model, forming JSON data, grabbing network and device stats, forming a JSON file and completing in under 5 seconds.   Our MiNiFi agent is scheduled to call the script every 10 seconds and grab images after 60 seconds. 


MiNiFi Flow



Flow Overview



Apache NiFi Flow





Results (Once an hour we update our E-Ink Display with Date, IP, Run Time, Label 1)





Example JSON Data

{"endtime": "1552164369.27", "memory": "19.1", "cputemp": "32", "ipaddress": "192.168.1.183", "diskusage": "50336.5", "score_2": "0.14", "score_1": "0.68", "runtime": "4.74", "host": "mv2", "starttime": "03/09/2019 15:46:04", "label_1": "hard disc, hard disk, fixed disk", "uuid": "20190309204609_05c9a240-d801-4bac-b029-e5bf38c02d40", "label_2": "buckle", "systemtime": "03/09/2019 15:46:09"}

Example Slack Alert


PS3 Eye USB Camera Capturing an Image


Image It Captured




Source Code

https://github.com/tspannhw/nifi-minifi-coral

Convert Your Flow To Config.YML For MiniFi (Look for a major innovation here soon).

 ./config.sh transform Coral_MiniFi_Agent_Flow.xml config.yml
config.sh: JAVA_HOME not set; results may vary

Java home: 
MiNiFi Toolkit home: /Volumes/TSPANN/2019/apps/minifi-toolkit-0.5.0



No validation errors found in converted configuration.


Example Call From MiNiFi 0.5.0 Java Agent to Apache NiFi 1.9.0 Server


2019-03-09 16:21:01,877 INFO [Timer-Driven Process Thread-10] o.a.nifi.remote.StandardRemoteGroupPort RemoteGroupPort[name=Coral Input,targets=http://hw13125.local:8080/nifi] Successfully sent [StandardFlowFileRecord[uuid=eab17784-2e76-4438-a60a-fd67df37a102,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1552166446123-3, container=default, section=3], offset=362347, length=685083],offset=0,name=d74bc911bfd167fe79d5a3aa780004fd66fa6d,size=685083], StandardFlowFileRecord[uuid=eb979d09-a936-4b2d-82ff-d204f9d768eb,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1552166446123-3, container=default, section=3], offset=1047430, length=361022],offset=0,name=2019-03-09_1541.jpg,size=361022], StandardFlowFileRecord[uuid=343a4c91-b863-440e-ac81-1f68d6210792,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1552166446123-3, container=default, section=3], offset=1408452, length=668],offset=0,name=3026822c780724b39e826230bdef43f8ed9786,size=668], StandardFlowFileRecord[uuid=97df9d3a-dc3c-4d03-b533-7b75c3180032,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1552166446123-3, container=default, section=3], offset=1409120, length=2133417],offset=0,name=abb6feaac5bda3c6d3660e7593cc4ef2e1cfce,size=2133417]] (3.03 MB) to http://hw13125.local:8080/nifi-api in 1416 milliseconds at a rate of 2.14 MB/sec


References