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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

京东技术解密

京东技术解密

京东研发体系 / 电子工业出版社 / 2014-11-18 / 65

京东高速的增长、闪电响应的供应链、庞大的团队规模等背后内幕,对于业界一直像谜一样神秘。随着成为中国B2C领导厂商以及在纳斯达克上市,京东越来越需要开放自己,与业界形成更好的交流与融合。《京东技术解密》的面世,就是京东技术团队首次向业界集体亮相。本书用翔实的内容为读者逐一解答——如何用技术支撑网站的综合竞争实力,如何把握技术革新的时间点,如何应对各种棘手问题及压力,如何在网站高速运转的情况下进行系统......一起来看看 《京东技术解密》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具