{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.11","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,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":11770747,"sourceType":"datasetVersion","datasetId":7389846},{"sourceId":11772384,"sourceType":"datasetVersion","datasetId":7390968},{"sourceId":11772420,"sourceType":"datasetVersion","datasetId":7390995}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"colab":{"provenance":[],"gpuType":"T4"},"accelerator":"GPU"},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Cell 1\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport tensorflow as tf\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\n","metadata":{"id":"dQuQAQt-cTp8","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T14:29:03.476772Z","iopub.execute_input":"2025-05-11T14:29:03.477047Z","iopub.status.idle":"2025-05-11T14:29:03.493092Z","shell.execute_reply.started":"2025-05-11T14:29:03.477020Z","shell.execute_reply":"2025-05-11T14:29:03.492124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Example: replace this with the actual shared folder path\nshared_folder = '/kaggle/input/statefarmtrain/train'\n\n# Make sure the folder exists\nassert os.path.exists(shared_folder), \"❌ The path doesn't exist. Double-check the shared folder path.\"\n","metadata":{"id":"g9MFbsgAQjrn","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T14:29:09.581925Z","iopub.execute_input":"2025-05-11T14:29:09.582215Z","iopub.status.idle":"2025-05-11T14:29:09.593049Z","shell.execute_reply.started":"2025-05-11T14:29:09.582196Z","shell.execute_reply":"2025-05-11T14:29:09.592116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimg_size = 224\nbatch_size = 32\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    validation_split=0.2,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    shared_folder,\n    target_size=(img_size, img_size),\n    batch_size=batch_size,\n    class_mode='categorical',\n    subset='training',\n    shuffle=True\n)\n\nval_generator = train_datagen.flow_from_directory(\n    shared_folder,\n    target_size=(img_size, img_size),\n    batch_size=batch_size,\n    class_mode='categorical',\n    subset='validation',\n    shuffle=False\n)\n","metadata":{"id":"IeB3xrhMQyOr","outputId":"d722c660-b2ac-471c-e323-0b18fcc0ba25","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T14:29:13.695896Z","iopub.execute_input":"2025-05-11T14:29:13.696214Z","iopub.status.idle":"2025-05-11T14:29:19.421111Z","shell.execute_reply.started":"2025-05-11T14:29:13.696181Z","shell.execute_reply":"2025-05-11T14:29:19.420122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\n\nbase_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3))\nbase_model.trainable = False  # Freeze base\n\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dropout(0.3),\n    Dense(128, activation='relu'),\n    Dropout(0.2),\n    Dense(train_generator.num_classes, activation='softmax')\n])\n","metadata":{"id":"eI4S5MCYQ-yk","outputId":"300b975e-bfeb-4ea9-c27e-1aebc9fdc6ac","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T14:29:25.611843Z","iopub.execute_input":"2025-05-11T14:29:25.612125Z","iopub.status.idle":"2025-05-11T14:29:28.333483Z","shell.execute_reply.started":"2025-05-11T14:29:25.612106Z","shell.execute_reply":"2025-05-11T14:29:28.332469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10\n)\n","metadata":{"id":"r5gxcDgXRB0K","outputId":"13337ae6-d574-40c5-85ea-bec922b6c0c3","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T14:29:35.701830Z","iopub.execute_input":"2025-05-11T14:29:35.702146Z","iopub.status.idle":"2025-05-11T16:21:22.296697Z","shell.execute_reply.started":"2025-05-11T14:29:35.702124Z","shell.execute_reply":"2025-05-11T16:21:22.295662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, acc = model.evaluate(val_generator)\nprint(f\"✅ Final Validation Accuracy: {acc:.4f}\")\n\nmodel.save(\"mobilenetv2_transfer_learning_model.h5\")\n","metadata":{"id":"NiXgf75wRD4g","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T16:21:36.616203Z","iopub.execute_input":"2025-05-11T16:21:36.616511Z","iopub.status.idle":"2025-05-11T16:23:48.710162Z","shell.execute_reply.started":"2025-05-11T16:21:36.616492Z","shell.execute_reply":"2025-05-11T16:23:48.709335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\n\ntest_image_path = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_100025.jpg\"\nimg = cv2.imread(test_image_path)\nimg = cv2.resize(img, (img_size, img_size))\nimg = img.astype(\"float32\") / 255.0\nimg = np.expand_dims(img, axis=0)\n\nprediction = model.predict(img)\npredicted_class = np.argmax(prediction)\nprint(f\"Predicted class index: {predicted_class}\")\n","metadata":{"id":"MMirz_MXRGFg","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T16:28:12.011059Z","iopub.execute_input":"2025-05-11T16:28:12.011430Z","iopub.status.idle":"2025-05-11T16:28:12.147644Z","shell.execute_reply.started":"2025-05-11T16:28:12.011406Z","shell.execute_reply":"2025-05-11T16:28:12.146423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nclass_labels = [\n    'safe driving', 'texting - right', 'talking on the phone - right', 'texting - left',\n    'talking on the phone - left', 'operating the radio', 'drinking', 'reaching behind',\n    'hair and makeup', 'talking to passenger'\n]\n\ntest_image_path = \"/kaggle/input/mmmmmm/WhatsApp Image 2025-05-11 at 7.31.17 PM.jpeg\"\nimg_size = 224  # adjust as per model requirement\n\n# Load and preprocess image\nimg = cv2.imread(test_image_path)\nimg_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # for displaying with correct colors\nimg_resized = cv2.resize(img, (img_size, img_size))\nimg_input = img_resized.astype(\"float32\") / 255.0\nimg_input = np.expand_dims(img_input, axis=0)\n\n# Predict\nprediction = model.predict(img_input)\npredicted_class = np.argmax(prediction)\npredicted_label = class_labels[predicted_class]\n\n# Display image with label\nplt.imshow(img_rgb)\nplt.title(f\"Predicted: {predicted_label}\")\nplt.axis(\"off\")\nplt.show()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T16:56:14.489488Z","iopub.execute_input":"2025-05-11T16:56:14.489859Z","iopub.status.idle":"2025-05-11T16:56:14.963817Z","shell.execute_reply.started":"2025-05-11T16:56:14.489836Z","shell.execute_reply":"2025-05-11T16:56:14.962739Z"}},"outputs":[],"execution_count":null}]}