4-torchvision数据集使用
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数据集使用的更多相关文章
- torchvision库简介(翻译)
部分跟新于:4.24日 torchvision 0.2.2.post3 torchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详细介绍在:http ...
- PyTorch教程之Training a classifier
我们已经了解了如何定义神经网络,计算损失并对网络的权重进行更新. 接下来的问题就是: 一.What about data? 通常处理图像.文本.音频或视频数据时,可以使用标准的python包将数据加载 ...
- PyTorch进行深度学习入门
一.PyTorch是什么? 这是一个基于Python的科学计算软件包,针对两组受众: ①.NumPy的替代品,可以使用GPU的强大功能 ②.深入学习研究平台,提供最大的灵活性和速度 二.入门 ①.张量 ...
- pytorch例子学习——TRAINING A CLASSIFIER
参考:https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar1 ...
- 【PyTorch深度学习60分钟快速入门 】Part4:训练一个分类器
太棒啦!到目前为止,你已经了解了如何定义神经网络.计算损失,以及更新网络权重.不过,现在你可能会思考以下几个方面: 0x01 数据集 通常,当你需要处理图像.文本.音频或视频数据时,你可以使用标准 ...
- 【pytorch】pytorch学习笔记(一)
原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...
- 【PyTorch v1.1.0文档研习】60分钟快速上手
阅读文档:使用 PyTorch 进行深度学习:60分钟快速入门. 本教程的目标是: 总体上理解 PyTorch 的张量库和神经网络 训练一个小的神经网络来进行图像分类 PyTorch 是个啥? 这是基 ...
- PyTorch深度学习:60分钟入门(Translation)
这是https://zhuanlan.zhihu.com/p/25572330的学习笔记. Tensors Tensors和numpy中的ndarrays较为相似, 因此Tensor也能够使用GPU来 ...
- pytorch资料
torchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详细介绍在:https://pypi.org/project/torchvision/ torch ...
- [PyTorch入门之60分钟入门闪击战]之训练分类器
训练分类器 目前为止,你已经知道如何定义神经网络.计算损失和更新网络的权重.现在你可能在想,那数据呢? What about data? 通常,当你需要处理图像.文本.音频或者视频数据时,你可以使用标 ...
随机推荐
- 深入浅出CPU眼中的函数调用&栈溢出攻击
深入浅出CPU眼中的函数调用--栈溢出攻击 原理解读 函数调用,大家再耳熟能详了,我们先看一个最简单的函数: #include <stdio.h> #include <stdlib. ...
- [源码系列:手写spring] IOC第三节:Bean实例化策略InstantiationStrategy
主要内容 在第二节中AbstractAutowireCapableBeanFactory类中使用class.newInstance()的方式创建实例,仅适用于无参构造器. 大家可以测试一下,将第二节 ...
- Lua程序设计笔记
未学:第10章URL编码及以后的示例 13章位和字节 Lua语言基础 一组命令或表达式组成的序列叫chunk程序段,因为Lua语言可以被用作数据定义语言,chunk的大小没有限制,几MB的程序段也很常 ...
- 【Python】Python使用Tk实现动态爱心效果
[Python]Python使用Tk实现动态爱心效果 深夜种下希望,梦中便能发芽 相对于之前的版本(晚上星月争辉,美梦陪你入睡),解决了看起来很卡.爱心跳动死板和外围光环不怎么灵动的问题,添加了文字功 ...
- C#连接小智服务器并将音频解码播放过程记录
前言 最近小智很火,本文记录C#连接小智服务器并将音频解码播放的过程,希望能帮助到对此感兴趣的开发者. 如果没有ESP-32也想体验小智AI,那么这两个项目很适合你. 1.https://github ...
- JSON对象、JSON字符串和Java对象互相转
JSON对象.JSON字符串和Java对象互相转 Java对象转json字符串(一般使用字符串存储redis或者数据库) public static String toJSONString(Objec ...
- 『Plotly实战指南』--雷达图绘制与应用
在数据分析和可视化领域,雷达图是一种适用于多维数据的可视化.综合评估和决策支持的工具. 雷达图通过将数据点沿多个轴分布,并通过多边形面积或线条连接来展示数据的多维度特征,能够直观地呈现数据在各个维度上 ...
- Windows系统设置开机自启动+分块压缩+文件共享
开机自启动+分块压缩+文件共享 一.设置开机自启动 win+R 打开运行窗口,输入 shell:startup 此时桌面会弹出一个目录文件夹,只需要将需要启动的软件放入该文件夹即可开机自启. C:\U ...
- MQTT服务器 apache-apollo
apache-apollo下载 下载地址 http://archive.apache.org/dist/activemq/activemq-apollo/1.7.1/ 原来的官网地址下载不到apach ...
- 高度混淆和多层嵌套的JSP案例免杀思路
免责声明:本文所涉及的技术仅供学习和参考,严禁使用本文内容从事违法行为和未授权行为,如因个人原因造成不良后果,均由使用者本人负责,作者及本博客不承担任何责任. 01 分析特征 目前webshell检测 ...