網站首頁 編程語言 正文
此案例教我們加載并處理TorchVision的FashionMNIST Dataset。
root 目錄是 train/test data 存儲的地方
download=True 如果root目錄沒有,則從網上下載
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()
)
運行得到的結果是這樣的:
遍歷并可視化數據集
給數據集手動加上序號sample_idx,并用matplotlib進行繪制:
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()
為我的文件自定義一個Dataset
一個自定義的Dataset必須有三個函數:__init__, __len__, and __getitem__
圖片存儲在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
原文鏈接:https://blog.csdn.net/m0_55097528/article/details/128467475
相關推薦
- 2022-03-13 C語言之直接插入排序算法的方法_C 語言
- 2022-12-03 Windows環境下Nginx?服務器?SSL?證書安裝部署操作過程_nginx
- 2022-05-15 C++設計模式中的觀察者模式一起來看看_C 語言
- 2023-02-05 Python實現前向和反向自動微分的示例代碼_python
- 2023-01-13 基于C#實現屏幕取色器_C#教程
- 2023-04-06 C++中的多態問題—理解虛函數表及多態實現原理_C 語言
- 2022-04-28 pytorch中torch.topk()函數的快速理解_python
- 2022-04-18 Python?socket如何解析HTTP請求內容_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支