1. torchvision数据集介绍

① torchvision中有很多数据集,当我们写代码时指定相应的数据集指定一些参数,它就可以自行下载。

② CIFAR-10数据集包含60000张32×32的彩色图片,一共10个类别,其中50000张训练图片,10000张测试图片。

2. torchvision数据集使用

① 在 Anaconda 终端里面,激活py3.6.3环境,再跳转到该项目的路径下。

② 运行python。导入torchvision包,输入

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

命令,即下载数据集到到该文件夹下。

import torchvision
help(torchvision.datasets.CIFAR10)
Help on class CIFAR10 in module torchvision.datasets.cifar:

class CIFAR10(torchvision.datasets.vision.VisionDataset)
| `CIFAR10 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.
|
| Args:
| 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.
|
| Method resolution order:
| CIFAR10
| torchvision.datasets.vision.VisionDataset
| torch.utils.data.dataset.Dataset
| typing.Generic
| builtins.object
|
| Methods defined here:
|
| __getitem__(self, index:int) -> Tuple[Any, Any]
| Args:
| index (int): Index
|
| Returns:
| tuple: (image, target) where target is index of the target class.
|
| __init__(self, root:str, train:bool=True, transform:Union[Callable, NoneType]=None, target_transform:Union[Callable, NoneType]=None, download:bool=False) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __len__(self) -> int
|
| download(self) -> None
|
| extra_repr(self) -> str
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __args__ = None
|
| __extra__ = None
|
| __next_in_mro__ = <class 'object'>
| The most base type
|
| __orig_bases__ = (torchvision.datasets.vision.VisionDataset,)
|
| __origin__ = None
|
| __parameters__ = ()
|
| __tree_hash__ = -9223371924072727236
|
| base_folder = 'cifar-10-batches-py'
|
| filename = 'cifar-10-python.tar.gz'
|
| meta = {'filename': 'batches.meta', 'key': 'label_names', 'md5': '5ff9...
|
| test_list = [['test_batch', '40351d587109b95175f43aff81a1287e']]
|
| tgz_md5 = 'c58f30108f718f92721af3b95e74349a'
|
| train_list = [['data_batch_1', 'c99cafc152244af753f735de768cd75f'], ['...
|
| url = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
|
| ----------------------------------------------------------------------
| Methods inherited from torchvision.datasets.vision.VisionDataset:
|
| __repr__(self) -> str
| Return repr(self).
|
| ----------------------------------------------------------------------
| Methods inherited from torch.utils.data.dataset.Dataset:
|
| __add__(self, other:'Dataset[T_co]') -> 'ConcatDataset[T_co]'
|
| __getattr__(self, attribute_name)
|
| ----------------------------------------------------------------------
| Class methods inherited from torch.utils.data.dataset.Dataset:
|
| register_datapipe_as_function(function_name, cls_to_register, enable_df_api_tracing=False) from typing.GenericMeta
|
| register_function(function_name, function) from typing.GenericMeta
|
| ----------------------------------------------------------------------
| Data descriptors inherited from torch.utils.data.dataset.Dataset:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from torch.utils.data.dataset.Dataset:
|
| __annotations__ = {'functions': typing.Dict[str, typing.Callable]}
|
| functions = {'concat': functools.partial(<function Dataset.register_da...
|
| ----------------------------------------------------------------------
| Static methods inherited from typing.Generic:
|
| __new__(cls, *args, **kwds)
| Create and return a new object. See help(type) for accurate signature.

3. 查看CIFAR10数据集内容

import torchvision
train_set = torchvision.datasets.CIFAR10(root="./dataset",train=True,download=True) # root为存放数据集的相对路线
test_set = torchvision.datasets.CIFAR10(root="./dataset",train=False,download=True) # train=True是训练集,train=False是测试集 print(test_set[0]) # 输出的3是target
print(test_set.classes) # 测试数据集中有多少种 img, target = test_set[0] # 分别获得图片、target
print(img)
print(target) print(test_set.classes[target]) # 3号target对应的种类
img.show()
Files already downloaded and verified
Files already downloaded and verified
(<PIL.Image.Image image mode=RGB size=32x32 at 0x1A4275AAF28>, 3)
['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
<PIL.Image.Image image mode=RGB size=32x32 at 0x1A4275AAA58>
3
cat

4. Tensorboard查看内容

import torchvision
from torch.utils.tensorboard import SummaryWriter dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
train_set = torchvision.datasets.CIFAR10(root="./dataset",train=True,transform=dataset_transform,download=True) # 将ToTensor应用到数据集中的每一张图片,每一张图片转为Tensor数据类型
test_set = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=dataset_transform,download=True) writer = SummaryWriter("logs")
for i in range(10):
img, target = test_set[i]
writer.add_image("test_set",img,i)
print(img.size()) writer.close() # 一定要把读写关闭,否则显示不出来图片
Files already downloaded and verified
Files already downloaded and verified
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])
torch.Size([3, 32, 32])

① 在 Anaconda 终端里面,激活py3.6.3环境,再输入

tensorboard --logdir=C:\Users\wangy\Desktop\03CV\logs   #绝对地址
或者使用下面指令:
tensorboard --logdir=logs #相对地址

命令,将网址赋值浏览器的网址栏,回车,即可查看tensorboard显示日志情况。

4-torchvision数据集使用的更多相关文章

  1. torchvision库简介(翻译)

    部分跟新于:4.24日    torchvision 0.2.2.post3 torchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详细介绍在:http ...

  2. PyTorch教程之Training a classifier

    我们已经了解了如何定义神经网络,计算损失并对网络的权重进行更新. 接下来的问题就是: 一.What about data? 通常处理图像.文本.音频或视频数据时,可以使用标准的python包将数据加载 ...

  3. PyTorch进行深度学习入门

    一.PyTorch是什么? 这是一个基于Python的科学计算软件包,针对两组受众: ①.NumPy的替代品,可以使用GPU的强大功能 ②.深入学习研究平台,提供最大的灵活性和速度 二.入门 ①.张量 ...

  4. pytorch例子学习——TRAINING A CLASSIFIER

    参考:https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar1 ...

  5. 【PyTorch深度学习60分钟快速入门 】Part4:训练一个分类器

      太棒啦!到目前为止,你已经了解了如何定义神经网络.计算损失,以及更新网络权重.不过,现在你可能会思考以下几个方面: 0x01 数据集 通常,当你需要处理图像.文本.音频或视频数据时,你可以使用标准 ...

  6. 【pytorch】pytorch学习笔记(一)

    原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...

  7. 【PyTorch v1.1.0文档研习】60分钟快速上手

    阅读文档:使用 PyTorch 进行深度学习:60分钟快速入门. 本教程的目标是: 总体上理解 PyTorch 的张量库和神经网络 训练一个小的神经网络来进行图像分类 PyTorch 是个啥? 这是基 ...

  8. PyTorch深度学习:60分钟入门(Translation)

    这是https://zhuanlan.zhihu.com/p/25572330的学习笔记. Tensors Tensors和numpy中的ndarrays较为相似, 因此Tensor也能够使用GPU来 ...

  9. pytorch资料

    torchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详细介绍在:https://pypi.org/project/torchvision/ torch ...

  10. [PyTorch入门之60分钟入门闪击战]之训练分类器

    训练分类器 目前为止,你已经知道如何定义神经网络.计算损失和更新网络的权重.现在你可能在想,那数据呢? What about data? 通常,当你需要处理图像.文本.音频或者视频数据时,你可以使用标 ...

随机推荐

  1. Kratos 下载与安装

    前置条件 请确保已经安装好 go git protoc 然后获取 kratos 工具 go get -u github.com/go-kratos/kratos/tool/kratos 验证是否安装成 ...

  2. Linux 关机与重启命令

    关机命令 我们可以使用以下三种命令来关机 Linux : 1.立刻关机(需要root用户) shutdown -h now 10 分钟后自动关机 shutdown -h 10 2.立刻关机 halt ...

  3. LinkedBlockingQueue的poll方法底层原理

    一.LinkedBlockingQueue的poll方法底层原理 LinkedBlockingQueue 的 poll 方法用于从队列头部移除并返回元素.如果队列为空,poll 方法会立即返回 nul ...

  4. FastAPI与SQLAlchemy同步数据库集成

    title: FastAPI与SQLAlchemy同步数据库集成 date: 2025/04/15 01:27:37 updated: 2025/04/15 01:27:37 author: cmdr ...

  5. Stream流式编程工具类,开发必备

    把自己写的流式编程工具分享出来,不涉及公司业务,非常便捷,不用在业务层看到一条龙式的Stream代码了: 大家用的最多的应该是转list,转set,以及setVFromE: 觉得好用点个赞就行 imp ...

  6. 前端速成之——Script

    Script 1-引入js和函数调用 function函数:必然存在一个返回值,绝对不会书写 void,要么返回 undefine,要么返回 return 的数据 function etoak(val ...

  7. C++基础——引用和指针篇

    一.指针(Pointer) 定义: 指针是一个变量,用于存储另一个变量的地址. 基本用法: #include <iostream> using namespace std; int mai ...

  8. 彻底掌握 PCA 降维

    PCA 这类的降维算法, 我算是接触好几年了有, 从我学营销的时候, 市场研究方面就经常会用到,相关的还有 "因子分析" 比如, 商品形象认知, 客户细分等场景. 其实多年前我就能 ...

  9. AI法律助手:打造普惠法律服务的未来

    当法律服务遇见人工智能,普通人的维权之路将不再艰难 当法律服务成为奢侈品,AI或许是唯一出路 2025年的一个深夜,我刷着手机,一条新闻让我停下了滑动的手指: "某平台家装工人因合同纠纷讨薪 ...

  10. C++11 shared_ptr(智能指针)

    在确保new动态分配的内存空间在使用结束之后,释放是一件麻烦事.C++11模板库的头文件中定义的智能指针,即shared_ptr模板,就是用来解决这个问题的. 它是将new运算符返回的指针p交给一个s ...