{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import zipfile\nimport os\n\n# Paths to the zip files\ntrain_zip_path = '/kaggle/input/dogs-vs-cats/train.zip'\ntrain_dir = '/kaggle/working/'\n\n# Extract train.zip\nwith zipfile.ZipFile(train_zip_path, 'r') as zip_ref:\n    zip_ref.extractall(train_dir)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-18T15:20:55.304829Z","iopub.execute_input":"2024-09-18T15:20:55.305442Z","iopub.status.idle":"2024-09-18T15:21:05.121874Z","shell.execute_reply.started":"2024-09-18T15:20:55.305394Z","shell.execute_reply":"2024-09-18T15:21:05.121017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = \"/kaggle/working/train1/\"\ntest = \"/kaggle/working/test1/\"\n\nos.mkdir(train)\nos.mkdir(test)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:21:05.123572Z","iopub.execute_input":"2024-09-18T15:21:05.123895Z","iopub.status.idle":"2024-09-18T15:21:05.128520Z","shell.execute_reply.started":"2024-09-18T15:21:05.123862Z","shell.execute_reply":"2024-09-18T15:21:05.127639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dogs_train_dir = \"/kaggle/working/train1/dogs\"\ndogs_test_dir = \"/kaggle/working/test1/dogs\"\n\ncats_train_dir = \"/kaggle/working/train1/cats\"\ncats_test_dir = \"/kaggle/working/test1/cats\"\n\nos.mkdir(dogs_train_dir)\nos.mkdir(dogs_test_dir)\nos.mkdir(cats_train_dir)\nos.mkdir(cats_test_dir)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:21:05.129648Z","iopub.execute_input":"2024-09-18T15:21:05.129981Z","iopub.status.idle":"2024-09-18T15:21:05.142927Z","shell.execute_reply.started":"2024-09-18T15:21:05.129949Z","shell.execute_reply":"2024-09-18T15:21:05.142173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\ndogs_count, cats_count = 0, 0\n\nfor filename in os.listdir(\"/kaggle/working/train\"):\n    \n    filepath = os.path.join('/kaggle/working/train', filename)\n    \n    if dogs_count > 1000 and cats_count > 1000:\n        break\n        \n    if filename.startswith(\"dog\"):\n        if dogs_count < 800:\n            shutil.move(filepath, dogs_train_dir)\n        elif dogs_count < 1000:\n            shutil.move(filepath, dogs_test_dir)\n        \n        dogs_count += 1\n        \n    if filename.startswith(\"cat\"):\n        if cats_count < 800:\n            shutil.move(filepath, cats_train_dir)\n        elif cats_count < 1000:\n            shutil.move(filepath, cats_test_dir)\n        cats_count += 1\n        ","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:21:05.145075Z","iopub.execute_input":"2024-09-18T15:21:05.145364Z","iopub.status.idle":"2024-09-18T15:21:05.264224Z","shell.execute_reply.started":"2024-09-18T15:21:05.145332Z","shell.execute_reply":"2024-09-18T15:21:05.263394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir(dogs_train_dir)))\nprint(len(os.listdir(dogs_test_dir)))\nprint(len(os.listdir(cats_train_dir)))\nprint(len(os.listdir(cats_test_dir)))","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:21:05.265299Z","iopub.execute_input":"2024-09-18T15:21:05.265583Z","iopub.status.idle":"2024-09-18T15:21:05.273206Z","shell.execute_reply.started":"2024-09-18T15:21:05.265552Z","shell.execute_reply":"2024-09-18T15:21:05.272230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:21:05.274282Z","iopub.execute_input":"2024-09-18T15:21:05.274560Z","iopub.status.idle":"2024-09-18T15:21:18.680292Z","shell.execute_reply.started":"2024-09-18T15:21:05.274529Z","shell.execute_reply":"2024-09-18T15:21:18.678607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255, rotation_range=40, width_shift_range=0.2,\n                                   height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_directory(train, target_size=(150, 150), batch_size=32, class_mode='binary')\nvalidation_generator = val_datagen.flow_from_directory(test, target_size=(150, 150), batch_size=32, class_mode='binary')\n","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:21:18.681569Z","iopub.execute_input":"2024-09-18T15:21:18.682111Z","iopub.status.idle":"2024-09-18T15:21:18.756900Z","shell.execute_reply.started":"2024-09-18T15:21:18.682076Z","shell.execute_reply":"2024-09-18T15:21:18.756112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG19\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.models import Model\n\n# Load VGG16 without the top classification layer\nbase_model = VGG19(weights = 'imagenet', include_top=False, input_shape=(150, 150, 3))\n\n# Freeze the base model (pre-trained layers)\nbase_model.trainable = False\n    \n# Add custom layers on top\nx = base_model.output\nx = Flatten()(x)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\npredictions = Dense(1, activation='sigmoid')(x)  # Binary classification\n\n# Define the final model\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:23:00.524543Z","iopub.execute_input":"2024-09-18T15:23:00.525250Z","iopub.status.idle":"2024-09-18T15:23:03.821227Z","shell.execute_reply.started":"2024-09-18T15:23:00.525189Z","shell.execute_reply":"2024-09-18T15:23:03.820432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:23:06.525587Z","iopub.execute_input":"2024-09-18T15:23:06.526007Z","iopub.status.idle":"2024-09-18T15:23:06.566638Z","shell.execute_reply.started":"2024-09-18T15:23:06.525966Z","shell.execute_reply":"2024-09-18T15:23:06.565709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\noptimizers = Adam(learning_rate = 0.001)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:23:13.118032Z","iopub.execute_input":"2024-09-18T15:23:13.118613Z","iopub.status.idle":"2024-09-18T15:23:13.125985Z","shell.execute_reply.started":"2024-09-18T15:23:13.118567Z","shell.execute_reply":"2024-09-18T15:23:13.125024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:23:16.385930Z","iopub.execute_input":"2024-09-18T15:23:16.386294Z","iopub.status.idle":"2024-09-18T15:23:16.394050Z","shell.execute_reply.started":"2024-09-18T15:23:16.386260Z","shell.execute_reply":"2024-09-18T15:23:16.393052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator, validation_data=validation_generator, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:23:20.140409Z","iopub.execute_input":"2024-09-18T15:23:20.140809Z","iopub.status.idle":"2024-09-18T15:25:18.580169Z","shell.execute_reply.started":"2024-09-18T15:23:20.140769Z","shell.execute_reply":"2024-09-18T15:25:18.579242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate(validation_generator)\nprint(f'Validation Loss: {loss}')\nprint(f'Validation Accuracy: {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2024-09-18T15:25:24.539660Z","iopub.execute_input":"2024-09-18T15:25:24.540074Z","iopub.status.idle":"2024-09-18T15:25:25.357072Z","shell.execute_reply.started":"2024-09-18T15:25:24.540036Z","shell.execute_reply":"2024-09-18T15:25:25.356041Z"},"trusted":true},"execution_count":null,"outputs":[]}]}