{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":35760,"databundleVersionId":3441104}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nimport os\nimport cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T05:57:06.149993Z","iopub.execute_input":"2026-03-26T05:57:06.150160Z","iopub.status.idle":"2026-03-26T05:57:07.944107Z","shell.execute_reply.started":"2026-03-26T05:57:06.150132Z","shell.execute_reply":"2026-03-26T05:57:07.943150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img.imread('/kaggle/input/competitions/cat-and-dog-classification-harper2022/Cat/0.jpg')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T05:57:07.945514Z","iopub.execute_input":"2026-03-26T05:57:07.945812Z","iopub.status.idle":"2026-03-26T05:57:08.008110Z","shell.execute_reply.started":"2026-03-26T05:57:07.945792Z","shell.execute_reply":"2026-03-26T05:57:08.007451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications.mobilenet_v2 import preprocess_input","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:37.725496Z","iopub.execute_input":"2026-03-26T06:11:37.725807Z","iopub.status.idle":"2026-03-26T06:11:37.737960Z","shell.execute_reply.started":"2026-03-26T06:11:37.725780Z","shell.execute_reply":"2026-03-26T06:11:37.736931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_cats = []\ny_cats = []\nfor i in os.listdir('/kaggle/input/competitions/cat-and-dog-classification-harper2022/Cat')[:1000]:\n    try:\n        image = img.imread(f'/kaggle/input/competitions/cat-and-dog-classification-harper2022/Cat/{i}')\n        image = cv2.resize(image, (224,224))\n        if len(image.shape) == 2:\n            image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)\n        image = image[:,:,:3]\n        image = preprocess_input(image) \n        X_cats.append(image)\n        y_cats.append(0)\n    except:\n        print(i)\nX_cats = np.array(X_cats, dtype='float32')\ny_cats = np.array(y_cats, dtype='float32')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:40.187071Z","iopub.execute_input":"2026-03-26T06:11:40.187362Z","iopub.status.idle":"2026-03-26T06:11:42.753569Z","shell.execute_reply.started":"2026-03-26T06:11:40.187338Z","shell.execute_reply":"2026-03-26T06:11:42.752912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_dogs = []\ny_dogs = []\nfor i in os.listdir('/kaggle/input/competitions/cat-and-dog-classification-harper2022/Dog')[:1000]:\n    try:\n        image = img.imread(f'/kaggle/input/competitions/cat-and-dog-classification-harper2022/Dog/{i}')\n        image = cv2.resize(image, (224,224))\n        if len(image.shape) == 2:\n            image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)\n        image = image[:,:,:3]\n        image = preprocess_input(image) \n        X_dogs.append(image)\n        y_dogs.append(1)\n    except:\n        print(i)\nX_dogs = np.array(X_dogs, dtype='float32')\ny_dogs = np.array(y_dogs, dtype='float32')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:44.579206Z","iopub.execute_input":"2026-03-26T06:11:44.579537Z","iopub.status.idle":"2026-03-26T06:11:47.181440Z","shell.execute_reply.started":"2026-03-26T06:11:44.579511Z","shell.execute_reply":"2026-03-26T06:11:47.180434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_cats.shape, y_cats.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:52.041988Z","iopub.execute_input":"2026-03-26T06:11:52.042357Z","iopub.status.idle":"2026-03-26T06:11:52.047839Z","shell.execute_reply.started":"2026-03-26T06:11:52.042236Z","shell.execute_reply":"2026-03-26T06:11:52.046936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_dogs.shape, y_dogs.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:53.536892Z","iopub.execute_input":"2026-03-26T06:11:53.537150Z","iopub.status.idle":"2026-03-26T06:11:53.542945Z","shell.execute_reply.started":"2026-03-26T06:11:53.537127Z","shell.execute_reply":"2026-03-26T06:11:53.541811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = np.concatenate([X_cats,X_dogs], axis=0)\ny_train = np.concatenate([y_cats,y_dogs], axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:55.395206Z","iopub.execute_input":"2026-03-26T06:11:55.395477Z","iopub.status.idle":"2026-03-26T06:11:55.603952Z","shell.execute_reply.started":"2026-03-26T06:11:55.395452Z","shell.execute_reply":"2026-03-26T06:11:55.602934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.utils import shuffle","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:56.976291Z","iopub.execute_input":"2026-03-26T06:11:56.976611Z","iopub.status.idle":"2026-03-26T06:11:56.979841Z","shell.execute_reply.started":"2026-03-26T06:11:56.976566Z","shell.execute_reply":"2026-03-26T06:11:56.979265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,y_train = shuffle(X_train,y_train, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:11:59.944013Z","iopub.execute_input":"2026-03-26T06:11:59.944309Z","iopub.status.idle":"2026-03-26T06:12:00.164396Z","shell.execute_reply.started":"2026-03-26T06:11:59.944285Z","shell.execute_reply":"2026-03-26T06:12:00.163477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.applications import MobileNetV2\nimport tensorflow as tf\nfrom keras.optimizers import Adam","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:12:01.611012Z","iopub.execute_input":"2026-03-26T06:12:01.611329Z","iopub.status.idle":"2026-03-26T06:12:01.616447Z","shell.execute_reply.started":"2026-03-26T06:12:01.611303Z","shell.execute_reply":"2026-03-26T06:12:01.615455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:18:01.897667Z","iopub.execute_input":"2026-03-26T06:18:01.897916Z","iopub.status.idle":"2026-03-26T06:18:02.415736Z","shell.execute_reply.started":"2026-03-26T06:18:01.897894Z","shell.execute_reply":"2026-03-26T06:18:02.414817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model.trainable=False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:18:03.649115Z","iopub.execute_input":"2026-03-26T06:18:03.649406Z","iopub.status.idle":"2026-03-26T06:18:03.654949Z","shell.execute_reply.started":"2026-03-26T06:18:03.649381Z","shell.execute_reply":"2026-03-26T06:18:03.654314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n            base_model, \n            GlobalAveragePooling2D(), \n            Dense(1, activation='sigmoid')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:18:04.912994Z","iopub.execute_input":"2026-03-26T06:18:04.913588Z","iopub.status.idle":"2026-03-26T06:18:04.989016Z","shell.execute_reply.started":"2026-03-26T06:18:04.913561Z","shell.execute_reply":"2026-03-26T06:18:04.988071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:18:07.504492Z","iopub.execute_input":"2026-03-26T06:18:07.505093Z","iopub.status.idle":"2026-03-26T06:18:07.517044Z","shell.execute_reply.started":"2026-03-26T06:18:07.505067Z","shell.execute_reply":"2026-03-26T06:18:07.515904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train,y_train, validation_split=0.15, epochs=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-26T06:18:12.420432Z","iopub.execute_input":"2026-03-26T06:18:12.420829Z","iopub.status.idle":"2026-03-26T06:23:08.820915Z","shell.execute_reply.started":"2026-03-26T06:18:12.420799Z","shell.execute_reply":"2026-03-26T06:23:08.820304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tagets = y_pred.flaten()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_names = os.listdir('/kaggle/input/competitions/cat-and-dog-classification-harper2022/Test')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dict_ = {\n    'Id': image_names,\n    'Target': targets\n}","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.DataFrame(dict_)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.to_csv('submission_file.csv', index=False, index_label=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}