{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.applications import ResNet50,VGG19\nfrom tensorflow.keras.models import load_model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-03T01:08:43.760538Z","iopub.execute_input":"2024-04-03T01:08:43.760823Z","iopub.status.idle":"2024-04-03T01:08:57.834948Z","shell.execute_reply.started":"2024-04-03T01:08:43.760797Z","shell.execute_reply":"2024-04-03T01:08:57.833972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:08:57.836567Z","iopub.execute_input":"2024-04-03T01:08:57.837263Z","iopub.status.idle":"2024-04-03T01:08:57.842016Z","shell.execute_reply.started":"2024-04-03T01:08:57.837235Z","shell.execute_reply":"2024-04-03T01:08:57.841151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:09:35.991643Z","iopub.execute_input":"2024-04-03T01:09:35.992224Z","iopub.status.idle":"2024-04-03T01:09:35.996417Z","shell.execute_reply.started":"2024-04-03T01:09:35.992192Z","shell.execute_reply":"2024-04-03T01:09:35.995438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2 \n)\n\nvalid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2 \n)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:09:36.958723Z","iopub.execute_input":"2024-04-03T01:09:36.959075Z","iopub.status.idle":"2024-04-03T01:09:36.964547Z","shell.execute_reply.started":"2024-04-03T01:09:36.959045Z","shell.execute_reply":"2024-04-03T01:09:36.963620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=64,\n    class_mode='categorical',\n    subset='training'  \n)\n\nvalidation_generator = valid_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=64,\n    class_mode='categorical',\n    subset='validation'  \n)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:09:40.232923Z","iopub.execute_input":"2024-04-03T01:09:40.233812Z","iopub.status.idle":"2024-04-03T01:09:53.216799Z","shell.execute_reply.started":"2024-04-03T01:09:40.233776Z","shell.execute_reply":"2024-04-03T01:09:53.216025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The first Dense Deep Neural Network","metadata":{}},{"cell_type":"code","source":"def make_dense_model():\n    model = tf.keras.Sequential([\n        tf.keras.layers.Flatten(input_shape=(150,150,3)),\n        tf.keras.layers.Dense(512,activation='relu'),\n        tf.keras.layers.Dense(128,activation='relu'),\n        tf.keras.layers.Dense(64,activation='relu'),\n        tf.keras.layers.Dense(32,activation='relu'),\n        tf.keras.layers.Dense(10,activation='softmax'),\n    ])\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-04-03T00:53:49.246319Z","iopub.status.idle":"2024-04-03T00:53:49.246627Z","shell.execute_reply.started":"2024-04-03T00:53:49.246476Z","shell.execute_reply":"2024-04-03T00:53:49.246488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = make_dense_model()\nmodel.summary()\nmodel.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.001),\n    metrics=['acc']\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:24:50.315330Z","iopub.execute_input":"2024-03-29T12:24:50.315922Z","iopub.status.idle":"2024-03-29T12:24:50.374092Z","shell.execute_reply.started":"2024-03-29T12:24:50.315891Z","shell.execute_reply":"2024-03-29T12:24:50.373225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#start training Dense model \ntrain_epochs=50\nhistory = model.fit(\n    train_generator,\n    validation_data=validation_generator,\n    steps_per_epoch = 17943 // 64,\n    epochs = train_epochs\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:25:10.370656Z","iopub.execute_input":"2024-03-29T12:25:10.371348Z","iopub.status.idle":"2024-03-29T12:28:55.606143Z","shell.execute_reply.started":"2024-03-29T12:25:10.371320Z","shell.execute_reply":"2024-03-29T12:28:55.605340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot epochs versus accuracy and loss\nplt.plot(np.arange(0, train_epochs), history.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, train_epochs), history.history['val_acc'], label='Validation Accuracy')\n\nplt.title('Dense Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()  \nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save the dense model \nmodel.save_weights('dense_model_weights.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-30T16:02:13.308044Z","iopub.execute_input":"2024-03-30T16:02:13.308710Z","iopub.status.idle":"2024-03-30T16:02:13.708736Z","shell.execute_reply.started":"2024-03-30T16:02:13.308682Z","shell.execute_reply":"2024-03-30T16:02:13.707643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_cnn_model():\n    model = tf.keras.Sequential([\n        tf.keras.layers.Conv2D(128,(3,3),activation='relu',input_shape=(150,150,3)),\n        tf.keras.layers.Conv2D(64,(3,3),activation='relu'),\n        tf.keras.layers.MaxPooling2D(),\n        \n        tf.keras.layers.Conv2D(32,(3,3),activation='relu'),\n        tf.keras.layers.Conv2D(32,(3,3),activation='relu'),\n        tf.keras.layers.MaxPooling2D(),\n        \n        tf.keras.layers.Conv2D(16,(3,3),activation='relu'),\n        tf.keras.layers.Conv2D(16,(3,3),activation='relu'),\n        tf.keras.layers.MaxPooling2D(),\n        \n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(128,activation='relu'),\n        tf.keras.layers.Dense(64,activation='relu'),\n        tf.keras.layers.Dense(10,activation='softmax')\n\n    ])\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:12:06.915298Z","iopub.status.idle":"2024-03-29T12:12:06.915730Z","shell.execute_reply.started":"2024-03-29T12:12:06.915520Z","shell.execute_reply":"2024-03-29T12:12:06.915537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn = make_cnn_model()\nmodel_cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.015116Z","iopub.status.idle":"2024-03-28T11:36:16.015642Z","shell.execute_reply.started":"2024-03-28T11:36:16.015394Z","shell.execute_reply":"2024-03-28T11:36:16.015415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.001),\n    metrics=['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.016989Z","iopub.status.idle":"2024-03-28T11:36:16.017484Z","shell.execute_reply.started":"2024-03-28T11:36:16.01723Z","shell.execute_reply":"2024-03-28T11:36:16.017259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_cnn = model_cnn.fit(\n    train_generator,\n    validation_data=validation_generator,\n    steps_per_epoch = 17943 // 64,\n    epochs = 50\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.018763Z","iopub.status.idle":"2024-03-28T11:36:16.019121Z","shell.execute_reply.started":"2024-03-28T11:36:16.018949Z","shell.execute_reply":"2024-03-28T11:36:16.018964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), history_cnn.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, 50), history_cnn.history['val_acc'], label='Validation Accuracy')\n\nplt.title('CNN Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()  \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.020924Z","iopub.status.idle":"2024-03-28T11:36:16.021333Z","shell.execute_reply.started":"2024-03-28T11:36:16.021116Z","shell.execute_reply":"2024-03-28T11:36:16.021132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save the cnn model \nmodel_cnn.save_weights('cnn_model_cnn_weights.weights.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3-Trying CNN wiht Transfer learning ","metadata":{}},{"cell_type":"markdown","source":"## 1- Trying VGG19 first ","metadata":{}},{"cell_type":"code","source":"conv_base = VGG19(weights='imagenet', include_top=False, input_shape=(150, 150, 3))\nconv_base.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-03-30T16:02:42.389000Z","iopub.execute_input":"2024-03-30T16:02:42.389705Z","iopub.status.idle":"2024-03-30T16:02:44.348033Z","shell.execute_reply.started":"2024-03-30T16:02:42.389677Z","shell.execute_reply":"2024-03-30T16:02:44.347195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg = tf.keras.models.Sequential()\nmodel_vgg.add(conv_base)\nmodel_vgg.add(tf.keras.layers.Flatten())\nmodel_vgg.add(tf.keras.layers.Dense(64,activation='relu'))\nmodel_vgg.add(tf.keras.layers.Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:35:37.974702Z","iopub.execute_input":"2024-03-29T12:35:37.975046Z","iopub.status.idle":"2024-03-29T12:35:37.984351Z","shell.execute_reply.started":"2024-03-29T12:35:37.975018Z","shell.execute_reply":"2024-03-29T12:35:37.983417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:35:39.518766Z","iopub.execute_input":"2024-03-29T12:35:39.519376Z","iopub.status.idle":"2024-03-29T12:35:39.537627Z","shell.execute_reply.started":"2024-03-29T12:35:39.519342Z","shell.execute_reply":"2024-03-29T12:35:39.536813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.001),\n    metrics=['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:35:42.247275Z","iopub.execute_input":"2024-03-29T12:35:42.247667Z","iopub.status.idle":"2024-03-29T12:35:42.256147Z","shell.execute_reply.started":"2024-03-29T12:35:42.247640Z","shell.execute_reply":"2024-03-29T12:35:42.255331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_tansfer_vgg = model_vgg.fit(\n    train_generator,\n    validation_data=validation_generator,\n    steps_per_epoch = 17943 // 64,\n    epochs = 50\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:35:47.533941Z","iopub.execute_input":"2024-03-29T12:35:47.534291Z","iopub.status.idle":"2024-03-29T12:39:39.882416Z","shell.execute_reply.started":"2024-03-29T12:35:47.534264Z","shell.execute_reply":"2024-03-29T12:39:39.881625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), history_tansfer_vgg.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, 50), history_tansfer_vgg.history['val_acc'], label='Validation Accuracy')\n\nplt.title('CNN Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()  \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T12:39:57.107885Z","iopub.execute_input":"2024-03-29T12:39:57.108245Z","iopub.status.idle":"2024-03-29T12:39:57.381017Z","shell.execute_reply.started":"2024-03-29T12:39:57.108215Z","shell.execute_reply":"2024-03-29T12:39:57.380117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save the transfer model \nmodel_vgg.save_weights('transfer_model_vgg_weights.weights.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2- trying resnet ","metadata":{}},{"cell_type":"code","source":"conv_base_resnet = ResNet50(weights='imagenet', include_top=False, input_shape=(150, 150, 3))\nconv_base_resnet.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-03-30T16:12:02.270315Z","iopub.execute_input":"2024-03-30T16:12:02.271109Z","iopub.status.idle":"2024-03-30T16:12:04.276294Z","shell.execute_reply.started":"2024-03-30T16:12:02.271072Z","shell.execute_reply":"2024-03-30T16:12:04.275406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_resnet = tf.keras.models.Sequential()\nmodel_resnet.add(conv_base_resnet)\nmodel_resnet.add(tf.keras.layers.Flatten())\nmodel_resnet.add(tf.keras.layers.Dense(64,activation='relu'))\nmodel_resnet.add(tf.keras.layers.Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-03-30T16:12:05.901014Z","iopub.execute_input":"2024-03-30T16:12:05.901373Z","iopub.status.idle":"2024-03-30T16:12:05.910752Z","shell.execute_reply.started":"2024-03-30T16:12:05.901344Z","shell.execute_reply":"2024-03-30T16:12:05.909692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_resnet.summary()\nmodel_resnet.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.001),\n    metrics=['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-30T16:12:08.121117Z","iopub.execute_input":"2024-03-30T16:12:08.121495Z","iopub.status.idle":"2024-03-30T16:12:08.162130Z","shell.execute_reply.started":"2024-03-30T16:12:08.121465Z","shell.execute_reply":"2024-03-30T16:12:08.161331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## create new data generator for image processing \nfrom tensorflow.keras.applications.resnet50 import preprocess_input\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=preprocess_input,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2 \n)\n\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=64,\n    class_mode='categorical',\n    subset='training'  \n)\n\nvalidation_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=64,\n    class_mode='categorical',\n    subset='validation'  \n)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T00:55:52.660658Z","iopub.execute_input":"2024-04-03T00:55:52.661485Z","iopub.status.idle":"2024-04-03T00:56:04.119768Z","shell.execute_reply.started":"2024-04-03T00:55:52.661452Z","shell.execute_reply":"2024-04-03T00:56:04.118838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_tansfer_resnet = model_resnet.fit(\n    train_generator,\n    validation_data=validation_generator,\n    steps_per_epoch = 17943 // 64,\n    epochs = 50\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T19:07:32.262642Z","iopub.execute_input":"2024-03-31T19:07:32.263427Z","iopub.status.idle":"2024-03-31T19:07:32.603436Z","shell.execute_reply.started":"2024-03-31T19:07:32.263391Z","shell.execute_reply":"2024-03-31T19:07:32.602292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), history_tansfer_resnet.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, 50), history_tansfer_resnet.history['val_acc'], label='Validation Accuracy')\n\nplt.title('CNN Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()  \n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model weights\nmodel_resnet.save_weights('transfer_model_resnet_weights.weights.h5')\n","metadata":{"execution":{"iopub.status.busy":"2024-03-30T17:36:07.652841Z","iopub.execute_input":"2024-03-30T17:36:07.653231Z","iopub.status.idle":"2024-03-30T17:36:08.188294Z","shell.execute_reply.started":"2024-03-30T17:36:07.653200Z","shell.execute_reply":"2024-03-30T17:36:08.187173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-30T17:36:04.539597Z","iopub.execute_input":"2024-03-30T17:36:04.539968Z","iopub.status.idle":"2024-03-30T17:36:04.546391Z","shell.execute_reply.started":"2024-03-30T17:36:04.539937Z","shell.execute_reply":"2024-03-30T17:36:04.545426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}