{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nimport numpy as np # linear algebra\nimport 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\nimport os\nfor 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-08-13T01:43:22.140802Z","iopub.execute_input":"2025-08-13T01:43:22.141080Z","iopub.status.idle":"2025-08-13T01:43:24.017312Z","shell.execute_reply.started":"2025-08-13T01:43:22.141059Z","shell.execute_reply":"2025-08-13T01:43:24.016601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport os\n\n# Giải nén train.zip\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train_raw')\n\n# Giải nén test1.zip\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:43:26.562111Z","iopub.execute_input":"2025-08-13T01:43:26.562939Z","iopub.status.idle":"2025-08-13T01:43:41.001925Z","shell.execute_reply.started":"2025-08-13T01:43:26.562913Z","shell.execute_reply":"2025-08-13T01:43:41.001018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\nimport random\n\n# Đường dẫn nguồn\nsource_dir = '/kaggle/working/train_raw/train'\nsplit_dir = '/kaggle/working/data_split'\n\n# Tạo thư mục mới\nfor split in ['train', 'val']:\n    for cls in ['cats', 'dogs']:\n        os.makedirs(os.path.join(split_dir, split, cls), exist_ok=True)\n\n# Danh sách ảnh\nall_files = os.listdir(source_dir)\ncat_files = [f for f in all_files if f.startswith(\"cat\")]\ndog_files = [f for f in all_files if f.startswith(\"dog\")]\n\n# Shuffle và split\nrandom.seed(42)\nrandom.shuffle(cat_files)\nrandom.shuffle(dog_files)\n\nsplit_cat = int(0.8 * len(cat_files))\nsplit_dog = int(0.8 * len(dog_files))\n\ntrain_cats, val_cats = cat_files[:split_cat], cat_files[split_cat:]\ntrain_dogs, val_dogs = dog_files[:split_dog], dog_files[split_dog:]\n\n# Copy file\ndef copy_files(files, src, dst):\n    for f in files:\n        shutil.copy(os.path.join(src, f), os.path.join(dst, f))\n\ncopy_files(train_cats, source_dir, os.path.join(split_dir, 'train', 'cats'))\ncopy_files(val_cats, source_dir, os.path.join(split_dir, 'val', 'cats'))\ncopy_files(train_dogs, source_dir, os.path.join(split_dir, 'train', 'dogs'))\ncopy_files(val_dogs, source_dir, os.path.join(split_dir, 'val', 'dogs'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:43:44.715200Z","iopub.execute_input":"2025-08-13T01:43:44.715463Z","iopub.status.idle":"2025-08-13T01:43:47.874559Z","shell.execute_reply.started":"2025-08-13T01:43:44.715435Z","shell.execute_reply":"2025-08-13T01:43:47.873874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n    os.path.join(split_dir, 'train'),\n    image_size=(180, 180),\n    batch_size=32\n)\n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n    os.path.join(split_dir, 'val'),\n    image_size=(180, 180),\n    batch_size=32\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:43:52.985670Z","iopub.execute_input":"2025-08-13T01:43:52.985942Z","iopub.status.idle":"2025-08-13T01:44:11.325305Z","shell.execute_reply.started":"2025-08-13T01:43:52.985920Z","shell.execute_reply":"2025-08-13T01:44:11.324475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nimport numpy as np\n\ntest_dir = '/kaggle/working/test/test1'\ntest_filenames = sorted(os.listdir(test_dir))  # Đảm bảo thứ tự\n\ndef load_test_images(image_dir, image_size=(150, 150)):\n    images = []\n    for fname in test_filenames:\n        img_path = os.path.join(image_dir, fname)\n        img = image.load_img(img_path, target_size=image_size)\n        img_array = image.img_to_array(img)\n        images.append(img_array)\n    return np.array(images) / 255.0  # Chuẩn hóa\n\ntest_ds = load_test_images(test_dir)\nprint(\"Shape of test images:\", test_ds.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:44:19.792315Z","iopub.execute_input":"2025-08-13T01:44:19.793069Z","iopub.status.idle":"2025-08-13T01:44:38.582398Z","shell.execute_reply.started":"2025-08-13T01:44:19.793043Z","shell.execute_reply":"2025-08-13T01:44:38.581601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#data processing\n\n#Resize image to 150x150\nimg_size = (150,150)\n\n#One-hot encoding label\nnum_classes = 2\n\ndef preprocess(image,label):\n    #resize theo yeu cau\n    image = tf.image.resize(image, img_size)\n    #dua ve [0,1]\n    image = image / 255.0\n\n    #one-hot label \n    label = tf.one_hot(label, depth=num_classes)\n    \n    return image, label\n\nAUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.map(preprocess).shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nval_ds  = val_ds.map(preprocess).prefetch(buffer_size=AUTOTUNE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:44:45.403669Z","iopub.execute_input":"2025-08-13T01:44:45.404258Z","iopub.status.idle":"2025-08-13T01:44:45.476453Z","shell.execute_reply.started":"2025-08-13T01:44:45.404235Z","shell.execute_reply":"2025-08-13T01:44:45.475655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for image, label in train_ds.take(1):\n    print(\"Image shape:\", image.shape)\n    print(\"Label (one-hot):\", label.numpy())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:44:48.281964Z","iopub.execute_input":"2025-08-13T01:44:48.282276Z","iopub.status.idle":"2025-08-13T01:45:00.823067Z","shell.execute_reply.started":"2025-08-13T01:44:48.282251Z","shell.execute_reply":"2025-08-13T01:45:00.822477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\n# Tạo layer data augmentation\ndata_augmentation = keras.Sequential([\n    layers.RandomFlip(\"horizontal\"),          # Lật ngang ngẫu nhiên\n    layers.RandomRotation(0.1),               # Xoay ±10%\n    layers.RandomZoom(0.1),                   # Zoom ±10%\n    layers.RandomContrast(0.1)                # Tăng/giảm độ tương phản\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:45:04.685090Z","iopub.execute_input":"2025-08-13T01:45:04.685768Z","iopub.status.idle":"2025-08-13T01:45:04.707053Z","shell.execute_reply.started":"2025-08-13T01:45:04.685739Z","shell.execute_reply":"2025-08-13T01:45:04.706529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model CNN\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\nmodel = keras.Sequential([\n\n    data_augmentation,\n\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),\n    layers.MaxPooling2D(),\n\n    layers.Conv2D(64, (3, 3), activation='relu',padding='same'),\n    layers.MaxPooling2D(),\n\n    layers.Conv2D(128, (3, 3), activation='relu',padding='same'),\n    layers.MaxPooling2D(),\n\n    layers.Conv2D(256, (3,3), activation='relu',padding='same'),\n    layers.MaxPooling2D(),\n\n    layers.Flatten(),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(64,activation='relu'),\n    layers.Dropout(0.4),\n    layers.Dense(2, activation='softmax')\n])\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-12T14:51:39.066754Z","iopub.execute_input":"2025-08-12T14:51:39.067345Z","iopub.status.idle":"2025-08-12T14:51:39.146373Z","shell.execute_reply.started":"2025-08-12T14:51:39.067323Z","shell.execute_reply":"2025-08-12T14:51:39.145695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#compile\n\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',  #da one_hot\n    metrics=['accuracy']\n)\n\nhistory = model.fit(\n    train_ds,\n    validation_data = val_ds,\n    epochs=30\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-12T14:30:31.173845Z","iopub.execute_input":"2025-08-12T14:30:31.174162Z","iopub.status.idle":"2025-08-12T14:45:45.726509Z","shell.execute_reply.started":"2025-08-12T14:30:31.174129Z","shell.execute_reply":"2025-08-12T14:45:45.725904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#plot accurancy and loss\nimport matplotlib.pyplot as plt\n\n# Accuracy\nplt.figure()\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Train vs Validation Accuracy')\nplt.legend()\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-12T14:46:10.135725Z","iopub.execute_input":"2025-08-12T14:46:10.136037Z","iopub.status.idle":"2025-08-12T14:46:10.635184Z","shell.execute_reply.started":"2025-08-12T14:46:10.136015Z","shell.execute_reply":"2025-08-12T14:46:10.634290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Loss\nplt.figure()\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Train vs Validation Loss')\nplt.legend()\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-12T14:46:16.294470Z","iopub.execute_input":"2025-08-12T14:46:16.295306Z","iopub.status.idle":"2025-08-12T14:46:16.492583Z","shell.execute_reply.started":"2025-08-12T14:46:16.295281Z","shell.execute_reply":"2025-08-12T14:46:16.491680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\n\n# Base model: MobileNetV2 (Transfer Learning)\nbase_model = MobileNetV2(input_shape=(150, 150, 3),\n                         include_top=False,\n                         weights='imagenet')\nbase_model.trainable = False  \n\nmodel = tf.keras.Sequential([\n    base_model,    \n    layers.GlobalAveragePooling2D(),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(64, activation='relu'),\n    layers.Dropout(0.5),\n    layers.Dense(2, activation='softmax')\n])\n\nmodel.summary()\n\n#Compile model\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:45:17.879992Z","iopub.execute_input":"2025-08-13T01:45:17.880795Z","iopub.status.idle":"2025-08-13T01:45:19.921549Z","shell.execute_reply.started":"2025-08-13T01:45:17.880767Z","shell.execute_reply":"2025-08-13T01:45:19.920919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_tl = model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=10,\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:45:32.291084Z","iopub.execute_input":"2025-08-13T01:45:32.291833Z","iopub.status.idle":"2025-08-13T01:49:58.472519Z","shell.execute_reply.started":"2025-08-13T01:45:32.291808Z","shell.execute_reply":"2025-08-13T01:49:58.471664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Accuracy\nplt.figure()\nplt.plot(history_tl.history['accuracy'], label='Train Accuracy')\nplt.plot(history_tl.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Train vs Validation Accuracy')\nplt.legend()\nplt.grid(True)\nplt.show()\n\n# Loss\nplt.figure()\nplt.plot(history_tl.history['loss'], label='Train Loss')\nplt.plot(history_tl.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Train vs Validation Loss')\nplt.legend()\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:50:47.353650Z","iopub.execute_input":"2025-08-13T01:50:47.354425Z","iopub.status.idle":"2025-08-13T01:50:47.834646Z","shell.execute_reply.started":"2025-08-13T01:50:47.354396Z","shell.execute_reply":"2025-08-13T01:50:47.833680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#fine tune\n\nbase_model.trainable = True\n\nfine_tune_at = 100 \nfor layer in base_model.layers[:fine_tune_at]:\n    layer.trainable = False\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:51:20.037805Z","iopub.execute_input":"2025-08-13T01:51:20.038158Z","iopub.status.idle":"2025-08-13T01:51:20.045991Z","shell.execute_reply.started":"2025-08-13T01:51:20.038093Z","shell.execute_reply":"2025-08-13T01:51:20.045050Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:53:24.174623Z","iopub.execute_input":"2025-08-13T01:53:24.174894Z","iopub.status.idle":"2025-08-13T01:53:24.187826Z","shell.execute_reply.started":"2025-08-13T01:53:24.174876Z","shell.execute_reply":"2025-08-13T01:53:24.187072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                             patience=3, min_lr=1e-3)\n\n# Train tiếp với fine-tuning\nhistory_ftune = model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=30,  # hoặc nhiều hơn nếu cần\n    callbacks=[early_stopping,reduce_lr]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T01:53:31.985430Z","iopub.execute_input":"2025-08-13T01:53:31.985767Z","iopub.status.idle":"2025-08-13T01:59:44.058417Z","shell.execute_reply.started":"2025-08-13T01:53:31.985744Z","shell.execute_reply":"2025-08-13T01:59:44.057555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Accuracy\nplt.figure()\nplt.plot(history_ftune.history['accuracy'], label='Train Accuracy')\nplt.plot(history_ftune.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Train vs Validation Accuracy')\nplt.legend()\nplt.grid(True)\nplt.show()\n\n# Loss\nplt.figure()\nplt.plot(history_ftune.history['loss'], label='Train Loss')\nplt.plot(history_ftune.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Train vs Validation Loss')\nplt.legend()\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-13T02:11:43.310052Z","iopub.execute_input":"2025-08-13T02:11:43.310395Z","iopub.status.idle":"2025-08-13T02:11:43.731958Z","shell.execute_reply.started":"2025-08-13T02:11:43.310373Z","shell.execute_reply":"2025-08-13T02:11:43.731138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}