A dataset of 32x32 rgb images with 100 classes. https://www.cs.toronto.edu/~kriz/cifar.html
!pip install git+https://github.com/netbrainml/nbml.git
from IPython.display import clear_output
from nbml.workshops.cifar100.utils import *
from nbml.workshops.models.mlp import *
clear_output()
from keras.datasets import cifar100
from tensorflow import keras
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
x_train, x_test = x_train/255, x_test/255
shapes(x_train, x_test, y_train, y_test)
from torch.utils.data import DataLoader, TensorDataset
x_train, x_test = torch.Tensor(x_train).transpose(-1,1), torch.Tensor(x_test).transpose(-1,1)
y_train, y_test = torch.Tensor(y_train).squeeze(), torch.Tensor(y_test).squeeze()
tdl = DataLoader(TensorDataset(x_train, y_train), batch_size=64, shuffle=True)
vdl = DataLoader(TensorDataset(x_test, y_test), batch_size=64, shuffle=True)
def conv(ni,nf): return nn.Sequential(nn.Conv2d(ni,nf,3,1), nn.ReLU(inplace=True), nn.MaxPool2d(2,2))
class CNN(BasicTrainableClassifier):
def __init__(self, ni, nc):
super().__init__()
self.conv = nn.Sequential(nn.Conv2d(ni,128,3,1),
conv(128,256), conv(256,512),
conv(512,1024))
self.pool = nn.AdaptiveMaxPool2d(1)
self.fc = MLP(get_layers(1024,512,100,256))
def forward(self, X):
return self.fc(torch.flatten(self.pool(self.conv(X)),start_dim=1))
cnn_m = CNN(3,100).cuda()
cnn_m
cnn_m.fit(tdl, valid_ds=vdl, epochs=5,
cbs=True, learning_rate=1e-4)
cnn_m.plot