{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":28450,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Distraction Driver Detection Project\n\n> In this notebook, I'll use the dataset which includes images of drivers while performing a number of tasks including drinking, texting etc. The aim is to correctly identify if the driver is distracted from driving. We might also like to check what activity the person is performing.\n\nThe notebook will be borken into the following steps:\n\n0. Import the Libraries.\n1. Import the Datasets.\n2. Create a vanilla CNN model.\n3. Create a vanilla CNN model with data augmentation.\n4. Train a CNN with Transfer Learning (VGG16).\n5. Kaggle Results.\n","metadata":{}},{"cell_type":"markdown","source":"## Import the Libraries\n\nI'll use Keras and Tensorflow libraries to create a **Convolutional Neural Network**. So, I'll import the necessary libraries to do the same.","metadata":{}},{"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 messages are not printed\n\nfrom tqdm import tqdm\n\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       \nfrom keras.utils import np_utils\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import log_loss\n\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, GlobalAveragePooling2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.applications.vgg16 import VGG16","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-27T05:25:10.431353Z","iopub.execute_input":"2023-12-27T05:25:10.431697Z","iopub.status.idle":"2023-12-27T05:25:12.843194Z","shell.execute_reply.started":"2023-12-27T05:25:10.431633Z","shell.execute_reply":"2023-12-27T05:25:12.841956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import the Datasets\n\nI'll import the `.csv` file to read the labels.","metadata":{}},{"cell_type":"code","source":"dataset = pd.read_csv('../input/driver_imgs_list.csv')\ndataset","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:25:12.845420Z","iopub.execute_input":"2023-12-27T05:25:12.845723Z","iopub.status.idle":"2023-12-27T05:25:12.925180Z","shell.execute_reply.started":"2023-12-27T05:25:12.845667Z","shell.execute_reply":"2023-12-27T05:25:12.924116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the csv file, I'll use the `classname` as the labels for the images and use the image names to match the labels with the correct images.","metadata":{}},{"cell_type":"markdown","source":"#### Import Driver Dataset","metadata":{}},{"cell_type":"code","source":"by_drivers = dataset.groupby('subject')\nunique_drivers = by_drivers.groups.keys()\nprint(unique_drivers)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:25:12.926976Z","iopub.execute_input":"2023-12-27T05:25:12.927341Z","iopub.status.idle":"2023-12-27T05:25:12.940338Z","shell.execute_reply.started":"2023-12-27T05:25:12.927278Z","shell.execute_reply":"2023-12-27T05:25:12.939064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the following, I prepare the code to import the dataset of the driver images. then populate a few variables.","metadata":{}},{"cell_type":"code","source":"# Load the dataset previously downloaded from Kaggle\nNUMBER_CLASSES = 10\n# Color type: 1 - grey, 3 - rgb\n\ndef get_cv2_image(path, img_rows, img_cols, color_type=3):\n    # Loading as Grayscale image\n    if color_type == 1:\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    elif color_type == 3:\n        img = cv2.imread(path, cv2.IMREAD_COLOR)\n    # Reduce size\n    img = cv2.resize(img, (img_rows, img_cols)) \n    return img\n\n# Training\ndef load_train(img_rows, img_cols, color_type=3):\n    start_time = time.time()\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('..','state-farm-distracted-driver-detection','imgs', 'train', 'c' + str(classed), '*.jpg'))\n#         directory = os.path.join('..','input','state-farm-distracted-driver-detection', 'imgs', 'train', 'c' + str(classed))\n        directory = os.path.join(f'/kaggle/input/imgs/train/c{str(classed)}')\n        files = [os.path.join(directory, f) for f in os.listdir(directory) if f.lower().endswith('.jpg')]\n#         print(files)\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    print(\"Data Loaded in {} second\".format(time.time() - start_time))\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 = np_utils.to_categorical(labels, 10)\n    x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n    \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# Validation\ndef load_test(size=200000, img_rows=64, img_cols=64, color_type=3):\n    path = os.path.join(f'/kaggle/input/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    \n    test_data = np.array(test_data, dtype=np.uint8)\n    test_data = test_data.reshape(-1,img_rows,img_cols,color_type)\n    \n    return test_data, test_ids","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:25:12.942052Z","iopub.execute_input":"2023-12-27T05:25:12.942406Z","iopub.status.idle":"2023-12-27T05:25:12.970884Z","shell.execute_reply.started":"2023-12-27T05:25:12.942339Z","shell.execute_reply":"2023-12-27T05:25:12.969660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rows = 64\nimg_cols = 64\ncolor_type = 1","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:25:12.975595Z","iopub.execute_input":"2023-12-27T05:25:12.976012Z","iopub.status.idle":"2023-12-27T05:25:12.983540Z","shell.execute_reply.started":"2023-12-27T05:25:12.975927Z","shell.execute_reply":"2023-12-27T05:25:12.982583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = read_and_normalize_train_data(img_rows, img_cols, color_type)\nprint('Train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:25:12.986981Z","iopub.execute_input":"2023-12-27T05:25:12.987324Z","iopub.status.idle":"2023-12-27T05:28:46.592219Z","shell.execute_reply.started":"2023-12-27T05:25:12.987265Z","shell.execute_reply":"2023-12-27T05:28:46.591338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_test_samples = 79726\ntest_files, test_targets = read_and_normalize_sampled_test_data(nb_test_samples, img_rows, img_cols, color_type)\nprint('Test shape:', test_files.shape)\nprint(test_files.shape[0], 'Test samples')","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:28:46.593875Z","iopub.execute_input":"2023-12-27T05:28:46.594193Z","iopub.status.idle":"2023-12-27T05:42:51.327330Z","shell.execute_reply.started":"2023-12-27T05:28:46.594139Z","shell.execute_reply":"2023-12-27T05:42:51.326322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Statistics","metadata":{}},{"cell_type":"code","source":"# Statistics\n# Load the list of names\nnames = [item[17:19] for item in sorted(glob('/kaggle/input/imgs/train/*/'))]\ntest_files_size = len(np.array(glob(os.path.join('/kaggle/input/imgs/test/*.jpg'))))\nx_train_size = len(x_train)\ncategories_size = len(names)\nx_test_size = len(x_test)\nprint('There are %s total images.\\n' % (test_files_size + x_train_size + x_test_size))\nprint('There are %d training images.' % x_train_size)\nprint('There are %d total training categories.' % categories_size)\nprint('There are %d validation images.' % x_test_size)\nprint('There are %d test images.'% test_files_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:51.329102Z","iopub.execute_input":"2023-12-27T05:42:51.329441Z","iopub.status.idle":"2023-12-27T05:42:51.706543Z","shell.execute_reply.started":"2023-12-27T05:42:51.329380Z","shell.execute_reply":"2023-12-27T05:42:51.705773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:51.708073Z","iopub.execute_input":"2023-12-27T05:42:51.708347Z","iopub.status.idle":"2023-12-27T05:42:51.713462Z","shell.execute_reply.started":"2023-12-27T05:42:51.708295Z","shell.execute_reply":"2023-12-27T05:42:51.712713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Dataset Visualization","metadata":{}},{"cell_type":"code","source":"# Plot figure size\nplt.figure(figsize = (10,10))\n# Count the number of images per category\nsns.countplot(x = 'classname', data = dataset)\n# Change the Axis names\nplt.ylabel('Count')\nplt.title('Categories Distribution')\n# Show plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:51.715080Z","iopub.execute_input":"2023-12-27T05:42:51.715381Z","iopub.status.idle":"2023-12-27T05:42:52.054349Z","shell.execute_reply.started":"2023-12-27T05:42:51.715327Z","shell.execute_reply":"2023-12-27T05:42:52.053283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the frequency of images per driver\ndrivers_id = pd.DataFrame((dataset['subject'].value_counts()).reset_index())\ndrivers_id.columns = ['driver_id', 'Counts']\ndrivers_id","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:52.056201Z","iopub.execute_input":"2023-12-27T05:42:52.056795Z","iopub.status.idle":"2023-12-27T05:42:52.097030Z","shell.execute_reply.started":"2023-12-27T05:42:52.056729Z","shell.execute_reply":"2023-12-27T05:42:52.096310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting class distribution\ndataset['class_type'] = dataset['classname'].str.extract('(\\d)',expand=False).astype(np.float)\nplt.figure(figsize = (20,20))\ndataset.hist('class_type', alpha=0.5, layout=(1,1), bins=10)\nplt.title('Class distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:52.098190Z","iopub.execute_input":"2023-12-27T05:42:52.098408Z","iopub.status.idle":"2023-12-27T05:42:52.402218Z","shell.execute_reply.started":"2023-12-27T05:42:52.098371Z","shell.execute_reply":"2023-12-27T05:42:52.400767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Images overview\n\nLet's take a look at the various images in the dataset. I'll plot an image for each of the 10 classes. As the directory names are not descriptive, I'll use a map to define the title for each image that is more descriptive.","metadata":{}},{"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'}","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:52.404351Z","iopub.execute_input":"2023-12-27T05:42:52.404948Z","iopub.status.idle":"2023-12-27T05:42:52.413420Z","shell.execute_reply.started":"2023-12-27T05:42:52.404711Z","shell.execute_reply":"2023-12-27T05:42:52.411738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (12, 20))\nimage_count = 1\nBASE_URL = '/kaggle/input/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":"2023-12-27T05:42:52.415471Z","iopub.execute_input":"2023-12-27T05:42:52.416107Z","iopub.status.idle":"2023-12-27T05:42:54.495883Z","shell.execute_reply.started":"2023-12-27T05:42:52.415855Z","shell.execute_reply":"2023-12-27T05:42:54.494656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_submission(predictions, test_id, info):\n    result = pd.DataFrame(predictions, columns=['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])\n#     result.loc[:, 'img'] = pd.Series(test_id, index=result.index)\n    result.insert(0, 'img', test_id)  # 将'img'列插入到第一列位置\n    \n    now = datetime.datetime.now()\n    \n    if not os.path.isdir('kaggle_submissions'):\n        os.mkdir('kaggle_submissions')\n\n#     suffix = \"{}_{}\".format(info,str(now.strftime(\"%Y-%m-%d-%H-%M\")))\n#     sub_file = os.path.join('kaggle_submissions', 'submission_' + suffix + '.csv')\n    sub_file = os.path.join('submission.csv')\n    result.to_csv(sub_file, index=False)\n    \n    return sub_file","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:54.497458Z","iopub.execute_input":"2023-12-27T05:42:54.497759Z","iopub.status.idle":"2023-12-27T05:42:54.505349Z","shell.execute_reply.started":"2023-12-27T05:42:54.497702Z","shell.execute_reply":"2023-12-27T05:42:54.504500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create a vanilla CNN model","metadata":{}},{"cell_type":"markdown","source":"#### Building the model\n\nI'll develop the model with a total of 4 Convolutional layers, then a Flatten layer and then 2 Dense layers. I'll use the optimizer as `rmsprop`, and loss as `categorical_crossentropy`.","metadata":{}},{"cell_type":"code","source":"batch_size = 40\nnb_epoch = 10","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:54.506941Z","iopub.execute_input":"2023-12-27T05:42:54.507376Z","iopub.status.idle":"2023-12-27T05:42:54.518429Z","shell.execute_reply.started":"2023-12-27T05:42:54.507193Z","shell.execute_reply":"2023-12-27T05:42:54.517584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -f saved_models/weights_best_vanilla.hdf5","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:54.519923Z","iopub.execute_input":"2023-12-27T05:42:54.520243Z","iopub.status.idle":"2023-12-27T05:42:55.519027Z","shell.execute_reply.started":"2023-12-27T05:42:54.520190Z","shell.execute_reply":"2023-12-27T05:42:55.517587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models_dir = \"saved_models\"\nif not os.path.exists(models_dir):\n    os.makedirs(models_dir)\n    \ncheckpointer = ModelCheckpoint(filepath='saved_models/weights_best_vanilla.hdf5', \n                               monitor='val_loss', mode='min',\n                               verbose=1, save_best_only=True)\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=2)\ncallbacks = [checkpointer, es]","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.521042Z","iopub.execute_input":"2023-12-27T05:42:55.521394Z","iopub.status.idle":"2023-12-27T05:42:55.528781Z","shell.execute_reply.started":"2023-12-27T05:42:55.521332Z","shell.execute_reply":"2023-12-27T05:42:55.527804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Model v1\n---","metadata":{}},{"cell_type":"code","source":"# def create_model_v1():\n#     # Vanilla CNN model\n#     model = Sequential()\n\n#     model.add(Conv2D(filters = 64, kernel_size = 3, padding='same', activation = 'relu', input_shape=(img_rows, img_cols, color_type)))\n#     model.add(MaxPooling2D(pool_size = 2))\n\n#     model.add(Conv2D(filters = 128, padding='same', kernel_size = 3, activation = 'relu'))\n#     model.add(MaxPooling2D(pool_size = 2))\n\n#     model.add(Conv2D(filters = 256, padding='same', kernel_size = 3, activation = 'relu'))\n#     model.add(MaxPooling2D(pool_size = 2))\n\n#     model.add(Conv2D(filters = 512, padding='same', kernel_size = 3, activation = 'relu'))\n#     model.add(MaxPooling2D(pool_size = 2))\n\n#     model.add(Dropout(0.5))\n\n#     model.add(Flatten())\n\n#     model.add(Dense(500, activation = 'relu'))\n#     model.add(Dropout(0.5))\n#     model.add(Dense(10, activation = 'softmax'))\n    \n#     return model","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2023-12-27T05:42:55.530357Z","iopub.execute_input":"2023-12-27T05:42:55.530706Z","iopub.status.idle":"2023-12-27T05:42:55.538456Z","shell.execute_reply.started":"2023-12-27T05:42:55.530637Z","shell.execute_reply":"2023-12-27T05:42:55.537547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_v1 = create_model_v1()\n\n# # More details about the layers\n# model_v1.summary()\n\n# # Compiling the model\n# model_v1.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.539963Z","iopub.execute_input":"2023-12-27T05:42:55.540290Z","iopub.status.idle":"2023-12-27T05:42:55.551762Z","shell.execute_reply.started":"2023-12-27T05:42:55.540232Z","shell.execute_reply":"2023-12-27T05:42:55.550746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Training the Vanilla Model version 1\n# history_v1 = model_v1.fit(x_train, y_train, \n#           validation_data=(x_test, y_test),\n#           callbacks=callbacks,\n#           epochs=nb_epoch, batch_size=batch_size, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.553222Z","iopub.execute_input":"2023-12-27T05:42:55.553541Z","iopub.status.idle":"2023-12-27T05:42:55.561843Z","shell.execute_reply.started":"2023-12-27T05:42:55.553485Z","shell.execute_reply":"2023-12-27T05:42:55.561093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load the classifier with the best validation loss","metadata":{}},{"cell_type":"code","source":"# model_v1.load_weights('saved_models/weights_best_vanilla.hdf5')","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.563313Z","iopub.execute_input":"2023-12-27T05:42:55.563538Z","iopub.status.idle":"2023-12-27T05:42:55.571671Z","shell.execute_reply.started":"2023-12-27T05:42:55.563501Z","shell.execute_reply":"2023-12-27T05:42:55.570817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_train_history(history):\n    # Summarize history for accuracy\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', '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()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:53:12.660830Z","iopub.execute_input":"2023-12-27T05:53:12.661309Z","iopub.status.idle":"2023-12-27T05:53:12.669523Z","shell.execute_reply.started":"2023-12-27T05:53:12.661121Z","shell.execute_reply":"2023-12-27T05:53:12.668632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_train_history(history_v1)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.582983Z","iopub.execute_input":"2023-12-27T05:42:55.583414Z","iopub.status.idle":"2023-12-27T05:42:55.590979Z","shell.execute_reply.started":"2023-12-27T05:42:55.583365Z","shell.execute_reply":"2023-12-27T05:42:55.590043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def plot_test_class(model, test_files, image_number, color_type=1):\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=batch_size, verbose=1)\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":"2023-12-27T05:42:55.592382Z","iopub.execute_input":"2023-12-27T05:42:55.592650Z","iopub.status.idle":"2023-12-27T05:42:55.603459Z","shell.execute_reply.started":"2023-12-27T05:42:55.592602Z","shell.execute_reply":"2023-12-27T05:42:55.602575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# score = model_v1.evaluate(x_test, y_test, verbose=1)\n# print('Score: ', score)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.604682Z","iopub.execute_input":"2023-12-27T05:42:55.605026Z","iopub.status.idle":"2023-12-27T05:42:55.613075Z","shell.execute_reply.started":"2023-12-27T05:42:55.604940Z","shell.execute_reply":"2023-12-27T05:42:55.612152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v1, test_files, 19)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.614377Z","iopub.execute_input":"2023-12-27T05:42:55.614696Z","iopub.status.idle":"2023-12-27T05:42:55.622087Z","shell.execute_reply.started":"2023-12-27T05:42:55.614637Z","shell.execute_reply":"2023-12-27T05:42:55.621310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -f saved_models/weights_best_vanilla.hdf5","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.623258Z","iopub.execute_input":"2023-12-27T05:42:55.623465Z","iopub.status.idle":"2023-12-27T05:42:55.631318Z","shell.execute_reply.started":"2023-12-27T05:42:55.623430Z","shell.execute_reply":"2023-12-27T05:42:55.630477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Optimised Vanilla CNN Model ","metadata":{}},{"cell_type":"markdown","source":"#### Model v2\n---","metadata":{}},{"cell_type":"code","source":"# def create_model_v2():\n#     # Optimised Vanilla CNN model\n#     model = Sequential()\n\n#     ## CNN 1\n#     model.add(Conv2D(32,(3,3),activation='relu',input_shape=(img_rows, img_cols, color_type)))\n#     model.add(BatchNormalization())\n#     model.add(Conv2D(32,(3,3),activation='relu',padding='same'))\n#     model.add(BatchNormalization(axis = 3))\n#     model.add(MaxPooling2D(pool_size=(2,2),padding='same'))\n#     model.add(Dropout(0.3))\n\n#     ## CNN 2\n#     model.add(Conv2D(64,(3,3),activation='relu',padding='same'))\n#     model.add(BatchNormalization())\n#     model.add(Conv2D(64,(3,3),activation='relu',padding='same'))\n#     model.add(BatchNormalization(axis = 3))\n#     model.add(MaxPooling2D(pool_size=(2,2),padding='same'))\n#     model.add(Dropout(0.3))\n\n#     ## CNN 3\n#     model.add(Conv2D(128,(3,3),activation='relu',padding='same'))\n#     model.add(BatchNormalization())\n#     model.add(Conv2D(128,(3,3),activation='relu',padding='same'))\n#     model.add(BatchNormalization(axis = 3))\n#     model.add(MaxPooling2D(pool_size=(2,2),padding='same'))\n#     model.add(Dropout(0.5))\n\n#     ## Output\n#     model.add(Flatten())\n#     model.add(Dense(512,activation='relu'))\n#     model.add(BatchNormalization())\n#     model.add(Dropout(0.5))\n#     model.add(Dense(128,activation='relu'))\n#     model.add(Dropout(0.25))\n#     model.add(Dense(10,activation='softmax'))\n\n#     return model","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.632555Z","iopub.execute_input":"2023-12-27T05:42:55.632805Z","iopub.status.idle":"2023-12-27T05:42:55.640791Z","shell.execute_reply.started":"2023-12-27T05:42:55.632759Z","shell.execute_reply":"2023-12-27T05:42:55.640085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_v2 = create_model_v2()\n\n# # More details about the layers\n# model_v2.summary()\n\n# # Compiling the model\n# model_v2.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.642089Z","iopub.execute_input":"2023-12-27T05:42:55.642400Z","iopub.status.idle":"2023-12-27T05:42:55.654106Z","shell.execute_reply.started":"2023-12-27T05:42:55.642344Z","shell.execute_reply":"2023-12-27T05:42:55.653324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Training the Vanilla Model\n# history_v2 = model_v2.fit(x_train, y_train, \n#           validation_data=(x_test, y_test),\n#           callbacks=callbacks,\n#           epochs=nb_epoch, batch_size=batch_size, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.655467Z","iopub.execute_input":"2023-12-27T05:42:55.655691Z","iopub.status.idle":"2023-12-27T05:42:55.664616Z","shell.execute_reply.started":"2023-12-27T05:42:55.655654Z","shell.execute_reply":"2023-12-27T05:42:55.664008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_train_history(history_v2)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.671341Z","iopub.execute_input":"2023-12-27T05:42:55.671578Z","iopub.status.idle":"2023-12-27T05:42:55.674889Z","shell.execute_reply.started":"2023-12-27T05:42:55.671538Z","shell.execute_reply":"2023-12-27T05:42:55.674249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_v2.load_weights('saved_models/weights_best_vanilla.hdf5')","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.676138Z","iopub.execute_input":"2023-12-27T05:42:55.676433Z","iopub.status.idle":"2023-12-27T05:42:55.683869Z","shell.execute_reply.started":"2023-12-27T05:42:55.676384Z","shell.execute_reply":"2023-12-27T05:42:55.683199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# score = model_v2.evaluate(x_test, y_test, verbose=1)\n# print('Score: ', score)\n\n# y_pred = model_v2.predict(x_test, batch_size=batch_size, verbose=1)\n# score = log_loss(y_test, y_pred)\n# print('Score log loss:', score)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.685098Z","iopub.execute_input":"2023-12-27T05:42:55.685315Z","iopub.status.idle":"2023-12-27T05:42:55.694223Z","shell.execute_reply.started":"2023-12-27T05:42:55.685279Z","shell.execute_reply":"2023-12-27T05:42:55.693440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 10) # The model really performs badly","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.695538Z","iopub.execute_input":"2023-12-27T05:42:55.695839Z","iopub.status.idle":"2023-12-27T05:42:55.707372Z","shell.execute_reply.started":"2023-12-27T05:42:55.695785Z","shell.execute_reply":"2023-12-27T05:42:55.706439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 1) # The model really performs badly","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.708675Z","iopub.execute_input":"2023-12-27T05:42:55.709041Z","iopub.status.idle":"2023-12-27T05:42:55.716269Z","shell.execute_reply.started":"2023-12-27T05:42:55.708986Z","shell.execute_reply":"2023-12-27T05:42:55.715602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 150) ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.717736Z","iopub.execute_input":"2023-12-27T05:42:55.718215Z","iopub.status.idle":"2023-12-27T05:42:55.725941Z","shell.execute_reply.started":"2023-12-27T05:42:55.718134Z","shell.execute_reply":"2023-12-27T05:42:55.725204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create a vanilla CNN model with data augmentation\n\nHere I'm augmenting the previous model `classifier`, I'll use the data on which I want to train the model. The folder `train` includes the images I need. I'll generate more images using **ImageDataGenerator** and split the training data into 80% train and 20% validation split.","metadata":{}},{"cell_type":"code","source":"# !rm -f saved_models/weights_best_vanilla.hdf5","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.727368Z","iopub.execute_input":"2023-12-27T05:42:55.727646Z","iopub.status.idle":"2023-12-27T05:42:55.735639Z","shell.execute_reply.started":"2023-12-27T05:42:55.727592Z","shell.execute_reply":"2023-12-27T05:42:55.734832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare data augmentation configuration\ntrain_datagen = ImageDataGenerator(rescale = 1.0/255, \n                                   shear_range = 0.2, \n                                   zoom_range = 0.2, \n                                   horizontal_flip = True, \n                                   validation_split = 0.2)\n\ntest_datagen = ImageDataGenerator(rescale=1.0/ 255, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:45:56.910918Z","iopub.execute_input":"2023-12-27T05:45:56.911345Z","iopub.status.idle":"2023-12-27T05:45:56.917405Z","shell.execute_reply.started":"2023-12-27T05:45:56.911284Z","shell.execute_reply":"2023-12-27T05:45:56.916515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# nb_train_samples = x_train.shape[0]\n# nb_validation_samples = x_test.shape[0]\n# print(nb_train_samples)\n# print(nb_validation_samples)\n# training_generator = train_datagen.flow(x_train, y_train, batch_size=batch_size)\n# validation_generator = test_datagen.flow(x_test, y_test, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.746518Z","iopub.execute_input":"2023-12-27T05:42:55.746790Z","iopub.status.idle":"2023-12-27T05:42:55.755184Z","shell.execute_reply.started":"2023-12-27T05:42:55.746738Z","shell.execute_reply":"2023-12-27T05:42:55.754547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Train the model with Data Augmentation\n\nUsing `fit_generator`, I'll train the model.","metadata":{}},{"cell_type":"code","source":"# checkpoint = ModelCheckpoint('saved_models/weights_best_vanilla.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n# history_v3 = model_v2.fit_generator(training_generator,\n#                          steps_per_epoch = nb_train_samples // batch_size,\n#                          epochs = 10, \n#                          callbacks=[es, checkpoint],\n#                          verbose = 1,\n#                          validation_data = validation_generator,\n#                          validation_steps = nb_validation_samples // batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.756378Z","iopub.execute_input":"2023-12-27T05:42:55.756653Z","iopub.status.idle":"2023-12-27T05:42:55.765777Z","shell.execute_reply.started":"2023-12-27T05:42:55.756606Z","shell.execute_reply":"2023-12-27T05:42:55.765072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_v2.load_weights('saved_models/weights_best_vanilla.hdf5')","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.767057Z","iopub.execute_input":"2023-12-27T05:42:55.767357Z","iopub.status.idle":"2023-12-27T05:42:55.774870Z","shell.execute_reply.started":"2023-12-27T05:42:55.767301Z","shell.execute_reply":"2023-12-27T05:42:55.774214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_train_history(history_v3)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.776063Z","iopub.execute_input":"2023-12-27T05:42:55.776317Z","iopub.status.idle":"2023-12-27T05:42:55.784508Z","shell.execute_reply.started":"2023-12-27T05:42:55.776267Z","shell.execute_reply":"2023-12-27T05:42:55.783738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Evaluate the performance of the new model\n# score = model_v2.evaluate_generator(validation_generator, nb_validation_samples // batch_size)\n# print(\"Test Score:\", score[0])\n# print(\"Test Accuracy:\", score[1])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.785587Z","iopub.execute_input":"2023-12-27T05:42:55.785847Z","iopub.status.idle":"2023-12-27T05:42:55.793721Z","shell.execute_reply.started":"2023-12-27T05:42:55.785801Z","shell.execute_reply":"2023-12-27T05:42:55.792487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 108)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.795192Z","iopub.execute_input":"2023-12-27T05:42:55.795547Z","iopub.status.idle":"2023-12-27T05:42:55.807302Z","shell.execute_reply.started":"2023-12-27T05:42:55.795488Z","shell.execute_reply":"2023-12-27T05:42:55.806548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 10) ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.808626Z","iopub.execute_input":"2023-12-27T05:42:55.808875Z","iopub.status.idle":"2023-12-27T05:42:55.817031Z","shell.execute_reply.started":"2023-12-27T05:42:55.808807Z","shell.execute_reply":"2023-12-27T05:42:55.816266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 14) ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.818022Z","iopub.execute_input":"2023-12-27T05:42:55.818298Z","iopub.status.idle":"2023-12-27T05:42:55.826113Z","shell.execute_reply.started":"2023-12-27T05:42:55.818248Z","shell.execute_reply":"2023-12-27T05:42:55.825277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_test_class(model_v2, test_files, 0) ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.827343Z","iopub.execute_input":"2023-12-27T05:42:55.827647Z","iopub.status.idle":"2023-12-27T05:42:55.834272Z","shell.execute_reply.started":"2023-12-27T05:42:55.827590Z","shell.execute_reply":"2023-12-27T05:42:55.833416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions = model_v2.predict(test_files, batch_size=batch_size)\n# FileLink(create_submission(predictions, test_targets, score[0]))\n# print(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.835324Z","iopub.execute_input":"2023-12-27T05:42:55.835558Z","iopub.status.idle":"2023-12-27T05:42:55.842847Z","shell.execute_reply.started":"2023-12-27T05:42:55.835503Z","shell.execute_reply":"2023-12-27T05:42:55.842062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The trained model achieved a validation accuracy of over 93%.","metadata":{}},{"cell_type":"code","source":"!rm -f saved_models/weights_best_vanilla.hdf5","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:42:55.843884Z","iopub.execute_input":"2023-12-27T05:42:55.844130Z","iopub.status.idle":"2023-12-27T05:42:56.836245Z","shell.execute_reply.started":"2023-12-27T05:42:55.844091Z","shell.execute_reply":"2023-12-27T05:42:56.835154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train a CNN with Transfer Learning (VGG, MobileNet)","metadata":{}},{"cell_type":"markdown","source":"To reduce training time without sacrificing accuracy, I'll train a CNN using **transfer learning**.","metadata":{}},{"cell_type":"code","source":"def vgg_std16_model(img_rows, img_cols, color_type=3):\n    nb_classes = 10\n    # Remove fully connected layer and replace\n    # with softmax for classifying 10 classes\n    vgg16_model = VGG16(weights=\"imagenet\", include_top=False)\n\n    # Freeze all layers of the pre-trained model\n    for layer in vgg16_model.layers:\n        layer.trainable = False\n        \n    x = vgg16_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(1024, activation='relu')(x)\n    predictions = Dense(nb_classes, activation = 'softmax')(x)\n\n    model = Model(input = vgg16_model.input, output = predictions)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:46:12.326589Z","iopub.execute_input":"2023-12-27T05:46:12.326906Z","iopub.status.idle":"2023-12-27T05:46:12.334001Z","shell.execute_reply.started":"2023-12-27T05:46:12.326863Z","shell.execute_reply":"2023-12-27T05:46:12.332944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the VGG16 network\nprint(\"Loading network...\")\nmodel_vgg16 = vgg_std16_model(img_rows, img_cols)\n\nmodel_vgg16.summary()\n\nmodel_vgg16.compile(loss='categorical_crossentropy',\n                         optimizer='rmsprop',\n                         metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:46:18.932728Z","iopub.execute_input":"2023-12-27T05:46:18.933097Z","iopub.status.idle":"2023-12-27T05:46:19.655282Z","shell.execute_reply.started":"2023-12-27T05:46:18.933034Z","shell.execute_reply":"2023-12-27T05:46:19.654054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_generator = train_datagen.flow_from_directory('/kaggle/input/imgs/train', \n                                                 target_size = (img_rows, img_cols), \n                                                 batch_size = batch_size,\n                                                 shuffle=True,\n                                                 class_mode='categorical', subset=\"training\")\n\nvalidation_generator = test_datagen.flow_from_directory('/kaggle/input/imgs/train', \n                                                   target_size = (img_rows, img_cols), \n                                                   batch_size = batch_size,\n                                                   shuffle=False,\n                                                   class_mode='categorical', subset=\"validation\")\nnb_train_samples = 17943\nnb_validation_samples = 4481","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:46:33.969759Z","iopub.execute_input":"2023-12-27T05:46:33.970084Z","iopub.status.idle":"2023-12-27T05:46:49.634337Z","shell.execute_reply.started":"2023-12-27T05:46:33.970036Z","shell.execute_reply":"2023-12-27T05:46:49.633405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -f saved_models/weights_best_vgg16.hdf5","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:46:55.518944Z","iopub.execute_input":"2023-12-27T05:46:55.519308Z","iopub.status.idle":"2023-12-27T05:46:56.496478Z","shell.execute_reply.started":"2023-12-27T05:46:55.519247Z","shell.execute_reply":"2023-12-27T05:46:56.495353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the Vanilla Model\ncheckpoint = ModelCheckpoint('saved_models/weights_best_vgg16.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')\nhistory_v4 = model_vgg16.fit_generator(training_generator,\n                         steps_per_epoch = nb_train_samples // batch_size,\n                         epochs = 10, \n                         callbacks=[es, checkpoint],\n                         verbose = 1,\n                         class_weight='auto',\n                         validation_data = validation_generator,\n                         validation_steps = nb_validation_samples // batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T07:42:18.502792Z","iopub.execute_input":"2023-12-27T07:42:18.503250Z","iopub.status.idle":"2023-12-27T07:42:21.165248Z","shell.execute_reply.started":"2023-12-27T07:42:18.503188Z","shell.execute_reply":"2023-12-27T07:42:21.163376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg16.load_weights('saved_models/weights_best_vgg16.hdf5')","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:52:22.803808Z","iopub.execute_input":"2023-12-27T05:52:22.804188Z","iopub.status.idle":"2023-12-27T05:52:23.000804Z","shell.execute_reply.started":"2023-12-27T05:52:22.804139Z","shell.execute_reply":"2023-12-27T05:52:23.000058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_history(history_v4)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:53:19.739517Z","iopub.execute_input":"2023-12-27T05:53:19.739820Z","iopub.status.idle":"2023-12-27T05:53:20.305419Z","shell.execute_reply.started":"2023-12-27T05:53:19.739778Z","shell.execute_reply":"2023-12-27T05:53:20.304144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_vgg16_test_class(model, test_files, image_number):\n    img_brute = test_files[image_number]\n\n    im = cv2.resize(cv2.cvtColor(img_brute, cv2.COLOR_BGR2RGB), (img_rows,img_cols)).astype(np.float32) / 255.0\n    im = np.expand_dims(im, axis =0)\n\n    img_display = cv2.resize(img_brute,(img_rows,img_cols))\n    plt.imshow(img_display, cmap='gray')\n\n    y_preds = model.predict(im, batch_size=batch_size, verbose=1)\n    print(y_preds)\n    y_prediction = np.argmax(y_preds)\n    print('Y Prediction: {}'.format(y_prediction))\n    print('Predicted as: {}'.format(activity_map.get('c{}'.format(y_prediction))))\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:53:24.958496Z","iopub.execute_input":"2023-12-27T05:53:24.958800Z","iopub.status.idle":"2023-12-27T05:53:24.967574Z","shell.execute_reply.started":"2023-12-27T05:53:24.958757Z","shell.execute_reply":"2023-12-27T05:53:24.966800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_vgg16_test_class(model_vgg16, test_files, 133) # Texting left","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:53:28.217333Z","iopub.execute_input":"2023-12-27T05:53:28.217694Z","iopub.status.idle":"2023-12-27T05:53:28.571922Z","shell.execute_reply.started":"2023-12-27T05:53:28.217635Z","shell.execute_reply":"2023-12-27T05:53:28.570433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_vgg16_test_class(model_vgg16, test_files, 29) # Talking to passenger","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:54:07.315054Z","iopub.execute_input":"2023-12-27T05:54:07.315381Z","iopub.status.idle":"2023-12-27T05:54:07.569086Z","shell.execute_reply.started":"2023-12-27T05:54:07.315337Z","shell.execute_reply":"2023-12-27T05:54:07.567661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_vgg16_test_class(model_vgg16, test_files, 120) # phone left","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:54:46.512701Z","iopub.execute_input":"2023-12-27T05:54:46.513083Z","iopub.status.idle":"2023-12-27T05:54:46.789099Z","shell.execute_reply.started":"2023-12-27T05:54:46.513030Z","shell.execute_reply":"2023-12-27T05:54:46.787451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the performance of the new model\nscore = model_vgg16.evaluate_generator(validation_generator, nb_validation_samples // batch_size, verbose = 1)\nprint(\"Test Score:\", score[0])\nprint(\"Test Accuracy:\", score[1])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:54:50.580762Z","iopub.execute_input":"2023-12-27T05:54:50.581104Z","iopub.status.idle":"2023-12-27T05:55:12.200226Z","shell.execute_reply.started":"2023-12-27T05:54:50.581054Z","shell.execute_reply":"2023-12-27T05:55:12.199368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nfor img in test_files:\n    # 将灰度图像转换为彩色图像\n    img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)\n\n    # 调整大小并扩展维度\n    img_resized = cv2.resize(img_rgb, (img_rows, img_cols)).astype(np.float32) / 255.0\n    im = np.expand_dims(img_resized, axis=0)\n\n    # 对图像进行预测，并将结果添加到 predictions 列表中\n    prediction = model_vgg16.predict(im, batch_size=batch_size)\n    predictions.append(prediction)\n\n# 将所有预测结果组合在一起\npredictions = np.array(predictions).squeeze()\nprint(predictions)\nFileLink(create_submission(predictions, test_targets, score[0]))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T05:55:42.098656Z","iopub.execute_input":"2023-12-27T05:55:42.099191Z","iopub.status.idle":"2023-12-27T05:59:49.151291Z","shell.execute_reply.started":"2023-12-27T05:55:42.098921Z","shell.execute_reply":"2023-12-27T05:59:49.150526Z"},"trusted":true},"execution_count":null,"outputs":[]}]}