Deploying Machine Learning projects using Tkinter

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

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-9 / 59.80

“一本非常鼓舞人心的书!”——托马斯•L•弗里德曼 《世界是平的》作者 一场革命开始了! 在《第二次机器革命》这本书中,埃里克•布莱恩约弗森和安德鲁•麦卡菲——这两位处于数字技术时代最前沿的思想家,向我们阐述了驱动我们经济和生活的发生变革的力量。他们认为,数字技术将会给我们带来难以想象的巨大变革:想象一下令人眩目的个人数字技术产品、一流的基础设施,都将会给我们带来极大的便利。数字技术(......一起来看看 《第二次机器革命》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具

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

RGB CMYK 互转工具