{"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":"!pip install split-folders","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:38:32.894291Z","iopub.status.busy":"2023-04-24T14:38:32.893895Z","iopub.status.idle":"2023-04-24T14:38:42.893205Z","shell.execute_reply":"2023-04-24T14:38:42.891860Z"},"papermill":{"duration":10.013688,"end_time":"2023-04-24T14:38:42.896256","exception":false,"start_time":"2023-04-24T14:38:32.882568","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport splitfolders\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-04-24T14:38:42.916567Z","iopub.status.busy":"2023-04-24T14:38:42.916155Z","iopub.status.idle":"2023-04-24T14:38:42.925955Z","shell.execute_reply":"2023-04-24T14:38:42.924995Z"},"papermill":{"duration":0.022702,"end_time":"2023-04-24T14:38:42.928216","exception":false,"start_time":"2023-04-24T14:38:42.905514","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display Data","metadata":{"papermill":{"duration":0.008893,"end_time":"2023-04-24T14:38:42.945926","exception":false,"start_time":"2023-04-24T14:38:42.937033","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df= pd.read_csv(\"/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv\")\ndf.head()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:38:42.965933Z","iopub.status.busy":"2023-04-24T14:38:42.965632Z","iopub.status.idle":"2023-04-24T14:38:43.019877Z","shell.execute_reply":"2023-04-24T14:38:43.018944Z"},"papermill":{"duration":0.067037,"end_time":"2023-04-24T14:38:43.021975","exception":false,"start_time":"2023-04-24T14:38:42.954938","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:38:43.041384Z","iopub.status.busy":"2023-04-24T14:38:43.041101Z","iopub.status.idle":"2023-04-24T14:38:43.047285Z","shell.execute_reply":"2023-04-24T14:38:43.046296Z"},"papermill":{"duration":0.018381,"end_time":"2023-04-24T14:38:43.049557","exception":false,"start_time":"2023-04-24T14:38:43.031176","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\npx.histogram(df , x=\"classname\" ,  color=\"classname\", title=\"Images By Categories \")","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:38:43.069306Z","iopub.status.busy":"2023-04-24T14:38:43.069040Z","iopub.status.idle":"2023-04-24T14:38:48.088150Z","shell.execute_reply":"2023-04-24T14:38:48.086973Z"},"papermill":{"duration":5.035973,"end_time":"2023-04-24T14:38:48.094428","exception":false,"start_time":"2023-04-24T14:38:43.058455","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:38:48.117296Z","iopub.status.busy":"2023-04-24T14:38:48.116811Z","iopub.status.idle":"2023-04-24T14:38:48.134111Z","shell.execute_reply":"2023-04-24T14:38:48.132970Z"},"papermill":{"duration":0.031576,"end_time":"2023-04-24T14:38:48.136415","exception":false,"start_time":"2023-04-24T14:38:48.104839","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mydata = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\nsplitfolders.ratio(mydata, output=\"mydata\",\n    seed=1337, ratio=(.8, .2), group_prefix=None, move=False)","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:38:48.159128Z","iopub.status.busy":"2023-04-24T14:38:48.158360Z","iopub.status.idle":"2023-04-24T14:41:24.178404Z","shell.execute_reply":"2023-04-24T14:41:24.175850Z"},"papermill":{"duration":156.033678,"end_time":"2023-04-24T14:41:24.180542","exception":false,"start_time":"2023-04-24T14:38:48.146864","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = '/kaggle/working/mydata/train'\nval = '/kaggle/working/mydata/val'","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:24.321601Z","iopub.status.busy":"2023-04-24T14:41:24.319834Z","iopub.status.idle":"2023-04-24T14:41:24.325840Z","shell.execute_reply":"2023-04-24T14:41:24.324923Z"},"papermill":{"duration":0.077621,"end_time":"2023-04-24T14:41:24.327918","exception":false,"start_time":"2023-04-24T14:41:24.250297","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:24.469882Z","iopub.status.busy":"2023-04-24T14:41:24.469578Z","iopub.status.idle":"2023-04-24T14:41:24.474170Z","shell.execute_reply":"2023-04-24T14:41:24.473163Z"},"papermill":{"duration":0.080073,"end_time":"2023-04-24T14:41:24.476734","exception":false,"start_time":"2023-04-24T14:41:24.396661","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_label= {'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.execute_input":"2023-04-24T14:41:24.624938Z","iopub.status.busy":"2023-04-24T14:41:24.623929Z","iopub.status.idle":"2023-04-24T14:41:24.630776Z","shell.execute_reply":"2023-04-24T14:41:24.629871Z"},"papermill":{"duration":0.080262,"end_time":"2023-04-24T14:41:24.632851","exception":false,"start_time":"2023-04-24T14:41:24.552589","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, axs = plt.subplots(5, 2, figsize=(10,10))\n\nsubdirs = [subdir for subdir in os.listdir(train) if os.path.isdir(os.path.join(train, subdir))]\n\nfig, axs = plt.subplots(5, 2, figsize=(16,12))\n\nfor i, subdir in enumerate(subdirs):\n    file = os.listdir(os.path.join(train, subdir))[0]\n\n    img = Image.open(os.path.join(train, subdir, file))\n    axs[i//2, i%2].imshow(img)\n    axs[i//2, i%2].set_title(f'{class_label[subdir]} ({subdir})')\n    axs[i//2, i%2].axis('off')\n\nplt.tight_layout()\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:24.771684Z","iopub.status.busy":"2023-04-24T14:41:24.770774Z","iopub.status.idle":"2023-04-24T14:41:26.289497Z","shell.execute_reply":"2023-04-24T14:41:26.287504Z"},"papermill":{"duration":1.603586,"end_time":"2023-04-24T14:41:26.304475","exception":false,"start_time":"2023-04-24T14:41:24.700889","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\nbatch_size = 128\n\ntrain_generator = train_datagen.flow_from_directory(\n        train,\n        target_size=(256, 256),\n        batch_size=128)\n\nvalidation_generator = test_datagen.flow_from_directory(\n        val,\n        target_size=(256, 256),\n        batch_size=128)\n     ","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:26.474542Z","iopub.status.busy":"2023-04-24T14:41:26.473842Z","iopub.status.idle":"2023-04-24T14:41:33.577340Z","shell.execute_reply":"2023-04-24T14:41:33.576291Z"},"papermill":{"duration":7.191809,"end_time":"2023-04-24T14:41:33.579645","exception":false,"start_time":"2023-04-24T14:41:26.387836","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.081171,"end_time":"2023-04-24T14:41:33.743489","exception":false,"start_time":"2023-04-24T14:41:33.662318","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN Model","metadata":{"papermill":{"duration":0.081464,"end_time":"2023-04-24T14:41:33.906256","exception":false,"start_time":"2023-04-24T14:41:33.824792","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from keras import layers\nfrom keras import models\n\nCNN_model = models.Sequential()\nCNN_model.add(layers.Conv2D(32, (3, 3), activation='relu',\n                        input_shape=(256, 256, 3)))\nCNN_model.add(layers.MaxPooling2D((2, 2)))\nCNN_model.add(layers.Conv2D(32, (3, 3), activation='relu'))\nCNN_model.add(layers.BatchNormalization())\nCNN_model.add(layers.MaxPooling2D((2, 2)))\n\nCNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\nCNN_model.add(layers.MaxPooling2D((2, 2)))\nCNN_model.add(layers.Dropout(0.2))\nCNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\nCNN_model.add(layers.BatchNormalization())\nCNN_model.add(layers.MaxPooling2D((2, 2)))\n\nCNN_model.add(layers.Conv2D(128, (3, 3), activation='relu'))\nCNN_model.add(layers.MaxPooling2D((2, 2)))\n\nCNN_model.add(layers.Flatten())\nCNN_model.add(layers.Dense(512, activation='relu'))\nCNN_model.add(layers.Dropout(0.3))\nCNN_model.add(layers.Dense(128, activation='relu'))\nCNN_model.add(layers.Dropout(0.2))\nCNN_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:34.072012Z","iopub.status.busy":"2023-04-24T14:41:34.070250Z","iopub.status.idle":"2023-04-24T14:41:36.832170Z","shell.execute_reply":"2023-04-24T14:41:36.831173Z"},"papermill":{"duration":2.84751,"end_time":"2023-04-24T14:41:36.834627","exception":false,"start_time":"2023-04-24T14:41:33.987117","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN_model.summary()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:37.002206Z","iopub.status.busy":"2023-04-24T14:41:37.001227Z","iopub.status.idle":"2023-04-24T14:41:37.054768Z","shell.execute_reply":"2023-04-24T14:41:37.053991Z"},"papermill":{"duration":0.16676,"end_time":"2023-04-24T14:41:37.083690","exception":false,"start_time":"2023-04-24T14:41:36.916930","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN_model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:37.258216Z","iopub.status.busy":"2023-04-24T14:41:37.257196Z","iopub.status.idle":"2023-04-24T14:41:37.276010Z","shell.execute_reply":"2023-04-24T14:41:37.275095Z"},"papermill":{"duration":0.110691,"end_time":"2023-04-24T14:41:37.278228","exception":false,"start_time":"2023-04-24T14:41:37.167537","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN_history=  CNN_model.fit(\n              train_generator,\n              epochs=15,\n              batch_size=128,\n              validation_data=validation_generator)\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T14:41:37.453449Z","iopub.status.busy":"2023-04-24T14:41:37.452780Z","iopub.status.idle":"2023-04-24T15:17:01.397952Z","shell.execute_reply":"2023-04-24T15:17:01.396778Z"},"papermill":{"duration":2124.036803,"end_time":"2023-04-24T15:17:01.400645","exception":false,"start_time":"2023-04-24T14:41:37.363842","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = CNN_model.evaluate(validation_generator)\nval_acc","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:01.798464Z","iopub.status.busy":"2023-04-24T15:17:01.797751Z","iopub.status.idle":"2023-04-24T15:17:29.458840Z","shell.execute_reply":"2023-04-24T15:17:29.457850Z"},"papermill":{"duration":27.865677,"end_time":"2023-04-24T15:17:29.461102","exception":false,"start_time":"2023-04-24T15:17:01.595425","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = CNN_history.history['accuracy']\nval_acc = CNN_history.history['val_accuracy']\nloss = CNN_history.history['loss']\nval_loss = CNN_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:29.854655Z","iopub.status.busy":"2023-04-24T15:17:29.854292Z","iopub.status.idle":"2023-04-24T15:17:30.288432Z","shell.execute_reply":"2023-04-24T15:17:30.287486Z"},"papermill":{"duration":0.634113,"end_time":"2023-04-24T15:17:30.290612","exception":false,"start_time":"2023-04-24T15:17:29.656499","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Augmentation","metadata":{"papermill":{"duration":0.197364,"end_time":"2023-04-24T15:17:30.684878","exception":false,"start_time":"2023-04-24T15:17:30.487514","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=10,\n    width_shift_range=0.05,\n    height_shift_range=0.05,\n    shear_range=0.05,\n    zoom_range=0.05,\n   fill_mode=\"nearest\"\n)\ndatagen_val = ImageDataGenerator(\n    rescale=1./255)","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:31.137961Z","iopub.status.busy":"2023-04-24T15:17:31.137574Z","iopub.status.idle":"2023-04-24T15:17:31.450898Z","shell.execute_reply":"2023-04-24T15:17:31.449791Z"},"papermill":{"duration":0.563551,"end_time":"2023-04-24T15:17:31.453394","exception":false,"start_time":"2023-04-24T15:17:30.889843","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow_from_directory(\n        train,\n        target_size=(256, 256),\n        batch_size=128)\n\nvalidation_generator = datagen_val.flow_from_directory(\n        val,\n        target_size=(256, 256),\n        batch_size=128)\n\n\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:31.850722Z","iopub.status.busy":"2023-04-24T15:17:31.849808Z","iopub.status.idle":"2023-04-24T15:17:32.399700Z","shell.execute_reply":"2023-04-24T15:17:32.398754Z"},"papermill":{"duration":0.752841,"end_time":"2023-04-24T15:17:32.402064","exception":false,"start_time":"2023-04-24T15:17:31.649223","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\nfrom keras import models\n\nmodel_aug = models.Sequential()\nmodel_aug.add(layers.Conv2D(32, (3, 3), activation='relu',\n                        input_shape=(256, 256, 3)))\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\n#model_aug.add(layers.Dropout(0.5))\n\nmodel_aug.add(layers.Conv2D(32, (3, 3), activation='relu'))\nmodel_aug.add(layers.BatchNormalization())\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\n#model_aug.add(layers.Dropout(0.2))\n\nmodel_aug.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\nmodel_aug.add(layers.Dropout(0.2))\n\nmodel_aug.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel_aug.add(layers.BatchNormalization())\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\n\nmodel_aug.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\n\nmodel_aug.add(layers.Flatten())\nmodel_aug.add(layers.Dense(512, activation='relu'))\nmodel_aug.add(layers.Dropout(0.3))\nmodel_aug.add(layers.Dense(128, activation='relu'))\nmodel_aug.add(layers.Dropout(0.2))\nmodel_aug.add(layers.Dense(10, activation='softmax'))\n\n\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:32.801320Z","iopub.status.busy":"2023-04-24T15:17:32.800954Z","iopub.status.idle":"2023-04-24T15:17:32.942234Z","shell.execute_reply":"2023-04-24T15:17:32.941247Z"},"papermill":{"duration":0.344039,"end_time":"2023-04-24T15:17:32.944403","exception":false,"start_time":"2023-04-24T15:17:32.600364","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel_aug.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:33.374812Z","iopub.status.busy":"2023-04-24T15:17:33.374444Z","iopub.status.idle":"2023-04-24T15:17:33.386155Z","shell.execute_reply":"2023-04-24T15:17:33.385248Z"},"papermill":{"duration":0.230152,"end_time":"2023-04-24T15:17:33.388345","exception":false,"start_time":"2023-04-24T15:17:33.158193","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_aug=model_aug.fit(\n      train_generator,\n    batch_size=128,\n     # steps_per_epoch=len(train)//128,\n      epochs=15,\n      validation_data=validation_generator)\n      #validation_steps=len(val)//128)\n    \n    ","metadata":{"execution":{"iopub.execute_input":"2023-04-24T15:17:33.796609Z","iopub.status.busy":"2023-04-24T15:17:33.796232Z","iopub.status.idle":"2023-04-24T16:48:11.188159Z","shell.execute_reply":"2023-04-24T16:48:11.186964Z"},"papermill":{"duration":5437.6014,"end_time":"2023-04-24T16:48:11.190708","exception":false,"start_time":"2023-04-24T15:17:33.589308","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = model_aug.evaluate(validation_generator)\nval_acc","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:11.817312Z","iopub.status.busy":"2023-04-24T16:48:11.816592Z","iopub.status.idle":"2023-04-24T16:48:39.427940Z","shell.execute_reply":"2023-04-24T16:48:39.426843Z"},"papermill":{"duration":27.926757,"end_time":"2023-04-24T16:48:39.430190","exception":false,"start_time":"2023-04-24T16:48:11.503433","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = hist_aug.history['accuracy']\nval_acc = hist_aug.history['val_accuracy']\nloss = hist_aug.history['loss']\nval_loss = hist_aug.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:40.112165Z","iopub.status.busy":"2023-04-24T16:48:40.111062Z","iopub.status.idle":"2023-04-24T16:48:40.544814Z","shell.execute_reply":"2023-04-24T16:48:40.543789Z"},"papermill":{"duration":0.805591,"end_time":"2023-04-24T16:48:40.547044","exception":false,"start_time":"2023-04-24T16:48:39.741453","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning VGG-16","metadata":{"papermill":{"duration":0.315946,"end_time":"2023-04-24T16:48:41.179088","exception":false,"start_time":"2023-04-24T16:48:40.863142","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.applications import VGG16","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:41.808522Z","iopub.status.busy":"2023-04-24T16:48:41.807942Z","iopub.status.idle":"2023-04-24T16:48:41.814800Z","shell.execute_reply":"2023-04-24T16:48:41.813856Z"},"papermill":{"duration":0.325835,"end_time":"2023-04-24T16:48:41.816978","exception":false,"start_time":"2023-04-24T16:48:41.491143","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    preprocessing_function= preprocess_input,\n    rotation_range=10,\n    width_shift_range=0.05,\n    height_shift_range=0.05,\n    shear_range=0.05,\n    zoom_range=0.05,\n   fill_mode=\"nearest\",\n    \n)\n\ndatagen_test = ImageDataGenerator(\n    preprocessing_function= preprocess_input\n)\n\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:42.543723Z","iopub.status.busy":"2023-04-24T16:48:42.543317Z","iopub.status.idle":"2023-04-24T16:48:42.548863Z","shell.execute_reply":"2023-04-24T16:48:42.547862Z"},"papermill":{"duration":0.41281,"end_time":"2023-04-24T16:48:42.550888","exception":false,"start_time":"2023-04-24T16:48:42.138078","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow_from_directory(\n        train,\n        target_size=(256, 256),\n        batch_size=128)\n\nvalidation_generator = datagen_test.flow_from_directory(\n        val,\n        target_size=(256, 256),\n        batch_size=128)\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:43.235692Z","iopub.status.busy":"2023-04-24T16:48:43.235315Z","iopub.status.idle":"2023-04-24T16:48:43.791656Z","shell.execute_reply":"2023-04-24T16:48:43.790666Z"},"papermill":{"duration":0.876735,"end_time":"2023-04-24T16:48:43.794339","exception":false,"start_time":"2023-04-24T16:48:42.917604","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_VGG = VGG16(\n    include_top=False,\n    weights='imagenet',\n    input_shape=(256, 256, 3)\n)\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:44.457775Z","iopub.status.busy":"2023-04-24T16:48:44.457394Z","iopub.status.idle":"2023-04-24T16:48:45.176377Z","shell.execute_reply":"2023-04-24T16:48:45.175129Z"},"papermill":{"duration":1.041832,"end_time":"2023-04-24T16:48:45.179891","exception":false,"start_time":"2023-04-24T16:48:44.138059","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_VGG.trainable = False","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:45.811061Z","iopub.status.busy":"2023-04-24T16:48:45.810265Z","iopub.status.idle":"2023-04-24T16:48:45.815655Z","shell.execute_reply":"2023-04-24T16:48:45.814663Z"},"papermill":{"duration":0.325919,"end_time":"2023-04-24T16:48:45.817800","exception":false,"start_time":"2023-04-24T16:48:45.491881","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_VGG.summary()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:46.505338Z","iopub.status.busy":"2023-04-24T16:48:46.504227Z","iopub.status.idle":"2023-04-24T16:48:46.546335Z","shell.execute_reply":"2023-04-24T16:48:46.545416Z"},"papermill":{"duration":0.392058,"end_time":"2023-04-24T16:48:46.578642","exception":false,"start_time":"2023-04-24T16:48:46.186584","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg = tf.keras.models.Sequential([\n    conv_VGG,\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation='relu'),\n    tf.keras.layers.Dropout(0.3),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:47.218098Z","iopub.status.busy":"2023-04-24T16:48:47.217116Z","iopub.status.idle":"2023-04-24T16:48:47.331811Z","shell.execute_reply":"2023-04-24T16:48:47.330803Z"},"papermill":{"duration":0.43669,"end_time":"2023-04-24T16:48:47.334181","exception":false,"start_time":"2023-04-24T16:48:46.897491","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.summary()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:47.970318Z","iopub.status.busy":"2023-04-24T16:48:47.969131Z","iopub.status.idle":"2023-04-24T16:48:48.001514Z","shell.execute_reply":"2023-04-24T16:48:48.000453Z"},"papermill":{"duration":0.362558,"end_time":"2023-04-24T16:48:48.011637","exception":false,"start_time":"2023-04-24T16:48:47.649079","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.compile(\n    optimizer=tf.keras.optimizers.Adam(),\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:48.703149Z","iopub.status.busy":"2023-04-24T16:48:48.702133Z","iopub.status.idle":"2023-04-24T16:48:48.714006Z","shell.execute_reply":"2023-04-24T16:48:48.713065Z"},"papermill":{"duration":0.3866,"end_time":"2023-04-24T16:48:48.716162","exception":false,"start_time":"2023-04-24T16:48:48.329562","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_vgg=model_vgg.fit(\n    train_generator,\n    batch_size=128,\n     # steps_per_epoch=len(train)//128,\n      epochs=15,\n      validation_data=validation_generator)\n\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T16:48:49.353429Z","iopub.status.busy":"2023-04-24T16:48:49.353061Z","iopub.status.idle":"2023-04-24T18:21:41.239738Z","shell.execute_reply":"2023-04-24T18:21:41.238628Z"},"papermill":{"duration":5572.207245,"end_time":"2023-04-24T18:21:41.242373","exception":false,"start_time":"2023-04-24T16:48:49.035128","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = model_vgg.evaluate(validation_generator)\nval_acc","metadata":{"execution":{"iopub.execute_input":"2023-04-24T18:21:42.115144Z","iopub.status.busy":"2023-04-24T18:21:42.114385Z","iopub.status.idle":"2023-04-24T18:22:23.937999Z","shell.execute_reply":"2023-04-24T18:22:23.936918Z"},"papermill":{"duration":42.262006,"end_time":"2023-04-24T18:22:23.940303","exception":false,"start_time":"2023-04-24T18:21:41.678297","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_vgg.history['accuracy']\nval_acc = history_vgg.history['val_accuracy']\nloss = history_vgg.history['loss']\nval_loss = history_vgg.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T18:22:24.892453Z","iopub.status.busy":"2023-04-24T18:22:24.892068Z","iopub.status.idle":"2023-04-24T18:22:25.318339Z","shell.execute_reply":"2023-04-24T18:22:25.317326Z"},"papermill":{"duration":0.938248,"end_time":"2023-04-24T18:22:25.320470","exception":false,"start_time":"2023-04-24T18:22:24.382222","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# submission","metadata":{"papermill":{"duration":0.439461,"end_time":"2023-04-24T18:22:26.197605","exception":false,"start_time":"2023-04-24T18:22:25.758144","status":"completed"},"tags":[]}},{"cell_type":"code","source":"datagen_test = ImageDataGenerator(preprocessing_function=preprocess_input)\ntest='/kaggle/input/state-farm-distracted-driver-detection/imgs/.'\n\ntestData = datagen_test.flow_from_directory(test,\n                                            shuffle=False,\n                                            target_size=(256, 256),\n                                            batch_size = 128,\n                                            classes=['test'])\n\npredict_test = model_vgg.predict(testData)\n","metadata":{"execution":{"iopub.execute_input":"2023-04-24T18:22:27.086561Z","iopub.status.busy":"2023-04-24T18:22:27.085575Z","iopub.status.idle":"2023-04-24T18:44:03.552724Z","shell.execute_reply":"2023-04-24T18:44:03.551542Z"},"papermill":{"duration":1297.031209,"end_time":"2023-04-24T18:44:03.669858","exception":false,"start_time":"2023-04-24T18:22:26.638649","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '../input/state-farm-distracted-driver-detection'\ntest_dir = os.path.join(base_dir, 'imgs')","metadata":{"execution":{"iopub.execute_input":"2023-04-24T18:44:04.691573Z","iopub.status.busy":"2023-04-24T18:44:04.691190Z","iopub.status.idle":"2023-04-24T18:44:04.696053Z","shell.execute_reply":"2023-04-24T18:44:04.694978Z"},"papermill":{"duration":0.481709,"end_time":"2023-04-24T18:44:04.698369","exception":false,"start_time":"2023-04-24T18:44:04.216660","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=os.path.join(base_dir, \"imgs/test\")\ntest_ids = sorted(os.listdir(test))\nsub_df = pd.DataFrame(columns = ['img','c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])\n\nfor i in range(len(predict_test)):\n    sub_df.loc[i, 'img'] = test_ids[i]\n    sub_df.loc[i, 'c0':'c9'] = predict_test[i]\n    \nsub_df.to_csv(\"/kaggle/working/submission.csv\",index=False)","metadata":{"execution":{"iopub.execute_input":"2023-04-24T18:44:05.643828Z","iopub.status.busy":"2023-04-24T18:44:05.643440Z","iopub.status.idle":"2023-04-24T18:53:52.619693Z","shell.execute_reply":"2023-04-24T18:53:52.618590Z"},"papermill":{"duration":587.454922,"end_time":"2023-04-24T18:53:52.622829","exception":false,"start_time":"2023-04-24T18:44:05.167907","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.head()","metadata":{"execution":{"iopub.execute_input":"2023-04-24T18:53:53.678959Z","iopub.status.busy":"2023-04-24T18:53:53.678560Z","iopub.status.idle":"2023-04-24T18:53:53.696723Z","shell.execute_reply":"2023-04-24T18:53:53.695826Z"},"papermill":{"duration":0.572432,"end_time":"2023-04-24T18:53:53.699014","exception":false,"start_time":"2023-04-24T18:53:53.126582","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}