{"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-02T12:45:05.304555Z","iopub.execute_input":"2023-09-02T12:45:05.305260Z","iopub.status.idle":"2023-09-02T12:45:05.326724Z","shell.execute_reply.started":"2023-09-02T12:45:05.305225Z","shell.execute_reply":"2023-09-02T12:45:05.325851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\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-02T12:45:05.328598Z","iopub.execute_input":"2023-09-02T12:45:05.328894Z","iopub.status.idle":"2023-09-02T12:45:49.420421Z","shell.execute_reply.started":"2023-09-02T12:45:05.328867Z","shell.execute_reply":"2023-09-02T12:45:49.419399Z"},"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-02T12:45:49.421748Z","iopub.execute_input":"2023-09-02T12:45:49.422319Z","iopub.status.idle":"2023-09-02T12:46:05.335236Z","shell.execute_reply.started":"2023-09-02T12:45:49.422286Z","shell.execute_reply":"2023-09-02T12:46:05.334287Z"},"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-02T12:46:05.336561Z","iopub.execute_input":"2023-09-02T12:46:05.336885Z","iopub.status.idle":"2023-09-02T12:46:05.638010Z","shell.execute_reply.started":"2023-09-02T12:46:05.336856Z","shell.execute_reply":"2023-09-02T12:46:05.637086Z"},"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-02T12:46:05.640617Z","iopub.execute_input":"2023-09-02T12:46:05.640947Z","iopub.status.idle":"2023-09-02T12:46:06.439727Z","shell.execute_reply.started":"2023-09-02T12:46:05.640901Z","shell.execute_reply":"2023-09-02T12:46:06.438523Z"},"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-02T12:46:06.441012Z","iopub.execute_input":"2023-09-02T12:46:06.441440Z","iopub.status.idle":"2023-09-02T12:46:07.975988Z","shell.execute_reply.started":"2023-09-02T12:46:06.441411Z","shell.execute_reply":"2023-09-02T12:46:07.974977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SIZE = (128, 128)  ","metadata":{"execution":{"iopub.status.busy":"2023-09-02T12:46:07.977269Z","iopub.execute_input":"2023-09-02T12:46:07.977591Z","iopub.status.idle":"2023-09-02T12:46:07.981679Z","shell.execute_reply.started":"2023-09-02T12:46:07.977562Z","shell.execute_reply":"2023-09-02T12:46:07.980906Z"},"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-02T12:46:07.982705Z","iopub.execute_input":"2023-09-02T12:46:07.982995Z","iopub.status.idle":"2023-09-02T12:46:07.994571Z","shell.execute_reply.started":"2023-09-02T12:46:07.982955Z","shell.execute_reply":"2023-09-02T12:46:07.993738Z"},"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-02T12:46:07.995570Z","iopub.execute_input":"2023-09-02T12:46:07.995959Z","iopub.status.idle":"2023-09-02T12:46:08.004618Z","shell.execute_reply.started":"2023-09-02T12:46:07.995931Z","shell.execute_reply":"2023-09-02T12:46:08.003765Z"},"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-02T12:46:08.005689Z","iopub.execute_input":"2023-09-02T12:46:08.006027Z","iopub.status.idle":"2023-09-02T12:47:37.242918Z","shell.execute_reply.started":"2023-09-02T12:46:08.005999Z","shell.execute_reply":"2023-09-02T12:47:37.241473Z"},"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-02T12:47:37.244543Z","iopub.execute_input":"2023-09-02T12:47:37.244910Z","iopub.status.idle":"2023-09-02T12:47:37.250630Z","shell.execute_reply.started":"2023-09-02T12:47:37.244878Z","shell.execute_reply":"2023-09-02T12:47:37.249585Z"},"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-02T12:47:37.251918Z","iopub.execute_input":"2023-09-02T12:47:37.252229Z","iopub.status.idle":"2023-09-02T12:47:37.533233Z","shell.execute_reply.started":"2023-09-02T12:47:37.252202Z","shell.execute_reply":"2023-09-02T12:47:37.531806Z"},"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-02T12:47:37.536871Z","iopub.execute_input":"2023-09-02T12:47:37.537215Z","iopub.status.idle":"2023-09-02T12:47:37.541679Z","shell.execute_reply.started":"2023-09-02T12:47:37.537187Z","shell.execute_reply":"2023-09-02T12:47:37.540773Z"},"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-02T12:47:37.542828Z","iopub.execute_input":"2023-09-02T12:47:37.543229Z","iopub.status.idle":"2023-09-02T12:47:41.101256Z","shell.execute_reply.started":"2023-09-02T12:47:37.543204Z","shell.execute_reply":"2023-09-02T12:47:41.099827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = 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-02T12:47:41.102841Z","iopub.execute_input":"2023-09-02T12:47:41.103271Z","iopub.status.idle":"2023-09-02T13:52:49.226597Z","shell.execute_reply.started":"2023-09-02T12:47:41.103237Z","shell.execute_reply":"2023-09-02T13:52:49.225316Z"},"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\"画像 {image_name} の処理中にエラーが発生しました: {e}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:54:46.259695Z","iopub.execute_input":"2023-09-02T13:54:46.260191Z","iopub.status.idle":"2023-09-02T13:55:31.271623Z","shell.execute_reply.started":"2023-09-02T13:54:46.260151Z","shell.execute_reply":"2023-09-02T13:55:31.270030Z"},"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-02T13:55:31.273377Z","iopub.execute_input":"2023-09-02T13:55:31.273704Z","iopub.status.idle":"2023-09-02T13:55:39.263962Z","shell.execute_reply.started":"2023-09-02T13:55:31.273677Z","shell.execute_reply":"2023-09-02T13:55:39.262701Z"},"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"}}