{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":"none","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Assemble the image generator","metadata":{}},{"cell_type":"code","source":"# Import primary functions\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nimport torch.nn as nn\nimport cv2\nimport random\nimport torch\nimport torchvision\n\n# Import secondary functions\nfrom colorama import Fore, Back, Style\nfrom PIL import Image\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom torchvision import transforms\n\nconvert_tensor = transforms.ToTensor()\n\n# Import tertiary functions\nimages = []\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if filename.find(\".jpg\") != -1:\n            if random.random() > 0.5:\n                images.append(convert_tensor(Image.open(dirname + \"/\" + filename)))\nimages = torch.stack(images)\nprint(images.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:07:52.681694Z","iopub.execute_input":"2024-04-30T07:07:52.682734Z","iopub.status.idle":"2024-04-30T07:08:55.042549Z","shell.execute_reply.started":"2024-04-30T07:07:52.682698Z","shell.execute_reply":"2024-04-30T07:08:55.041264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda.0\" if torch.cuda.is_available() else \"cpu\")\nimages = images.view(images.shape[0], -1).to(device)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:08:58.227650Z","iopub.execute_input":"2024-04-30T07:08:58.228507Z","iopub.status.idle":"2024-04-30T07:08:58.236470Z","shell.execute_reply.started":"2024-04-30T07:08:58.228468Z","shell.execute_reply":"2024-04-30T07:08:58.234786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# There's a palette","metadata":{}},{"cell_type":"code","source":"# Assign color inputs along with TPU generator\ny_ = Fore.YELLOW\nr_ = Fore.RED\ng_ = Fore.GREEN\nb_ = Fore.BLUE\nm_ = Fore.MAGENTA\n\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas:', strategy.num_replicas_in_sync)\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\n\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:01.336168Z","iopub.execute_input":"2024-04-30T07:09:01.336599Z","iopub.status.idle":"2024-04-30T07:09:01.347556Z","shell.execute_reply.started":"2024-04-30T07:09:01.336571Z","shell.execute_reply":"2024-04-30T07:09:01.346403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pimp the frame","metadata":{}},{"cell_type":"code","source":"monet_jpg_directory = '../input/gan-getting-started/monet_jpg/'\nphoto_jpg_directory = '../input/gan-getting-started/photo_jpg/'","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:06.584055Z","iopub.execute_input":"2024-04-30T07:09:06.584532Z","iopub.status.idle":"2024-04-30T07:09:06.589623Z","shell.execute_reply.started":"2024-04-30T07:09:06.584495Z","shell.execute_reply":"2024-04-30T07:09:06.588652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getImagePaths(path):\n    image_names = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in filenames:\n            fullpath = os.path.join(dirname, filename)\n            image_names.append(fullpath)\n    return image_names","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:08.661931Z","iopub.execute_input":"2024-04-30T07:09:08.662423Z","iopub.status.idle":"2024-04-30T07:09:08.668878Z","shell.execute_reply.started":"2024-04-30T07:09:08.662389Z","shell.execute_reply":"2024-04-30T07:09:08.667722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_images_path = getImagePaths(monet_jpg_directory)\nphoto_images_path = getImagePaths(photo_jpg_directory)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:11.848066Z","iopub.execute_input":"2024-04-30T07:09:11.848508Z","iopub.status.idle":"2024-04-30T07:09:13.371519Z","shell.execute_reply.started":"2024-04-30T07:09:11.848478Z","shell.execute_reply":"2024-04-30T07:09:13.370073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"{y_}Number of monet images: {g_} {len(monet_images_path)}\\n\")\nprint(f\"{y_}Number of photo images: {g_} {len(photo_images_path)}\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:15.266171Z","iopub.execute_input":"2024-04-30T07:09:15.267242Z","iopub.status.idle":"2024-04-30T07:09:15.272855Z","shell.execute_reply.started":"2024-04-30T07:09:15.267196Z","shell.execute_reply":"2024-04-30T07:09:15.271539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getShape(images_paths):\n    shape = cv2.imread(images_paths[0]).shape\n    for image_path in images_paths:\n        image_shape=cv2.imread(image_path).shape\n        if (image_shape!=shape):\n            return \"Different image shape\"\n        else:\n            return \"Same image shape \" + str(shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:17.945274Z","iopub.execute_input":"2024-04-30T07:09:17.945712Z","iopub.status.idle":"2024-04-30T07:09:17.951605Z","shell.execute_reply.started":"2024-04-30T07:09:17.945681Z","shell.execute_reply":"2024-04-30T07:09:17.950458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_multiple_img(images_paths, rows, cols):\n    figure, ax = plt.subplots(nrows=rows, ncols=cols, figsize=(16, 8))\n    for ind, image_path in enumerate(images_paths):\n        image=cv2.imread(image_path)\n        image=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:20.365665Z","iopub.execute_input":"2024-04-30T07:09:20.366183Z","iopub.status.idle":"2024-04-30T07:09:20.373604Z","shell.execute_reply.started":"2024-04-30T07:09:20.366150Z","shell.execute_reply":"2024-04-30T07:09:20.372259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_multiple_img(monet_images_path, 4, 4)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:10:19.339789Z","iopub.execute_input":"2024-04-30T07:10:19.340770Z","iopub.status.idle":"2024-04-30T07:10:21.288574Z","shell.execute_reply.started":"2024-04-30T07:10:19.340715Z","shell.execute_reply":"2024-04-30T07:10:21.286772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_multiple_img(photo_images_path, 4, 4)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:09:33.114228Z","iopub.execute_input":"2024-04-30T07:09:33.114667Z","iopub.status.idle":"2024-04-30T07:10:09.447647Z","shell.execute_reply.started":"2024-04-30T07:09:33.114637Z","shell.execute_reply":"2024-04-30T07:10:09.446070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Skeet plots","metadata":{}},{"cell_type":"code","source":"def styling():\n    for spine in plt.gca().spines.values():\n        spine.set_visible(False)\n        plt.xticks([])\n        plt.yticks([])","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:10:29.540153Z","iopub.execute_input":"2024-04-30T07:10:29.540604Z","iopub.status.idle":"2024-04-30T07:10:29.545946Z","shell.execute_reply.started":"2024-04-30T07:10:29.540573Z","shell.execute_reply":"2024-04-30T07:10:29.545038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def hist(image_path):\n    plt.figure(figsize=(16, 3))\n    \n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    \n    plt.subplot(1, 5, 1)\n    plt.imshow(img)\n    styling()\n    \n    custom_colors = [\"#ef233c\", \"#76da71\", \"#2667ff\",\"#aea3b0\"]\n    labels = ['Red Channel', 'Green Channel', 'Blue Channel','Total']\n    \n    for i in range(1,4):\n        plt.subplot(1, 5, i+1)\n        plt.hist(img[:, :, i-1].reshape(-1),bins=64,color=custom_colors[i-1],alpha = 0.6)\n        plt.xlabel(labels[i-1],fontsize=10)\n        styling()\n        \n    plt.subplot(1, 5, 5)\n    plt.hist(img.reshape(-1),bins=128,color=custom_colors[3],alpha = 0.6)\n    plt.xlabel(labels[3],fontsize=10)\n    styling()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:10:32.287592Z","iopub.execute_input":"2024-04-30T07:10:32.288094Z","iopub.status.idle":"2024-04-30T07:10:32.296857Z","shell.execute_reply.started":"2024-04-30T07:10:32.288057Z","shell.execute_reply":"2024-04-30T07:10:32.296051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_hist(images_paths):\n        for ind,image_path in enumerate(images_paths):\n            if (ind<6):\n                hist(image_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:10:35.152487Z","iopub.execute_input":"2024-04-30T07:10:35.152940Z","iopub.status.idle":"2024-04-30T07:10:35.158515Z","shell.execute_reply.started":"2024-04-30T07:10:35.152910Z","shell.execute_reply":"2024-04-30T07:10:35.157547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_hist(monet_images_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:10:37.158975Z","iopub.execute_input":"2024-04-30T07:10:37.159408Z","iopub.status.idle":"2024-04-30T07:10:41.807239Z","shell.execute_reply.started":"2024-04-30T07:10:37.159378Z","shell.execute_reply":"2024-04-30T07:10:41.806135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_hist(photo_images_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:10:43.716164Z","iopub.execute_input":"2024-04-30T07:10:43.716642Z","iopub.status.idle":"2024-04-30T07:10:48.422763Z","shell.execute_reply.started":"2024-04-30T07:10:43.716611Z","shell.execute_reply":"2024-04-30T07:10:48.421882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Take the train","metadata":{}},{"cell_type":"code","source":"class GAN(nn.Module):\n    def __init__(self, in_size, hidden_size, out_size):\n        super().__init__()\n        self.picasso = nn.Sequential(\n            nn.Linear(in_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, out_size),\n            nn.Sigmoid()\n        )\n        self.critic = nn.Sequential(\n            nn.Linear(out_size, 1),\n            nn.Sigmoid()\n        )\n    def forward(self, ins):\n        return self.picasso(ins)\n    def gan_forward(self, ins):\n        return self.critic(ins)\n    def __call__(self, ins):\n        return self.forward(ins)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:11:11.148694Z","iopub.execute_input":"2024-04-30T07:11:11.149493Z","iopub.status.idle":"2024-04-30T07:11:11.157262Z","shell.execute_reply.started":"2024-04-30T07:11:11.149455Z","shell.execute_reply":"2024-04-30T07:11:11.156042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = GAN(10, 200, 3 * 256 * 256).to(device)\nnet.gan_forward(net(torch.randn(10).to(device)))","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:11:14.863987Z","iopub.execute_input":"2024-04-30T07:11:14.864433Z","iopub.status.idle":"2024-04-30T07:11:15.295472Z","shell.execute_reply.started":"2024-04-30T07:11:14.864400Z","shell.execute_reply":"2024-04-30T07:11:15.294626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 2\nbatch_size = 2\ntrain_len = len(images)\nnum_steps = (train_len//batch_size) * num_epochs\n\nlr = 0.0001\noptimG = torch.optim.AdamW(net.picasso.parameters(), lr=lr)\noptimD = torch.optim.SGD(net.critic.parameters(), lr=lr)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:11:17.313325Z","iopub.execute_input":"2024-04-30T07:11:17.314052Z","iopub.status.idle":"2024-04-30T07:11:17.320666Z","shell.execute_reply.started":"2024-04-30T07:11:17.314013Z","shell.execute_reply":"2024-04-30T07:11:17.319480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for step in range(num_steps):\n    d_batch_real = images[torch.randint(1, train_len-1, (batch_size//2,))]\n    d_batch_gen = net(torch.randn(batch_size//2, 10).to(device))\n    d_batch = torch.cat((d_batch_real, d_batch_gen)).to(device)\n    d_ys = torch.cat((torch.zeros(batch_size//2, 1), torch.ones(batch_size//2, 1))).to(device)\n    d_out = net.gan_forward(d_batch)\n    \n    d_loss = nn.functional.binary_cross_entropy(d_out, d_ys)\n    optimD.zero_grad()\n    d_loss.backward()\n    optimD.step()\n    \n    g_batch = torch.randn(batch_size, 10).to(device)\n    g_out = net(g_batch)\n    \n    g_loss = net.gan_forward(g_out).sum() / len(g_out)\n    optimG.zero_grad()\n    g_loss.backward()\n    optimG.step()\n    \n    if step % 100 == 0:\n        print(\"Step\", str(step) + \", loss D:\", str(d_loss.item()) + \", loss G:\", g_loss.item())","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:11:20.399571Z","iopub.execute_input":"2024-04-30T07:11:20.400071Z","iopub.status.idle":"2024-04-30T07:24:24.262766Z","shell.execute_reply.started":"2024-04-30T07:11:20.400029Z","shell.execute_reply":"2024-04-30T07:24:24.261440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    ins = torch.randn(10).to(device)\n    net_out = net(ins).view(3, 256, 256)\n    image = torchvision.transforms.functional.to_pil_image(net_out)\n    display(image)","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:24:33.556460Z","iopub.execute_input":"2024-04-30T07:24:33.556942Z","iopub.status.idle":"2024-04-30T07:24:33.612109Z","shell.execute_reply.started":"2024-04-30T07:24:33.556910Z","shell.execute_reply":"2024-04-30T07:24:33.610878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a submission","metadata":{}},{"cell_type":"code","source":"import PIL\n! mkdir ../images\n\nfor i in range(8000):\n    seed = torch.randn(10).to(device)\n    out = net(seed).view(3, 256, 256)\n    image = torchvision.transforms.functional.to_pil_image(out)\n    image.save(\"../images/\" + str(i) + \".jpg\")\n    i += 1\n\nimport shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/images\")\nprint(\"Successfully saved as compressed file\")","metadata":{"execution":{"iopub.status.busy":"2024-04-30T07:24:58.874494Z","iopub.execute_input":"2024-04-30T07:24:58.875020Z","iopub.status.idle":"2024-04-30T07:26:46.284758Z","shell.execute_reply.started":"2024-04-30T07:24:58.874974Z","shell.execute_reply":"2024-04-30T07:26:46.282999Z"},"trusted":true},"execution_count":null,"outputs":[]}]}