{"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 os\nimport random\nimport numpy as np\nimport pandas as pd \nfrom skimage import io\nfrom skimage import color\nfrom PIL import Image\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom dask.array.image import imread\nfrom dask import bag, threaded\nfrom dask.diagnostics import ProgressBar\nimport cv2\nfrom sklearn.model_selection import train_test_split\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n\n\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dropout, Flatten, Dense,GlobalAveragePooling2D\nfrom keras.layers import Flatten,Dropout\nfrom keras.layers import Conv2D, MaxPooling2D,BatchNormalization\nfrom keras.utils import to_categorical\nfrom keras.preprocessing import image \n \nfrom keras import optimizers\n","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:30:28.209223Z","iopub.execute_input":"2023-03-30T16:30:28.20978Z","iopub.status.idle":"2023-03-30T16:30:37.980946Z","shell.execute_reply.started":"2023-03-30T16:30:28.20973Z","shell.execute_reply":"2023-03-30T16:30:37.979869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_details = pd.read_csv('/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv',na_values='na')\nprint(driver_details.head(5))","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:30:37.983326Z","iopub.execute_input":"2023-03-30T16:30:37.984386Z","iopub.status.idle":"2023-03-30T16:30:38.026758Z","shell.execute_reply.started":"2023-03-30T16:30:37.984346Z","shell.execute_reply":"2023-03-30T16:30:38.025651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Getting all the images\n\ntrain_image = []\nimage_label = []\n\n\nfor i in range(10):\n    print('now we are in the folder C',i)\n    imgs = os.listdir(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c\"+str(i))\n    for j in range(len(imgs)):\n    #for j in range(100):\n        img_name = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c\"+str(i)+\"/\"+imgs[j]\n        img = cv2.imread(img_name)\n        #img = color.rgb2gray(img)\n        img = img[50:,120:-50]\n        img = cv2.resize(img,(224,224))\n        label = i\n        driver = driver_details[driver_details['img'] == imgs[j]]['subject'].values[0]\n        train_image.append([img,label,driver])\n        image_label.append(i)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:30:38.029776Z","iopub.execute_input":"2023-03-30T16:30:38.030232Z","iopub.status.idle":"2023-03-30T16:36:32.50949Z","shell.execute_reply.started":"2023-03-30T16:30:38.030191Z","shell.execute_reply":"2023-03-30T16:36:32.508416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Randomly shuffling the images\n\nimport random\nrandom.shuffle(train_image)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:32.511423Z","iopub.execute_input":"2023-03-30T16:36:32.512057Z","iopub.status.idle":"2023-03-30T16:36:32.536694Z","shell.execute_reply.started":"2023-03-30T16:36:32.512017Z","shell.execute_reply":"2023-03-30T16:36:32.535508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driv_selected = ['p050', 'p015', 'p022', 'p056']","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:32.539613Z","iopub.execute_input":"2023-03-30T16:36:32.540255Z","iopub.status.idle":"2023-03-30T16:36:32.550381Z","shell.execute_reply.started":"2023-03-30T16:36:32.540217Z","shell.execute_reply":"2023-03-30T16:36:32.5492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Splitting the train and test\n\nX_train= []\ny_train = []\nX_test = []\ny_test = []\nD_train = []\nD_test = []\n\nfor features,labels,drivers in train_image:\n    if drivers in driv_selected:\n        X_test.append(features)\n        y_test.append(labels)\n        D_test.append(drivers)\n    \n    else:\n        X_train.append(features)\n        y_train.append(labels)\n        D_train.append(drivers)\n    \nprint (len(X_train),len(X_test))\nprint (len(y_train),len(y_test))","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:32.551974Z","iopub.execute_input":"2023-03-30T16:36:32.552388Z","iopub.status.idle":"2023-03-30T16:36:32.579342Z","shell.execute_reply.started":"2023-03-30T16:36:32.55235Z","shell.execute_reply":"2023-03-30T16:36:32.578046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Converting images to nparray. Encoding the Y\n\nX_train = np.array(X_train[:6000]).reshape(-1,224,224,3)\nX_test = np.array(X_test[:6000]).reshape(-1,224,224,3)\ny_train = to_categorical(y_train[:6000])\ny_test = to_categorical(y_test[:6000])\n\n\nprint (X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:32.582332Z","iopub.execute_input":"2023-03-30T16:36:32.582981Z","iopub.status.idle":"2023-03-30T16:36:33.020641Z","shell.execute_reply.started":"2023-03-30T16:36:32.582954Z","shell.execute_reply":"2023-03-30T16:36:33.019396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Defining the input\n\nfrom keras.layers import Input\nvgg16_input = Input(shape = (224, 224, 3), name = 'Image_input')\n\n\n## The VGG model\n\nfrom keras.applications.vgg16 import VGG16, preprocess_input\n\n#Get back the convolutional part of a VGG network trained on ImageNet\nmodel_vgg16_conv = VGG16(weights='imagenet', include_top=False, input_tensor = vgg16_input)\nmodel_vgg16_conv.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:33.022199Z","iopub.execute_input":"2023-03-30T16:36:33.023166Z","iopub.status.idle":"2023-03-30T16:36:38.790832Z","shell.execute_reply.started":"2023-03-30T16:36:33.023134Z","shell.execute_reply":"2023-03-30T16:36:38.789998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Use the generated model \nfrom keras.models import Model\n\n\noutput_vgg16_conv = model_vgg16_conv(vgg16_input)\n\n#Add the fully-connected layers \nx=GlobalAveragePooling2D()(output_vgg16_conv)\nx=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.\nx = Dropout(0.1)(x) # **reduce dropout \nx=Dense(1024,activation='relu')(x) #dense layer 2\nx = BatchNormalization()(x)\nx = Dropout(0.5)(x)\nx = Dense(512,activation='relu')(x) #dense layer 3\nx = Dense(10, activation='softmax', name='predictions')(x)\n\nvgg16_pretrained = Model(inputs = vgg16_input, outputs = x)\nvgg16_pretrained.summary()\n\n# Compile CNN model\nsgd = optimizers.SGD(lr = 0.001)\nvgg16_pretrained.compile(loss='categorical_crossentropy',optimizer = sgd,metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:38.791922Z","iopub.execute_input":"2023-03-30T16:36:38.792473Z","iopub.status.idle":"2023-03-30T16:36:38.9968Z","shell.execute_reply.started":"2023-03-30T16:36:38.792441Z","shell.execute_reply":"2023-03-30T16:36:38.995718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.preprocessing.image import ImageDataGenerator\ncheckpointer = ModelCheckpoint('vgg_weights_aug_setval_layers_sgd2.hdf5', verbose=1, save_best_only=True)\nearlystopper = EarlyStopping(monitor='val_loss', patience=10, verbose=1)\n\ndatagen = ImageDataGenerator(\n    height_shift_range=0.5,\n    width_shift_range = 0.5,\n    zoom_range = 0.5,\n    rotation_range=30\n        )\n\ndata_generator = datagen.flow(X_train, y_train, batch_size = 64)\n\nvgg16_model = vgg16_pretrained.fit_generator(data_generator,steps_per_epoch = len(X_train) / 64, callbacks=[checkpointer, earlystopper],\n                                                            epochs = 25, verbose = 2, validation_data = (X_test, y_test))\n","metadata":{"execution":{"iopub.status.busy":"2023-03-30T16:36:38.998363Z","iopub.execute_input":"2023-03-30T16:36:38.999388Z","iopub.status.idle":"2023-03-30T17:15:30.612782Z","shell.execute_reply.started":"2023-03-30T16:36:38.99935Z","shell.execute_reply":"2023-03-30T17:15:30.611792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_model.save('driver_distraction.h5')","metadata":{"execution":{"iopub.status.busy":"2023-03-30T17:17:24.78616Z","iopub.execute_input":"2023-03-30T17:17:24.787127Z","iopub.status.idle":"2023-03-30T17:17:25.148328Z","shell.execute_reply.started":"2023-03-30T17:17:24.787079Z","shell.execute_reply":"2023-03-30T17:17:25.146614Z"},"trusted":true},"execution_count":null,"outputs":[]}]}