{"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)\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-24T19:26:21.133595Z","iopub.execute_input":"2022-11-24T19:26:21.134602Z","iopub.status.idle":"2022-11-24T19:26:21.158105Z","shell.execute_reply.started":"2022-11-24T19:26:21.134511Z","shell.execute_reply":"2022-11-24T19:26:21.157128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torchvision import transforms,datasets\nfrom torch.utils.data import DataLoader,SubsetRandomSampler\nimport torch.nn.functional as F\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-11-24T19:26:48.480118Z","iopub.execute_input":"2022-11-24T19:26:48.48061Z","iopub.status.idle":"2022-11-24T19:26:50.616847Z","shell.execute_reply.started":"2022-11-24T19:26:48.480567Z","shell.execute_reply":"2022-11-24T19:26:50.615883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(torch.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-11-24T19:26:51.677177Z","iopub.execute_input":"2022-11-24T19:26:51.677657Z","iopub.status.idle":"2022-11-24T19:26:51.684066Z","shell.execute_reply.started":"2022-11-24T19:26:51.677626Z","shell.execute_reply":"2022-11-24T19:26:51.682354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device =  'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2022-11-24T19:26:52.407121Z","iopub.execute_input":"2022-11-24T19:26:52.408031Z","iopub.status.idle":"2022-11-24T19:26:52.484314Z","shell.execute_reply.started":"2022-11-24T19:26:52.407982Z","shell.execute_reply":"2022-11-24T19:26:52.483261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(device)","metadata":{"execution":{"iopub.status.busy":"2022-11-24T19:26:52.876026Z","iopub.execute_input":"2022-11-24T19:26:52.876402Z","iopub.status.idle":"2022-11-24T19:26:52.882269Z","shell.execute_reply.started":"2022-11-24T19:26:52.876366Z","shell.execute_reply":"2022-11-24T19:26:52.881031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_transfor =  transforms.Compose([\n    transforms.Resize((224,224)),\n    transforms.ToTensor()\n])","metadata":{"execution":{"iopub.status.busy":"2022-11-24T19:26:53.2959Z","iopub.execute_input":"2022-11-24T19:26:53.296972Z","iopub.status.idle":"2022-11-24T19:26:53.302422Z","shell.execute_reply.started":"2022-11-24T19:26:53.29693Z","shell.execute_reply":"2022-11-24T19:26:53.301242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = datasets.ImageFolder('/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train',image_transfor)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-24T19:26:54.191983Z","iopub.execute_input":"2022-11-24T19:26:54.192826Z","iopub.status.idle":"2022-11-24T19:49:05.926707Z","shell.execute_reply.started":"2022-11-24T19:26:54.192792Z","shell.execute_reply":"2022-11-24T19:49:05.925651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 1000000\n#print(classes [ datset[idx][1]])\nplt.imshow(train_dataset[idx][0].permute(2,1,0).numpy())","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:18:18.625381Z","iopub.execute_input":"2022-11-24T20:18:18.62576Z","iopub.status.idle":"2022-11-24T20:18:18.934826Z","shell.execute_reply.started":"2022-11-24T20:18:18.62571Z","shell.execute_reply":"2022-11-24T20:18:18.933793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.class_to_idx;","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:18:32.845825Z","iopub.execute_input":"2022-11-24T20:18:32.84618Z","iopub.status.idle":"2022-11-24T20:18:32.851018Z","shell.execute_reply.started":"2022-11-24T20:18:32.846149Z","shell.execute_reply":"2022-11-24T20:18:32.849877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indeces =  list(range(len(train_dataset)))\nval_split_idx = int (np.floor(0.2 * len(train_dataset) ))\ntrain_idx ,  val_idx = indeces[val_split_idx : ], indeces[0:val_split_idx]\ntrain_smapler =  SubsetRandomSampler(train_idx)\nval_smapler =  SubsetRandomSampler(val_idx)","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:18:35.3479Z","iopub.execute_input":"2022-11-24T20:18:35.348285Z","iopub.status.idle":"2022-11-24T20:18:35.412566Z","shell.execute_reply.started":"2022-11-24T20:18:35.348253Z","shell.execute_reply":"2022-11-24T20:18:35.411617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader =  DataLoader(train_dataset , 32 , shuffle =False  , sampler = train_smapler)\nval_loader =  DataLoader(train_dataset , 32 , shuffle =False , sampler =val_smapler  )","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:18:36.629036Z","iopub.execute_input":"2022-11-24T20:18:36.629392Z","iopub.status.idle":"2022-11-24T20:18:36.634493Z","shell.execute_reply.started":"2022-11-24T20:18:36.629351Z","shell.execute_reply":"2022-11-24T20:18:36.633395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.class_to_idx","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:23:11.985431Z","iopub.execute_input":"2022-11-24T20:23:11.985888Z","iopub.status.idle":"2022-11-24T20:23:12.019115Z","shell.execute_reply.started":"2022-11-24T20:23:11.985845Z","shell.execute_reply":"2022-11-24T20:23:12.017945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x,y in train_loader:\n    print(y)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:19:50.655323Z","iopub.execute_input":"2022-11-24T20:19:50.655679Z","iopub.status.idle":"2022-11-24T20:19:53.498238Z","shell.execute_reply.started":"2022-11-24T20:19:50.65565Z","shell.execute_reply":"2022-11-24T20:19:53.496415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SReluModel(nn.Module):\n    def __init__(self):\n        super(SReluModel,self).__init__()\n        self.block1 =  self.conv_block(cin = 3 , cout = 5 , dropout = 0.1 ,kernel_size=3 , stride =1  ,padding =1  )\n        self.block2 =  self.conv_block(cin = 5 , cout = 15 , dropout = 0.1 ,kernel_size=3)\n        self.maxp = nn.MaxPool2d(2)\n        self.fc1 = nn.Linear(in_features = 726000 , out_features =  1000)\n    def conv_block(self, cin, cout, dropout , **kwargs):\n        seq_block = nn.Sequential(\n                nn.Conv2d(cin , cin ,**kwargs),\n                nn.BatchNorm2d(cin),\n                nn.Conv2d(cin , cout ,**kwargs),\n                nn.BatchNorm2d(cout),\n                #nn.ReLU(cout)\n                )\n        return seq_block\n    def forward(self,x):\n        x = self.block1(x)\n        x0 = nn.ReLU()(x)\n        x1 = nn.Sigmoid()(x)\n        x =  x0 * x1 + x1\n        x = self.block2(x)\n        x0 = nn.ReLU()(x)\n        x1 = nn.Sigmoid()(x)\n        x =  x0 * x1 + x1\n        x = x.view(x.size()[0],-1)\n        x = self.fc1(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:24:04.339802Z","iopub.execute_input":"2022-11-24T20:24:04.34017Z","iopub.status.idle":"2022-11-24T20:24:04.350448Z","shell.execute_reply.started":"2022-11-24T20:24:04.34014Z","shell.execute_reply":"2022-11-24T20:24:04.349449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model =  SReluModel()\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:24:14.612038Z","iopub.execute_input":"2022-11-24T20:24:14.612398Z","iopub.status.idle":"2022-11-24T20:24:25.400647Z","shell.execute_reply.started":"2022-11-24T20:24:14.612362Z","shell.execute_reply":"2022-11-24T20:24:25.399676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer= torch.optim.SGD(model.parameters() , lr= 0.01)","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:24:28.892989Z","iopub.execute_input":"2022-11-24T20:24:28.893447Z","iopub.status.idle":"2022-11-24T20:24:28.899121Z","shell.execute_reply.started":"2022-11-24T20:24:28.893408Z","shell.execute_reply":"2022-11-24T20:24:28.898003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def binary_acc(y_pred , y_test):\n    y_pred_tag =  torch.log_softmax(y_pred , dim =1)\n    _ , tag = torch.max(y_pred_tag ,  dim = 1)\n    correct_sum =  (tag == y_test).sum().float()\n    acc =  correct_sum / y_test.shape[0]\n    acc =  torch.round(acc *100)\n    return acc","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:24:42.814676Z","iopub.execute_input":"2022-11-24T20:24:42.81541Z","iopub.status.idle":"2022-11-24T20:24:42.821302Z","shell.execute_reply.started":"2022-11-24T20:24:42.81537Z","shell.execute_reply":"2022-11-24T20:24:42.820178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny = model(torch.rand(3,224,224).unsqueeze(0).to(device))\ny","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:24:47.764874Z","iopub.execute_input":"2022-11-24T20:24:47.765265Z","iopub.status.idle":"2022-11-24T20:24:54.096691Z","shell.execute_reply.started":"2022-11-24T20:24:47.765232Z","shell.execute_reply":"2022-11-24T20:24:54.09568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs  =1\nfor i in range (epochs):\n    train_epoch_loss =  0\n    train_epoch_acc = 0\n    val_epoch_loss =  0\n    val_epoch_acc = 0\n    model.train()\n    for j, (x,y) in enumerate (train_loader):\n        x = x.to(device)\n        y = y.to(device)\n        pred = model(x)\n        loss = criterion(pred ,y)\n        acc = binary_acc(pred,y)\n        loss.backward()\n        optimizer.step()\n        optimizer.zero_grad()\n        train_epoch_loss +=loss.item()\n        train_epoch_acc += acc.item()\n        if (j!=0 and j%1000== 0):\n            print(f'batch {j}:  Train loss: {train_epoch_loss/ j} | Train accuracy: {train_epoch_acc/ j} ')\n    with torch.no_grad():\n        model.eval()\n        for x,y in val_loader:\n            x = x.to(device)\n            y = y.to(device)\n            pred = model(x)\n            acc = binary_acc(pred,y.type(torch.float32))\n            optimizer.step()\n            optimizer.zero_grad()\n            val_epoch_acc += acc.item()\n    print(f'epoch {i}:  Train loss: {train_epoch_loss/ len (train_loader)} | Train accuracy: {train_epoch_acc/ len (train_loader)} | val accuracy: {val_epoch_acc / len (val_loader)}')","metadata":{"execution":{"iopub.status.busy":"2022-11-24T20:27:11.726537Z","iopub.execute_input":"2022-11-24T20:27:11.727324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}