{"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":"# Importing GPU libraries\nimport cudf as pd\nimport cupy as np\nimport cuml","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:36:59.027895Z","iopub.execute_input":"2022-03-16T16:36:59.028249Z","iopub.status.idle":"2022-03-16T16:37:02.944197Z","shell.execute_reply.started":"2022-03-16T16:36:59.028205Z","shell.execute_reply":"2022-03-16T16:37:02.943441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cuml.naive_bayes import GaussianNB\nfrom cuml.svm import SVC\nfrom cuml.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:02.946011Z","iopub.execute_input":"2022-03-16T16:37:02.946270Z","iopub.status.idle":"2022-03-16T16:37:02.950344Z","shell.execute_reply.started":"2022-03-16T16:37:02.946236Z","shell.execute_reply":"2022-03-16T16:37:02.949478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:02.951643Z","iopub.execute_input":"2022-03-16T16:37:02.952042Z","iopub.status.idle":"2022-03-16T16:37:03.618361Z","shell.execute_reply.started":"2022-03-16T16:37:02.951986Z","shell.execute_reply":"2022-03-16T16:37:03.617461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Importing the required libraries****","metadata":{}},{"cell_type":"code","source":"\nfrom PIL import Image\n#import torch\nimport matplotlib.pyplot as plt\nimport csv\n#import torch.nn.functional as F\n#from torch import nn\nimport os\nimport random\nimport time\n%matplotlib inline\nfrom datetime import datetime\nfrom torchvision import datasets, transforms, models","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:03.621220Z","iopub.execute_input":"2022-03-16T16:37:03.622169Z","iopub.status.idle":"2022-03-16T16:37:05.127406Z","shell.execute_reply.started":"2022-03-16T16:37:03.622135Z","shell.execute_reply":"2022-03-16T16:37:05.126509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cuml.model_selection import train_test_split\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:05.130071Z","iopub.execute_input":"2022-03-16T16:37:05.130568Z","iopub.status.idle":"2022-03-16T16:37:05.134746Z","shell.execute_reply.started":"2022-03-16T16:37:05.130531Z","shell.execute_reply":"2022-03-16T16:37:05.133833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score\nfrom sklearn.datasets import load_files\nfrom keras.utils import np_utils\nfrom keras.utils.vis_utils import plot_model\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.models import Sequential\nfrom sklearn.metrics import confusion_matrix\nfrom keras.preprocessing import image                  \nimport pickle\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras import Model\nfrom keras.layers import Input, GlobalAveragePooling2D, BatchNormalization, Dropout, Dense\nfrom tensorflow.keras.applications import EfficientNetB3\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:05.136330Z","iopub.execute_input":"2022-03-16T16:37:05.136880Z","iopub.status.idle":"2022-03-16T16:37:10.077917Z","shell.execute_reply.started":"2022-03-16T16:37:05.136842Z","shell.execute_reply":"2022-03-16T16:37:10.077165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/state-farm-distracted-driver-detection/sample_submission.csv') ","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:10.079082Z","iopub.execute_input":"2022-03-16T16:37:10.079325Z","iopub.status.idle":"2022-03-16T16:37:18.158801Z","shell.execute_reply.started":"2022-03-16T16:37:10.079293Z","shell.execute_reply":"2022-03-16T16:37:18.157898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.160371Z","iopub.execute_input":"2022-03-16T16:37:18.160650Z","iopub.status.idle":"2022-03-16T16:37:18.218194Z","shell.execute_reply.started":"2022-03-16T16:37:18.160611Z","shell.execute_reply":"2022-03-16T16:37:18.217453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/state-farm-distracted-driver-detection/driver_imgs_list.csv')   \ntrain.head(10) ","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.219386Z","iopub.execute_input":"2022-03-16T16:37:18.219658Z","iopub.status.idle":"2022-03-16T16:37:18.251444Z","shell.execute_reply.started":"2022-03-16T16:37:18.219608Z","shell.execute_reply":"2022-03-16T16:37:18.250665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.252957Z","iopub.execute_input":"2022-03-16T16:37:18.253240Z","iopub.status.idle":"2022-03-16T16:37:18.270084Z","shell.execute_reply.started":"2022-03-16T16:37:18.253204Z","shell.execute_reply":"2022-03-16T16:37:18.269268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['img'].unique()\n#train['classname'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.271542Z","iopub.execute_input":"2022-03-16T16:37:18.271825Z","iopub.status.idle":"2022-03-16T16:37:18.294576Z","shell.execute_reply.started":"2022-03-16T16:37:18.271791Z","shell.execute_reply":"2022-03-16T16:37:18.293751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"da =train['classname'].unique()\nda","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.296049Z","iopub.execute_input":"2022-03-16T16:37:18.296315Z","iopub.status.idle":"2022-03-16T16:37:18.306405Z","shell.execute_reply.started":"2022-03-16T16:37:18.296277Z","shell.execute_reply":"2022-03-16T16:37:18.305476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['classname'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.307767Z","iopub.execute_input":"2022-03-16T16:37:18.308493Z","iopub.status.idle":"2022-03-16T16:37:18.342572Z","shell.execute_reply.started":"2022-03-16T16:37:18.308455Z","shell.execute_reply":"2022-03-16T16:37:18.341789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking whether dataset is balanced or not","metadata":{}},{"cell_type":"code","source":"print(train['classname'].value_counts())\npd.DataFrame(train['classname'].value_counts()).to_pandas().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.346463Z","iopub.execute_input":"2022-03-16T16:37:18.346684Z","iopub.status.idle":"2022-03-16T16:37:18.626971Z","shell.execute_reply.started":"2022-03-16T16:37:18.346659Z","shell.execute_reply":"2022-03-16T16:37:18.626283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:18.628076Z","iopub.execute_input":"2022-03-16T16:37:18.628323Z","iopub.status.idle":"2022-03-16T16:37:19.275288Z","shell.execute_reply.started":"2022-03-16T16:37:18.628288Z","shell.execute_reply":"2022-03-16T16:37:19.274484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:19.276491Z","iopub.execute_input":"2022-03-16T16:37:19.276822Z","iopub.status.idle":"2022-03-16T16:37:19.282908Z","shell.execute_reply.started":"2022-03-16T16:37:19.276785Z","shell.execute_reply":"2022-03-16T16:37:19.281956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Observation:\n\n There are total 22424 training samples\n\n The training dataset is equally balanced to a great extent and hence we do not require any downsampling of the data","metadata":{}},{"cell_type":"code","source":"# my goal is to predict the likelihood of what the driver is doing in each picture. \n\n# The 10 classes to predict are:\n\n#  c0: safe driving\n#  c1: texting - right\n#  c2: talking on the phone - right\n#  c3: texting - left\n#  c4: talking on the phone - left\n#  c5: operating the radio\n#  c6: drinking\n#  c7: reaching behind\n#  c8: hair and makeup\n#  c9: talking to passenger","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:19.284605Z","iopub.execute_input":"2022-03-16T16:37:19.284943Z","iopub.status.idle":"2022-03-16T16:37:19.291983Z","shell.execute_reply.started":"2022-03-16T16:37:19.284900Z","shell.execute_reply":"2022-03-16T16:37:19.291133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting into 64*64 images","metadata":{}},{"cell_type":"markdown","source":"We can also substitute 64,64 to 224,224 for better results only if ram is > 32 GB","metadata":{}},{"cell_type":"code","source":"train_img = []\nfor i in tqdm(range(train.shape[0])):\n    img = image.load_img('../input/state-farm-distracted-driver-detection/imgs/train/'+train[\"classname\"][i]+\"/\"+train[\"img\"][i],target_size=(64,64,3))\n    img = image.img_to_array(img).flatten() #flattens a matrix to one dimension \n    img = img/255 #dividing by 255 will convert it to range from 0 to 1 \n    train_img.append(img)\nX = np.array(train_img)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:37:19.293920Z","iopub.execute_input":"2022-03-16T16:37:19.294307Z","iopub.status.idle":"2022-03-16T16:44:03.615699Z","shell.execute_reply.started":"2022-03-16T16:37:19.294181Z","shell.execute_reply":"2022-03-16T16:44:03.614922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encodes the object as an enumerated type or categorical variable.","metadata":{}},{"cell_type":"code","source":"fact = pd.factorize(train['classname'])\n# This method is useful for obtaining a numeric representation of an array\n# y = fact[0]\n# print(y)\nrep = fact[1]\ny = fact[0]\nprint(y)\nprint(rep)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:03.616915Z","iopub.execute_input":"2022-03-16T16:44:03.617408Z","iopub.status.idle":"2022-03-16T16:44:03.729703Z","shell.execute_reply.started":"2022-03-16T16:44:03.617371Z","shell.execute_reply":"2022-03-16T16:44:03.728993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:03.730851Z","iopub.execute_input":"2022-03-16T16:44:03.731147Z","iopub.status.idle":"2022-03-16T16:44:03.736970Z","shell.execute_reply.started":"2022-03-16T16:44:03.731112Z","shell.execute_reply":"2022-03-16T16:44:03.735969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:03.738637Z","iopub.execute_input":"2022-03-16T16:44:03.739179Z","iopub.status.idle":"2022-03-16T16:44:03.747949Z","shell.execute_reply.started":"2022-03-16T16:44:03.739141Z","shell.execute_reply":"2022-03-16T16:44:03.747028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting into Train and Test sets","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:10:16.987120Z","iopub.execute_input":"2022-03-16T17:10:16.987386Z","iopub.status.idle":"2022-03-16T17:10:17.032945Z","shell.execute_reply.started":"2022-03-16T17:10:16.987357Z","shell.execute_reply":"2022-03-16T17:10:17.032235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Support Vector Classifier\n\nTaking a lot of memory accuracy is 0.9924","metadata":{}},{"cell_type":"code","source":"# clf_svc = SVC(probability=True)\n# clf_svc.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:05.370235Z","iopub.execute_input":"2022-03-16T16:44:05.370755Z","iopub.status.idle":"2022-03-16T16:44:05.375205Z","shell.execute_reply.started":"2022-03-16T16:44:05.370716Z","shell.execute_reply":"2022-03-16T16:44:05.374569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preds_prob= clf_svc.predict_proba(X_test) # predicting probability\n# preds_prob[0]\n\n# preds_prob[1]\n\n# preds= clf_svc.predict(X_test)\n# cu_score = cuml.metrics.accuracy_score( y_test, preds )\n# print(cu_score)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:05.376380Z","iopub.execute_input":"2022-03-16T16:44:05.376702Z","iopub.status.idle":"2022-03-16T16:44:05.386351Z","shell.execute_reply.started":"2022-03-16T16:44:05.376664Z","shell.execute_reply":"2022-03-16T16:44:05.385565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"clf_lr = LogisticRegression()\nclf_lr.fit(X_train, y_train)\npred= clf_lr.predict(X_test)\ncu_score = cuml.metrics.accuracy_score( y_test, pred )\nprint(cu_score)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:05.388651Z","iopub.execute_input":"2022-03-16T16:44:05.389631Z","iopub.status.idle":"2022-03-16T16:44:16.806795Z","shell.execute_reply.started":"2022-03-16T16:44:05.389570Z","shell.execute_reply":"2022-03-16T16:44:16.806021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Calculating Confusion Matrix","metadata":{}},{"cell_type":"code","source":"from cuml.metrics import confusion_matrix\ncm=confusion_matrix(y_test.astype(\"int32\"),pred.astype(\"int32\"))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:10:32.820726Z","iopub.execute_input":"2022-03-16T17:10:32.821458Z","iopub.status.idle":"2022-03-16T17:10:32.845466Z","shell.execute_reply.started":"2022-03-16T17:10:32.821415Z","shell.execute_reply":"2022-03-16T17:10:32.844763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(np.asnumpy(cm),annot=True, cmap='CMRmap',fmt='g')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:10:33.297816Z","iopub.execute_input":"2022-03-16T17:10:33.298416Z","iopub.status.idle":"2022-03-16T17:10:34.171979Z","shell.execute_reply.started":"2022-03-16T17:10:33.298380Z","shell.execute_reply":"2022-03-16T17:10:34.169673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_prob= clf_lr.predict_proba(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:10:37.483223Z","iopub.execute_input":"2022-03-16T17:10:37.483473Z","iopub.status.idle":"2022-03-16T17:10:37.494284Z","shell.execute_reply.started":"2022-03-16T17:10:37.483445Z","shell.execute_reply":"2022-03-16T17:10:37.493487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_prob[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:10:39.459727Z","iopub.execute_input":"2022-03-16T17:10:39.460225Z","iopub.status.idle":"2022-03-16T17:10:39.466932Z","shell.execute_reply.started":"2022-03-16T17:10:39.460193Z","shell.execute_reply":"2022-03-16T17:10:39.465204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# XGBoost \ndidn't work because\n\nFree memory: 1260126208\n\nRequested memory: 1983971328\n","metadata":{}},{"cell_type":"code","source":"# import xgboost as xgb\n# xgb_clf = xgb.XGBClassifier(use_label_encoder=False,tree_method='gpu_hist')\n# xgb_clf.fit(X_train, y_train)\n# pred_prob_xgb=xgb_clf.predict_proba(X_test)\n# pred= xgb_clf.predict(X_test)\n# cu_score = cuml.metrics.accuracy_score( y_test, pred)\n# print(cu_score)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:24.569561Z","iopub.execute_input":"2022-03-16T16:44:24.570027Z","iopub.status.idle":"2022-03-16T16:44:24.576257Z","shell.execute_reply.started":"2022-03-16T16:44:24.569990Z","shell.execute_reply":"2022-03-16T16:44:24.575553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lightgbm\n\nMessage came your notebook tried to take more memory than allocated","metadata":{}},{"cell_type":"code","source":"# import lightgbm as lgb\n# lgb_clf = lgb.LGBMClassifier(boosting_type='dart',learning_rate=0.18, max_depth=7,\n#                n_estimators=450,objective='binary',device='gpu',\n#                random_state=42)\n# lgb_clf.fit(np.asnumpy(X_train),np.asnumpy(y_train))\n# pred_prob_lgb=lgb_clf.predict_proba(np.asnumpy(X_test))\n# pred= lgb_clf.predict(np.asnumpy(X_test))\n# cu_score = cuml.metrics.accuracy_score( y_test, pred )\n# print(cu_score)\n# accuracy is 0.9977699","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:24.577614Z","iopub.execute_input":"2022-03-16T16:44:24.578670Z","iopub.status.idle":"2022-03-16T16:44:24.589554Z","shell.execute_reply.started":"2022-03-16T16:44:24.577813Z","shell.execute_reply":"2022-03-16T16:44:24.588795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest Classifier","metadata":{}},{"cell_type":"code","source":"# from cuml.ensemble import RandomForestClassifier\n# rdf_clf=RandomForestClassifier(n_estimators=600,random_state=42, verbose=0,warm_start=False)\n# rdf_clf.fit(X_train, y_train)\n# preds_prob_rdf=rdf_clf.predict_proba(X_test)\n# preds= rdf_clf.predict(X_test)\n# cu_score = cuml.metrics.accuracy_score( y_test, preds )\n# print(cu_score) accuracy is 0.97993","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:44:24.590891Z","iopub.execute_input":"2022-03-16T16:44:24.591152Z","iopub.status.idle":"2022-03-16T16:44:24.598780Z","shell.execute_reply.started":"2022-03-16T16:44:24.591119Z","shell.execute_reply":"2022-03-16T16:44:24.598044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/state-farm-distracted-driver-detection/sample_submission.csv')   \ntest.head() ","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:10:45.209861Z","iopub.execute_input":"2022-03-16T17:10:45.210603Z","iopub.status.idle":"2022-03-16T17:10:45.274128Z","shell.execute_reply.started":"2022-03-16T17:10:45.210540Z","shell.execute_reply":"2022-03-16T17:10:45.273351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:14:36.782693Z","iopub.execute_input":"2022-03-16T17:14:36.783138Z","iopub.status.idle":"2022-03-16T17:14:36.789321Z","shell.execute_reply.started":"2022-03-16T17:14:36.783101Z","shell.execute_reply":"2022-03-16T17:14:36.788467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:24:51.737830Z","iopub.execute_input":"2022-03-16T17:24:51.738499Z","iopub.status.idle":"2022-03-16T17:24:51.757710Z","shell.execute_reply.started":"2022-03-16T17:24:51.738462Z","shell.execute_reply":"2022-03-16T17:24:51.756989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:26:53.351359Z","iopub.execute_input":"2022-03-16T17:26:53.352019Z","iopub.status.idle":"2022-03-16T17:26:53.360228Z","shell.execute_reply.started":"2022-03-16T17:26:53.351983Z","shell.execute_reply":"2022-03-16T17:26:53.359513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}