{"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":"markdown","source":"# Machine Learning Engineer Nanodegree\n\n## Capstone Project\n\n## Project: Write an Algorithm for Distracted Driver Detection \n\n---\n\n\n\n>**Note:** Code and Markdown cells can be executed using the **Shift + Enter** keyboard shortcut.  Markdown cells can be edited by double-clicking the cell to enter edit mode.\n\n\n\n---\n\n### The Road Ahead\n\nThe notebook is broken into separate steps as shown below.\n\n* [Step 0](#step0): Import Datasets\n* [Step 1](#step1): Create and train a CNN to Classify Driver Images (from Scratch)\n* [Step 2](#step3): Train a CNN with Transfer Learning (Using Fine-tuned VGG16)\n* [Step 3](#step4): Kaggle Results\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true}},{"cell_type":"markdown","source":"## Defining the train,test and model directories¶","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport os\nimport keras\n\nfrom keras import regularizers\nfrom keras.models import Model, Sequential\nfrom keras.preprocessing import image    \nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.layers import ZeroPadding2D, Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Input\nfrom keras.layers import GlobalAveragePooling2D, MaxPooling2D\nfrom keras.models import Sequential\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.utils import to_categorical\nfrom sklearn.metrics import confusion_matrix\n\nfrom tqdm import tqdm\n\nimport seaborn as sns\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score\n%matplotlib inline\n","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:45:10.068925Z","iopub.execute_input":"2021-10-26T06:45:10.069452Z","iopub.status.idle":"2021-10-26T06:45:11.868998Z","shell.execute_reply.started":"2021-10-26T06:45:10.069369Z","shell.execute_reply":"2021-10-26T06:45:11.867575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE   = 224 \nCOLOR_TYPE = 3\nCLASSES    = 10\nEPOCHS     = 6\nBATCHES    = 32\nLearning_rate = 0.001","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:45:11.870624Z","iopub.execute_input":"2021-10-26T06:45:11.870903Z","iopub.status.idle":"2021-10-26T06:45:11.875983Z","shell.execute_reply.started":"2021-10-26T06:45:11.870856Z","shell.execute_reply":"2021-10-26T06:45:11.875059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"../input/state-farm-distracted-driver-detection/imgs\"\nTEST_DIR  = os.path.join(DATA_DIR,\"test\")\nTRAIN_DIR = os.path.join(DATA_DIR,\"train\")\nRANDOM_DIR = '../input/driverdistractionimages'\n\nMODEL_PATH = os.path.join(os.getcwd(),\"model\",\"trained\")\n# PICKLE_DIR = os.path.join(os.getcwd(),\"pickle_files\")\nCSV_DIR = os.path.join(os.getcwd(),\"csv_files\")","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:45:39.511881Z","iopub.execute_input":"2021-10-26T06:45:39.512167Z","iopub.status.idle":"2021-10-26T06:45:39.517456Z","shell.execute_reply.started":"2021-10-26T06:45:39.512122Z","shell.execute_reply":"2021-10-26T06:45:39.516448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(TRAIN_DIR):\n    print(\"Training data does not exists\")\nif not os.path.exists(TEST_DIR):\n    print(\"Testing data does not exists\")\nif not os.path.exists(RANDOM_DIR):\n    print(\"Random data does not exists\")\n############################################    \nif not os.path.exists(MODEL_PATH):\n    print(\"Model path does not exists\")\n    os.makedirs(MODEL_PATH)\n    print(\"Model path created\")\n############################################\nif not os.path.exists(CSV_DIR):\n    os.makedirs(CSV_DIR)","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:45:40.298549Z","iopub.execute_input":"2021-10-26T06:45:40.29885Z","iopub.status.idle":"2021-10-26T06:45:40.309828Z","shell.execute_reply.started":"2021-10-26T06:45:40.298797Z","shell.execute_reply":"2021-10-26T06:45:40.308795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"markdown","source":"### Train and valid CSV Files","metadata":{}},{"cell_type":"code","source":"def create_csv(DATA_DIR,filename):\n    class_names = os.listdir(DATA_DIR) # train , test\n    data = list()\n    \n    if(os.path.isdir(os.path.join(DATA_DIR,class_names[0]))):            # if in train file\n        for class_name in class_names:                                   # which class in the train file\n            file_names = os.listdir(os.path.join(DATA_DIR,class_name))   # images in this class (c0, c1, ...., c9)\n            for file in file_names:                                      # create a dic of the image path and its class name(c0, c1, ...., c9)\n                data.append({\n                    \"Filename\":os.path.join(DATA_DIR,class_name,file),\n                    \"ClassName\":class_name\n                })\n    else:                                                                 # if in test file\n        class_name = \"test\"\n        file_names = os.listdir(DATA_DIR)                                 # images in the test file\n        for file in file_names:                                           # create a dic of the image path and its class name (test)\n            data.append(({\n                \"FileName\":os.path.join(DATA_DIR,file),\n                \"ClassName\":class_name\n            }))\n    \n    data = pd.DataFrame(data)                                               # create a DataFrame using Pandas\n    data.to_csv(os.path.join(os.getcwd(),\"csv_files\",filename),index=False) # convert the DataFrame to CSV file","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:45:47.847234Z","iopub.execute_input":"2021-10-26T06:45:47.847547Z","iopub.status.idle":"2021-10-26T06:45:47.856162Z","shell.execute_reply.started":"2021-10-26T06:45:47.847491Z","shell.execute_reply":"2021-10-26T06:45:47.855322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_csv(TRAIN_DIR,\"train.csv\")\ncreate_csv(TEST_DIR,\"test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:49:39.721146Z","iopub.execute_input":"2021-10-26T06:49:39.721468Z","iopub.status.idle":"2021-10-26T06:49:40.585923Z","shell.execute_reply.started":"2021-10-26T06:49:39.721394Z","shell.execute_reply":"2021-10-26T06:49:40.585166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"train.csv\"))\ndata_train.info()","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:49:44.630361Z","iopub.execute_input":"2021-10-26T06:49:44.630825Z","iopub.status.idle":"2021-10-26T06:49:44.679496Z","shell.execute_reply.started":"2021-10-26T06:49:44.630731Z","shell.execute_reply":"2021-10-26T06:49:44.678746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train['ClassName'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-26T06:49:48.006164Z","iopub.execute_input":"2021-10-26T06:49:48.006472Z","iopub.status.idle":"2021-10-26T06:49:48.024727Z","shell.execute_reply.started":"2021-10-26T06:49:48.006394Z","shell.execute_reply":"2021-10-26T06:49:48.023895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.describe()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T01:51:46.457007Z","iopub.execute_input":"2021-07-09T01:51:46.457254Z","iopub.status.idle":"2021-07-09T01:51:46.512549Z","shell.execute_reply.started":"2021-07-09T01:51:46.457207Z","shell.execute_reply":"2021-07-09T01:51:46.511828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\n\ncount_img_in_each_class= data_train['ClassName'].value_counts(sort=False)\ncategories = data_train['ClassName'].value_counts(sort=False).index.tolist()\n\ny = np.array(count_img_in_each_class)\nwidth = 1/1.5\nN = len(y)\nx = range(N)\n\nfig = plt.figure(figsize=(20,15))\nay = fig.add_subplot(211)\n\nplt.xticks(x, categories, size=15)\nplt.yticks(size=15)\n\nay.bar(x, y, width, color=\"#169DE3\")\n\nplt.title('Bar Chart',size=25)\nplt.xlabel('classname',size=15)\nplt.ylabel('Count',size=15)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T01:51:46.668007Z","iopub.execute_input":"2021-07-09T01:51:46.668228Z","iopub.status.idle":"2021-07-09T01:51:47.184306Z","shell.execute_reply.started":"2021-07-09T01:51:46.668184Z","shell.execute_reply":"2021-07-09T01:51:47.183617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Converting into numerical values¶","metadata":{}},{"cell_type":"code","source":"labels_list = list(set(data_train['ClassName'].values.tolist()))\ndata_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T01:51:47.436048Z","iopub.execute_input":"2021-07-09T01:51:47.436288Z","iopub.status.idle":"2021-07-09T01:51:47.453996Z","shell.execute_reply.started":"2021-07-09T01:51:47.436242Z","shell.execute_reply":"2021-07-09T01:51:47.453426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nfor label in labels_list : \n\n    for id in re.findall(r'\\d+',label) : \n        data_train['ClassName'].replace(label,id,inplace=True)\n\ndata_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T01:51:56.128396Z","iopub.execute_input":"2021-07-09T01:51:56.128789Z","iopub.status.idle":"2021-07-09T01:51:56.158938Z","shell.execute_reply.started":"2021-07-09T01:51:56.128713Z","shell.execute_reply":"2021-07-09T01:51:56.157791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Converting into categorical values","metadata":{}},{"cell_type":"code","source":"labels = to_categorical(data_train['ClassName'])\nprint(labels.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T01:51:57.119472Z","iopub.execute_input":"2021-07-09T01:51:57.119776Z","iopub.status.idle":"2021-07-09T01:51:57.130363Z","shell.execute_reply.started":"2021-07-09T01:51:57.119718Z","shell.execute_reply":"2021-07-09T01:51:57.129348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"step0\"></a>\n## Step 0: Import Datasets\n\n### Import Driver Dataset\n\nIn the following code cell, we create a instance of ImageDataGenerator which does all preprocessing operations on images that we are going to feed to our CNN","metadata":{}},{"cell_type":"code","source":"gen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\ntest_gen = ImageDataGenerator(rescale=1./255)\n\n#         shear_range=0.2,\n#         zoom_range=0.2,\n#         horizontal_flip=True)\n\n# ,height_shift_range=0.5,\n# width_shift_range = 0.5,\n# rotation_range=30,\n","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2021-07-09T03:43:10.153664Z","iopub.execute_input":"2021-07-09T03:43:10.153939Z","iopub.status.idle":"2021-07-09T03:43:10.158533Z","shell.execute_reply.started":"2021-07-09T03:43:10.153885Z","shell.execute_reply":"2021-07-09T03:43:10.157681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the following code, we are loading only 32 images at once into memory and performing all the preprocessing operations on loaded images. The flow_from_directory loads a defined set of images from the location of images, instead of loading all images at once into memory.\n\n- train_generator contains training set\n- val_generator contains validation set","metadata":{}},{"cell_type":"code","source":"\ntrain_generator = gen.flow_from_directory(\n        TRAIN_DIR,\n        target_size=(IMG_SIZE, IMG_SIZE),\n        batch_size=BATCHES,\n        class_mode='categorical',\n        subset='training')\n\n\nval_generator = gen.flow_from_directory(\n        TRAIN_DIR,\n        target_size=(IMG_SIZE,IMG_SIZE),\n        batch_size=BATCHES,\n        class_mode='categorical',\n        subset='validation')\n\ntest_generator = gen.flow_from_directory(\n        DATA_DIR,\n        classes=['test'],\n        target_size=(IMG_SIZE,IMG_SIZE),\n        shuffle=False,\n        class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:43:22.998843Z","iopub.execute_input":"2021-07-09T03:43:22.999591Z","iopub.status.idle":"2021-07-09T03:50:58.507819Z","shell.execute_reply.started":"2021-07-09T03:43:22.999435Z","shell.execute_reply":"2021-07-09T03:50:58.507027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nLabels_Map=  [\"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\"]\n\nCAT_MAP = {'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'}\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:50:58.509051Z","iopub.execute_input":"2021-07-09T03:50:58.509588Z","iopub.status.idle":"2021-07-09T03:50:58.518138Z","shell.execute_reply.started":"2021-07-09T03:50:58.509529Z","shell.execute_reply":"2021-07-09T03:50:58.517311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, labels = next(train_generator)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:50:58.519369Z","iopub.execute_input":"2021-07-09T03:50:58.519856Z","iopub.status.idle":"2021-07-09T03:50:58.74905Z","shell.execute_reply.started":"2021-07-09T03:50:58.519629Z","shell.execute_reply":"2021-07-09T03:50:58.748401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = next(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:50:58.750364Z","iopub.execute_input":"2021-07-09T03:50:58.750632Z","iopub.status.idle":"2021-07-09T03:50:59.209703Z","shell.execute_reply.started":"2021-07-09T03:50:58.75059Z","shell.execute_reply":"2021-07-09T03:50:59.208962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id=\"step2\"></a>\n##  Step 2: Train a CNN with Transfer Learning (Using Fine-tuned VGG16 Model)\n\n- In the following steps, we are going to load a VGG16 model, without top Fully connected layers, with its trained weights\n- Then we are going add Fully Connected Layers on top of GlobalAveragePooling layer with Dropout layers in between\n- The following code block contains a method load_VGG16, which loads VGG16 model with weights loaded when weights file location is given","metadata":{}},{"cell_type":"code","source":"\ndef load_VGG16(weights_path=None, no_top=True):\n\n    input_shape = (IMG_SIZE, IMG_SIZE, COLOR_TYPE)\n\n    #Instantiate an empty model\n    img_input = Input(shape=input_shape)   # Block 1\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)\n\n    # Block 2\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n\n    # Block 3\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n\n    # Block 4\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n\n    # Block 5\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)\n    \n    x = GlobalAveragePooling2D()(x)\n    my_model = Model(img_input, x, name='vgg16')\n    \n    if weights_path is not None:\n        print(\"Weights have been loaded.\")\n        my_model.load_weights(weights_path)\n\n    return my_model\n","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:50:59.212946Z","iopub.execute_input":"2021-07-09T03:50:59.213166Z","iopub.status.idle":"2021-07-09T03:50:59.226899Z","shell.execute_reply.started":"2021-07-09T03:50:59.213127Z","shell.execute_reply":"2021-07-09T03:50:59.225926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- In the following code block, we have added 3 Dense layers with dropout layers on top of VGG16 CNN model.","metadata":{}},{"cell_type":"code","source":"vgg_model_raw = load_VGG16('../input/d/towever/vgg16-weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')\n\nvgg_model = vgg_model_raw.output\n\nvgg_model = Dense(5000, activation='relu',kernel_regularizer=regularizers.l2(0.00001))(vgg_model)\nvgg_model = Dropout(0.1)(vgg_model)\n\nvgg_model = Dense(500, activation='relu',kernel_regularizer=regularizers.l2(0.00001))(vgg_model)\nvgg_model = Dropout(0.1)(vgg_model)\n\nvgg_model = Dense(10, activation='softmax')(vgg_model)\n\nmy_vgg_model = Model(inputs=vgg_model_raw.input, outputs= vgg_model)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:50:59.227993Z","iopub.execute_input":"2021-07-09T03:50:59.228255Z","iopub.status.idle":"2021-07-09T03:51:01.342705Z","shell.execute_reply.started":"2021-07-09T03:50:59.228211Z","shell.execute_reply":"2021-07-09T03:51:01.341882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compile the vgg16 model with categorical crossentropy as loss function and SGD as optimizer","metadata":{}},{"cell_type":"code","source":"my_vgg_model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(0.001), metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:51:01.343792Z","iopub.execute_input":"2021-07-09T03:51:01.344054Z","iopub.status.idle":"2021-07-09T03:51:01.383853Z","shell.execute_reply.started":"2021-07-09T03:51:01.344001Z","shell.execute_reply":"2021-07-09T03:51:01.383122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.optimizers import Adam\n# opt = Adam(lr=Learning_rate)\n\n# model.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T02:20:11.317937Z","iopub.execute_input":"2021-07-09T02:20:11.318214Z","iopub.status.idle":"2021-07-09T02:20:11.361639Z","shell.execute_reply.started":"2021-07-09T02:20:11.318163Z","shell.execute_reply":"2021-07-09T02:20:11.360922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_vgg_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T03:51:01.385161Z","iopub.execute_input":"2021-07-09T03:51:01.385563Z","iopub.status.idle":"2021-07-09T03:51:01.397044Z","shell.execute_reply.started":"2021-07-09T03:51:01.385388Z","shell.execute_reply":"2021-07-09T03:51:01.395141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Create a Checkpoint to save best weights, when loss in improvised\n- Train the model for 6 epochs","metadata":{}},{"cell_type":"code","source":"\ncheckpoint = ModelCheckpoint('vgg_model.h5', \n                             save_best_only=True, \n                             verbose=1)\n\n# early = EarlyStopping(monitor='val_acc',\n#                       min_delta=0,\n#                       verbose=1,\n#                       mode='auto')\n\nHist = model.fit_generator(train_generator,\n                           steps_per_epoch=len(train_generator),\n                           epochs=30,\n                           validation_data = val_generator,\n                           validation_steps=len(val_generator),\n                           callbacks=[checkpoint] )","metadata":{"execution":{"iopub.status.busy":"2021-07-09T04:45:53.419794Z","iopub.execute_input":"2021-07-09T04:45:53.420072Z","iopub.status.idle":"2021-07-09T06:26:48.811739Z","shell.execute_reply.started":"2021-07-09T04:45:53.420023Z","shell.execute_reply":"2021-07-09T06:26:48.808677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visulaizing The Model History","metadata":{}},{"cell_type":"code","source":"print(Hist.history.keys())","metadata":{"execution":{"iopub.status.busy":"2021-07-09T06:37:52.749318Z","iopub.execute_input":"2021-07-09T06:37:52.75071Z","iopub.status.idle":"2021-07-09T06:37:52.755451Z","shell.execute_reply.started":"2021-07-09T06:37:52.750637Z","shell.execute_reply":"2021-07-09T06:37:52.754572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_train_history(history = Hist):\n    # Summarize history for accuracy\n    plt.figure(figsize = (8, 5))\n    #plt.xticks(np.arange(0, 10))\n    #plt.yticks(np.arange(0, 100))\n    plt.plot(history.history['acc'])\n    plt.plot(history.history['val_acc'])\n    plt.title('Model Accuracy')\n    plt.ylabel('Accuracy')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='lower right')\n    plt.show()\n\n    # Summarize history for loss\n    plt.figure(figsize = (8, 5))\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title('Model loss')\n    plt.ylabel('loss')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'validation'], loc='upper right')\n    plt.show()\n\nplot_train_history(Hist)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T06:37:55.232075Z","iopub.execute_input":"2021-07-09T06:37:55.232339Z","iopub.status.idle":"2021-07-09T06:37:55.902303Z","shell.execute_reply.started":"2021-07-09T06:37:55.232291Z","shell.execute_reply":"2021-07-09T06:37:55.901557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(Hist.history[\"acc\"])\nplt.plot(Hist.history['val_acc'])\nplt.plot(Hist.history['loss'])\nplt.plot(Hist.history['val_loss'])\nplt.title(\"model accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.legend([\"Accuracy\",\"Validation Accuracy\",\"loss\",\"Validation Loss\"])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T06:38:01.348204Z","iopub.execute_input":"2021-07-09T06:38:01.348531Z","iopub.status.idle":"2021-07-09T06:38:01.61334Z","shell.execute_reply.started":"2021-07-09T06:38:01.348476Z","shell.execute_reply":"2021-07-09T06:38:01.612564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save Model","metadata":{}},{"cell_type":"code","source":"Model_path =os.path.join(MODEL_PATH,\"the-complete-model.hdf5\")\nvgg_m.save(Model_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T06:38:07.436807Z","iopub.execute_input":"2021-07-09T06:38:07.43707Z","iopub.status.idle":"2021-07-09T06:38:08.254633Z","shell.execute_reply.started":"2021-07-09T06:38:07.437024Z","shell.execute_reply":"2021-07-09T06:38:08.253946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing","metadata":{}},{"cell_type":"code","source":"# import glob\n\n# images_paths = list(glob.glob(os.path.join('../input/driverdistractionimages','*.*')))\n# tensors = paths_to_tensor(images_paths)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T00:32:22.638779Z","iopub.execute_input":"2021-07-09T00:32:22.639043Z","iopub.status.idle":"2021-07-09T00:32:22.642424Z","shell.execute_reply.started":"2021-07-09T00:32:22.638999Z","shell.execute_reply":"2021-07-09T00:32:22.641661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_generator.reset() \ny = model.predict_generator(val_generator,steps= len(val_generator)//32 )","metadata":{"execution":{"iopub.status.busy":"2021-07-09T01:08:41.934935Z","iopub.execute_input":"2021-07-09T01:08:41.935263Z","iopub.status.idle":"2021-07-09T01:08:42.855337Z","shell.execute_reply.started":"2021-07-09T01:08:41.93521Z","shell.execute_reply":"2021-07-09T01:08:42.85456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport matplotlib.image as mpimg\n# mapping categotical\n\nclasses = [ 'safe driving', ' texting - right', 'talking on the phone - right',\n           'texting - left', 'talking on the phone - left', 'operating the radio',\n           'drinking', 'reaching behind', 'hair and makeup', 'talking to passenger']\n\n\nPredictions = model_saved.predict(tensors)\nPredicted_Labels = np.argmax(Predictions, axis = 1)\nfig = plt.figure(figsize = (20,40))\n\nfor i, LAB in enumerate(Predicted_Labels):\n    \n    fig.add_subplot(10, 6, i+1)\n    plt.imshow(tensors[i][:][:])\n    plt.axis('off')\n    plt.title('Predicted Label: \\n C{}->{}'.format(LAB , classes[LAB]))","metadata":{"execution":{"iopub.status.busy":"2021-07-08T23:12:59.507655Z","iopub.execute_input":"2021-07-08T23:12:59.507924Z","iopub.status.idle":"2021-07-08T23:13:04.234535Z","shell.execute_reply.started":"2021-07-08T23:12:59.507873Z","shell.execute_reply":"2021-07-08T23:13:04.233813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Following method gives a batch of 32 images at each call, just like a generator","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}