{"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\nfrom tensorflow.keras.applications.resnet50 import preprocess_input","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T10:16:07.886240Z","iopub.execute_input":"2024-04-04T10:16:07.887059Z","iopub.status.idle":"2024-04-04T10:16:21.863309Z","shell.execute_reply.started":"2024-04-04T10:16:07.887023Z","shell.execute_reply":"2024-04-04T10:16:21.862493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:25.155992Z","iopub.execute_input":"2024-04-04T10:16:25.156891Z","iopub.status.idle":"2024-04-04T10:16:25.162409Z","shell.execute_reply.started":"2024-04-04T10:16:25.156857Z","shell.execute_reply":"2024-04-04T10:16:25.161284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:26.548958Z","iopub.execute_input":"2024-04-04T10:16:26.549308Z","iopub.status.idle":"2024-04-04T10:16:26.553604Z","shell.execute_reply.started":"2024-04-04T10:16:26.549280Z","shell.execute_reply":"2024-04-04T10:16:26.552638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    validation_split=0.2 \n)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:26:44.198280Z","iopub.execute_input":"2024-04-04T10:26:44.198664Z","iopub.status.idle":"2024-04-04T10:26:44.203770Z","shell.execute_reply.started":"2024-04-04T10:26:44.198636Z","shell.execute_reply":"2024-04-04T10:26:44.202713Z"},"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 = 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-04T10:26:45.084782Z","iopub.execute_input":"2024-04-04T10:26:45.085139Z","iopub.status.idle":"2024-04-04T10:26:49.951369Z","shell.execute_reply.started":"2024-04-04T10:26:45.085105Z","shell.execute_reply":"2024-04-04T10:26:49.950529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-04T10:16:45.427636Z","iopub.execute_input":"2024-04-04T10:16:45.428398Z","iopub.status.idle":"2024-04-04T10:16:45.434117Z","shell.execute_reply.started":"2024-04-04T10:16:45.428363Z","shell.execute_reply":"2024-04-04T10:16:45.433267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_cnn_model():\n    model = tf.keras.Sequential([\n        tf.keras.layers.Conv2D(64,(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(10,activation='softmax')\n\n    ])\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:45.435617Z","iopub.execute_input":"2024-04-04T10:16:45.436368Z","iopub.status.idle":"2024-04-04T10:16:45.460044Z","shell.execute_reply.started":"2024-04-04T10:16:45.436317Z","shell.execute_reply":"2024-04-04T10:16:45.459054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = make_dense_model()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:45.462435Z","iopub.execute_input":"2024-04-04T10:16:45.463084Z","iopub.status.idle":"2024-04-04T10:16:46.330988Z","shell.execute_reply.started":"2024-04-04T10:16:45.463049Z","shell.execute_reply":"2024-04-04T10:16:46.330203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:46.332276Z","iopub.execute_input":"2024-04-04T10:16:46.332546Z","iopub.status.idle":"2024-04-04T10:16:46.353789Z","shell.execute_reply.started":"2024-04-04T10:16:46.332516Z","shell.execute_reply":"2024-04-04T10:16:46.352947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.0001),\n    metrics=['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:46.354864Z","iopub.execute_input":"2024-04-04T10:16:46.355129Z","iopub.status.idle":"2024-04-04T10:16:46.368058Z","shell.execute_reply.started":"2024-04-04T10:16:46.355100Z","shell.execute_reply":"2024-04-04T10:16:46.367185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.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-04-04T10:16:46.369927Z","iopub.execute_input":"2024-04-04T10:16:46.370192Z","iopub.status.idle":"2024-04-04T10:17:05.576871Z","shell.execute_reply.started":"2024-04-04T10:16:46.370169Z","shell.execute_reply":"2024-04-04T10:17:05.575228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), hist.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, 50), hist.history['val_acc'], label='Validation Accuracy')\n\nplt.title('Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend() \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:13.621673Z","iopub.execute_input":"2024-03-28T11:36:13.622015Z","iopub.status.idle":"2024-03-28T11:36:16.013924Z","shell.execute_reply.started":"2024-03-28T11:36:13.621976Z","shell.execute_reply":"2024-03-28T11:36:16.012419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), hist.history['loss'], label='Training loss')\nplt.plot(np.arange(0, 50), hist.history['val_loss'], label='Validation loss')\n\nplt.title('Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('loss')\nplt.legend() \n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = make_cnn_model()\nmodel.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.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.0001),\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.017230Z","shell.execute_reply":"2024-03-28T11:36:16.017259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.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), hist.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, 50), hist.history['val_acc'], label='Validation Accuracy')\n\nplt.title('Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()  # Show legend with labels\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":"plt.plot(np.arange(0, 50), hist.history['loss'], label='Training loss')\nplt.plot(np.arange(0, 50), hist.history['val_loss'], label='Validation loss')\n\nplt.title('Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('loss')\nplt.legend() \n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2 \n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    train_dir,\n    batch_size=64,\n    class_mode='categorical',\n    subset='training'  \n)\n\nvalidation_generator = train_datagen.flow_from_directory(\n    train_dir,\n    batch_size=64,\n    class_mode='categorical',\n    subset='validation'  \n)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base = ResNet50(weights='imagenet', include_top=False)\nconv_base.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.022946Z","iopub.status.idle":"2024-03-28T11:36:16.023459Z","shell.execute_reply.started":"2024-03-28T11:36:16.023176Z","shell.execute_reply":"2024-03-28T11:36:16.023196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential()\nmodel.add(conv_base)\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(64,activation='relu'))\nmodel.add(tf.keras.layers.Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.024467Z","iopub.status.idle":"2024-03-28T11:36:16.024811Z","shell.execute_reply.started":"2024-03-28T11:36:16.024640Z","shell.execute_reply":"2024-03-28T11:36:16.024655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.026106Z","iopub.status.idle":"2024-03-28T11:36:16.026495Z","shell.execute_reply.started":"2024-03-28T11:36:16.026321Z","shell.execute_reply":"2024-03-28T11:36:16.026336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss = 'categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(0.0001),\n    metrics=['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:36:16.027592Z","iopub.status.idle":"2024-03-28T11:36:16.027938Z","shell.execute_reply.started":"2024-03-28T11:36:16.027767Z","shell.execute_reply":"2024-03-28T11:36:16.027782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.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.029615Z","iopub.status.idle":"2024-03-28T11:36:16.030086Z","shell.execute_reply.started":"2024-03-28T11:36:16.029839Z","shell.execute_reply":"2024-03-28T11:36:16.029861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), hist.history['acc'], label='Training Accuracy')\nplt.plot(np.arange(0, 50), hist.history['val_acc'], label='Validation Accuracy')\n\nplt.title('Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()  # Show legend with labels\n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.arange(0, 50), hist.history['loss'], label='Training loss')\nplt.plot(np.arange(0, 50), hist.history['val_loss'], label='Validation loss')\n\nplt.title('Model Training Progress')\nplt.xlabel('Epoch')\nplt.ylabel('loss')\nplt.legend() \n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]}]}