<jupyter_start><jupyter_text>Transfer Learning CNNs with PyTorch Lightning !  Install Quickvision !<jupyter_code>!pip install -q git+https://github.com/Quick-AI/quickvision.git
import pytorch_lightning as pl
import torch.optim as optim
import torchvision.transforms as T
from torch.nn import functional as F
from torch.utils.data import DataLoader
import torch
import torchvision
from quickvision.models.classification.cnn import lit_cnn<jupyter_output><empty_output><jupyter_text>Create Datasets and DataLoaders<jupyter_code>train_transforms = T.Compose([T.ToTensor(), T.Normalize((0.5,), (0.5,))])
valid_transforms = T.Compose([T.ToTensor(), T.Normalize((0.5,), (0.5,))])
train_dataset = torchvision.datasets.CIFAR10("./data", download=True, train=True, transform=train_transforms)
valid_dataset = torchvision.datasets.CIFAR10("./data", download=True, train=False, transform=valid_transforms)
TRAIN_BATCH_SIZE = 512  # Training Batch Size
VALID_BATCH_SIZE = 512  # Validation Batch Size
train_loader = DataLoader(train_dataset, TRAIN_BATCH_SIZE, shuffle=True)
valid_loader = DataLoader(valid_dataset, VALID_BATCH_SIZE, shuffle=False)<jupyter_output><empty_output><jupyter_text>Creating Model and Training ! - Create a model with pretrained imagenet weights<jupyter_code>model_imagenet = lit_cnn("resnet18", num_classes=10, pretrained="imagenet")<jupyter_output><empty_output><jupyter_text>- You can pass all the possible Trainer Arguments.- Quickvision Does not overwrite any !<jupyter_code>trainer = pl.Trainer(max_epochs=2, gpus=1)
trainer.fit(model_imagenet, train_loader, valid_loader)<jupyter_output><empty_output><jupyter_text>- Training without any pretrained weights.<jupyter_code>model_ssl = lit_cnn("resnet18", num_classes=10, pretrained=None)
trainer = pl.Trainer(max_epochs=2, gpus=1)
trainer.fit(model_ssl, train_loader, valid_loader)<jupyter_output><empty_output><jupyter_text>Custom Training with Lightning ! - To write your own Training logic, metrics, logging. Subclass the `lit_cnn` and write your own logic !<jupyter_code>class CustomTraining(lit_cnn):
    def training_step(self, batch, batch_idx):
        images, targets = batch
        outputs = self.forward(images)
        train_loss = F.cross_entropy(outputs, targets, reduction='sum')
        # Possible we can compute top-1 and top-5 accuracy here.
        return {"loss": train_loss}

    def validation_step(self, batch, batch_idx):
        images, targets = batch
        outputs = self.forward(images)
        val_loss = F.cross_entropy(outputs, targets, reduction='sum')
        # Possible we can compute top-1 and top-5 accuracy here.
        return {"loss": val_loss}

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=self.learning_rate)<jupyter_output><empty_output><jupyter_text>- Create Model provided by Quickvision !<jupyter_code>model_imagenet = CustomTraining("resnet18", num_classes=10, pretrained="imagenet")<jupyter_output><empty_output><jupyter_text>- Train with PL Trainer !<jupyter_code>trainer = pl.Trainer(max_epochs=2, gpus=1)
trainer.fit(model_imagenet, train_loader, valid_loader)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10709759188846642, 'fraction_numerical': 0.012674271229404309, 'mean_word_length': 6.534606205250597, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '649709', 'license': 'Apache-2.0', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1030, 'n_words': 221, 'path': 'examples/notebooks/Transfer_Learning_with_PyTorch_Lightning.ipynb', 'repo_name': 'zlapp/quickvision', 'size': '3156'}