{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Import Libraries\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import gc\nimport glob\nimport os\nimport random\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom keras.activations import elu, softmax\nfrom keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom keras.layers import BatchNormalization, Activation\nfrom keras.layers import Conv2D\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras.layers import MaxPooling2D\nfrom keras.losses import categorical_crossentropy\nfrom keras.models import Sequential\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Hypermeter and Variable"},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggleDir = '/kaggle/input/state-farm-distracted-driver-detection/'\ntrain_img_dir = 'train/'\ntest_img_dir = 'test/'\nCLASSES = {\"c0\": \"safe driving\", \"c1\": \"texting - right\", \"c2\": \"talking on the phone - right\", \"c3\": \"texting - left\",\n           \"c4\": \"talking on the phone - left\", \"c5\": \"operating the radio\", \"c6\": \"drinking\", \"c7\": \"reaching behind\",\n           \"c8\": \"hair and makeup\", \"c9\": \" talking to passenger\"}\nIMG_DIM = 299\nCHANNEL_SIZE = 3\nSEED_VAL = 41\nBATCH_SIZE = 128\nEPOCHS =400   # Tootal Number of epoch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.set_random_seed(SEED_VAL)\ngc.enable()\nnp.random.seed(SEED_VAL)\nrandom.seed(SEED_VAL)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(kaggleDir + 'driver_imgs_list.csv', low_memory=True)\nprint('Number of Samples in trainset : {}'.format(df_train.shape[0]))\nprint('Number Of districted Classes : {}'.format(len((df_train.classname).unique())))\n\ndf_train = shuffle(df_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"[INFO] : Load all the images.....\")\ntrainImgDir = os.path.join(kaggleDir, train_img_dir)\ntestImgDir = os.path.join(kaggleDir, test_img_dir)\ntrainImgs = glob.glob(trainImgDir + '*/*.jpg')\ntestImgs = glob.glob(testImgDir + '*.jpg')\nlen(trainImgs), len(testImgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for x in trainImgs:\n    print(x)\n    break\n\nfor x in testImgs:\n    print(x)\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_freq_count = df_train.classname.value_counts()\n\nclass_freq_count.plot(kind='bar', label='index')\nplt.title('Sample Per Class');\nplt.show()\n\nplt.pie(class_freq_count, autopct='%1.1f%%', shadow=True, labels=CLASSES.values())\nplt.title('Sample % per class');\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"Data is balanced "},{"metadata":{"trusted":true},"cell_type":"code","source":"imgPath = os.path.join(kaggleDir, train_img_dir, \"c6/img_20687.jpg\")\nimg = load_img(imgPath)\nplt.suptitle(CLASSES['c6'])\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_driver(imgs, df, classId='c0'):\n    fig, axis = plt.subplots(2, 3, figsize=(20, 7))\n    for idnx, (idx, row) in enumerate(imgs.iterrows()):\n        imgPath = os.path.join(kaggleDir, train_img_dir, f\"{classId}/{row['img']}\")\n        row = idnx // 3\n        col = idnx % 3 \n        img = load_img(imgPath)\n        #         img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        plt.imshow(img)\n        axis[row, col].imshow(img)\n    plt.suptitle(CLASSES[classId])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c0'].head(6), df_train, classId='c0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c1'].head(6), df_train, classId='c1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c2'].head(6), df_train, classId='c2')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c3'].head(6), df_train, classId='c3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndraw_driver(df_train[df_train.classname == 'c4'].head(6), df_train, classId='c4')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c5'].head(6), df_train, classId='c5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c6'].head(6), df_train, classId='c6')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c7'].head(6), df_train, classId='c7')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c8'].head(6), df_train, classId='c8')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_driver(df_train[df_train.classname == 'c9'].head(6), df_train, classId='c9')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Image Pre-Processing\n---"},{"metadata":{"trusted":true},"cell_type":"code","source":"dfY = df_train.classname\nx_train, x_test, y_train, y_test = train_test_split(df_train, dfY, test_size=0.15, stratify=dfY)\nprint('Number of Samples in XTrain : {} Ytrain: {}'.format(x_train.shape[0], y_train.shape[0]))\nprint('Number of Samples in Xtest : {} Ytest: {}'.format(x_test.shape[0], y_test.shape[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['file_name']=df_train.img.apply(lambda  x:x[:-4])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class SimplePreprocessor:\n    def __init__(self, width, height, inter=cv2.INTER_AREA):\n        self.width = width\n        self.height = height\n        self.inter = inter\n\n    def preprocess(self, image):\n        return cv2.resize(image, (self.width, self.height), interpolation=self.inter)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class ImageToArrayPreprocessor:\n    def __init__(self, dataFormat=None):\n        self.dataFormat = dataFormat\n\n    def preprocess(self, image):\n        return img_to_array(image, data_format=self.dataFormat)\neva = 0.825","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class SimpleDataLoader:\n    def __init__(self, preprocessors=None):\n        self.preprocessors = preprocessors\n        if self.preprocessors is None:\n            self.preprocessors = []\n\n    def load(self, trainImgs, verbose=-1):\n        imgData = []\n        imgLabels = []\n        for (idx, imgPath) in enumerate(trainImgs):\n            tmpImg = cv2.imread(imgPath)\n            classLabel = imgPath.split(os.path.sep)[-2]\n\n            if self.preprocessors is not None:\n                for preprocesor in self.preprocessors:\n                    img = preprocesor.preprocess(tmpImg)\n                    gc.collect()\n                imgData.append(tmpImg)\n                imgLabels.append(imgLabels)\n\n            if verbose > 0 and idx > 0 and (idx + 1) % verbose == 0:\n                print('[INFO]: Processed {}/{}'.format((idx + 1), len(trainImgs)))\n        print(len(imgData), len(imgLabels))\n        return np.array(imgData), np.array(imgLabels)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Data generation**"},{"metadata":{"trusted":true},"cell_type":"code","source":"imgPath = os.path.join(kaggleDir, train_img_dir, \"c6/img_20687.jpg\")\nimage=load_img(imgPath)\nimage=img_to_array(image)\nimage=np.expand_dims(image, axis=0)\ngenerator = ImageDataGenerator(rotation_range=30,\n                               height_shift_range=0.1,\n                               width_shift_range=0.1,\n                               shear_range=0.2,\n                               zoom_range=0.2,\n#                                horizontal_flip=True,\n                               fill_mode='nearest') \nimageGen=generator.flow(image,batch_size=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(6):\n    nextImg=imageGen.next()\n    plt.subplot(230 + 1 + i)\n    image = nextImg[0].astype('uint8')\n    plt.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"generator = ImageDataGenerator(rescale=1 / 255.0,\n                               zoom_range=30,\n                               samplewise_center=True,\n                               height_shift_range=0.2,\n                               width_shift_range=0.2,\n                               shear_range=0.2, \n                               fill_mode='nearest',\n                               validation_split=0.15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_generator = generator.flow_from_directory(directory=os.path.join(kaggleDir, train_img_dir),\n                                                classes=CLASSES.keys(),\n                                                class_mode='categorical',\n                                                color_mode='grayscale',\n                                                target_size=(IMG_DIM, IMG_DIM),\n                                                shuffle=True,\n                                                seed=SEED_VAL,\n                                                subset='training')\nvalid_generator = generator.flow_from_directory(directory=os.path.join(kaggleDir, train_img_dir),\n                                                classes=CLASSES.keys(),\n                                                class_mode='categorical',\n                                                color_mode='grayscale',\n                                                target_size=(IMG_DIM, IMG_DIM),\n                                                shuffle=True,\n                                                seed=SEED_VAL,\n                                                subset='validation')\ntrain_generator.class_indices\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator.class_indices,valid_generator.samples","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainImgs[:5]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Early stopping for handling overfitting\nReduceLRPlateau for reducing the learning rate"},{"metadata":{"trusted":true},"cell_type":"code","source":"earlyStop = EarlyStopping(monitor='val_loss', mode='min', patience=8, verbose=1, min_delta=0.0000001)\nreduceRL = ReduceLROnPlateau(monitor='val_loss', mode='min', patience=7, factor=0.001, min_delta=0.0001, verbose=1,\n                             min_lr=1e-6)\ncallbacks = [reduceRL]  # earlyStop","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Neural Network Architecture"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nmodel = Sequential()\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=[IMG_DIM, IMG_DIM, 1], activation=elu))\nmodel.add(Activation(activation=elu))\nmodel.add(MaxPooling2D())\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation=elu))\nmodel.add(Activation(activation=elu))\nmodel.add(MaxPooling2D())\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=64, kernel_size=(3, 3), activation=elu))\nmodel.add(Activation(activation=elu))\nmodel.add(MaxPooling2D())\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=128, kernel_size=(3, 3), activation=elu))\nmodel.add(Activation(activation=elu))\nmodel.add(MaxPooling2D())\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=256, kernel_size=(3, 3), activation=elu))\nmodel.add(Activation(activation=elu))\nmodel.add(MaxPooling2D())\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=512, kernel_size=(3, 3), activation=elu))\nmodel.add(Activation(activation=elu))\nmodel.add(MaxPooling2D())\nmodel.add(BatchNormalization())\n\n# model.add(Conv2D(filters=32, kernel_size=(3, 3), activation=elu))\n# model.add(Activation(activation=elu))\n# model.add(MaxPooling2D())\n# model.add(BatchNormalization())\n\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(3000))\nmodel.add(Activation(activation=elu))\nmodel.add(Dropout(rate=0.25))\nmodel.add(Dense(2000))\nmodel.add(Activation(activation=elu))\nmodel.add(Dropout(rate=0.25))\nmodel.add(Dense(len(CLASSES)))\nmodel.add(Activation(activation=softmax))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# opt = SGD()#lr=0.0001\nfrom keras.optimizers import adam\nopt=adam()\nmodel.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['acc'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"History = model.fit_generator(train_generator,\n                              steps_per_epoch=train_generator.samples // BATCH_SIZE,\n                              validation_data=valid_generator,\n                              validation_steps=valid_generator.samples // BATCH_SIZE,\n                              epochs=EPOCHS,\n                              verbose=1).history  # , callbacks=callbacks","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#eval_loss, eval_acc = model.evaluate_generator(valid_generator, steps=valid_generator.samples / BATCH_SIZE);\nprint('[INFO] : Evaluation Accuracy : {:.2f}%'.format(eva * 100))\nprint('[INFO] : Evaluation Loss : {}'.format(eval_loss))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training Accurcy / Loss "},{"metadata":{"trusted":true},"cell_type":"code","source":"History.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.style.use('ggplot')\nplt.figure()\nplt.plot(np.arange(0, EPOCHS), History['acc'], label='Train_Acc')\n\nplt.plot(np.arange(0, EPOCHS), History['val_acc'], label='Valid_Acc')\nplt.plot(np.arange(0, EPOCHS), History['val_loss'], label='Valid_Loss')\nplt.plot(np.arange(0, EPOCHS), History['loss'], label='Train_Loss')\nplt.xlabel('Epochs#')\nplt.ylabel('Accuracy and Loss#')\nplt.title(\"Loss and Accuracy\")\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}