Simple GUI for Model Inferences

Running inferences by loading models via scripts is cool but the end user may not be familiar with these methods of obtaining predictions. If deep learning is to be useful as a tool to the user, then it should be available in the form of a GUI, either web based or desktop based. 

Keeping that in mind I decided to see what tools are available for me to create a GUI on the desktop that will allow me to:
1) Load a model
2) Feed that model inputs via a file explorer
3) Obtain inference results and display it via labels. Display any additional data as required.

Since I was programming on the Windows Desktop, I decided to check out the machine learning offerings available via the Visual C# IDE. I found out that Microsoft's CNTK was available on C# so I decided to give that a go.

I already had a pretrained model that I'd used for pneumonia detection (I trained and tested it in my previous post). I now needed a way to use that model in C# via CNTK. Luckily, there was ONNX, which allows inter-operability of ML models between different libraries like Tensorflow, Caffe2, CNTK etc. I downloaded the onnxmltools via pip and used it to convert my existing Keras model to an onnx format.

To start coding in Visual C#, I used visual studio installer to install the necessary components. I already had the visual studio community edition so the IDE was all set. I obtained the CNTK library via the nifty NuGet package manager available in VS. I also needed a way to load image data into my program so I got Magick.net as well.

Once all this was set, I started reading up about the API and such to set up my program. With the info that I obtained via scouring the official MS CNTK C# site and other  sites, I managed to get a program running into which I could load a model, drag and drop images and obtain predictions.

The only problem was that I couldn't find any additional useful information or examples that would allow me to create class activation maps (CAM) within this framework. I also think for C#, MS is working on ML.NET so I don't think C# CNTK is being supported all that much. The python version of CNTK is still quite popular.

I didn't want to continue working with a set of tools that might not be supported in the future. So I decided to take a look at the GUI tools that Python had to offer. Turns out that Python comes its own set of GUI tools called Tkinter. I decided to take a stab at it. The biggest advantage is obviously that almost all my machine learning code is in Python with in frameworks like Keras and Tensorflow that will be supported in the future.

Porting the code from my previous inference work in Python was easy and I just needed to figure out my way around the GUI framework which also wasn't too hard either. And since I'd already worked on the CAM code, I simply copied it over.

To convert the python code into a package that could be used on any Windows machine, I installed pyinstaller via pip. It makes an executable that runs your python code and supplies all dependencies along with it. This video helped with fixing some issues that I was encountering while working with pyinstaller.

In the end, everything came together nicely and I had a GUI program that was in line with my requirements. The first image below is from my Python program and the second one is from visual C#.





As you can see in the first image, I'm able to make predictions and get the class activation map for the given input.



Comments

Popular posts from this blog

First ever Kaggle competition - Pneumonia Detection

Densenet, Pneumonia Detection, Activation Maps and AUROC