pytorch图像分类

You just developed a cool ML model.

您刚刚开发了一个很酷的ML模型。

You are proud of it. You want to show it to your friends through a web demo so they can interact with your model and provide feedback.

您为此感到骄傲。 您希望通过网络演示将其展示给您的朋友,以便他们可以与您的模型进行交互并提供反馈。

However, you are not familiar with common frameworks such as Django and Flask.

但是,您对Django和Flask等常见框架不熟悉。

You start to ask yourself: Is there a way to build a quick web demo with minimal framework?

您开始问自己:有没有办法用最少的框架构建快速的Web演示?

好奇心表 (Table of Curiosities)

  1. What is Streamlit?

    什么是Streamlit?

  2. How to make the UI?

    如何制作UI?

  3. How to create the image classification model?

    如何创建图像分类模型?

  4. What does the result look like?

    结果是什么样的?

  5. What can we do next?

    接下来我们该怎么办?

总览 (Overview)

In this post, I will walk through a quick example of how you can use Streamlit to build a simple web app.

在本文中,我将通过一个简单的示例演示如何使用Streamlit构建简单的Web应用程序。

Streamlit is an open-source Python library that makes it easy to build custom web apps for machine learning and data science [1]. Check out its gallery here to see some applications that other people have created.

Streamlit是一个开放源代码的Python库,可轻松构建用于机器学习和数据科学的自定义Web应用程序[1]。 在此处查看其图库,以查看其他人创建的一些应用程序。

I have chosen image classification here as an example because computer vision (CV) is one of the most popular areas of AI currently, powered by deep learning algorithms. It also has a wide range of applications, such as classifying medical images to help doctors in disease diagnosis [2]. Learn more about image classification with deep learning models here.

我在这里选择图像分类作为示例,因为计算机视觉(CV)是当前由深度学习算法提供支持的AI最受欢迎的领域之一。 它还具有广泛的应用,例如对医学图像进行分类以帮助医生进行疾病诊断[2]。 在此处了解有关使用深度学习模型进行图像分类的更多信息。

For demonstration purposes, I will use a pretrained ResNet model from PyTorch, and for the same task, you can always use other libraries (TensorFlow, Keras, etc.), other architecture, or even customize your own model.

出于演示目的,我将使用PyTorch中经过预训练的ResNet模型,并且对于同一任务,您始终可以使用其他库(TensorFlow,Keras等),其他体系结构,甚至自定义您自己的模型。

To see my full Python code, check out my Github page.

要查看我的完整Python代码,请查看我的Github页面 。

Now without further ado, let’s get started!

现在,事不宜迟,让我们开始吧!

安装Streamlit (Install Streamlit)

The first step is to install the Streamlit library, and you can do that using the pip command. I recommend that you use a Python virtual environment to keep your dependencies separately for each project.

第一步是安装Streamlit库,您可以使用pip命令执行此操作。 我建议您使用Python虚拟环境为每个项目分别保留依赖项。

$ pip install streamlit

After it is installed successfully, you can do a quick check with a simple ‘Hello World’ app:

成功安装后,您可以使用简单的“ Hello World”应用进行快速检查:

$ streamlit hello

用户界面 (User Interface)

For our application, one key element is to enable users to upload images for the model to make predictions, and this can be done with the ‘file_uploader’ function:

对于我们的应用程序,一个关键要素是使用户能够上传图像以供模型进行预测,这可以通过' file_uploader '函数来完成:

import streamlit as stfile_up = st.file_uploader("Upload an image", type="jpg")

Make sure to specify the appropriate file type so it works properly with the classification model.

确保指定适当的文件类型,以便它与分类模型一起正常工作。

Then we can display this image:

然后我们可以显示此图像:

from PIL import Imageimage = Image.open(file_up)st.image(image, caption='Uploaded Image.', use_column_width=True)

Apart from that, we will also use ‘set_title’ to create a title for the app and ‘write’ to display prediction results.

除此之外,我们还将使用“ set_title”为应用创建标题,并使用“ write”显示预测结果。

分类模型 (Classification Model)

To use a pretrained model from PyTorch, make sure you have installed both ‘torch’ and ‘torchvision’ library. Now we can create a ResNet model:

要使用来自PyTorch的预训练模型,请确保已安装“ torch”和“ torchvision”库。 现在我们可以创建一个ResNet模型:

from torchvision import models, transformsimport torchresnet = models.resnet101(pretrained=True)

To put it in simpler terms, Residual Network (ResNet) is an improved Convolutional Neuron Network (CNN), trained in the well-known ImageNet database, and it has achieved great performance in CV tasks such as image classification and object detection. This particular model ‘resnet101’ means it has 101 layers (Deep!). Read more about ResNet and its variant here.

简而言之,残差网络(ResNet)是经过改进的卷积神经元网络(CNN),在著名的ImageNet数据库中进行了训练,并且在CV任务(例如图像分类和对象检测)中取得了出色的性能 。 此特定模型'resnet101'表示它具有101层(深!)。 在此处阅读有关ResNet及其变体的更多信息。

After we create this model, we need to transform the input image through resizing, normalization, etc. Here we make use of the ‘transforms’ module in ‘torchvision’:

创建此模型后,我们需要通过调整大小,归一化等方法来变换输入图像。在这里,我们使用“ torchvision”中的“ transforms”模块:

transform = transforms.Compose([    transforms.Resize(256),    transforms.CenterCrop(224),    transforms.ToTensor(),    transforms.Normalize(    mean=[0.485, 0.456, 0.406],    std=[0.229, 0.224, 0.225]    )])

Note that the images are normalized according to its mean and standard deviation. See the documentation for details.

请注意,图像根据其平均值和标准偏差进行了归一化。 有关详细信息,请参见文档 。

Now we are able to load the image, pre-process it, and make predictions:

现在,我们可以加载图像,对其进行预处理并做出预测了:

img = Image.open(image_path)batch_t = torch.unsqueeze(transform(img), 0)resnet.eval()out = resnet(batch_t)

Next, we can return the top 5 predictions ranked by highest probabilities. Make sure the names of the 1000 categories from ImageNet are available so we can output the predictions through index:

接下来,我们可以返回按最高概率排名的前5个预测。 确保ImageNet的1000个类别的名称可用,以便我们可以通过索引输出预测:

prob = torch.nn.functional.softmax(out, dim=1)[0] * 100_, indices = torch.sort(out, descending=True)return [(classes[idx], prob[idx].item()) for idx in indices[0][:5]]

结果 (Results)

Now we can run the application by typing ‘streamlit run <the python file containing UI>’ in the command prompt/terminal. Let’s try two examples and see what the results look like.

现在,我们可以通过在命令提示符/终端中键入“ streamlit run <包含UI的python文件”来运行应用程序。 让我们尝试两个示例,看看结果如何。

First example:

第一个例子:

Helena Lopes on Helena Lopes在《 UnsplashUnsplash》上

Great! Golden retriever is at the top of the list, with high confidence score (~97).

大! 金毛寻回犬以较高的置信度得分(〜97)在列表中排名最高。

Let’s try another example:

让我们尝试另一个示例:

Zdenek Rosenthaler on 像素上的 PexelsZdenek Rosenthaler

Green snake is at the top of the list! Again, its confidence score is quite high.

绿蛇在榜首! 同样,它的置信度很高。

One thing to be cautious here is that since the model was trained on ImageNet, its performance might not be as good for images outside of the 1000 categories.

这里要注意的一件事是,由于该模型是在ImageNet上进行训练的,因此它的性能对于1000个类别以外的图像可能不太好。

下一步 (Next Steps)

The natural next step after you create a web app is to deploy and host it. As an example, I deployed an Iris Classification app through Heroku and you can check it out through this Github page.

创建Web应用程序后,自然的下一步就是部署和托管它。 例如,我通过Heroku部署了一个Iris分类应用程序,您可以通过此Github页面检出它。

In addition, if you are interested in other neural network models (VGG, Densenet, etc.), you can try upload different images and then compare their performances.

此外,如果您对其他神经网络模型(VGG,Densenet等)感兴趣,可以尝试上传不同的图像,然后比较它们的性能。

摘要 (Summary)

Let’s quickly recap.

让我们快速回顾一下。

We built a simple web app through Streamlit, and in this app, users can upload images to see the model’s top predictions as well as confidence scores. We also went through how to build a pretrained ResNet model through PyTorch.

我们通过Streamlit构建了一个简单的Web应用程序,在该应用程序中,用户可以上传图像以查看模型的最高预测以及置信度得分。 我们还介绍了如何通过PyTorch构建预训练的ResNet模型。

I hope you enjoy this blog post and please share any thought that you may have :)

希望您喜欢这篇博客文章,并分享您可能有的任何想法:)

Check out my other post on sentiment classification on Yelp reviews using logistic regression:

在Logistic回归上查看关于Yelp评论的情感分类的其他文章:

翻译自: https://towardsdatascience.com/create-an-image-classification-web-app-using-pytorch-and-streamlit-f043ddf00c24

pytorch图像分类


http://www.taodudu.cc/news/show-863750.html

相关文章:

  • 深度学习之对象检测_深度学习时代您应该阅读的12篇文章,以了解对象检测
  • python 梯度下降_Python解释的闭合形式和梯度下降回归
  • 内容管理系统_内容
  • opencv图像深度-1_OpenCV空间AI竞赛之旅(第1部分-初始设置+深度)
  • 概率编程编程_概率编程语言的温和介绍
  • TensorFlow 2.X中的动手NLP深度学习模型准备
  • 时间序列 线性回归 区别_时间序列分析的完整介绍(带R)::线性过程I
  • 深度学习学习7步骤_如何通过4个简单步骤为深度学习标记音频
  • 邮件伪造_伪造品背后的数学
  • 图像匹配与OpenCV模板匹配
  • 边缘计算边缘计算edge_Edge AI-边缘上的计算机视觉推理
  • arduino 入门套件_计算机视觉入门套件
  • 了解LSTM和GRU
  • 使用TensorFlow 2.0+和Keras实现AlexNet CNN架构
  • power bi_如何将Power BI模型的尺寸减少90%!
  • 使用Optuna的XGBoost模型的高效超参数优化
  • latex 表格中虚线_如何识别和修复表格识别中的虚线
  • 构建强化学习_如何构建强化学习项目(第1部分)
  • sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端
  • pca 主成分分析_六分钟的主成分分析(PCA)的直观说明。
  • seaborn 教程_使用Seaborn进行数据可视化教程
  • alexnet 结构_AlexNet的体系结构和实现
  • python做作业没头绪_使用Python做作业
  • 使用Python构建推荐系统的机器学习
  • DeepR —训练TensorFlow模型进行生产
  • 通化红灯_我们如何构建廉价,可扩展的架构来对世界进行卡通化!
  • 机器学习学习吴恩达逻辑回归_机器学习基础:逻辑回归
  • 软件测试 测试停止标准_停止正常测试
  • cloud 部署_使用Google Cloud AI平台开发,训练和部署TensorFlow模型
  • 机器学习多元线性回归_过度简化的机器学习(1):多元回归

pytorch图像分类_使用PyTorch和Streamlit创建图像分类Web应用相关推荐

  1. 小程序中input标签没有反应_鸢尾花预测:如何创建机器学习Web应用程序?

    全文共2485字,预计学习时长12分钟 图源:unsplash 数据科学的生命周期主要包括数据收集.数据清理.探索性数据分析.模型构建和模型部署.作为数据科学家或机器学习工程师,能够部署数据科学项目非 ...

  2. 机器学习花朵图像分类_在PyTorch中使用转移学习进行图像分类

    想了解更多好玩的人工智能应用,请关注公众号"机器AI学习 数据AI挖掘","智能应用"菜单中包括:颜值检测.植物花卉识别.文字识别.人脸美妆等有趣的智能应用.. ...

  3. pytorch机器学习_机器学习— PyTorch

    pytorch机器学习 PyTorch : It is an open source machine learning library based on the Torch library (whic ...

  4. pytorch自动编码_用pytorch第2部分从头开始编码ppo 4

    pytorch自动编码 Welcome to Part 2 of our series, where we shall start coding Proximal Policy Optimizatio ...

  5. pytorch 归一化_用PyTorch进行语义分割

    点击上方"机器学习与生成对抗网络",关注"星标" 获取有趣.好玩的前沿干货! 木易 发自 凹非寺  量子位 报道 | 公众号 QbitAI 很久没给大家带来教程 ...

  6. pytorch 安卓_兼容PyTorch、TF,史上最灵活Python机器学习框架发布 | 一周AI最火论文...

    大数据文摘出品 作者:Christopher Dossman 编译:Olivia.Joey.云舟 呜啦啦啦啦啦啦啦大家好,本周的AI Scholar Weekly栏目又和大家见面啦!AI Schola ...

  7. Pytorch ——基础指北_叁 [Pytorch API 构建基础模型]

    Pytorch --基础指北_叁 系列文章目录 Pytorch --基础指北_零 Pytorch --基础指北_壹 Pytorch --基础指北_贰 Pytorch --基础指北_叁 文章目录 Pyt ...

  8. 【Pytorch进阶一】基于LeNet的CIFAR10图像分类

    [Pytorch进阶一]基于LeNet的CIFAR10图像分类 一.LeNet网络介绍 二.CIFAR10数据集介绍 三.程序架构介绍 3.1 LeNet模型(model.py) 3.2 训练(tra ...

  9. python实现yolo目标检测_从零开始PyTorch项目:YOLO v3目标检测实现

    在过去几个月中,我一直在实验室中研究提升目标检测的方法.在这之中我获得的最大启发就是意识到:学习目标检测的最佳方法就是自己动手实现这些算法,而这正是本教程引导你去做的. 在本教程中,我们将使用 PyT ...

最新文章

  1. Spring简单总结
  2. 小白 C++ 入门到大神发疯学习路线
  3. vs2008网站模式下不能设置rdlc的数据源
  4. git 撤销merge_相见恨晚的 Git 命令动画演示,一看就懂!
  5. Linux 用inotify监听文件和目录
  6. Java固定资产管理系统源码
  7. 餐厅点餐系统设计思路
  8. Cox模型中的时间依存协变量和时间依存系数(R语言)第一部分
  9. 金山云个人用户实名认证步骤详解(图文教程)
  10. 深度学习: 数据扩充 (Data Augmentation)
  11. YTU 问题 : 排序
  12. 关闭打印机和无线服务器,打印机无线连接断开了怎么办?
  13. 各种语言的特点和介绍
  14. Windows磁盘管理(虚拟磁盘)
  15. 【互联网安全】移动APP漏洞风险与解决方案
  16. Docker 技术部署基于 LAMP 服务器环境网站
  17. c语言数据类型、内存空间详解
  18. EMC现场测试-EFT、ESD、Surge和场辐射
  19. 2018北京师范大学第十六届程序设计竞赛决赛
  20. OSI 模型 TCP/IP 各层的作用以及协议 vlan的三种端口 (交换部分二)

热门文章

  1. X210串口配置与stdio移植
  2. 商城项目:装nginx时碰到的各种问题
  3. android R.id.转化为view
  4. PHP 面向对象使用案例
  5. WPF Application 类介绍以及怎样修改启动方式
  6. ASP.NET 服务器控件授权
  7. 优化你的DiscuzNT3.0,让它跑起来(4)asp.net 缓存和死锁
  8. ASP.NET 中 Cookie 的基本知识
  9. mysql query browser的使用_影响MySQL性能的配置参数
  10. spring源码:资源管理器Resource