{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"},{"sourceId":17748,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":14783,"modelId":22393}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfrom glob import glob\nimport random\nimport time\nimport tensorflow\nimport datetime\nos.environ['KERAS_BACKEND'] = 'tensorflow'\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 3 = INFO, WARNING, and ERROR\nfrom tqdm import tqdm\nimport numpy as np\nimport pandas as pd\nfrom IPython.display import FileLink\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\nimport seaborn as sns \n%matplotlib inline\nfrom IPython.display import display, Image\nimport matplotlib.image as mpimg\nimport cv2\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.datasets import load_files       \n\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import log_loss , confusion_matrix, ConfusionMatrixDisplay\n\nfrom tensorflow import keras \nfrom keras.utils import to_categorical\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, GlobalAveragePooling2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization, concatenate\n\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import concatenate, Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.applications import InceptionV3, ResNet50\nfrom tensorflow.keras.optimizers import Adam\n\nfrom keras.models import load_model\ndataset = pd.read_csv('../input/state-farm-distracted-driver-detection/driver_imgs_list.csv')\ndataset.head(5)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-06T15:38:31.806004Z","iopub.execute_input":"2024-10-06T15:38:31.806404Z","iopub.status.idle":"2024-10-06T15:38:51.250500Z","shell.execute_reply.started":"2024-10-06T15:38:31.806370Z","shell.execute_reply":"2024-10-06T15:38:51.249005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUMBER_CLASSES = 10 # 10 classes\n# Read with opencv\ndef get_cv2_image(path, img_rows, img_cols, color_type=3):\n    if color_type == 1: # Loading as Grayscale image\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    elif color_type == 3: # Loading as color image\n        img = cv2.imread(path, cv2.IMREAD_COLOR)\n    img = cv2.resize(img, (img_rows, img_cols)) # Reduce size\n    return img\n\n# Loading Training dataset\ndef load_train(img_rows, img_cols, color_type=3):\n    train_images = [] \n    train_labels = []\n    # Loop over the training folder \n    for classed in tqdm(range(NUMBER_CLASSES)):\n        print('Loading directory c{}'.format(classed))\n        files = glob(os.path.join('../input/state-farm-distracted-driver-detection/imgs/train/c' + str(classed), '*.jpg'))\n        for file in files:\n            img = get_cv2_image(file, img_rows, img_cols, color_type)\n            train_images.append(img)\n            train_labels.append(classed)\n    return train_images, train_labels \n\ndef read_and_normalize_train_data(img_rows, img_cols, color_type):\n    X, labels = load_train(img_rows, img_cols, color_type)\n    y = to_categorical(labels, 10) #categorical train label\n    x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # split into train and test\n    x_train = np.array(x_train, dtype=np.uint8).reshape(-1,img_rows,img_cols,color_type)\n    x_test = np.array(x_test, dtype=np.uint8).reshape(-1,img_rows,img_cols,color_type)\n    \n    return x_train, x_test, y_train, y_test\n\n# Loading validation dataset\ndef load_test(size=200000, img_rows=64, img_cols=64, color_type=3):\n    path = os.path.join('/kaggle/input/state-farm-distracted-driver-detection/imgs/test', '*.jpg')\n    files = sorted(glob(path))\n    X_test, X_test_id = [], []\n    total = 0\n    files_size = len(files)\n    for file in tqdm(files):\n        if total >= size or total >= files_size:\n            break\n        file_base = os.path.basename(file)\n        img = get_cv2_image(file, img_rows, img_cols, color_type)\n        X_test.append(img)\n        X_test_id.append(file_base)\n        total += 1\n    return X_test, X_test_id\n\ndef read_and_normalize_sampled_test_data(size, img_rows, img_cols, color_type=3):\n    test_data, test_ids = load_test(size, img_rows, img_cols, color_type)   \n    test_data = np.array(test_data, dtype=np.uint8)\n    test_data = test_data.reshape(-1,img_rows,img_cols,color_type)\n    return test_data, test_ids","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rows = 64 \nimg_cols = 64\ncolor_type = 1 \nnb_test_samples = 200\n\n# loading train images\nx_train, x_test, y_train, y_test = read_and_normalize_train_data(img_rows, img_cols, color_type)\n\n# loading validation images\ntest_files, test_targets = read_and_normalize_sampled_test_data(nb_test_samples, img_rows, img_cols, color_type)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-24T12:25:57.478753Z","iopub.execute_input":"2024-06-24T12:25:57.479098Z","iopub.status.idle":"2024-06-24T12:29:38.618533Z","shell.execute_reply.started":"2024-06-24T12:25:57.479071Z","shell.execute_reply":"2024-06-24T12:29:38.617562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\npx.histogram(dataset, x=\"classname\", color=\"classname\", title=\"Number of images by categories \")","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:03:25.673974Z","iopub.execute_input":"2024-03-13T11:03:25.674261Z","iopub.status.idle":"2024-03-13T11:03:28.783993Z","shell.execute_reply.started":"2024-03-13T11:03:25.674237Z","shell.execute_reply":"2024-03-13T11:03:28.783096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activity_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'}\nplt.figure(figsize = (12, 20))\nimage_count = 1\nBASE_URL = '../input/state-farm-distracted-driver-detection/imgs/train/'\nfor directory in os.listdir(BASE_URL):\n    if directory[0] != '.':\n        for i, file in enumerate(os.listdir(BASE_URL + directory)):\n            if i == 1:\n                break\n            else:\n                fig = plt.subplot(5, 2, image_count)\n                image_count += 1\n                image = mpimg.imread(BASE_URL + directory + '/' + file)\n                plt.imshow(image)\n                plt.title(activity_map[directory])","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:03:28.785130Z","iopub.execute_input":"2024-03-13T11:03:28.785409Z","iopub.status.idle":"2024-03-13T11:03:31.912262Z","shell.execute_reply.started":"2024-03-13T11:03:28.785385Z","shell.execute_reply":"2024-03-13T11:03:31.910787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#inception model \n\ndef create_inception_model():\n    inputs = Input(shape=(img_rows, img_cols, color_type))\n    \n    tower_1 = Conv2D(32, (1, 1), padding='same', activation='relu')(inputs)\n    tower_1 = Conv2D(32, (5, 5), padding='same', activation='relu')(tower_1)\n\n    tower_2 = Conv2D(32, (1, 1), padding='same', activation='relu')(inputs)\n    tower_2 = Conv2D(32, (3, 3), padding='same', activation='relu')(tower_2)\n\n    tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(inputs)\n    tower_3 = Conv2D(32, (1, 1), padding='same', activation='relu')(tower_3)\n\n    x = concatenate([tower_1, tower_2, tower_3], axis=-1)\n    x = BatchNormalization()(x)\n    x = MaxPooling2D(pool_size=(2, 2))(x)\n    x = Dropout(0.3)(x)\n\n    x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)\n    x = BatchNormalization()(x)\n    x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)\n    x = BatchNormalization()(x)\n    x = MaxPooling2D(pool_size=(2, 2))(x)\n    x = Dropout(0.3)(x)\n\n    x = Conv2D(128, (3, 3), padding='same', activation='relu')(x)\n    x = BatchNormalization()(x)\n    x = Conv2D(128, (3, 3), padding='same', activation='relu')(x)\n    x = BatchNormalization()(x)\n    x = MaxPooling2D(pool_size=(2, 2))(x)\n    x = Dropout(0.5)(x)\n\n    x = Flatten()(x)\n    x = Dense(512, activation='relu')(x)\n    x = BatchNormalization()(x)\n    x = Dropout(0.5)(x)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.25)(x)\n    outputs = Dense(10, activation='softmax')(x)\n\n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:03:31.913451Z","iopub.execute_input":"2024-03-13T11:03:31.913774Z","iopub.status.idle":"2024-03-13T11:03:31.928489Z","shell.execute_reply.started":"2024-03-13T11:03:31.913748Z","shell.execute_reply":"2024-03-13T11:03:31.927361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_inception_model()\n\n# More details about the layers\nmodel.summary()\n\n# Compiling the model\nmodel.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:03:31.929569Z","iopub.execute_input":"2024-03-13T11:03:31.929847Z","iopub.status.idle":"2024-03-13T11:03:33.348702Z","shell.execute_reply.started":"2024-03-13T11:03:31.929825Z","shell.execute_reply":"2024-03-13T11:03:33.347790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\ncallback = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:57:35.781162Z","iopub.execute_input":"2024-03-13T11:57:35.781915Z","iopub.status.idle":"2024-03-13T11:57:35.787310Z","shell.execute_reply.started":"2024-03-13T11:57:35.781882Z","shell.execute_reply":"2024-03-13T11:57:35.786395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory = model.fit(x_train, y_train,validation_data=(x_test, y_test),\n          epochs=50, batch_size=32, verbose=1,callbacks=callback)\n\n# model.save('/kaggle/working/newmodel_1.h5')\n\nval_loss , Accurecy = model.evaluate(x_test , y_test)\n\nprint(f\"Accurecy = {Accurecy * 100 :.2f} %\")","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:00:45.301756Z","iopub.execute_input":"2024-03-13T12:00:45.302680Z","iopub.status.idle":"2024-03-13T12:02:44.629700Z","shell.execute_reply.started":"2024-03-13T12:00:45.302648Z","shell.execute_reply":"2024-03-13T12:02:44.628784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_train_history(history):\n    \"\"\"\n    Plot the validation accuracy and validation loss over epochs\n    \"\"\"\n    # Summarize history for accuracy\n    plt.plot(history.history['accuracy'])\n    plt.plot(history.history['val_accuracy'])\n    plt.title('Model accuracy')\n    plt.ylabel('accuracy')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'test'], loc='upper left')\n    plt.show()\n\n    # Summarize history for loss\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', 'test'], loc='upper left')\n    plt.show()\n    \nplot_train_history(history)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:36:59.371086Z","iopub.execute_input":"2024-03-13T11:36:59.371967Z","iopub.status.idle":"2024-03-13T11:36:59.807627Z","shell.execute_reply.started":"2024-03-13T11:36:59.371930Z","shell.execute_reply":"2024-03-13T11:36:59.806715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# when we run saved model\nmodel_saved = load_model(\"/kaggle/input/models_1/tensorflow2/models/1/newmodel_1.h5\")\n\nres = model_saved.evaluate(x_test , y_test )\n\nAccurecy = res[1]\n\nprint(f\"Accurecy = {Accurecy * 100 :.2f} %\")","metadata":{"execution":{"iopub.status.busy":"2024-06-24T12:30:36.550868Z","iopub.execute_input":"2024-06-24T12:30:36.551588Z","iopub.status.idle":"2024-06-24T12:30:43.870375Z","shell.execute_reply.started":"2024-06-24T12:30:36.551557Z","shell.execute_reply":"2024-06-24T12:30:43.869403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_saved.predict(x_test)\ny_pred_classes = np.argmax(y_pred, axis=1)\ny_true = np.argmax(y_test, axis=1)\n\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred_classes)\n\n# Display confusion matrix\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=range(NUMBER_CLASSES))\ndisp.plot(cmap=plt.cm.Blues)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-24T12:30:58.410901Z","iopub.execute_input":"2024-06-24T12:30:58.411692Z","iopub.status.idle":"2024-06-24T12:31:00.237725Z","shell.execute_reply.started":"2024-06-24T12:30:58.411660Z","shell.execute_reply":"2024-06-24T12:31:00.236800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_test_class(model, test_files, image_number, color_type=1):\n    \"\"\"\n    Function that tests or model on test images and show the results\n    \"\"\"\n    img_brute = test_files[image_number]\n    img_brute = cv2.resize(img_brute,(img_rows,img_cols))\n    plt.imshow(img_brute, cmap='gray')\n\n    new_img = img_brute.reshape(-1,img_rows,img_cols,color_type)\n   \n    y_prediction = model.predict(new_img, batch_size=32, verbose=1)\n    predicted_class = np.argmax(y_prediction)\n#     print('Y prediction: {}'.format(y_prediction))\n    print('Predicted: {}'.format(activity_map.get('c{}'.format(np.argmax(y_prediction)))))\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:02:58.823409Z","iopub.execute_input":"2024-03-13T12:02:58.824086Z","iopub.status.idle":"2024-03-13T12:02:58.831622Z","shell.execute_reply.started":"2024-03-13T12:02:58.824054Z","shell.execute_reply":"2024-03-13T12:02:58.830639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score1 = model_saved.evaluate(x_test, y_test, verbose=1)\nprint(f'Accuracy: { score1[1]*100:.2f} %')","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:03:01.413664Z","iopub.execute_input":"2024-03-13T12:03:01.414059Z","iopub.status.idle":"2024-03-13T12:03:02.709821Z","shell.execute_reply.started":"2024-03-13T12:03:01.414030Z","shell.execute_reply":"2024-03-13T12:03:02.708944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(40):\n    plot_test_class(model_saved, test_files, i)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:03:04.425489Z","iopub.execute_input":"2024-03-13T12:03:04.426348Z","iopub.status.idle":"2024-03-13T12:03:13.508794Z","shell.execute_reply.started":"2024-03-13T12:03:04.426313Z","shell.execute_reply":"2024-03-13T12:03:13.507909Z"},"trusted":true},"execution_count":null,"outputs":[]}]}