{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":22881,"databundleVersionId":1552852,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-21T10:49:25.328921Z","iopub.execute_input":"2025-07-21T10:49:25.329235Z","iopub.status.idle":"2025-07-21T10:49:25.333982Z","shell.execute_reply.started":"2025-07-21T10:49:25.329210Z","shell.execute_reply":"2025-07-21T10:49:25.332807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input\nimport os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T10:49:30.004856Z","iopub.execute_input":"2025-07-21T10:49:30.005205Z","iopub.status.idle":"2025-07-21T10:49:30.010360Z","shell.execute_reply.started":"2025-07-21T10:49:30.005178Z","shell.execute_reply":"2025-07-21T10:49:30.009353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_path = \"/kaggle/input/11-785-fall-20-homework-2-part-2\"\n\ntrain_dir = os.path.join(base_path, \"classification_data/train_data\")\nval_dir   = \"/kaggle/input/11-785-fall-20-homework-2-part-2/classification_data/val_data\"\ntest_dir  = os.path.join(base_path, \"classification_data/test_data\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T10:55:58.750583Z","iopub.execute_input":"2025-07-21T10:55:58.750935Z","iopub.status.idle":"2025-07-21T10:55:58.756206Z","shell.execute_reply.started":"2025-07-21T10:55:58.750909Z","shell.execute_reply":"2025-07-21T10:55:58.755120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_size = (224, 224)\nbatch_size = 32\n\ntrain_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    validation_split=0.2\n)\n\ntrain_gen = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=img_size,\n    batch_size=batch_size,\n    class_mode='categorical',\n)\n\nval_gen = train_datagen.flow_from_directory(\n    val_dir,\n    target_size=img_size,\n    batch_size=batch_size,\n    class_mode='categorical',\n)\n\ntest_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntest_gen = test_datagen.flow_from_directory(\n    test_dir,\n    target_size=img_size,\n    batch_size=batch_size,\n    class_mode='categorical'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T11:00:46.849166Z","iopub.execute_input":"2025-07-21T11:00:46.849599Z","iopub.status.idle":"2025-07-21T11:03:06.265438Z","shell.execute_reply.started":"2025-07-21T11:00:46.849562Z","shell.execute_reply":"2025-07-21T11:03:06.264596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense\n\nbase_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))\nbase_model.trainable = False\n\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(128, activation='relu')(x)\noutputs = Dense(train_gen.num_classes, activation='softmax')(x)\n\nmodel = Model(inputs=base_model.input, outputs=outputs)\n\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(train_gen, validation_data=val_gen, epochs=5)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}