{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install MyVision","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Introduction to MyVision\nThis is a library I made to combine everything I <3 about PyTorch.\nMy goal is \"Do more with less code\". Well `MyVision` is a wrapper over PyTorch.\nThat means u must know PyTorch before working with it and if u know PyTorch you can yourself make any customizations. Just have at look at the source code on github.\n\nWith this aside let's start our example. It's the MNIST example as u might have guessed already :P","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# torch imports\nimport torch\nimport torchvision\nfrom torchvision import datasets\nfrom torchvision import transforms\nfrom torch.utils.data import DataLoader","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# standard \"Every ML/DL problem\" imports\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So, let me give u a brief overview of what `MyVision` offers:\nThere are two important things at the heart of it.\n1. Dataset\n2. Trainer\n<br>\nThe former one we will go through in another example.\nHere, we go through `Trainer`\n\nSo what is `Trainer`?\nSimply, Trainer provides trainig and validation methods, normally in PyTorch you have to write your\ncustom loop, which let me tell you, gives you ultimate customization. But I wanted to do something like what keras `.fit()` offers. So I decided to build it up.\n\nTrainer offers you this keras like `.fit()` magic. With proper parameters you can simply `.fit()` and *boom!* training begins.\n\nSo, let's import the specifics. \nOur `Trainer` is present in `MyVision.engine.Engine`","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import MyVision\nfrom MyVision.dataset.Dataset import DatasetUtils\nfrom MyVision.engine.Engine import Trainer","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Below we just make the two DataLoaders because as you know in PyTorch `DataLoader` is where the heavylifting takes place. Our trainer expects these DataLoaders\n<br>\n### **Disclaimer: I'm not using the data provided in this competetion, this notebook is just to introduce the library or wrapper I created using PyTorch called `MyVision`**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(\n    datasets.MNIST('data', train=True, download=True, transform=transforms.Compose([\n                           transforms.ToTensor(),\n                           transforms.Normalize((0.1307,), (0.3081,))\n                       ])),\n    batch_size=128, shuffle=True\n)\ntest_loader = torch.utils.data.DataLoader(\n    datasets.MNIST('data', train=False, transform=transforms.Compose([\n                           transforms.ToTensor(),\n                           transforms.Normalize((0.1307,), (0.3081,))\n                       ])),\n    batch_size=128, shuffle=True\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Next we do usual stuff i.e. define our `model`, `optimizer` & `loss` ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = torchvision.models.resnet18(pretrained=True)\nmodel.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\nmodel.fc = torch.nn.Linear(in_features=model.fc.in_features, out_features=10)\nloss = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adadelta(model.parameters(), lr=0.01)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Below is the part to what we have been building upto aka `Trainer`.\nLet's have a look at what the `Trainer` takes.\nRun the cell below to see:","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"?Trainer","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Trainer \n\nYou will see that out `Trainer` just takes in the usual stuff:\n1. Training, Validation & Test(if specified) DataLoaders\n2. device(either `cpu` or `cuda`)\n3. loss\n4. optimizer\n5. model\n6. learning rate scheduler(if you want)\n<br>\nWhatever you don't want just specify it as `None`.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"trainer = Trainer(\n    train_loader=train_loader,\n    val_loader=test_loader,\n    test_loader=None,\n    device='cuda',\n    loss=loss,\n    optimizer=optimizer,\n    model=model.to('cuda'),\n    lr_scheduler=None\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Finally, for the magic to begin specifiy number of epochs and the scheduler metric in the `.fit()`\nNow just run the cell below and we are off !","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"trainer.fit(5, 'accuracy')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As you must have seen most of it just regular PyTorch stuff but with lots of convinience. If you know PyTorch it's just a breeze for you to understand :) ","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Also `MyVision` creates a models folder in the directory where you run the script and saves the best models to it epoch-wise ","execution_count":null},{"metadata":{"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}