日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pytorch图像分类_使用PyTorch和Streamlit创建图像分类Web应用

發布時間:2023/12/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch图像分类_使用PyTorch和Streamlit创建图像分类Web应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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)

  • What is Streamlit?

    什么是Streamlit?

  • How to make the UI?

    如何制作UI?

  • How to create the image classification model?

    如何創建圖像分類模型?

  • What does the result look like?

    結果是什么樣的?

  • 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 st
    file_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 Image
    image = 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, transforms
    import torch
    resnet = 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圖像分類

    總結

    以上是生活随笔為你收集整理的pytorch图像分类_使用PyTorch和Streamlit创建图像分类Web应用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。