{"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":"none","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-12-18T05:54:51.525109Z","iopub.execute_input":"2024-12-18T05:54:51.525977Z","iopub.status.idle":"2024-12-18T05:54:55.248445Z","shell.execute_reply.started":"2024-12-18T05:54:51.525918Z","shell.execute_reply":"2024-12-18T05:54:55.247139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport glob\nfrom PIL import Image\nimport zipfile\n\n# Clone the CycleGAN repository\n!git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix\n%cd pytorch-CycleGAN-and-pix2pix\n\n# Download the pre-trained CycleGAN model for Monet style transfer\n!bash ./scripts/download_cyclegan_model.sh style_monet\n\n# Install necessary dependencies\n!pip install -r requirements.txt\n!pip install Pillow\n\n# Preprocess the input images\npreprocessed_dir = '/kaggle/working/preprocessed_images'\nos.makedirs(preprocessed_dir, exist_ok=True)\n\nsource_dir = '/kaggle/input/gan-getting-started/photo_jpg'\ntarget_size = (256, 256)\n\nfor img_path in glob.glob(os.path.join(source_dir, '*')):\n    img = Image.open(img_path).resize(target_size).convert('RGB')\n    img.save(os.path.join(preprocessed_dir, os.path.basename(img_path)))\n\nprint(f\"Number of preprocessed images: {len(os.listdir(preprocessed_dir))}\")\n\n# Run the CycleGAN model to generate Monet-style images\n!python test.py --dataroot /kaggle/working/preprocessed_images --name style_monet_pretrained --model test --no_dropout --gpu_ids -1 --num_test 7035\n\n# Compress the generated images into a ZIP file\nwith zipfile.ZipFile('/kaggle/working/images.zip', 'w') as zipf:\n    generated_dir = './results/style_monet_pretrained/test_latest/images'\n\n    count = 0\n    for img_path in glob.glob(os.path.join(generated_dir, '*_fake.png')):\n        # Convert PNG to JPEG and write to the zip file\n        img = Image.open(img_path).convert('RGB')\n        img_jpeg_path = img_path.replace('.png', '.jpg')\n        img.save(img_jpeg_path, 'JPEG')\n        \n        zipf.write(img_jpeg_path, os.path.basename(img_jpeg_path))\n        count += 1\n\n    print(f\"Total images added to zip: {count}\")\n\nprint(\"All Monet-style images are saved in /kaggle/working/images.zip\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T05:54:55.250652Z","iopub.execute_input":"2024-12-18T05:54:55.251251Z","iopub.status.idle":"2024-12-18T06:38:20.396212Z","shell.execute_reply.started":"2024-12-18T05:54:55.251203Z","shell.execute_reply":"2024-12-18T06:38:20.394627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}