Deploying Machine Learning projects using Tkinter

栏目: IT技术 · 发布时间: 4年前

Deploying Machine Learning projects using Tkinter

A complete guide towards deploying for free Machine Learning projects as applications in executable file format.

Deploying Machine Learning projects using Tkinter

Photo by R Mo on Unsplash

Introduction

Thanks to a wide variety of open-source libraries, it is relatively easy nowadays to start exploring datasets and making some first predictions using simple Machine Learning (ML) algorithms in Python. Although, to make these trained models useful in the real world, it is necessary to share them and make them easily accessible on other users machines to make predictions. Only in this way Machine Learning can be used to provide benefit to society.

In this article, I will walk you through how to easily create a Graphical User Interface (GUI) for your Machine Learning project and then share your application as an executable file which can be run on other machines (whiteout needing the end-user to have Python or any library installed!). In case you are interested in finding out more, all the code used for this article (and more!) is available on my GitHub profile .

Transfer Learning Image Classification

In order to shift our focus on the Graphical Interface development and deployment, in this article, we are going to use the VGG16 pre-trained model available on Tensorflow to easily build a Portable Image Classifier. In Video 1, is available a quick animation demonstrating the final workflow of our application.

Deploying Machine Learning projects using Tkinter

Video 1: GUI Demonstration

Transfer Learning, is an area of research focused on transferring useful knowledge acquired by a Machine Learning model to another model which we are planning to use for a different (but still to some extent related) topic. This can be particularly useful when working with a limited amount of data (Figure 1).

Deploying Machine Learning projects using Tkinter

Figure 1: Transfer Learning Workflow (Source)

One of the most common applications of transfer learning isComputer Vision. For example, in this article, we are going to use the VGG16 model pre-trained on the ImageNet dataset in order to quickly build a robust image classifier. In fact, the ImageNet dataset comprised of a huge amount of images (14 million) and about 21 thousand classes, making it therefore quite complete for this type of task.

Researchers are currently working on applying transfer learning techniques also in other fields such as Music [1] and Text classification [2].

Graphical User Interface (GUI)

First of all, we need to import all our necessaries dependencies.

Now, we can make use of the Tkinter library in order to create our Graphical Interface (as shown in Video 1). We start first by creating the base of our window ( root ) and we then add on top of it different elements such as a program title ( tit ), a frame ( frame ) a button to load an image to display on the frame ( chose_image ) and a button to fire our image classifier ( class_image ). Finally, we instantiate our VGG16 model and launch our GUI ( using root.mainloop() ).

In order to associate actions to perform when a user press either of the two GUI buttons, we can define two functions which get activated on click ( load_img() and classify() ).

In the load_img() function, we first clear the frame in case there is any widget attached, then we let the user upload an image and finally we rescale and display the image and its filename on the window frame.

In the classify() function, we instead preprocess our image (using the Keras guidelines for this pre-trained model) and finally print on the frame the top 5 predictions of our classifier and it’s percentage confidence.

Once our program is perfectly functioning locally, we can then export it into an executable format.

Deployment

One of the easiest ways to convert a Python file into an executable (for either Windows or Linux platforms) is to use Pyinstaller (Figure 2).

Deploying Machine Learning projects using Tkinter

Figure 2: Pyinstaller [3]

In the case of a project with a single file and not too many dependencies, this can be done with just one command line statement:

pyinstaller my_script_name.py

Although, for more complicated projects might be a better idea to create a virtual environment for the project and use a Spec file in order to give clear indications to Pyinstaller about how to create the executable and what assets to include. An example of Spec file for this project is available at this link .

Finally, in case our executable might require different assets (eg. images, videos, etc…) then it could be a great idea to include everything in an installation system file using NSIS (making so easier to install the executable and all the required dependencies on another machine).

In case you can be interested in using this application yourself, a download link is available on my personal website.

Additionally, some alternative techniques which can be used in order to deploy Machine Learning systems are using:

  • Cloud Services (eg. Heroku, AWS, Google Cloud)
  • Online Dashboards (eg. Dash, R-Shiny)
  • Application Programming Interfaces (A.P.I.)

These techniques are widely used nowadays and they can make Machine Learning models easily available on the Web, although they will most likely incur in hosting charges (which are instead not needed using executables).


以上所述就是小编给大家介绍的《Deploying Machine Learning projects using Tkinter》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

A Common-Sense Guide to Data Structures and Algorithms

A Common-Sense Guide to Data Structures and Algorithms

Jay Wengrow / Pragmatic Bookshelf / 2017-8-13 / USD 45.95

If you last saw algorithms in a university course or at a job interview, you’re missing out on what they can do for your code. Learn different sorting and searching techniques, and when to use each. F......一起来看看 《A Common-Sense Guide to Data Structures and Algorithms》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具