{"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-16T19:06:37.241231Z","iopub.execute_input":"2022-03-16T19:06:37.241665Z","iopub.status.idle":"2022-03-16T19:06:37.246870Z","shell.execute_reply.started":"2022-03-16T19:06:37.241608Z","shell.execute_reply":"2022-03-16T19:06:37.246143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow-gpu","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:37.248653Z","iopub.execute_input":"2022-03-16T19:06:37.249196Z","iopub.status.idle":"2022-03-16T19:06:37.257268Z","shell.execute_reply.started":"2022-03-16T19:06:37.249157Z","shell.execute_reply":"2022-03-16T19:06:37.256289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cudf as cd\nimport cupy as cp\nimport tensorflow as tf\nfrom tensorflow import keras as k\nimport cuml\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:37.258815Z","iopub.execute_input":"2022-03-16T19:06:37.259258Z","iopub.status.idle":"2022-03-16T19:06:45.704432Z","shell.execute_reply.started":"2022-03-16T19:06:37.259218Z","shell.execute_reply":"2022-03-16T19:06:45.703589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names = cd.read_csv('../input/state-farm-distracted-driver-detection/driver_imgs_list.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:45.706800Z","iopub.execute_input":"2022-03-16T19:06:45.707085Z","iopub.status.idle":"2022-03-16T19:06:50.861689Z","shell.execute_reply.started":"2022-03-16T19:06:45.707038Z","shell.execute_reply":"2022-03-16T19:06:50.860839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:50.863105Z","iopub.execute_input":"2022-03-16T19:06:50.863367Z","iopub.status.idle":"2022-03-16T19:06:50.903519Z","shell.execute_reply.started":"2022-03-16T19:06:50.863331Z","shell.execute_reply":"2022-03-16T19:06:50.902877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:50.904691Z","iopub.execute_input":"2022-03-16T19:06:50.904957Z","iopub.status.idle":"2022-03-16T19:06:50.911787Z","shell.execute_reply.started":"2022-03-16T19:06:50.904922Z","shell.execute_reply":"2022-03-16T19:06:50.910970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:50.913239Z","iopub.execute_input":"2022-03-16T19:06:50.913742Z","iopub.status.idle":"2022-03-16T19:06:50.936198Z","shell.execute_reply.started":"2022-03-16T19:06:50.913706Z","shell.execute_reply":"2022-03-16T19:06:50.935471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names['subject'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:50.937656Z","iopub.execute_input":"2022-03-16T19:06:50.937929Z","iopub.status.idle":"2022-03-16T19:06:50.957446Z","shell.execute_reply.started":"2022-03-16T19:06:50.937894Z","shell.execute_reply":"2022-03-16T19:06:50.956648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names['classname'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:50.958597Z","iopub.execute_input":"2022-03-16T19:06:50.958955Z","iopub.status.idle":"2022-03-16T19:06:50.995903Z","shell.execute_reply.started":"2022-03-16T19:06:50.958914Z","shell.execute_reply":"2022-03-16T19:06:50.995063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image \nimg = image.load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', \n        target_size = (128, 128))\nimg","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:50.998872Z","iopub.execute_input":"2022-03-16T19:06:50.999128Z","iopub.status.idle":"2022-03-16T19:06:51.200223Z","shell.execute_reply.started":"2022-03-16T19:06:50.999094Z","shell.execute_reply":"2022-03-16T19:06:51.199447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = image.img_to_array(img)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.201535Z","iopub.execute_input":"2022-03-16T19:06:51.202498Z","iopub.status.idle":"2022-03-16T19:06:51.209373Z","shell.execute_reply.started":"2022-03-16T19:06:51.202450Z","shell.execute_reply":"2022-03-16T19:06:51.208599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_names.drop('subject', 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.210955Z","iopub.execute_input":"2022-03-16T19:06:51.211541Z","iopub.status.idle":"2022-03-16T19:06:51.218422Z","shell.execute_reply.started":"2022-03-16T19:06:51.211462Z","shell.execute_reply":"2022-03-16T19:06:51.217304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dic = {'c0': 0, 'c1': 1, 'c2': 2, 'c3': 3, 'c4': 4, 'c5': 5, 'c6': 6, 'c7': 7, 'c8': 8, 'c9': 9}\nclasses = [dic[str(i)] for i in train_img_names['classname'].to_arrow()]\ntrain_img_names['classname'] = classes\ntrain_img_names.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.220290Z","iopub.execute_input":"2022-03-16T19:06:51.220712Z","iopub.status.idle":"2022-03-16T19:06:51.286192Z","shell.execute_reply.started":"2022-03-16T19:06:51.220667Z","shell.execute_reply":"2022-03-16T19:06:51.285429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train_img_names['img'].to_pandas()\ny = train_img_names['classname'].to_pandas()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.287463Z","iopub.execute_input":"2022-03-16T19:06:51.287892Z","iopub.status.idle":"2022-03-16T19:06:51.301830Z","shell.execute_reply.started":"2022-03-16T19:06:51.287855Z","shell.execute_reply":"2022-03-16T19:06:51.301082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split as tts\nx_train, x_test, y_train, y_test = tts(x, y, test_size = 0.3, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:28.354218Z","iopub.execute_input":"2022-03-16T19:15:28.354503Z","iopub.status.idle":"2022-03-16T19:15:28.365657Z","shell.execute_reply.started":"2022-03-16T19:15:28.354472Z","shell.execute_reply":"2022-03-16T19:15:28.364805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.314187Z","iopub.execute_input":"2022-03-16T19:06:51.314802Z","iopub.status.idle":"2022-03-16T19:06:51.322138Z","shell.execute_reply.started":"2022-03-16T19:06:51.314738Z","shell.execute_reply":"2022-03-16T19:06:51.321341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = cd.DataFrame(x_train.reset_index())\nx_test = cd.DataFrame(x_test.reset_index())\ny_train = cd.DataFrame(y_train.reset_index())\ny_test = cd.DataFrame(y_test.reset_index())","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:32.701108Z","iopub.execute_input":"2022-03-16T19:15:32.701713Z","iopub.status.idle":"2022-03-16T19:15:32.723521Z","shell.execute_reply.started":"2022-03-16T19:15:32.701663Z","shell.execute_reply":"2022-03-16T19:15:32.722820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_rgb(image_name, cl):\n    path = '../input/state-farm-distracted-driver-detection/imgs/train/' + 'c' + str(cl) + '/' + image_name\n    mImage = image.load_img(path,target_size = (64, 64))\n    mImage = image.img_to_array(mImage)\n    return mImage","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.348465Z","iopub.execute_input":"2022-03-16T19:06:51.348734Z","iopub.status.idle":"2022-03-16T19:06:51.353634Z","shell.execute_reply.started":"2022-03-16T19:06:51.348699Z","shell.execute_reply":"2022-03-16T19:06:51.352829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain = [image_rgb(x_train['img'][i], y_train['classname'][i]) for i in range(len(x_train))]\nxtest = [image_rgb(x_test['img'][i], y_test['classname'][i]) for i in range(len(x_test))]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:06:51.355017Z","iopub.execute_input":"2022-03-16T19:06:51.355496Z","iopub.status.idle":"2022-03-16T19:13:15.164795Z","shell.execute_reply.started":"2022-03-16T19:06:51.355458Z","shell.execute_reply":"2022-03-16T19:13:15.163955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain = np.array(xtrain)\nxtest = np.array(xtest)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:14:29.687946Z","iopub.execute_input":"2022-03-16T19:14:29.688912Z","iopub.status.idle":"2022-03-16T19:14:30.093102Z","shell.execute_reply.started":"2022-03-16T19:14:29.688782Z","shell.execute_reply":"2022-03-16T19:14:30.092185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:13:15.581209Z","iopub.execute_input":"2022-03-16T19:13:15.581479Z","iopub.status.idle":"2022-03-16T19:13:15.588521Z","shell.execute_reply.started":"2022-03-16T19:13:15.581444Z","shell.execute_reply":"2022-03-16T19:13:15.587824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtr = xtrain / 255.0\nxte = xtest / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:13:23.437585Z","iopub.execute_input":"2022-03-16T19:13:23.438188Z","iopub.status.idle":"2022-03-16T19:13:23.729409Z","shell.execute_reply.started":"2022-03-16T19:13:23.438145Z","shell.execute_reply":"2022-03-16T19:13:23.728610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = k.Sequential()\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2,padding='same', activation='relu', input_shape=(64,64,3)))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=2))\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2,padding='same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=2))\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2,padding='same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=2))\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2,padding='same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=2))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(500, activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:13:27.012387Z","iopub.execute_input":"2022-03-16T19:13:27.013168Z","iopub.status.idle":"2022-03-16T19:13:27.612084Z","shell.execute_reply.started":"2022-03-16T19:13:27.013127Z","shell.execute_reply":"2022-03-16T19:13:27.611305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.to_pandas().to_numpy()\ny_train = y_train.to_pandas().to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:47.194770Z","iopub.execute_input":"2022-03-16T19:15:47.195459Z","iopub.status.idle":"2022-03-16T19:15:47.205208Z","shell.execute_reply.started":"2022-03-16T19:15:47.195419Z","shell.execute_reply":"2022-03-16T19:15:47.204293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train[:, 1]\ny_test = y_test[:, 1]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:49.356459Z","iopub.execute_input":"2022-03-16T19:15:49.356833Z","iopub.status.idle":"2022-03-16T19:15:49.362658Z","shell.execute_reply.started":"2022-03-16T19:15:49.356795Z","shell.execute_reply":"2022-03-16T19:15:49.360472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:52.001231Z","iopub.execute_input":"2022-03-16T19:15:52.001824Z","iopub.status.idle":"2022-03-16T19:15:52.007405Z","shell.execute_reply.started":"2022-03-16T19:15:52.001779Z","shell.execute_reply":"2022-03-16T19:15:52.006687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = k.utils.to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:55.539968Z","iopub.execute_input":"2022-03-16T19:15:55.540530Z","iopub.status.idle":"2022-03-16T19:15:55.545911Z","shell.execute_reply.started":"2022-03-16T19:15:55.540489Z","shell.execute_reply":"2022-03-16T19:15:55.544974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = k.utils.to_categorical(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:15:58.004357Z","iopub.execute_input":"2022-03-16T19:15:58.004956Z","iopub.status.idle":"2022-03-16T19:15:58.009683Z","shell.execute_reply.started":"2022-03-16T19:15:58.004914Z","shell.execute_reply":"2022-03-16T19:15:58.008579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:16:00.239568Z","iopub.execute_input":"2022-03-16T19:16:00.240305Z","iopub.status.idle":"2022-03-16T19:16:00.246922Z","shell.execute_reply.started":"2022-03-16T19:16:00.240263Z","shell.execute_reply":"2022-03-16T19:16:00.246184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(xtr, y_train,epochs=20, validation_data = (xte, y_test), batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T19:16:04.160931Z","iopub.execute_input":"2022-03-16T19:16:04.161644Z","iopub.status.idle":"2022-03-16T19:17:39.410932Z","shell.execute_reply.started":"2022-03-16T19:16:04.161598Z","shell.execute_reply":"2022-03-16T19:17:39.410197Z"},"trusted":true},"execution_count":null,"outputs":[]}]}