import logging,pickle,gzip,os,time,shutil,torch,matplotlib as mpl
from pathlib import Path
from torch import tensor,nn,optim
from torch.utils.data import DataLoader
import torch.nn.functional as F
from datasets import load_dataset,load_dataset_builder
import torchvision.transforms.functional as TF
from fastcore.test import test_close
This is not my content it’s a part of Fastai’s From Deep Learning Foundations to Stable Diffusion course. I add some notes for me to understand better thats all. For the source check Fastai course page.
Datasets, Dataloaders
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
from __future__ import annotations
import math,numpy as np,matplotlib.pyplot as plt
from operator import itemgetter
from itertools import zip_longest
import fastcore.all as fc
from torch.utils.data import default_collate
from miniai.training import *
:::
=2, linewidth=140, sci_mode=False)
torch.set_printoptions(precision1)
torch.manual_seed('image.cmap'] = 'gray' mpl.rcParams[
logging.disable(logging.WARNING)
Hugging Face Datasets
= "fashion_mnist"
name = load_dataset_builder(name)
ds_builder print(ds_builder.info.description)
ds_builder.info.features
ds_builder.info.splits
= load_dataset(name)
dsd dsd
= dsd['train'],dsd['test']
train,test 0] train[
= ds_builder.info.features x,y
x,y
= 'image','label'
x,y = train[0][x]
img img
= train[:5][x]
xb = train[:5][y]
yb yb
= train.features[y]
featy featy
featy.int2str(yb)
'label'][:5] train[
def collate_fn(b):
return {x:torch.stack([TF.to_tensor(o[x]) for o in b]),
for o in b])} y:tensor([o[y]
= DataLoader(train, collate_fn=collate_fn, batch_size=16)
dl = next(iter(dl))
b b[x].shape,b[y]
def transforms(b):
= [TF.to_tensor(o) for o in b[x]]
b[x] return b
= train.with_transform(transforms)
tds = DataLoader(tds, batch_size=16)
dl = next(iter(dl))
b b[x].shape,b[y]
def _transformi(b): b[x] = [torch.flatten(TF.to_tensor(o)) for o in b[x]]
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
def inplace(f):
def _f(b):
f(b)return b
return _f
:::
= inplace(_transformi) transformi
= train.with_transform(transformi)[0]
r r[x].shape,r[y]
@inplace
def transformi(b): b[x] = [torch.flatten(TF.to_tensor(o)) for o in b[x]]
= train.with_transform(transformi)
tdsf = tdsf[0]
r r[x].shape,r[y]
= dict(a=1,b=2,c=3)
d = itemgetter('a','c')
ig ig(d)
class D:
def __getitem__(self, k): return 1 if k=='a' else 2 if k=='b' else 3
= D()
d ig(d)
list(tdsf.features)
= dict(a=[1],b=[2]), dict(a=[3],b=[4])
batch default_collate(batch)
miniai
around 1:17 there is an explanation of miniai and its installation
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
def collate_dict(ds):
= itemgetter(*ds.features)
get def _f(b): return get(default_collate(b))
return _f
:::
= DataLoader(tdsf, batch_size=4, collate_fn=collate_dict(tdsf))
dlf = next(iter(dlf))
xb,yb xb.shape,yb
Plotting images
= next(iter(dl))
b = b['image']
xb = xb[0]
img 0]); plt.imshow(img[
**kwargs
@fc.delegates
makes imshow kwargs visible. Great.
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
@fc.delegates(plt.Axes.imshow)
def show_image(im, ax=None, figsize=None, title=None, noframe=True, **kwargs):
"Show a PIL or PyTorch image on `ax`."
if fc.hasattrs(im, ('cpu','permute','detach')):
= im.detach().cpu()
im if len(im.shape)==3 and im.shape[0]<5: im=im.permute(1,2,0)
elif not isinstance(im,np.ndarray): im=np.array(im)
if im.shape[-1]==1: im=im[...,0]
if ax is None: _,ax = plt.subplots(figsize=figsize)
**kwargs)
ax.imshow(im, if title is not None: ax.set_title(title)
ax.set_xticks([])
ax.set_yticks([]) if noframe: ax.axis('off')
return ax
:::
help(show_image)
=(2,2)); show_image(img, figsize
= plt.subplots(1,2)
fig,axs 0])
show_image(img, axs[1], axs[1]); show_image(xb[
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
@fc.delegates(plt.subplots, keep=True)
def subplots(
int=1, # Number of rows in returned axes grid
nrows:int=1, # Number of columns in returned axes grid
ncols:tuple=None, # Width, height in inches of the returned figure
figsize:int=3, # Size (in inches) of images that will be displayed in the returned figure
imsize:str=None, # Title to be set to returned figure
suptitle:**kwargs
# fig and axs
): "A figure and set of subplots to display images of `imsize` inches"
if figsize is None: figsize=(ncols*imsize, nrows*imsize)
= plt.subplots(nrows, ncols, figsize=figsize, **kwargs)
fig,ax if suptitle is not None: fig.suptitle(suptitle)
if nrows*ncols==1: ax = np.array([ax])
return fig,ax
:::
from nbdev.showdoc import show_doc
show_doc(subplots)
= subplots(3,3, imsize=1)
fig,axs = xb[:8]
imgs for ax,img in zip(axs.flat,imgs): show_image(img, ax)
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
@fc.delegates(subplots)
def get_grid(
int, # Number of axes
n:int=None, # Number of rows, defaulting to `int(math.sqrt(n))`
nrows:int=None, # Number of columns, defaulting to `ceil(n/rows)`
ncols:str=None, # If passed, title set to the figure
title:str='bold', # Title font weight
weight:int=14, # Title font size
size:**kwargs,
# fig and axs
): "Return a grid of `n` axes, `rows` by `cols`"
if nrows: ncols = ncols or int(np.floor(n/nrows))
elif ncols: nrows = nrows or int(np.ceil(n/ncols))
else:
= int(math.sqrt(n))
nrows = int(np.floor(n/nrows))
ncols = subplots(nrows, ncols, **kwargs)
fig,axs for i in range(n, nrows*ncols): axs.flat[i].set_axis_off()
if title is not None: fig.suptitle(title, weight=weight, size=size)
return fig,axs
:::
= get_grid(8, nrows=3, imsize=1)
fig,axs for ax,img in zip(axs.flat,imgs): show_image(img, ax)
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
@fc.delegates(subplots)
def show_images(ims:list, # Images to show
int|None=None, # Number of rows in grid
nrows:int|None=None, # Number of columns in grid (auto-calculated if None)
ncols:list|None=None, # Optional list of titles for each image
titles:**kwargs):
"Show all images `ims` as subplots with `rows` using `titles`"
= get_grid(len(ims), nrows, ncols, **kwargs)[1].flat
axs for im,t,ax in zip_longest(ims, titles or [], axs): show_image(im, ax=ax, title=t)
:::
= b['label']
yb = yb[:8] lbls
= "Top Trouser Pullover Dress Coat Sandal Shirt Sneaker Bag Boot".split()
names = itemgetter(*lbls)(names)
titles ' '.join(titles)
=1.7, titles=titles) show_images(imgs, imsize
::: {.cell 0=‘e’ 1=‘x’ 2=‘p’ 3=‘o’ 4=‘r’ 5=‘t’}
class DataLoaders:
def __init__(self, *dls): self.train,self.valid = dls[:2]
@classmethod
def from_dd(cls, dd, batch_size, as_tuple=True, **kwargs):
= collate_dict(dd['train'])
f return cls(*get_dls(*dd.values(), bs=batch_size, collate_fn=f))
:::
Export -
import nbdev; nbdev.nbdev_export()