{"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":"import pytorch_lightning as pl\nimport torch\nimport torchvision\nimport pandas as pd\nimport numpy as np\nimport torchvision.models as models","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-08T08:26:31.061966Z","iopub.execute_input":"2022-01-08T08:26:31.062409Z","iopub.status.idle":"2022-01-08T08:26:34.388277Z","shell.execute_reply.started":"2022-01-08T08:26:31.062291Z","shell.execute_reply":"2022-01-08T08:26:34.387482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_csv(path):\n    df=pd.read_csv(path)\n    return df\n\npd.set_option('max_colwidth', 100)\n\ndef get_dfs():\n    \n    train_path='../input/state-farm-distracted-driver-detection/driver_imgs_list.csv'\n    test_path = '../input/state-farm-distracted-driver-detection/sample_submission.csv'\n    train_df=read_csv(train_path)\n    test_df=read_csv(test_path)\n    train_base_path='../input/state-farm-distracted-driver-detection/imgs/train'\n    train_df['path']= train_base_path +'/'+ train_df.classname +'/'+train_df.img\n    \n    return train_df,test_df\n\n\ntrain_df,test_df=get_dfs()\n\ndisplay(train_df)\ndisplay(test_df)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-08T08:26:34.390074Z","iopub.execute_input":"2022-01-08T08:26:34.390825Z","iopub.status.idle":"2022-01-08T08:26:34.672408Z","shell.execute_reply.started":"2022-01-08T08:26:34.390785Z","shell.execute_reply":"2022-01-08T08:26:34.671676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_file(path):\n    \n    img=torchvision.io.decode_jpeg(path)\n    Resize=torchvision.transforms.Resize(size)\n    img=Resize(img)\n    \n    return img\n\n\nclass Dataset(torch.utils.data.Dataset):\n    def __init__(self,paths,labels):\n        \n        self.df=labels\n        self.labels=paths\n        \n        \n        \n    def __len__(self):\n        return len(self.labels)\n        \n        \n        \n    def __getitem__(self,idx):\n        \n        img=get_label(self.paths[idx])\n        \n        label=self.labels[idx]\n        \n        return {'img':img , 'label': label}\n    \n\n    \nclass DataGen(pl.LightningDataModule):\n    \n    def __init__(self,ds,train_paths,val_paths,label,val_labels,batch,shuffle):\n        self.ds=ds\n        self.labels=labels\n        self.train_paths=train_paths\n        self.val_paths=val_paths\n        self.val_labels=val_labels\n        self.batch=batch\n        self.shuffle=shuffle\n        \n        \n    def train_dataloader(self):\n        \n        ds=self.ds(self.train_paths,self.labels)\n        \n        return torch.utils.data.DataLoader(ds,self.batch,self.shuffle)\n    \n    def val_dataloader(self):\n        \n        ds=self.ds(self.val_paths,self.val_labels)\n        \n        return torch.utils.data.DataLoader(ds,self.batch,False)\n    \n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-08T08:26:34.673950Z","iopub.execute_input":"2022-01-08T08:26:34.674274Z","iopub.status.idle":"2022-01-08T08:26:34.686246Z","shell.execute_reply.started":"2022-01-08T08:26:34.674233Z","shell.execute_reply":"2022-01-08T08:26:34.685390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Classifier(pl.LightningModule):\n    \n    def __init__(\n                self,\n                model ,\n                batch_size , \n                shuffle_while_train,\n                \n            ):\n        self.model=model\n        self.shuffle=shuffle_while_train\n        self.batch=batch\n        \n        \n    def training_step(self, batch,batch_index):\n        \n        x,y=batch\n        perdictions=self.model(x)\n        loss=get_loss(pred,y)\n        \n        return {'loss': loss}\n    \n    def validaation_step(self,batch,batch_index):\n        x,y=batch\n        pred=self.model(x)\n        loss=get_loss(pred,y)\n        \n        return {'val_loss': loss}\n    \n    def training_epoch(self, training_step_outputs):\n        loss=0\n        for values in trianing_step_outputs:\n            loss+=values['loss']\n            \n        \n        \n        \n    def validation_epoch(self, validation_step_outputs):\n        loss=0\n        for values in validation_step_outputs:\n            loss+=values['val_loss']\n            \n            \nclass Model(pl.LightningModule):\n    def __init__(\n\n                    self,\n                    layers,\n                    in_channels,\n                    num_classes\n        \n                ):\n        self.Conv1d= torch.nn.Conv2d(in_channels, 4)\n        self.Flatten = torch.nn.Flatten()\n        slef.Prediction_layer= torch.nn.Linear(num_classes)\n        \n    \n    \n    def forward(self,x):\n        out=self.Conv1d(x)\n\n        for i in range(self.layers):\n            out=self.Conv1d(out)\n            \n        out=self.Flatten(out)\n        out=self.Prediction_layer(out)\n        \n        return out","metadata":{"execution":{"iopub.status.busy":"2022-01-08T08:38:12.368743Z","iopub.execute_input":"2022-01-08T08:38:12.369512Z","iopub.status.idle":"2022-01-08T08:38:12.382668Z","shell.execute_reply.started":"2022-01-08T08:38:12.369474Z","shell.execute_reply":"2022-01-08T08:38:12.381707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}