STATUS: SUPER EARLY ALPHA

This package is NOT yet ready for PUBLIC CONSUMPTION. Use at your own RISK!!!!

Everything, including the API (and even the existence of this module) are subject to breaking change...

These are utilities for adapting Pytorch/torchvision Datasets to be used in fastai TPU training.

download_torch_dsets[source]

download_torch_dsets(path, torch_dset)

Download cifar10 datasets using torchvision utils

Arguments: path (pathlib.Path): path to download the dataset (aka root)

load_torch_items[source]

load_torch_items(downloaded_list, path, check=False)

loads cifar test/train items into tuple(data, target)

scrobbled together from torch.data.utils.datasets.CIFAR10 code https://pytorch.org/docs/stable/_modules/torchvision/datasets/cifar.html#CIFAR10

Arguments: downloaded_list : a list of file names with their checksum, see CIFAR10.train_list or CIFAR10.test_list. path (pathlib.Path): the root path where the dataset was downloaded check(bool, optional): whether to perform an integrity check on the downloaded files (default: False)

load_classes[source]

load_classes(path)

Load classes to used to map categories to target labels

class CifarNP2ImageTransform[source]

CifarNP2ImageTransform(enc=None, dec=None, split_idx=None, order=None) :: Transform

Delegates (__call__,decode,setup) to (encodes,decodes,setups) if split_idx matches

class Int2TensorTransform[source]

Int2TensorTransform(enc=None, dec=None, split_idx=None, order=None) :: Transform

Delegates (__call__,decode,setup) to (encodes,decodes,setups) if split_idx matches

class CifarImageTransform[source]

CifarImageTransform(enc=None, dec=None, split_idx=None, order=None) :: Transform

Delegates (__call__,decode,setup) to (encodes,decodes,setups) if split_idx matches

class CifarImage2FloatTransform[source]

CifarImage2FloatTransform(enc=None, dec=None, split_idx=None, order=None) :: Transform

Delegates (__call__,decode,setup) to (encodes,decodes,setups) if split_idx matches

make_torch_tfms[source]

make_torch_tfms()

th_train_tfms, th_test_tfms = make_torch_tfms()

class CifarTupleTransform[source]

CifarTupleTransform(x_tfm, y_tfm) :: ItemTransform

A transform that always take tuples as items

class TupleTorchDS[source]

TupleTorchDS(*args, **kwds) :: Dataset

An abstract class representing a :class:Dataset.

All datasets that represent a map from keys to data samples should subclass it. All subclasses should overwrite :meth:__getitem__, supporting fetching a data sample for a given key. Subclasses could also optionally overwrite :meth:__len__, which is expected to return the size of the dataset by many :class:~torch.utils.data.Sampler implementations and the default options of :class:~torch.utils.data.DataLoader.

.. note:: :class:~torch.utils.data.DataLoader by default constructs a index sampler that yields integral indices. To make it work with a map-style dataset with non-integral indices/keys, a custom sampler must be provided.

make_cifar_item_tfm[source]

make_cifar_item_tfm(th_img_tfms=None)

all_fastai_tfms = make_cifar_item_tfm()
mixed_fastai_train_tfms = make_cifar_item_tfm(th_train_tfms)

make_cifar_tls[source]

make_cifar_tls(file_list, path, item_tfm, check=True)

make_cifar_dl[source]

make_cifar_dl(file_list, path, th_img_tfms=None, check=True, bs=64, **kwargs)

train_tls = make_cifar_tls(cifar_dsets.CIFAR10.train_list,
                           cifar_root,mixed_fastai_train_tfms)
train_tls.tfms[0].x_tfm[1]
Compose:
encodes: (object,object) -> Composedecodes: 
train_dl1 = make_cifar_dl(cifar_dsets.CIFAR10.train_list, cifar_root)

make_fastai_cifar_dls[source]

make_fastai_cifar_dls(path, bs=64, check=True, device=None, **kwargs)

import torch.cuda
import torch
device = torch.device(torch.cuda.current_device()) if torch.cuda.is_available() else torch.device('cpu')
device
device(type='cuda', index=0)
cifar_dls = make_fastai_cifar_dls(cifar_root, device=device)
(cifar_root/'cifar-10-batches-py').ls()
(#8) [Path('/root/.fastai/data/cifar/cifar-10-batches-py/data_batch_1'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/data_batch_2'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/readme.html'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/batches.meta'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/test_batch'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/data_batch_4'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/data_batch_5'),Path('/root/.fastai/data/cifar/cifar-10-batches-py/data_batch_3')]
cifar_dsets.CIFAR10.train_list
[['data_batch_1', 'c99cafc152244af753f735de768cd75f'],
 ['data_batch_2', 'd4bba439e000b95fd0a9bffe97cbabec'],
 ['data_batch_3', '54ebc095f3ab1f0389bbae665268c751'],
 ['data_batch_4', '634d18415352ddfa80567beed471001a'],
 ['data_batch_5', '482c414d41f54cd18b22e5b47cb7c3cb']]
xb, yb = cifar_dls.one_batch()
xb.dtype
torch.float32
xb.shape
torch.Size([64, 3, 32, 32])
yb.dtype
torch.int64
yb.shape
torch.Size([64])
xb.device
device(type='cuda', index=0)
yb.device
device(type='cuda', index=0)
cifar_dls.train.after_batch
Pipeline: 
type(cifar_dls.train.dataset)
fastai.data.core.TfmdLists
hasattr(cifar_dls, 'device')
True
cifar_dls.device is None
False
cifar_dls.path
Path('.')
hasattr(cifar_dls.loaders[0],'to')
True
cifar_dls.loaders[0].device is None
False
from fastai.vision.all import *
import fastai.callback.progress
from fastai.vision.models import resnet18
from fastai.metrics import accuracy
import torch.nn as nn
learner = cnn_learner(cifar_dls, resnet18, n_out=10, pretrained=False, 
                      normalize=False,
                      loss_func=nn.CrossEntropyLoss(),metrics=accuracy)
learner.show_training_loop()
Start Fit
   - before_fit     : [TrainEvalCallback, Recorder, ProgressCallback]
  Start Epoch Loop
     - before_epoch   : [Recorder, ProgressCallback]
    Start Train
       - before_train   : [TrainEvalCallback, Recorder, ProgressCallback]
      Start Batch Loop
         - before_batch   : []
         - after_pred     : []
         - after_loss     : []
         - before_backward: []
         - before_step    : []
         - after_step     : []
         - after_cancel_batch: []
         - after_batch    : [TrainEvalCallback, Recorder, ProgressCallback]
      End Batch Loop
    End Train
     - after_cancel_train: [Recorder]
     - after_train    : [Recorder, ProgressCallback]
    Start Valid
       - before_validate: [TrainEvalCallback, Recorder, ProgressCallback]
      Start Batch Loop
         - **CBs same as train batch**: []
      End Batch Loop
    End Valid
     - after_cancel_validate: [Recorder]
     - after_validate : [Recorder, ProgressCallback]
  End Epoch Loop
   - after_cancel_epoch: []
   - after_epoch    : [Recorder]
End Fit
 - after_cancel_fit: []
 - after_fit      : [ProgressCallback]
learner.summary()
Sequential (Input shape: 64)
============================================================================
Layer (type)         Output Shape         Param #    Trainable 
============================================================================
                     64 x 64 x 16 x 16   
Conv2d                                    9408       True      
BatchNorm2d                               128        True      
ReLU                                                           
MaxPool2d                                                      
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
ReLU                                                           
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
ReLU                                                           
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
____________________________________________________________________________
                     64 x 128 x 4 x 4    
Conv2d                                    73728      True      
BatchNorm2d                               256        True      
ReLU                                                           
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
Conv2d                                    8192       True      
BatchNorm2d                               256        True      
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
ReLU                                                           
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
____________________________________________________________________________
                     64 x 256 x 2 x 2    
Conv2d                                    294912     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
Conv2d                                    32768      True      
BatchNorm2d                               512        True      
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
____________________________________________________________________________
                     64 x 512 x 1 x 1    
Conv2d                                    1179648    True      
BatchNorm2d                               1024       True      
ReLU                                                           
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
Conv2d                                    131072     True      
BatchNorm2d                               1024       True      
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
ReLU                                                           
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
AdaptiveAvgPool2d                                              
AdaptiveMaxPool2d                                              
Flatten                                                        
BatchNorm1d                               2048       True      
Dropout                                                        
____________________________________________________________________________
                     64 x 512            
Linear                                    524288     True      
ReLU                                                           
BatchNorm1d                               1024       True      
Dropout                                                        
____________________________________________________________________________
                     64 x 10             
Linear                                    5120       True      
____________________________________________________________________________

Total params: 11,708,992
Total trainable params: 11,708,992
Total non-trainable params: 0

Optimizer used: <function Adam at 0x7fd7bc7e2e18>
Loss function: CrossEntropyLoss()

Callbacks:
  - TrainEvalCallback
  - Recorder
  - ProgressCallback
%%time
learner.fit_one_cycle(5)
epoch train_loss valid_loss accuracy time
0 1.889744 1.896039 0.365500 00:51
1 1.525709 1.402483 0.493500 00:52
2 1.160433 1.045405 0.641900 00:51
3 1.002535 0.890528 0.687900 00:51
4 0.874758 0.821122 0.709400 00:51
CPU times: user 2min 38s, sys: 3.3 s, total: 2min 42s
Wall time: 4min 18s