{"metadata":{"accelerator":"GPU","colab":{"gpuType":"L4","machine_shape":"hm","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Step-by-Step Guide for Generating and Packaging Monet-Style Images Using CycleGAN\n\nIn this guide, we walk through the process of generating Monet-style images from a dataset of photos using a pre-trained CycleGAN model. Here's a breakdown of what happens in each step of the code snippet:\n\n1. **Environment Setup**: We clone the CycleGAN repository and navigate to its directory to set up our working environment.\n2. **Download the Pre-trained Model**: We download the pre-trained CycleGAN model for style transfer to Monet-style images.\n3. **Install Dependencies**: We install the necessary dependencies, including those listed in the `requirements.txt` and the `Pillow` library for image processing.\n4. **Preprocess Input Images**: We resize and convert the input images to the required format (256x256 RGB) and save them to a writable directory for further processing.\n5. **Generate Monet-Style Images**: We use the pre-trained CycleGAN model to convert the preprocessed images into Monet-style images.\n6. **Convert and Zip Images**: We convert the generated PNG images to JPEG format and add them directly to a zip file named `images.zip`. This zip file is the sole output of the notebook, containing all the generated Monet-style images ready for easy sharing and submission.","metadata":{"id":"lxieNDLPwoNu"}},{"cell_type":"code","source":"# Set Up Environment\n!git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix\n%cd pytorch-CycleGAN-and-pix2pix\n\n# Download the Pre-trained Model\n!bash ./scripts/download_cyclegan_model.sh style_monet\n\n# Install Dependencies\n!pip install -r requirements.txt\n!pip install Pillow\n\n# Preprocess and Copy Your Photos\nimport os\nimport glob\nfrom PIL import Image\nimport zipfile\n\n# Create a directory for preprocessed images in a writable location\npreprocessed_dir = '/kaggle/working/preprocessed_images'\nos.makedirs(preprocessed_dir, exist_ok=True)\n\n# Path to input images\nsource_dir = '/kaggle/input/gan-getting-started/photo_jpg'\ntarget_size = (256, 256)\n\n# Preprocess images: resize to 256x256 (CycleGAN's default input size)\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)))  # Save to writable directory\n\n# Verify number of preprocessed images\nprint(f\"Number of preprocessed images: {len(os.listdir(preprocessed_dir))}\")\n\n\n# Use pre-trained CycleGAN model to convert photos into Monet-style art\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# Create a zip file and add generated images directly to it\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}\")","metadata":{},"execution_count":null,"outputs":[]}]}