{"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-21T13:30:03.116920Z","iopub.execute_input":"2024-12-21T13:30:03.117694Z","iopub.status.idle":"2024-12-21T13:30:09.975507Z","shell.execute_reply.started":"2024-12-21T13:30:03.117656Z","shell.execute_reply":"2024-12-21T13:30:09.973898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import tensorflow as tf\n# print(tf.__version__)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-21T13:30:09.978044Z","iopub.execute_input":"2024-12-21T13:30:09.978485Z","iopub.status.idle":"2024-12-21T13:30:09.983850Z","shell.execute_reply.started":"2024-12-21T13:30:09.978440Z","shell.execute_reply":"2024-12-21T13:30:09.982453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport zipfile\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom PIL import Image\nfrom tqdm import tqdm\n\n# ----------------------------------------\n# Parameter\n# ----------------------------------------\nLATENT_DIM = 100  # Dimensi noise untuk generator\nIMG_SHAPE = (256, 256, 3)  # Ukuran gambar keluaran\nNUM_IMAGES = 7000  # Jumlah gambar yang akan dihasilkan\nOUTPUT_ZIP = \"images.zip\"  # Nama file output zip\n\n# ----------------------------------------\n# Generator\n# ----------------------------------------\ndef build_generator():\n    model = tf.keras.Sequential()\n    \n    # Gunakan Input(shape) sebagai layer pertama\n    model.add(layers.Input(shape=(LATENT_DIM,)))\n    model.add(layers.Dense(16 * 16 * 256, activation=\"relu\"))\n    model.add(layers.Reshape((16, 16, 256)))\n    model.add(layers.Conv2DTranspose(128, kernel_size=4, strides=2, padding=\"same\", activation=\"relu\"))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Conv2DTranspose(64, kernel_size=4, strides=2, padding=\"same\", activation=\"relu\"))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Conv2DTranspose(32, kernel_size=4, strides=2, padding=\"same\", activation=\"relu\"))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Conv2DTranspose(3, kernel_size=4, strides=2, padding=\"same\", activation=\"tanh\"))\n    \n    return model\n\n# ----------------------------------------\n# Fungsi untuk Menghasilkan dan Menyimpan Gambar\n# ----------------------------------------\ndef generate_and_save_images(generator, num_images, output_zip):\n    os.makedirs(\"tmp_images\", exist_ok=True)  # Buat direktori sementara untuk menyimpan gambar\n    latent_vectors = np.random.normal(0, 1, (num_images, LATENT_DIM))  # Noise untuk generator\n    generated_images = generator.predict(latent_vectors, verbose=0)  # Hasilkan gambar\n\n    # Rescale gambar dari [-1, 1] ke [0, 255] dan simpan sebagai .jpg\n    with zipfile.ZipFile(output_zip, \"w\") as zf:\n        for i, img in tqdm(enumerate(generated_images), total=num_images, desc=\"Saving images\"):\n            img = ((img + 1) * 127.5).astype(np.uint8)  # Denormalisasi\n            img = Image.fromarray(img)\n            img_path = f\"tmp_images/image_{i + 1}.jpg\"\n            img.save(img_path)\n            zf.write(img_path, arcname=f\"image_{i + 1}.jpg\")\n            os.remove(img_path)  # Hapus file sementara setelah dimasukkan ke zip\n\n# ----------------------------------------\n# Main\n# ----------------------------------------\nif __name__ == \"__main__\":\n    # Bangun Generator\n    generator = build_generator()\n\n    # Latih atau load model generator yang sudah dilatih\n    # Contoh: generator.load_weights(\"generator_weights.h5\")\n\n    # Hasilkan 7,000 gambar Monet-style\n    print(\"Generating Monet-style images...\")\n    generate_and_save_images(generator, NUM_IMAGES, OUTPUT_ZIP)\n    print(f\"All images saved to {OUTPUT_ZIP}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-21T13:30:09.985237Z","iopub.execute_input":"2024-12-21T13:30:09.985768Z","iopub.status.idle":"2024-12-21T13:32:21.375550Z","shell.execute_reply.started":"2024-12-21T13:30:09.985719Z","shell.execute_reply":"2024-12-21T13:32:21.374266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}