{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nimport torchvision as tv\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optims\nimport torchvision.transforms as transforms\nfrom PIL import Image\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n\n","metadata":{"_uuid":"3d4c015f-53b8-4057-9cf4-20316977c544","_cell_guid":"8172d2de-fe68-45b0-a8f4-916f967c2092","scrolled":true,"execution":{"iopub.status.busy":"2023-03-31T02:15:20.49034Z","iopub.execute_input":"2023-03-31T02:15:20.49096Z","iopub.status.idle":"2023-03-31T02:15:21.969232Z","shell.execute_reply.started":"2023-03-31T02:15:20.490918Z","shell.execute_reply":"2023-03-31T02:15:21.968151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_image(src, size=(128, 128), bgc=\"white\"):\n    src.thumbnail(size, Image.ANTIALIAS)\n    \n    new_image = Image.new(\"RGB\", size, bgc)\n    \n    new_image.paste(src, (int((size[0]-src.size[0]) / 2)), int((size[1] - src.size[1]) / 2))\n    \n    return new_image","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:15:21.971477Z","iopub.execute_input":"2023-03-31T02:15:21.972114Z","iopub.status.idle":"2023-03-31T02:15:21.980078Z","shell.execute_reply.started":"2023-03-31T02:15:21.972071Z","shell.execute_reply":"2023-03-31T02:15:21.978832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize([256, 256]),\n    transforms.RandomHorizontalFlip(),\n    transforms.RandomVerticalFlip(),\n    transforms.ColorJitter(brightness=0.5, contrast=0),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:15:21.98138Z","iopub.execute_input":"2023-03-31T02:15:21.98231Z","iopub.status.idle":"2023-03-31T02:15:21.990821Z","shell.execute_reply.started":"2023-03-31T02:15:21.982273Z","shell.execute_reply":"2023-03-31T02:15:21.989706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load(path):\n    dataset = tv.datasets.ImageFolder(root=path,transform = transform)\n    \n    train_size = int(0.7 * len(dataset))\n    test_size = len(dataset) - train_size\n    \n    train_set, test_set = torch.utils.data.random_split(dataset, [train_size, test_size])\n    \n    train_loader = torch.utils.data.DataLoader(\n        train_set,\n        batch_size=50,\n        num_workers=0,\n        shuffle=False\n    )\n    \n    test_loader = torch.utils.data.DataLoader(\n        test_set,\n        batch_size=50,\n        num_workers=0,\n        shuffle=False\n    )\n\n    return train_loader, test_loader\n\ntrain_loader, test_loader = load('/kaggle/input/imagenetmini-1000/imagenet-mini/train')\nprint(f\"train loaders: {train_loader}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:15:21.993238Z","iopub.execute_input":"2023-03-31T02:15:21.994549Z","iopub.status.idle":"2023-03-31T02:15:32.232847Z","shell.execute_reply.started":"2023-03-31T02:15:21.994509Z","shell.execute_reply":"2023-03-31T02:15:32.231694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\nmodel = tv.models.resnet50(weights=tv.models.ResNet50_Weights.DEFAULT)\n\nmodel = model.to(device)\n\ncriterion = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.05)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:15:32.234874Z","iopub.execute_input":"2023-03-31T02:15:32.235753Z","iopub.status.idle":"2023-03-31T02:15:36.504655Z","shell.execute_reply.started":"2023-03-31T02:15:32.235695Z","shell.execute_reply":"2023-03-31T02:15:36.503436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = []\n\nfor epoch, (inputs, labels) in enumerate(train_loader):\n    print(f'Epoch {epoch+1}')  \n          \n    # Move input and label tensors to the device\n    inputs = inputs.to(device)\n    labels = labels.to(device)\n\n    # Zero out the optimizer\n    optimizer.zero_grad()\n\n    # Forward pass\n    outputs = model(inputs)\n    print(f'Outputs: {outputs}')\n    loss.append(criterion(outputs, labels))\n\n    # Backward pass\n    loss[epoch].backward()\n    optimizer.step()\n\n    # Print the loss for every epoch\n    print(f'Loss: {loss[epoch].item():.4f}')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:15:36.511243Z","iopub.execute_input":"2023-03-31T02:15:36.515319Z","iopub.status.idle":"2023-03-31T02:24:58.086994Z","shell.execute_reply.started":"2023-03-31T02:15:36.515273Z","shell.execute_reply":"2023-03-31T02:24:58.085604Z"},"_kg_hide-output":true,"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nloss = [round(i.item(), 4) for i in loss]\nx, y = [i for i in range(len(loss))], loss\n\nfix, ax = plt.subplots()\nax.plot(x, y)\nax.set_xlabel(\"Epoch\")\nax.set_ylabel(\"Loss\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:24:58.08894Z","iopub.execute_input":"2023-03-31T02:24:58.089733Z","iopub.status.idle":"2023-03-31T02:24:58.401048Z","shell.execute_reply.started":"2023-03-31T02:24:58.089689Z","shell.execute_reply":"2023-03-31T02:24:58.399785Z"},"trusted":true},"execution_count":null,"outputs":[]}]}