{"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\nimport 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-07-06T15:10:57.198387Z","iopub.execute_input":"2022-07-06T15:10:57.198903Z","iopub.status.idle":"2022-07-06T15:10:57.205333Z","shell.execute_reply.started":"2022-07-06T15:10:57.198854Z","shell.execute_reply":"2022-07-06T15:10:57.204273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport random\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom keras.preprocessing import image                  \nfrom keras.layers import Dropout, Flatten, Dense\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.213944Z","iopub.execute_input":"2022-07-06T15:10:57.215086Z","iopub.status.idle":"2022-07-06T15:10:57.222771Z","shell.execute_reply.started":"2022-07-06T15:10:57.215046Z","shell.execute_reply":"2022-07-06T15:10:57.221209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading Data","metadata":{}},{"cell_type":"code","source":"BASE_DIR=('../input/state-farm-distracted-driver-detection')\ntrain_dir=os.path.join(BASE_DIR,'imgs/train/')\ntest_dir=os.path.join(BASE_DIR,'imgs/test/')\ndf = pd.read_csv(os.path.join(BASE_DIR, 'driver_imgs_list.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.229240Z","iopub.execute_input":"2022-07-06T15:10:57.229830Z","iopub.status.idle":"2022-07-06T15:10:57.257070Z","shell.execute_reply.started":"2022-07-06T15:10:57.229794Z","shell.execute_reply":"2022-07-06T15:10:57.256198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.258747Z","iopub.execute_input":"2022-07-06T15:10:57.259355Z","iopub.status.idle":"2022-07-06T15:10:57.270878Z","shell.execute_reply.started":"2022-07-06T15:10:57.259309Z","shell.execute_reply":"2022-07-06T15:10:57.269780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_count = df.classname.value_counts()\nfig = class_count.plot(kind='bar',figsize=(20,15))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.276571Z","iopub.execute_input":"2022-07-06T15:10:57.278262Z","iopub.status.idle":"2022-07-06T15:10:57.742969Z","shell.execute_reply.started":"2022-07-06T15:10:57.278220Z","shell.execute_reply":"2022-07-06T15:10:57.741941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Groupby subjects\nby_drivers = df.groupby('subject') \n# Groupby unique drivers\n# unique_drivers = by_drivers.groups.keys() # drivers id\n# print('There are : ',len(unique_drivers), ' unique drivers')\n# print('There is a mean of ',round(dataset.groupby('subject').count()['classname'].mean()), ' images by driver.')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.745271Z","iopub.execute_input":"2022-07-06T15:10:57.746276Z","iopub.status.idle":"2022-07-06T15:10:57.751197Z","shell.execute_reply.started":"2022-07-06T15:10:57.746238Z","shell.execute_reply":"2022-07-06T15:10:57.750204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUMBER_CLASSES = 10 # 10 classes","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.752592Z","iopub.execute_input":"2022-07-06T15:10:57.752962Z","iopub.status.idle":"2022-07-06T15:10:57.763012Z","shell.execute_reply.started":"2022-07-06T15:10:57.752927Z","shell.execute_reply":"2022-07-06T15:10:57.762045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_csv(DATA_DIR,filename):\n    class_names = os.listdir(DATA_DIR)\n    data = list()\n    if(os.path.isdir(os.path.join(DATA_DIR,class_names[0]))):\n        for class_name in class_names:\n            file_names = os.listdir(os.path.join(DATA_DIR,class_name))\n            for file in file_names:\n                data.append({\n                    \"Filename\":os.path.join(DATA_DIR,class_name,file),\n                    \"ClassName\":class_name\n                })\n    else:\n        class_name = \"test\"\n        file_names = os.listdir(DATA_DIR)\n        for file in file_names:\n            data.append(({\n                \"FileName\":os.path.join(DATA_DIR,file),\n                \"ClassName\":class_name\n            }))\n    data = pd.DataFrame(data)\n    data.to_csv(os.path.join(os.getcwd(),\"csv_files\",filename),index=False)\nCSV_FILES_DIR = os.path.join(os.getcwd(),\"csv_files\")\nif not os.path.exists(CSV_FILES_DIR):\n    os.makedirs(CSV_FILES_DIR)\n    \ncreate_csv(train_dir,\"train.csv\")\ncreate_csv(test_dir,\"test.csv\")\ntrain_data = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"train.csv\"))\ntest_data = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"test.csv\"))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:57.766452Z","iopub.execute_input":"2022-07-06T15:10:57.766788Z","iopub.status.idle":"2022-07-06T15:11:02.881666Z","shell.execute_reply.started":"2022-07-06T15:10:57.766761Z","shell.execute_reply":"2022-07-06T15:11:02.880684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:02.883078Z","iopub.execute_input":"2022-07-06T15:11:02.883524Z","iopub.status.idle":"2022-07-06T15:11:02.893288Z","shell.execute_reply.started":"2022-07-06T15:11:02.883486Z","shell.execute_reply":"2022-07-06T15:11:02.892219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['ClassName'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:02.895317Z","iopub.execute_input":"2022-07-06T15:11:02.896127Z","iopub.status.idle":"2022-07-06T15:11:02.910371Z","shell.execute_reply.started":"2022-07-06T15:11:02.896090Z","shell.execute_reply":"2022-07-06T15:11:02.909186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['Filename'][11]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:02.911981Z","iopub.execute_input":"2022-07-06T15:11:02.912785Z","iopub.status.idle":"2022-07-06T15:11:02.920720Z","shell.execute_reply.started":"2022-07-06T15:11:02.912749Z","shell.execute_reply":"2022-07-06T15:11:02.919776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.classname.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:02.922392Z","iopub.execute_input":"2022-07-06T15:11:02.923099Z","iopub.status.idle":"2022-07-06T15:11:02.935496Z","shell.execute_reply.started":"2022-07-06T15:11:02.923039Z","shell.execute_reply":"2022-07-06T15:11:02.934441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### show random image","metadata":{}},{"cell_type":"code","source":"num = random.randint(0, 22423)\n# print(num)\nclass_name=df[\"classname\"][num]\nimg=df[\"img\"][num]\npath=train_dir+class_name+\"/\"+img\nprint(path)\nimg = cv2.imread(path) \nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nprint(img.shape)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:02.936832Z","iopub.execute_input":"2022-07-06T15:11:02.937455Z","iopub.status.idle":"2022-07-06T15:11:03.211736Z","shell.execute_reply.started":"2022-07-06T15:11:02.937420Z","shell.execute_reply":"2022-07-06T15:11:03.210786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.215845Z","iopub.execute_input":"2022-07-06T15:11:03.217152Z","iopub.status.idle":"2022-07-06T15:11:03.223701Z","shell.execute_reply.started":"2022-07-06T15:11:03.217113Z","shell.execute_reply":"2022-07-06T15:11:03.222602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = list(set(train_data['ClassName'].values.tolist()))\nlabels_id = {label_name:id for id,label_name in enumerate(labels_list)}\nprint(labels_id)\ntrain_data['ClassName'].replace(labels_id,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.225107Z","iopub.execute_input":"2022-07-06T15:11:03.226507Z","iopub.status.idle":"2022-07-06T15:11:03.257845Z","shell.execute_reply.started":"2022-07-06T15:11:03.226454Z","shell.execute_reply":"2022-07-06T15:11:03.256847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\nlabels = to_categorical(train_data['ClassName'])\nprint(labels.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.259853Z","iopub.execute_input":"2022-07-06T15:11:03.260748Z","iopub.status.idle":"2022-07-06T15:11:03.602143Z","shell.execute_reply.started":"2022-07-06T15:11:03.260713Z","shell.execute_reply":"2022-07-06T15:11:03.601091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_val, y_train, y_val = train_test_split(train_data.iloc[:,0],labels,test_size = 0.2,random_state=42,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.603715Z","iopub.execute_input":"2022-07-06T15:11:03.604100Z","iopub.status.idle":"2022-07-06T15:11:03.890986Z","shell.execute_reply.started":"2022-07-06T15:11:03.604061Z","shell.execute_reply":"2022-07-06T15:11:03.889957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.892492Z","iopub.execute_input":"2022-07-06T15:11:03.892984Z","iopub.status.idle":"2022-07-06T15:11:03.902193Z","shell.execute_reply.started":"2022-07-06T15:11:03.892940Z","shell.execute_reply":"2022-07-06T15:11:03.900978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"Filename\"].shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.903949Z","iopub.execute_input":"2022-07-06T15:11:03.904396Z","iopub.status.idle":"2022-07-06T15:11:03.911495Z","shell.execute_reply.started":"2022-07-06T15:11:03.904351Z","shell.execute_reply":"2022-07-06T15:11:03.910412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.912956Z","iopub.execute_input":"2022-07-06T15:11:03.913596Z","iopub.status.idle":"2022-07-06T15:11:03.923391Z","shell.execute_reply.started":"2022-07-06T15:11:03.913536Z","shell.execute_reply":"2022-07-06T15:11:03.922337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_images = train_images.reshape((22424, 28 * 28))\n# train_images = train_images.astype('float32') / 255\n\n# # test_images = test_images.reshape((10000, 28 * 28))\n# # test_images = test_images.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.924784Z","iopub.execute_input":"2022-07-06T15:11:03.925273Z","iopub.status.idle":"2022-07-06T15:11:03.931841Z","shell.execute_reply.started":"2022-07-06T15:11:03.925237Z","shell.execute_reply":"2022-07-06T15:11:03.930570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def path_to_tensor(img_path):\n    # loads RGB image as PIL.Image.Image type\n    img = image.load_img(img_path, target_size=(64, 64))\n    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)\n    x = image.img_to_array(img)\n    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor\n    return np.expand_dims(x, axis=0)\n\ndef paths_to_tensor(img_paths):\n    list_of_tensors = [path_to_tensor(img_path) for img_path in img_paths]\n    return np.vstack(list_of_tensors)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.933576Z","iopub.execute_input":"2022-07-06T15:11:03.933997Z","iopub.status.idle":"2022-07-06T15:11:03.942181Z","shell.execute_reply.started":"2022-07-06T15:11:03.933961Z","shell.execute_reply":"2022-07-06T15:11:03.941213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tensors = paths_to_tensor(x_train).astype('float32')/255 ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:11:03.944152Z","iopub.execute_input":"2022-07-06T15:11:03.944818Z","iopub.status.idle":"2022-07-06T15:14:00.793835Z","shell.execute_reply.started":"2022-07-06T15:11:03.944782Z","shell.execute_reply":"2022-07-06T15:14:00.792812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_tensors = paths_to_tensor(x_val).astype('float32')/255 ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:00.795414Z","iopub.execute_input":"2022-07-06T15:14:00.795768Z","iopub.status.idle":"2022-07-06T15:14:44.381812Z","shell.execute_reply.started":"2022-07-06T15:14:00.795734Z","shell.execute_reply":"2022-07-06T15:14:44.380831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tensors","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:44.383327Z","iopub.execute_input":"2022-07-06T15:14:44.383724Z","iopub.status.idle":"2022-07-06T15:14:44.401738Z","shell.execute_reply.started":"2022-07-06T15:14:44.383690Z","shell.execute_reply":"2022-07-06T15:14:44.400912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import models\nfrom tensorflow.keras import layers\n\nnetwork = models.Sequential()\nnetwork.add(layers.Dense(512, activation='relu', name='Layer_1', input_shape=(64,64,3)))\nnetwork.add(layers.Dense(512, activation='relu', name='Layer_2'))\nnetwork.add(layers.Dense(256, activation='relu', name='Layer_3'))\nnetwork.add(Flatten())\nnetwork.add(layers.Dense(50, activation='relu', name='Layer_4'))\nnetwork.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:44.403261Z","iopub.execute_input":"2022-07-06T15:14:44.403605Z","iopub.status.idle":"2022-07-06T15:14:46.901821Z","shell.execute_reply.started":"2022-07-06T15:14:44.403570Z","shell.execute_reply":"2022-07-06T15:14:46.899981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:46.903051Z","iopub.execute_input":"2022-07-06T15:14:46.903388Z","iopub.status.idle":"2022-07-06T15:14:46.910623Z","shell.execute_reply.started":"2022-07-06T15:14:46.903351Z","shell.execute_reply":"2022-07-06T15:14:46.909682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(network)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:46.912416Z","iopub.execute_input":"2022-07-06T15:14:46.913279Z","iopub.status.idle":"2022-07-06T15:14:47.845234Z","shell.execute_reply.started":"2022-07-06T15:14:46.913241Z","shell.execute_reply":"2022-07-06T15:14:47.844098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network.compile(optimizer='rmsprop',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:47.847029Z","iopub.execute_input":"2022-07-06T15:14:47.847328Z","iopub.status.idle":"2022-07-06T15:14:47.864361Z","shell.execute_reply.started":"2022-07-06T15:14:47.847302Z","shell.execute_reply":"2022-07-06T15:14:47.863539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network.fit(train_tensors, y_train, epochs=5, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:14:47.866185Z","iopub.execute_input":"2022-07-06T15:14:47.867097Z","iopub.status.idle":"2022-07-06T15:18:19.533381Z","shell.execute_reply.started":"2022-07-06T15:14:47.867043Z","shell.execute_reply":"2022-07-06T15:18:19.532082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = network.evaluate(valid_tensors, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:18:19.535096Z","iopub.execute_input":"2022-07-06T15:18:19.535758Z","iopub.status.idle":"2022-07-06T15:18:25.056880Z","shell.execute_reply.started":"2022-07-06T15:18:19.535720Z","shell.execute_reply":"2022-07-06T15:18:25.055688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}