欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Pytorch中Dataset的使用

 更新時(shí)間:2022年12月29日 14:48:53   作者:LRJ-jonas  
這篇文章主要為大家詳細(xì)介紹了如何加載并處理TorchVision的FashionMNIST Dataset,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

此案例教我們加載并處理TorchVision的FashionMNIST Dataset。

root 目錄是 train/test data 存儲(chǔ)的地方

download=True 如果root目錄沒(méi)有,則從網(wǎng)上下載

transform and target_transform specify the feature and label transformations

import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
 
 
training_data = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=ToTensor()
)
 
test_data = datasets.FashionMNIST(
    root="data",
    train=False,
    download=True,
    transform=ToTensor()
)

運(yùn)行得到的結(jié)果是這樣的:

遍歷并可視化數(shù)據(jù)集

給數(shù)據(jù)集手動(dòng)加上序號(hào)sample_idx,并用matplotlib進(jìn)行繪制:

labels_map = {
    0: "T-Shirt",
    1: "Trouser",
    2: "Pullover",
    3: "Dress",
    4: "Coat",
    5: "Sandal",
    6: "Shirt",
    7: "Sneaker",
    8: "Bag",
    9: "Ankle Boot",
}
figure = plt.figure(figsize=(8, 8))
cols, rows = 3, 3
for i in range(1, cols * rows + 1):
    sample_idx = torch.randint(len(training_data), size=(1,)).item()
    img, label = training_data[sample_idx]
    figure.add_subplot(rows, cols, i)
    plt.title(labels_map[label])
    plt.axis("off")
    plt.imshow(img.squeeze(), cmap="gray")
plt.show()

traning_data

torch.randint(len(training_data), size=(1,)).item()

為我的文件自定義一個(gè)Dataset

一個(gè)自定義的Dataset必須有三個(gè)函數(shù):__init__, __len__, and __getitem__

圖片存儲(chǔ)在img_dir

import os
import pandas as pd
from torchvision.io import read_image
 
class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
        self.img_labels = pd.read_csv(annotations_file)
        self.img_dir = img_dir
        self.transform = transform
        self.target_transform = target_transform
 
    def __len__(self):
        return len(self.img_labels)
 
    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label

到此這篇關(guān)于詳解Pytorch中Dataset的使用的文章就介紹到這了,更多相關(guān)Pytorch Dataset使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flask搭建Web應(yīng)用程序的方法示例

    Flask搭建Web應(yīng)用程序的方法示例

    Flask是一個(gè)使用Python編寫的輕量級(jí)Web應(yīng)用框架,本文我們將介紹一個(gè)使用Flask逐步搭建Web應(yīng)用程序的簡(jiǎn)單入門示例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • pytorch單元測(cè)試的實(shí)現(xiàn)示例

    pytorch單元測(cè)試的實(shí)現(xiàn)示例

    單元測(cè)試是一種軟件測(cè)試方法,本文主要介紹了pytorch單元測(cè)試的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • python裝飾器初探(推薦)

    python裝飾器初探(推薦)

    下面小編就為大家?guī)?lái)一篇python裝飾器初探(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實(shí)例詳解

    Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實(shí)例詳解

    這篇文章主要介紹了Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式,以實(shí)例形式較為詳細(xì)的分析了Python函數(shù)參數(shù)的使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python 必須了解的5種高級(jí)特征

    Python 必須了解的5種高級(jí)特征

    Python 多好用不用多說(shuō),大家看看自己用的語(yǔ)言就知道了。但是 Python 隱藏的高級(jí)功能你都 get 了嗎?本文中,作者列舉了 Python 中五種略高級(jí)的特征以及它們的使用方法,快來(lái)一探究竟吧!
    2020-09-09
  • Flask框架路由和視圖用法實(shí)例分析

    Flask框架路由和視圖用法實(shí)例分析

    這篇文章主要介紹了Flask框架路由和視圖用法,結(jié)合實(shí)例形式分析了Flask路由和視圖相關(guān)原理、定義與使用方法,需要的朋友可以參考下
    2019-11-11
  • 基于Python爬取51cto博客頁(yè)面信息過(guò)程解析

    基于Python爬取51cto博客頁(yè)面信息過(guò)程解析

    這篇文章主要介紹了基于Python爬取51cto博客頁(yè)面信息過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 最新評(píng)論