analitics

Pages

Monday, December 17, 2018

Using pytorch - another way.

Yes. I used pytorch and is working well. Is not perfect the GitHub come every day with a full stack of issues.
Let's continue this series with another step: torchvision.
If you take a closer look at that gift, you will see that it comes with a special label that can really help us.
This label is a named torchvision.
The torchvision python module is a package consists of popular datasets, model architectures, and common image transformations for computer vision.
Most operations pass through filters and date already recognized.
  • torchvision.datasets: (MNIST,Fashion-MNIST,EMNIST,COCO,LSUN,ImageFolder,DatasetFolder,Imagenet-12,CIFAR,STL10,SVHN,PhotoTour,SBU,Flickr,VOC)
  • torchvision.models: (Alexnet,VGG,ResNet,SqueezeNet,DenseNet,Inception v3)
  • torchvision.transforms: (Transforms on PIL Image,Transforms on torch.*Tensor,Conversion Transforms,Generic Transforms,Functional Transforms)
  • torchvision.utils
This part of the gift help you to load and prepare dataset but into certain order.
Using this special label, we will be able to use the gift-breaking information.
Let's see the example:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torchvision
>>> import torchvision.transforms as transforms
>>> transform = transforms.Compose(
...     [transforms.ToTensor(),
...      transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
>>> trainset = torchvision.datasets.CIFAR10(root='./data', train=True,download=T
rue, transform=transform)
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./data\ci
far-10-python.tar.gz
You will ask me: How is this special gift label linked?
In this way:
>>> import torch
>>> trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,shuffle=Tru
e, num_workers=2)
Let's take a closer look at the information in the special label.
>>> print(trainset)
Dataset CIFAR10
    Number of datapoints: 50000
    Split: train
    Root Location: ./data
    Transforms (if any): Compose(
                             ToTensor()
                             Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)
)
                         )
    Target Transforms (if any): None
>>> print(dir(trainset))
['__add__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq_
_', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash
__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__
', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__'
, '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_integrity'
, 'base_folder', 'download', 'filename', 'root', 'target_transform', 'test_list'
, 'tgz_md5', 'train', 'train_data', 'train_labels', 'train_list', 'transform', '
url']
Let's look more closely at the information that can be used by the gift with the special label.
>>> print(trainloader)

>>> print(dir(trainloader))
['_DataLoader__initialized', '__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__ha
sh__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__
', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bat
ch_sampler', 'batch_size', 'collate_fn', 'dataset', 'drop_last', 'num_workers',
'pin_memory', 'sampler', 'timeout', 'worker_init_fn']
Beware, CIFAR10 is just one of the training databases.
About the CIFAR-10 dataset, that consists of 60,000 32x32 color images in 10 classes, with 6,000 images per class.
There are 50,000 training images (5,000 per class) and 10,000 test images.