{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30587,"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_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os \nimport cv2\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader , Dataset\nfrom torchvision.transforms import transforms\nfrom torchvision.utils import make_grid","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:44:47.069248Z","iopub.execute_input":"2023-12-01T18:44:47.069676Z","iopub.status.idle":"2023-12-01T18:44:50.795404Z","shell.execute_reply.started":"2023-12-01T18:44:47.069612Z","shell.execute_reply":"2023-12-01T18:44:50.794301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 5\nmonet_images_path = '/kaggle/input/gan-getting-started/monet_jpg'\ntest_image_path   = '/kaggle/input/gan-getting-started/photo_jpg'","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:44:53.234151Z","iopub.execute_input":"2023-12-01T18:44:53.234742Z","iopub.status.idle":"2023-12-01T18:44:53.239709Z","shell.execute_reply.started":"2023-12-01T18:44:53.234702Z","shell.execute_reply":"2023-12-01T18:44:53.23888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.01\nn_epochs = 100\nbeta1=0.5\nbeta2 = 0.996","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:44:55.090422Z","iopub.execute_input":"2023-12-01T18:44:55.090817Z","iopub.status.idle":"2023-12-01T18:44:55.095659Z","shell.execute_reply.started":"2023-12-01T18:44:55.090787Z","shell.execute_reply":"2023-12-01T18:44:55.09475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms_dataset = transforms.Compose([\n    transforms.RandomHorizontalFlip(),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:53:26.99139Z","iopub.execute_input":"2023-12-01T18:53:26.991855Z","iopub.status.idle":"2023-12-01T18:53:26.998356Z","shell.execute_reply.started":"2023-12-01T18:53:26.991818Z","shell.execute_reply":"2023-12-01T18:53:26.997039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageDataset(Dataset):\n    def __init__(self, directory_x, directory_y, test=False, transforms=None):\n        self.transforms = transforms\n        \n        if test:\n            self.monet_images_X = [directory_x + \"/\" + name for name in sorted(os.listdir(directory_x))[250:]]\n            self.test_images_Y = [directory_y + \"/\" + name for name in sorted(os.listdir(directory_y))[250:301]]\n        else:\n            self.monet_images_X = [directory_x + \"/\" + name for name in sorted(os.listdir(directory_x))[:250]]\n            self.test_images_Y = [directory_y + \"/\" + name for name in sorted(os.listdir(directory_y))[:250]]\n        \n    def __len__(self):\n        return len(self.monet_images_X)\n    \n    def __getitem__(self, index):\n        x_img =  Image.open(self.monet_images_X[index])\n        y_img =  Image.open(self.test_images_Y[index])\n        \n        if self.transforms is not None:\n            x_img = self.transforms(x_img)\n            y_img = self.transforms(y_img)\n        return x_img, y_img","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:55:36.125116Z","iopub.execute_input":"2023-12-01T18:55:36.125559Z","iopub.status.idle":"2023-12-01T18:55:36.138366Z","shell.execute_reply.started":"2023-12-01T18:55:36.125523Z","shell.execute_reply":"2023-12-01T18:55:36.136967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(ImageDataset(directory_x = monet_images_path,\n                                       directory_y = test_image_path,\n                                       test = False,\n                                       transforms = transforms_dataset ),\n                                       batch_size = 5 , \n                                       shuffle=True,\n                                       num_workers = 3)\n\ntest_loader = DataLoader(ImageDataset(directory_x = monet_images_path,\n                                      directory_y = test_image_path,\n                                      test=True,\n                                      transforms = transforms_dataset),\n                                      batch_size=5,\n                                      shuffle=True,\n                                      num_workers=3)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T19:02:15.405219Z","iopub.execute_input":"2023-12-01T19:02:15.405691Z","iopub.status.idle":"2023-12-01T19:02:15.430496Z","shell.execute_reply.started":"2023-12-01T19:02:15.405653Z","shell.execute_reply":"2023-12-01T19:02:15.429246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Discriminator(nn.Module):\n    def __init__(self,in_channels):\n        super(self).__init__()\n        self.scale_factor = 16\n        self.model = nn.Sequential(\n         nn.Conv2d(in_channels,64,4,stride =2,padding=1),\n         nn.LeakyReLU(0.2,inplace = True),\n         nn.Conv2d(64.128,4,stride = 2 , padding = 1),\n         nn.InstanceNorm2d(128),\n         nn.ReLU(0.2,inplace=True),\n         nn.Conv2d(128,256,4,stride = 2,padding=1),\n         nn.InstanceNorm2d(256),\n         nn.ReLU(0.2,inplace=True),\n         nn.Conv2d(256,512,4,stride=2,padding = 1),\n         nn.InstanceNorm2d(512),\n         nn.ReLU(0.2,inplace=True),\n         nn.ZeroPad2do(1,0,1,0),\n         nn.Conv2d(512,1,4,padding=1)\n)\n        def forward(self,x):\n            return self.model(x)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T19:19:38.564283Z","iopub.execute_input":"2023-12-01T19:19:38.564694Z","iopub.status.idle":"2023-12-01T19:19:38.576349Z","shell.execute_reply.started":"2023-12-01T19:19:38.564659Z","shell.execute_reply":"2023-12-01T19:19:38.57519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)\ninput","metadata":{"execution":{"iopub.status.busy":"2023-12-01T19:32:07.003901Z","iopub.execute_input":"2023-12-01T19:32:07.004358Z","iopub.status.idle":"2023-12-01T19:32:07.120934Z","shell.execute_reply.started":"2023-12-01T19:32:07.004318Z","shell.execute_reply":"2023-12-01T19:32:07.119407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResidualBlock(nn.Module):\n    def __init__(self,in_channels):\n        super().__init__()\n        \n        self.black = nn.Sequential(\n        nn.ReflectionPad2d(1),\n        nn.Conv2d(in_channels,in_channels,3),\n        nn.InstanceNorm2d(in_channels),\n        nn.ReLU(inplace=True),\n        nn.ReflectionPad2d(1),\n        nn.Conv2d(in_channels,in_channels,3),\n        nn.InstanceNorm2d(in_channels)\n        )\n    def forward(self,x):\n        return x + self.black(x)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T19:40:38.163262Z","iopub.execute_input":"2023-12-01T19:40:38.163722Z","iopub.status.idle":"2023-12-01T19:40:38.171858Z","shell.execute_reply.started":"2023-12-01T19:40:38.163685Z","shell.execute_reply":"2023-12-01T19:40:38.17065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}