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

查看所有标签

猜你喜欢:

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

高可用MySQL

高可用MySQL

)Charles Bell Mats Kindahl Lars Thalmann / 宁青、唐李洋 诸云萍 / 电子工业出版社 / 2011-10 / 98.00元

《高可用mysql:构建健壮的数据中心》是“mysql high availability”的中文翻译版,主要讲解真实环境下如何使用mysql 的复制、集群和监控特性,揭示mysql 可靠性和高可用性的方方面面。本书由mysql 开发团队亲自执笔,定位于解决mysql 数据库的常见应用瓶颈,在保持mysql 的持续可用性的前提下,挖潜各种提高性能的解决方案。本书分为三个部分。第一部分讲述mysql......一起来看看 《高可用MySQL》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具