Streamlit —使用数据应用程序更好地测试模型

介绍 (Introduction)

We use all kinds of techniques from creating a very reliable validation set to using k-fold cross-validation or coming up with all sorts of fancy metrics to determine how good our model performs. However, nothing beats looking at the raw output. When you look at sample output, you figure out things that none of the other methods can tell you.

我们使用各种技术,从创建非常可靠的验证集到使用k倍交叉验证或提出各种幻想指标来确定模型的性能。 但是,看原始输出无所不能。 查看示例输出时,您会发现其他方法无法告诉您的内容。

For example, if you are performing object detection on crops, you might see that when the crop is aligned in a certain way because of the wind, our bounding boxes don’t encapsulate the crop properly.

例如,如果要对农作物执行对象检测,则可能会看到,由于风将农作物以某种方式对齐时,我们的边界框无法正确封装农作物。

However, looking at sample outputs is a tedious task. Say we want to test various NMS values, and we also want to test it on a bunch of images. We can always write a function for this, but running it, again and again, is boring.

但是,查看样本输出是一项繁琐的任务。 假设我们要测试各种NMS值,并且还要对一堆图像进行测试。 我们总是可以为此编写一个函数,但是一次又一次地运行它很无聊。

Wouldn’t it be nice to have an app where we can upload an image and use a slider to adjust the NMS value? Welcome, Streamlit.

拥有一个可以上传图像并使用滑块调整NMS值的应用程序不是很好吗? 欢迎,Streamlit。

Image for post

什么是Streamlit? (What is Streamlit?)

According to their website, Streamlit is an open-source web framework for data scientists and machine learning engineers to create beautiful, performant apps in just a few hours, all in pure Python.

根据他们的网站,Streamlit是一个开放源代码Web框架,供数据科学家和机器学习工程师在短短几个小时内使用纯Python编写漂亮,高性能的应用程序。

安装 (Installation)

pip install streamlit

入门 (Getting started)

For this demo, I will be using the model I trained for wheat object detection using a FasterRCNN. You can go back to that article, train or, download my model and then follow along.

对于此演示,我将使用经过FasterRCNN训练用于小麦对象检测的模型 。 您可以返回该文章,进行培训或下载我的模型,然后继续学习。

Streamlit is just a standard python file run from top to bottom. We will start by creating a file called app.py, and writing the following code in it.

Streamlit只是从上到下运行的标准python文件。 我们将从创建一个名为app.py的文件开始,并在其中写入以下代码。

Image for post

We will run this as follows:

我们将如下运行:

streamlit run app.py

It will open up our browser with the following output.

它将打开我们的浏览器,并显示以下输出。

Image for post

Notice that we used a # inside st.write(). This indicates that we are writing markdown inside it. The first thing we need is a file uploader to upload images to Streamlit. Again, this is as simple as doing:

注意,我们在st.write()中使用了#。 这表明我们正在其中编写markdown。 我们需要的第一件事是文件上传器,将图像上传到Streamlit。 同样,这很简单:

Image for post

We pass the type of files allowed (jpg and png in this case), and when we do attach one, we read it using PIL. When we save app.py now:

我们传递允许的文件类型(在这种情况下为jpg和png),当我们附加一个文件时,我们使用PIL进行读取。 当我们现在保存app.py时:

Image for post

we see that Streamlit identifies that the source file has changed and asks if we want to rerun. And we do. Hence, we select “always rerun” for change to reflect automatically. Now our browser looks like this:

我们看到Streamlit确定源文件已更改,并询问是否要重新运行。 我们做。 因此,我们选择“始终重新运行”进行更改以自动反映。 现在,我们的浏览器如下所示:

Image for post

To display the image, we can use st.image(). However, after generating predictions, we want to replace the input image with our predicted image. To do so, we create an empty container and display everything in it.

要显示图像,我们可以使用st.image()。 但是,在生成预测后,我们希望将输入图像替换为预测图像。 为此,我们创建一个空容器并显示其中的所有内容。

Image for post

Another thing I like about using a container is that you can set use_column_width = True, and you won’t have to worry about image resizing. Now we can drag and drop images into our app.

我喜欢使用容器的另一件事是,您可以设置use_column_width = True,而不必担心图像调整大小。 现在我们可以将图像拖放到我们的应用程序中。

Image for post

Finally, we can convert our image to a tensor, load our model, generate outputs, and write it to our container.

最后,我们可以将图像转换为张量,加载模型,生成输出,并将其写入容器。

Image for post

To vary NMS values, we can either use a slider or an input box to type the value or use the ‘+’ and ‘-’ buttons to increase or decrease the value. I’m going to go with the slider. Our code changes to.

要更改NMS值,我们可以使用滑块或输入框键入值,也可以使用“ +”和“-”按钮增大或减小值。 我将使用滑块。 我们的代码更改为。

Image for post

And that’s it. In about just 50 lines of code, we can create this super useful app to test our model. You can also create templates for various problems like image classification, segmentation, object detection, and so on and use them every time you train a new model.

就是这样。 在大约50行代码中,我们可以创建这个超级有用的应用程序来测试我们的模型。 您还可以针对各种问题(例如图像分类,分割,对象检测等)创建模板,并在每次训练新模型时使用它们。

Finally, when we change the NMS values using the slider, if we’ve already generated predictions for a particular value, we don’t want to generate them again. Hence you can stick a simple decorator on top of the function:

最后,当我们使用滑块更改NMS值时,如果我们已经生成了特定值的预测,则我们不想再次生成它们。 因此,您可以在该函数之上粘贴一个简单的装饰器:

Image for post

and it will cache the results for you. This way it won’t rerun the same threshold every time but just used the cached result. Cool, isn’t it?

它将为您缓存结果。 这样,它不会每次都重新运行相同的阈值,而只是使用了缓存的结果。 不错,不是吗?

结论: (Conclusion:)

That will be it for this article. I would really recommend everyone to use Streamlit and create data apps. It works well for all kinds of data and caching helps with expensive operations like working with large data frames. Give it a try.

本文就是这样。 我真的建议大家使用Streamlit并创建数据应用程序。 它适用于所有类型的数据,并且缓存有助于处理昂贵的操作,例如处理大型数据帧。 试试看。

If you want to learn more about deep learning check out my deep learning series.

如果您想了解有关深度学习的更多信息,请查看我的深度学习系列。

~happy learning

〜快乐学习

翻译自: https://towardsdatascience.com/streamlit-use-data-apps-to-better-test-your-model-4a14dad235f5

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390719.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring MVC Boot Cloud 技术教程汇总(长期更新)

昨天我们发布了Java成神之路上的知识汇总,今天继续。 Java成神之路技术整理(长期更新) 以下是Java技术栈微信公众号发布的关于 Spring/ Spring MVC/ Spring Boot/ Spring Cloud 的技术干货,本文长期更新。 Spring 系列 Java 必看的…

X Window系统

X Window系统 一种以位图方式显示的软件窗口系统。诞生于1984,比Microsoft Windows要早。是一套独立于内核的软件 Linux上的X Window系统 X Window系统由三个基本元素组成:X Server、X Client和二者通信的通道。 X Server:是控制输出及输入…

冒名顶替上大学罗彩霞_什么是冒名顶替综合症,您如何克服?

冒名顶替上大学罗彩霞冒名顶替综合症 (Imposter Syndrome) Imposter Syndrome is a feeling of being a fraud or not being good enough to get the job done. Its common among software engineers, developers and designers working in tech companies, especially those n…

Linux命令----用户管理

修改用户密码: sudo passwd (当前)用户名  【sudo是super user do的简写,passwd是password的简写】 显示当前正在操作系统的用户:whoami   显示当前登录系统的用户信息:who am i 注意: 普通…

lasso回归和岭回归_如何计划新产品和服务机会的回归

lasso回归和岭回归Marketers sometimes have to be creative to offer customers something new without the luxury of that new item being a brand-new product or built-from-scratch service. In fact, incrementally introducing features is familiar to marketers of c…

python代码

原始字符串,不做任何特殊的处理 print("Newlines are indicated by \n")#Newlines are indicated by print(r"Newlines are indicated by \n")#Newlines are indicated by \n 格式输出,转化为字符串由format自动完成 ag…

Linux 设备管理和进程管理

设备管理 Linux系统中设备是用文件来表示的,每种设备都被抽象为设备文件的形式,这样,就给应用程序一个一致的文件界面,方便应用程序和操作系统之间的通信。 设备文件集中放置在/dev目录下,一般有几千个,不…

乐高ev3涉及到的一些赛事_使您成为英雄的前五名开发者技能(提示:涉及LEGO)

乐高ev3涉及到的一些赛事Programming is like building something with LEGOs. Any developer can pick up a brand new LEGO set and build it following the instructions. This is very easy. Think of it as coding school assignments or entry level tutorials.编程就像用…

贝叶斯 定理_贝叶斯定理实际上是一个直观的分数

贝叶斯 定理Bayes’ Theorem is one of the most known to the field of probability, and it is used often as a baseline model in machine learning. It is, however, too often memorized and chanted by people who don’t really know what P(B|E) P(E|B) * P(B) / P(E…

winfrom 点击按钮button弹框显示颜色集

1.窗体托一个按钮button; 2.单击事件: 1 private void btnForeColor_Click(object sender, EventArgs e)2 {3 using (ColorDialog cdialog new ColorDialog())4 {5 cdialog.AnyColor true;6 …

JavaScript时间事件:setTimeout和setInterval

Programmers use timing events to delay the execution of certain code, or to repeat code at a specific interval.程序员使用时序事件来延迟某些代码的执行,或以特定的时间间隔重复代码。 There are two native functions in the JavaScript library used to …

webservice 基本要点

webservice的特点 webservices是自我包含的 webservices是自我描述的 webservices是跨平台和语言的 webservices是基于开放和标准的 webservices是可以组合的 webservices是松散耦合的 webservices提供编程访问的能力 webservices通过网络进行发布,查找和使用 发布w…

文本数据可视化_如何使用TextHero快速预处理和可视化文本数据

文本数据可视化自然语言处理 (Natural Language Processing) When we are working on any NLP project or competition, we spend most of our time on preprocessing the text such as removing digits, punctuations, stopwords, whitespaces, etc and sometimes visualizati…

Less变量

Less变量 定义变量 Less 中的变量和其他编程语言一样,可以实现值的复用,同样它也有作用域(scope)。简单的讲,变量作用域就是局部变量和全局变量的概念。 Less 中,变量作用域采用的是就近原则,换…

渐进式web应用程序_如何在渐进式Web应用程序中添加到主屏幕

渐进式web应用程序添加到主屏幕 (Add To Homescreen) Here the web app install banner is focused on web app, with the feature of add to homescreen.在此,Web应用程序安装标语专注于Web应用程序,具有添加到主屏幕的功能。 浏览器对“添加到主屏幕”…

linux shell 编程

shell的作用 shell是用户和系统内核之间的接口程序shell是命令解释器 shell程序 Shell程序的特点及用途: shell程序可以认为是将shell命令按照控制结构组织到一个文本文件中,批量的交给shell去执行 不同的shell解释器使用不同的shell命令语法 shell…

Leetcode之javascript解题(No33-34)

附上我的github仓库,会不断更新leetcode解题答案,提供一个思路,大家共勉 在我的主页和github上可以看到更多的关于leetcode的解题报告!(因为不知道为什么掘金没有将其发布出来,目前已经联系掘金客服&#x…

真实感人故事_您的数据可以告诉您真实故事吗?

真实感人故事Many are passionate about Data Analytics. Many love matplotlib and Seaborn. Many enjoy designing and working on Classifiers. We are quick to grab a data set and launch Jupyter Notebook, import pandas and NumPy and get to work. But wait a minute…

转:防止跨站攻击,安全过滤

转:http://blog.csdn.net/zpf0918/article/details/43952511 Spring MVC防御CSRF、XSS和SQL注入攻击 本文说一下SpringMVC如何防御CSRF(Cross-site request forgery跨站请求伪造)和XSS(Cross site script跨站脚本攻击)。 说说CSRF 对CSRF来说,其实Spring…

Linux c编程

c语言标准 ANSI CPOSIX(提高UNIX程序可移植性)SVID(POSIX的扩展超集)XPG(X/Open可移植性指南)GNU C(唯一能编译Linux内核的编译器) gcc 简介 名称: GNU project C an…