亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PyTorch如何創(chuàng)建自己的數(shù)據(jù)集

 更新時間:2022年11月28日 15:06:09   作者:ZQ_ZHU  
這篇文章主要介紹了PyTorch如何創(chuàng)建自己的數(shù)據(jù)集,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

PyTorch創(chuàng)建自己的數(shù)據(jù)集

圖片文件在同一的文件夾下

思路是繼承 torch.utils.data.Dataset,并重點重寫其 __getitem__方法,示例代碼如下:

class ImageFolder(Dataset):
? ? def __init__(self, folder_path):
? ? ? ? self.files = sorted(glob.glob('%s/*.*' % folder_path))

? ? def __getitem__(self, index):
? ? ? ? path = self.files[index % len(self.files)]
? ? ? ? img = np.array(Image.open(path))
? ? ? ? h, w, c = img.shape
? ? ? ? pad = ((40, 40), (4, 4), (0, 0))

? ? ? ? # img = np.pad(img, pad, 'constant', constant_values=0) / 255
? ? ? ? img = np.pad(img, pad, mode='edge') / 255.0
? ? ? ? img = torch.from_numpy(img).float()
? ? ? ? patches = np.reshape(img, (3, 10, 128, 11, 128))
? ? ? ? patches = np.transpose(patches, (0, 1, 3, 2, 4))

? ? ? ? return img, patches, path

? ? def __len__(self):
? ? ? ? return len(self.files)

圖片文件在不同的文件夾下

比如我們有數(shù)據(jù)如下:

─── data
├── train
│ ├── 0.jpg
│ └── 1.jpg
├── test
│ ├── 0.jpg
│ └── 1.jpg
└── val
├── 1.jpg
└── 2.jpg

此時我們只需要將以上代碼稍作修改即可,修改的代碼如下:

self.files = sorted(glob.glob('%s/**/*.*' % folder_path, recursive=True))

其他代碼不變。

pytorch常用數(shù)據(jù)集的使用

對于pytorch數(shù)據(jù)集的使用,示例代碼如下:

from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import Compose
from torchvision import transforms
import torchvision
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

dataset_transform = Compose([transforms.ToTensor()])


# 關(guān)于官方數(shù)據(jù)集的使用還是關(guān)鍵要看pytorch的官方文檔
train_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=True,transform=dataset_transform,download=True)
test_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=False,transform=dataset_transform,download=True)

# 查看測試數(shù)據(jù)集中的第一個數(shù)據(jù)
# print(test_set[0])
# 查看測試數(shù)據(jù)集中的分類情況
# print(test_set.classes)
#
# 取出第一個數(shù)據(jù)中的圖片(img)和分類結(jié)果(target)
# img,target = test_set[0]
# 查看圖片數(shù)據(jù)的類型
# print(img)
# print(target)
# 輸出類別
# print(test_set.classes[target])
# 查看圖片
# img.show()

# 使用tensorboard顯示tensor數(shù)據(jù)類型的圖片
writer = SummaryWriter("logs")
for i in range(10):
	# 取出數(shù)據(jù)中的圖片(img)和分類結(jié)果(target)
    img,target = test_set[i]
    writer.add_image("test_set",img,i)

writer.close()

上述代碼運行結(jié)果在tensorboard可視化:

代碼

train_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=True,transform=dataset_transform,download=True)

常用參數(shù)講解

  • root:根目錄,存放數(shù)據(jù)集的位置
  • train:若為True,則劃分為訓練數(shù)據(jù)集,若為False,則劃分為測試數(shù)據(jù)集
  • transform:指定輸入數(shù)據(jù)集處理方式
  • download:若為True,則會將數(shù)據(jù)集下載到root指定的目錄下,否則不會下載

官方文檔對參數(shù)的解釋:

root (string) – Root directory of dataset where directory cifar-10-batches-py exists or will be saved to if download is set to True.

train (bool, optional) – If True, creates dataset from training set, otherwise creates from test set.

transform (callable, optional) – A function/transform that takes in an PIL image and returns a transformed version. E.g, transforms.RandomCrop

target_transform (callable, optional) – A function/transform that takes in the target and transforms it.

download (bool, optional) – If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again.

注意:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python+OpenCV實現(xiàn)角度測量的示例代碼

    Python+OpenCV實現(xiàn)角度測量的示例代碼

    本文介紹如何使用python語言實現(xiàn)角度測量,程序包括鼠標選點、直線斜率計算、角度計算三個子程序和一個主程序,感興趣的可以了解一下
    2022-03-03
  • Python的string模塊中的Template類字符串模板用法

    Python的string模塊中的Template類字符串模板用法

    通過string.Template我們可以為Python定制字符串的替換標準,這里我們就來通過示例解析Python的string模塊中的Template類字符串模板用法:
    2016-06-06
  • python制作可視化GUI界面自動分類管理文件

    python制作可視化GUI界面自動分類管理文件

    這篇文章主要為大家介紹了python制作可視化GUI界面實現(xiàn)自動分類管理文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • sklearn的predict_proba使用說明

    sklearn的predict_proba使用說明

    這篇文章主要介紹了sklearn的predict_proba使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 基于Tensorflow批量數(shù)據(jù)的輸入實現(xiàn)方式

    基于Tensorflow批量數(shù)據(jù)的輸入實現(xiàn)方式

    今天小編就為大家分享一篇基于Tensorflow批量數(shù)據(jù)的輸入實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python靜態(tài)方法實例

    python靜態(tài)方法實例

    這篇文章主要介紹了python靜態(tài)方法,實例分析了python靜態(tài)方法的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • Python中用append()連接后多出一列Unnamed的解決

    Python中用append()連接后多出一列Unnamed的解決

    Python中用append()連接后多出一列Unnamed的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python基礎教程之Pandas數(shù)據(jù)分析庫詳解

    Python基礎教程之Pandas數(shù)據(jù)分析庫詳解

    Pandas是一個基于 NumPy 的非常強大的開源數(shù)據(jù)處理庫,它提供了高效、靈活和豐富的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析工具,本文中,我們將學習如何使用Pandas來處理和分析數(shù)據(jù),感興趣的小伙伴跟著小編一起來看看吧
    2023-07-07
  • pyqt 實現(xiàn)為長內(nèi)容添加滑輪 scrollArea

    pyqt 實現(xiàn)為長內(nèi)容添加滑輪 scrollArea

    今天小編就為大家分享一篇pyqt 實現(xiàn)為長內(nèi)容添加滑輪 scrollArea,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 關(guān)于Python時間日期常見的一些操作方法

    關(guān)于Python時間日期常見的一些操作方法

    Python的datetime模塊是處理日期和時間的強大工具,datetime類可以獲取當前時間、指定日期、計算時間差、訪問時間屬性及格式化時間,這些功能使得在Python中進行時間日期處理變得簡單高效,需要的朋友可以參考下
    2024-09-09

最新評論