{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.8.17","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-04T09:06:42.756502Z","iopub.execute_input":"2023-09-04T09:06:42.756903Z","iopub.status.idle":"2023-09-04T09:06:42.765019Z","shell.execute_reply.started":"2023-09-04T09:06:42.756871Z","shell.execute_reply":"2023-09-04T09:06:42.764040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport zipfile\nimport shutil\nimport glob\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import layers, models","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:06:43.006756Z","iopub.execute_input":"2023-09-04T09:06:43.007041Z","iopub.status.idle":"2023-09-04T09:07:12.771312Z","shell.execute_reply.started":"2023-09-04T09:06:43.007016Z","shell.execute_reply":"2023-09-04T09:07:12.770376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Detect and initialize the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# Instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:12.772891Z","iopub.execute_input":"2023-09-04T09:07:12.773383Z","iopub.status.idle":"2023-09-04T09:07:21.234469Z","shell.execute_reply.started":"2023-09-04T09:07:12.773354Z","shell.execute_reply":"2023-09-04T09:07:21.233315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train')\n\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test')","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:21.235684Z","iopub.execute_input":"2023-09-04T09:07:21.236003Z","iopub.status.idle":"2023-09-04T09:07:38.525587Z","shell.execute_reply.started":"2023-09-04T09:07:21.235974Z","shell.execute_reply":"2023-09-04T09:07:38.524380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_images_dir = '/kaggle/working/train/train'\n\n\ntrain_image_files = os.listdir(train_images_dir)\n\n\nsample_image_path = os.path.join(train_images_dir, train_image_files[0])\n\n\nimg = cv2.imread(sample_image_path)\n\n\nimg_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n\nplt.imshow(img_rgb)\nplt.axis('off')  \nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:38.527692Z","iopub.execute_input":"2023-09-04T09:07:38.528005Z","iopub.status.idle":"2023-09-04T09:07:38.772895Z","shell.execute_reply.started":"2023-09-04T09:07:38.527980Z","shell.execute_reply":"2023-09-04T09:07:38.771748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndirectory_path = [\n    \"/kaggle/working/data/train/dogs\",\n    \"/kaggle/working/data/train/cats\",\n    \"/kaggle/working/data/valid/dogs\",\n    \"/kaggle/working/data/valid/cats\",\n    \"/kaggle/working/data/test\",\n]\n\n\nfor path in directory_path:\n    os.makedirs(path, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:38.773898Z","iopub.execute_input":"2023-09-04T09:07:38.774170Z","iopub.status.idle":"2023-09-04T09:07:38.779835Z","shell.execute_reply.started":"2023-09-04T09:07:38.774145Z","shell.execute_reply":"2023-09-04T09:07:38.778898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def move_images(source_directory, destination_directories, file_filter=None):\n    for key, dest_dir in destination_directories.items():\n        files = [file for file in os.listdir(source_directory) if file_filter(file, key)]\n        for file in files:\n            source_path = os.path.join(source_directory, file)\n            destination_path = os.path.join(dest_dir, file)\n            shutil.move(source_path, destination_path)\n\nsource_directory = \"/kaggle/working/train/train\"\ntest_source_directory = \"/kaggle/working/test/test\"\n\ntrain_dest_directories = {\n    \"dog.\": \"/kaggle/working/data/train/dogs\",\n    \"cat.\": \"/kaggle/working/data/train/cats\",\n}\n\ntest_dest_directory = \"/kaggle/working/data/test/\"\n\n\ndef train_file_filter(file, key):\n    return file.startswith(key)\n\ndef test_file_filter(file, key):\n    return file.endswith(\".jpg\")\n\nmove_images(source_directory, train_dest_directories, file_filter=train_file_filter)\nmove_images(test_source_directory, {None: test_dest_directory}, file_filter=test_file_filter)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:38.781041Z","iopub.execute_input":"2023-09-04T09:07:38.781369Z","iopub.status.idle":"2023-09-04T09:07:40.239587Z","shell.execute_reply.started":"2023-09-04T09:07:38.781326Z","shell.execute_reply":"2023-09-04T09:07:40.238476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SIZE = (128, 128)  ","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:40.240810Z","iopub.execute_input":"2023-09-04T09:07:40.241156Z","iopub.status.idle":"2023-09-04T09:07:40.245527Z","shell.execute_reply.started":"2023-09-04T09:07:40.241126Z","shell.execute_reply":"2023-09-04T09:07:40.244609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_data(x, y=None):\n    \n    x = tf.reshape(x, (-1, *TARGET_SIZE, 1))\n   \n    x = tf.cast(x, tf.float32) / 255.0\n    if y is not None:\n\n        y = tf.cast(y, tf.int32)\n        return x, y\n    return x\n\n\nfeature_data, label_data = [], []","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:40.246553Z","iopub.execute_input":"2023-09-04T09:07:40.246825Z","iopub.status.idle":"2023-09-04T09:07:40.258251Z","shell.execute_reply.started":"2023-09-04T09:07:40.246801Z","shell.execute_reply":"2023-09-04T09:07:40.257353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATADIR = \"/kaggle/working/data/train\"\nTESTDIR = \"/kaggle/working/data/test\"\nCATEGORIES = [\"dogs\", \"cats\"]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:40.259277Z","iopub.execute_input":"2023-09-04T09:07:40.259611Z","iopub.status.idle":"2023-09-04T09:07:40.268830Z","shell.execute_reply.started":"2023-09-04T09:07:40.259585Z","shell.execute_reply":"2023-09-04T09:07:40.267950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for class_num, category in enumerate(CATEGORIES):\n    path = os.path.join(DATADIR, category)\n    for image_name in os.listdir(path):\n        try:\n            img_file = cv2.imread(os.path.join(path, image_name))\n            \n      \n            # print(\"Loaded image shape:\", img_file.shape)\n            \n            gray_image = cv2.cvtColor(img_file, cv2.COLOR_BGR2GRAY)\n            \n       \n            # print(\"Converted image shape:\", gray_image.shape)\n            \n            resize_file = cv2.resize(gray_image, TARGET_SIZE)\n            \n            feature_data.append(resize_file)\n            label_data.append(class_num)\n        except Exception as e:\n            print(\"Error processing image:\", e)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:07:40.272080Z","iopub.execute_input":"2023-09-04T09:07:40.272377Z","iopub.status.idle":"2023-09-04T09:09:08.647987Z","shell.execute_reply.started":"2023-09-04T09:07:40.272352Z","shell.execute_reply":"2023-09-04T09:09:08.646706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(feature_data))\nprint(len(label_data))","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:08.649283Z","iopub.execute_input":"2023-09-04T09:09:08.649589Z","iopub.status.idle":"2023-09-04T09:09:08.654739Z","shell.execute_reply.started":"2023-09-04T09:09:08.649561Z","shell.execute_reply":"2023-09-04T09:09:08.653841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:08.655797Z","iopub.execute_input":"2023-09-04T09:09:08.656107Z","iopub.status.idle":"2023-09-04T09:09:08.679570Z","shell.execute_reply.started":"2023-09-04T09:09:08.656080Z","shell.execute_reply":"2023-09-04T09:09:08.678626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:08.680675Z","iopub.execute_input":"2023-09-04T09:09:08.680981Z","iopub.status.idle":"2023-09-04T09:09:08.702817Z","shell.execute_reply.started":"2023-09-04T09:09:08.680954Z","shell.execute_reply":"2023-09-04T09:09:08.701928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x, valid_x, train_y, valid_y = train_test_split(\n    np.array(feature_data), np.array(label_data), test_size=0.1, random_state=50\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:08.703973Z","iopub.execute_input":"2023-09-04T09:09:08.704283Z","iopub.status.idle":"2023-09-04T09:09:08.982849Z","shell.execute_reply.started":"2023-09-04T09:09:08.704259Z","shell.execute_reply":"2023-09-04T09:09:08.981642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nEPOCHS = 25 \nNUM_CLASSES = 2","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:08.984114Z","iopub.execute_input":"2023-09-04T09:09:08.984402Z","iopub.status.idle":"2023-09-04T09:09:08.988586Z","shell.execute_reply.started":"2023-09-04T09:09:08.984377Z","shell.execute_reply":"2023-09-04T09:09:08.987659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_ds = tf.data.Dataset.from_tensor_slices((train_x, train_y))\nvalid_ds = tf.data.Dataset.from_tensor_slices((valid_x, valid_y))\n\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\n\n\ntrain_ds = (\n    train_ds.shuffle(len(train_x))  \n    .repeat()                      \n    .batch(BATCH_SIZE)              \n    .map(convert_data)               \n    .prefetch(buffer_size=AUTOTUNE)  \n)\n\nvalid_ds = valid_ds.batch(BATCH_SIZE) \nvalid_ds = valid_ds.map(convert_data) ","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:08.989692Z","iopub.execute_input":"2023-09-04T09:09:08.989974Z","iopub.status.idle":"2023-09-04T09:09:09.448471Z","shell.execute_reply.started":"2023-09-04T09:09:08.989948Z","shell.execute_reply":"2023-09-04T09:09:09.447337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tpu_strategy.scope():\n    model = models.Sequential([\n    layers.Input(shape=(*TARGET_SIZE, 1)),  \n    layers.Conv2D(32, (9, 9), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (5, 5), activation='relu'),  \n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(128, (3, 3), activation='relu'),  \n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(NUM_CLASSES, activation='softmax')  \n])\n\nmodel.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nhistory = model.fit(train_ds, epochs=EPOCHS, validation_data=valid_ds, steps_per_epoch=len(train_x) // BATCH_SIZE)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:09:09.449598Z","iopub.execute_input":"2023-09-04T09:09:09.449869Z","iopub.status.idle":"2023-09-04T09:13:36.155221Z","shell.execute_reply.started":"2023-09-04T09:09:09.449844Z","shell.execute_reply":"2023-09-04T09:13:36.153770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_test_data = []\n\n\nfor image_name in os.listdir(TESTDIR):\n    try:\n       \n        img_file = cv2.imread(os.path.join(TESTDIR, image_name))\n      \n        gray_image = cv2.cvtColor(img_file, cv2.COLOR_BGR2GRAY)\n      \n        resize_file = cv2.resize(gray_image, TARGET_SIZE)\n      \n        feature_test_data.append(resize_file)\n    except Exception as e:\n        \n        print(f\"Error occurred while processing image {image_name}: {e}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:13:36.157383Z","iopub.execute_input":"2023-09-04T09:13:36.157752Z","iopub.status.idle":"2023-09-04T09:14:20.445748Z","shell.execute_reply.started":"2023-09-04T09:13:36.157720Z","shell.execute_reply":"2023-09-04T09:14:20.444340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = tf.data.Dataset.from_tensor_slices(np.array(feature_test_data))\ntest_ds = test_ds.batch(BATCH_SIZE).map(convert_data)\n\ny_pred = model.predict(test_ds)\npredictions = np.argmax(y_pred, axis=1)\n\n\nsubmission = pd.DataFrame({\"id\": range(1, len(predictions) + 1), \"label\": predictions})\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T09:14:20.447241Z","iopub.execute_input":"2023-09-04T09:14:20.447545Z","iopub.status.idle":"2023-09-04T09:14:39.891683Z","shell.execute_reply.started":"2023-09-04T09:14:20.447517Z","shell.execute_reply":"2023-09-04T09:14:39.890268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}