{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Painter - Cycle GAN","metadata":{}},{"cell_type":"code","source":"#our basic libraries\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\n# basic libraries related to torch\nimport torch\nimport torch.nn as nn\nimport torchvision\n#libraries for actions on dataset\nfrom torch.utils.data import Dataset, DataLoader \nimport torchvision.transforms as transforms\nfrom torchvision.transforms import ToTensor\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2 # for data aurmentaion\n#optimizer funtion\nimport torch.optim as optim\n\n# tqdm is for progress bar\nfrom tqdm import tqdm  \n\nfrom torchvision.utils import save_image\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:27.337473Z","iopub.execute_input":"2021-08-08T04:23:27.337828Z","iopub.status.idle":"2021-08-08T04:23:27.344093Z","shell.execute_reply.started":"2021-08-08T04:23:27.337797Z","shell.execute_reply":"2021-08-08T04:23:27.343143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We want convert photos to monet style, now we have given data names as photo and monet.\nData is available in tfrec format also but as our main aim is to learn Cycle GAN here Lets use use jpg format directly for simplicity.","metadata":{}},{"cell_type":"markdown","source":"Lets check if we ahve GPU access and assign it a variable.\nIf we have access to GPU we will have \"cuda\" assigned to variable or it will be \"cpu\" other wise","metadata":{}},{"cell_type":"code","source":"device = (\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:29.662094Z","iopub.execute_input":"2021-08-08T04:23:29.662451Z","iopub.status.idle":"2021-08-08T04:23:29.669223Z","shell.execute_reply.started":"2021-08-08T04:23:29.662423Z","shell.execute_reply":"2021-08-08T04:23:29.668134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset","metadata":{}},{"cell_type":"markdown","source":"As you can see we are using PyTorch so lets create a dataset which can return a pair of images.\nAs we want to create a model which can be trained to convert an **Photo to Monet** style.\n(Note: Thanks to Cycle GAN we need not worry about correspondece of these two images as PixtoPix needs images which are tightly corelated to each other from different domain, but that is not the case with Cycle GAN we just want images from two different domains.)","metadata":{}},{"cell_type":"markdown","source":"Here I am just messing around, you can directly copy paste the dir from the kaggle input","metadata":{}},{"cell_type":"code","source":"main = \"../input/gan-getting-started\"\ndirs = os.listdir(main) \n#print(type(dirs))\nfor dir_1 in dirs:\n    if(\"monet\" in dir_1 and \"jpg\" in dir_1):\n        monets_dir = main + \"/\" + dir_1\n    if(\"photo\" in dir_1 and \"jpg\" in dir_1):\n        photos_dir = main + \"/\" + dir_1\nprint(monets_dir, photos_dir)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:32.471118Z","iopub.execute_input":"2021-08-08T04:23:32.471462Z","iopub.status.idle":"2021-08-08T04:23:32.478781Z","shell.execute_reply.started":"2021-08-08T04:23:32.471435Z","shell.execute_reply":"2021-08-08T04:23:32.477614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets create a class here named as mydata, which can inherit fucntions from Dataset.\nSome observations about the dataset.\n1. It is already given that images are 256*256 so we dont need to do any resizing and reshaping.\n2. As we already have a lot of data lets refrain from using any other augmentations/transformations.\n3. We will just do one transform of making all the value to -1 to 1.","metadata":{}},{"cell_type":"code","source":"# I havbe written a generic function here ,\nclass mydata(Dataset):\n    def __init__(self, img_dir_1,img_dir_2, transforms=None):\n        #as we want to use the arguments received in other functions of this class using self.\n        self.img_dir_1 = img_dir_1\n        self.img_dir_2 = img_dir_2\n        self.transforms = transforms\n        #use self if you want to use it across all the function in thsi class\n        self.images_1_names = os.listdir(img_dir_1)\n        self.images_2_names = os.listdir(img_dir_2)\n        self.len_dataset = max(len(self.images_1_names), len(self.images_2_names))\n        self.len_images_1_names = len(self.images_1_names)\n        self.len_images_2_names = len(self.images_2_names)\n        \n    def __len__(self):#whenever we call len() on any object of this class it will call this functin.\n        return self.len_dataset\n    \n    def __getitem__(self, idx): #whenever we call this a[] using object of this class it will invoke this function.\n        #no need to use self. if you done need to use that variable in any other class\n        image_1_name = self.images_1_names[idx % self.len_images_1_names]\n        image_2_name = self.images_2_names[idx % self.len_images_2_names]\n        \n        image_1_path = os.path.join(self.img_dir_1, image_1_name )\n        image_2_path = os.path.join(self.img_dir_2, image_2_name )\n        \n        images_1 = np.array(Image.open(image_1_path).convert('RGB'))#making it np.array so that just to plot images as it\n        images_2 = np.array(Image.open(image_2_path).convert('RGB'))# we dont have to use transform To Tensor\n        if self.transforms:\n            augmentations = self.transforms(image=images_1, image0=images_2)\n            images_1 = augmentations[\"image\"]\n            images_2 = augmentations[\"image0\"]\n\n        return (images_1, images_2)\n# As we can see image_1 corresponds to the images from img_dir_1","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:35.608763Z","iopub.execute_input":"2021-08-08T04:23:35.609139Z","iopub.status.idle":"2021-08-08T04:23:35.625011Z","shell.execute_reply.started":"2021-08-08T04:23:35.609105Z","shell.execute_reply":"2021-08-08T04:23:35.624195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = mydata(photos_dir, monets_dir)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:36.821898Z","iopub.execute_input":"2021-08-08T04:23:36.822274Z","iopub.status.idle":"2021-08-08T04:23:36.833194Z","shell.execute_reply.started":"2021-08-08T04:23:36.822243Z","shell.execute_reply":"2021-08-08T04:23:36.83208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_some(data):\n    figure = plt.figure(figsize = (18,18))\n    rows, cols = 8, 8\n    for i in range(1, rows*cols + 1):\n        index = torch.randint(low = 0, high = len(data), size = (1,)).item()\n        _,img = data[index] #getting images of zebras \n        if(i>=33):\n            img,_ = data[index]  # getting images of zebras\n        figure.add_subplot(rows, cols, i)\n        plt.imshow(img.squeeze())\n        plt.axis('off')\n    plt.title(\"Monets and Photos\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:37.901593Z","iopub.execute_input":"2021-08-08T04:23:37.90194Z","iopub.status.idle":"2021-08-08T04:23:37.909136Z","shell.execute_reply.started":"2021-08-08T04:23:37.901908Z","shell.execute_reply":"2021-08-08T04:23:37.907887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplot_some(data)\n#note that these have not been applied with transform yet.","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:39.283949Z","iopub.execute_input":"2021-08-08T04:23:39.284314Z","iopub.status.idle":"2021-08-08T04:23:43.840684Z","shell.execute_reply.started":"2021-08-08T04:23:39.284286Z","shell.execute_reply":"2021-08-08T04:23:43.833448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DataLoader\n\nDataloader is the one who provides the data to our model in a particular manner.","metadata":{}},{"cell_type":"code","source":"batch_size = 1\nloader = DataLoader(data, batch_size = batch_size, shuffle = False, num_workers = 4, pin_memory = True)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:43.842589Z","iopub.execute_input":"2021-08-08T04:23:43.842972Z","iopub.status.idle":"2021-08-08T04:23:43.848798Z","shell.execute_reply.started":"2021-08-08T04:23:43.842934Z","shell.execute_reply":"2021-08-08T04:23:43.84771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"phot,mon = next(iter(loader))","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:43.85081Z","iopub.execute_input":"2021-08-08T04:23:43.851726Z","iopub.status.idle":"2021-08-08T04:23:44.099544Z","shell.execute_reply.started":"2021-08-08T04:23:43.851676Z","shell.execute_reply":"2021-08-08T04:23:44.097437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(phot.squeeze())\nplt.axis(\"off\")\nplt.title(\"Photo\")","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:44.101595Z","iopub.execute_input":"2021-08-08T04:23:44.102028Z","iopub.status.idle":"2021-08-08T04:23:44.223607Z","shell.execute_reply.started":"2021-08-08T04:23:44.101985Z","shell.execute_reply":"2021-08-08T04:23:44.222581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mon.squeeze())\nplt.axis(\"off\")\nplt.title(\"Monet\")","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:44.225296Z","iopub.execute_input":"2021-08-08T04:23:44.225652Z","iopub.status.idle":"2021-08-08T04:23:44.343502Z","shell.execute_reply.started":"2021-08-08T04:23:44.225615Z","shell.execute_reply":"2021-08-08T04:23:44.342486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Discriminator\n\nNow discriminators has some layers repeating in same manner so I will be creating a another which will contain those layers and then we can use this class in the main generator function as many time wewant","metadata":{}},{"cell_type":"code","source":"class Block(nn.Module):\n    def __init__(self, in_channels, out_channels, stride):\n        super().__init__()\n        self.conv = nn.Sequential(\n            nn.Conv2d(in_channels, out_channels, 4, stride, 1, bias= True, padding_mode='reflect'),\n            nn.InstanceNorm2d(out_channels),\n            nn.LeakyReLU(0.2),\n        )\n    def forward(self, x):\n        return self.conv(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:46.025509Z","iopub.execute_input":"2021-08-08T04:23:46.025932Z","iopub.status.idle":"2021-08-08T04:23:46.038269Z","shell.execute_reply.started":"2021-08-08T04:23:46.025892Z","shell.execute_reply":"2021-08-08T04:23:46.037226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Discriminator(nn.Module):\n    def __init__(self,in_channels = 3, features = [64,128,256,512]): #we will be using conv blocks for all of this\n        super().__init__()\n        self.initial = nn.Sequential(\n            nn.Conv2d(in_channels, features[0], kernel_size=4, stride=2, padding = 1, padding_mode='reflect'),\n            nn.LeakyReLU()\n        )\n        layers = []\n        for i in range(1, len(features)):\n            layers.append(Block(features[i-1], features[i], stride =1 if i == len(features)-1 else 2))\n        layers.append(nn.Conv2d(features[len(features)-1],1,  kernel_size = 4 , stride = 1, padding =1 , padding_mode='reflect' ))\n        self.model = nn.Sequential(*layers)\n        \n    def forward(self,x):\n        x = self.initial(x)\n        x = self.model(x)\n        sig = nn.Sigmoid()\n        return sig(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:47.618196Z","iopub.execute_input":"2021-08-08T04:23:47.618551Z","iopub.status.idle":"2021-08-08T04:23:47.628124Z","shell.execute_reply.started":"2021-08-08T04:23:47.61852Z","shell.execute_reply":"2021-08-08T04:23:47.627027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generator\nComing to the most important part of the model , the generator.","metadata":{}},{"cell_type":"code","source":"class ConvBlock(nn.Module):\n    def __init__(self,in_channels, out_channels, down =  True, use_act =  True , **kwargs):#some other keyword arguments like stride, padding etc.\n        super().__init__()\n        self.conv = nn.Sequential(\n            nn.Conv2d(in_channels, out_channels, padding_mode = \"reflect\", **kwargs)\n            if down\n            else nn.ConvTranspose2d(in_channels, out_channels, **kwargs),\n            nn.InstanceNorm2d(out_channels),\n            nn.LeakyReLU(inplace = True) if use_act else nn.Identity()\n        )\n    def forward(self, x):\n        return self.conv(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:49.390484Z","iopub.execute_input":"2021-08-08T04:23:49.390808Z","iopub.status.idle":"2021-08-08T04:23:49.398895Z","shell.execute_reply.started":"2021-08-08T04:23:49.39078Z","shell.execute_reply":"2021-08-08T04:23:49.39779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResBlock(nn.Module):\n    def __init__(self, channels):\n        super().__init__()\n        self.block = nn.Sequential(\n            ConvBlock(channels, channels, kernel_size = 3, padding = 1),\n            ConvBlock(channels, channels, use_act = False, kernel_size = 3, padding =1 ),\n        )\n    def forward(self, x):\n        return x + self.block(x) # we are not changin in output respected to x","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:50.195141Z","iopub.execute_input":"2021-08-08T04:23:50.195469Z","iopub.status.idle":"2021-08-08T04:23:50.201464Z","shell.execute_reply.started":"2021-08-08T04:23:50.195442Z","shell.execute_reply":"2021-08-08T04:23:50.200438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Generator(nn.Module):\n    def __init__(self, img_channels,num_feature = 64,  num_residuals = 9):\n        super().__init__()\n        self.initial = nn.Sequential(\n            nn.Conv2d(img_channels, num_feature, kernel_size  = 7, stride = 1, padding = 3 , padding_mode = \"reflect\"),\n            nn.ReLU(inplace =True)\n        )\n        # down sampling\n        self.down_blocks = nn.ModuleList(\n            [\n                ConvBlock(num_feature, num_feature*2, kernel_size = 3, stride = 2, padding =1),\n                ConvBlock(num_feature*2, num_feature*4, kernel_size = 3, stride = 2 , padding = 1),   \n            ]\n        )\n        # does not much change the input       \n        self.resblocks = nn.Sequential(\n            *[ResBlock(num_feature*4) for _ in range(num_residuals)]\n        )\n        # up sampling\n        self.up_blocks = nn.ModuleList(\n        [\n           ConvBlock(num_feature*4, num_feature*2,down=False,  kernel_size = 3, stride = 2, padding =1, output_padding = 1),\n           ConvBlock(num_feature*2, num_feature, down=False, kernel_size = 3, stride = 2 , padding = 1, output_padding = 1),\n        ])\n        # converting it ot RGB\n        self.last = nn.Conv2d(num_feature*1, img_channels,kernel_size = 7, stride = 1, padding  =  3, padding_mode = \"reflect\")\n        \n    def forward(self, x):\n        x = self.initial(x)\n        for layer in self.down_blocks:\n            x = layer(x)\n        x = self.resblocks(x)\n        for layer in self.up_blocks:\n            x = layer(x)\n        return torch.tanh(self.last(x))","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:23:58.110349Z","iopub.execute_input":"2021-08-08T04:23:58.110698Z","iopub.status.idle":"2021-08-08T04:23:58.120817Z","shell.execute_reply.started":"2021-08-08T04:23:58.110671Z","shell.execute_reply":"2021-08-08T04:23:58.120032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\nbatch_size = 1\nlearning_rate = 1e-5\nlambda_identity = 0.5\nlambda_cycle = 10\nnum_workers = 4\nnum_epochs = 2\nload_model = False\nsave_model = True\ncheckpoint_g_photo = \"../input/weights1/g_photo.pth.tar\"\ncheckpoint_g_monet = \"../input/weights1/g_monet.pth.tar\"\ncheckpoint_d_photo = \"../input/weights1/d_photo.pth.tar\"\ncheckpoint_d_monet = \"../input/weights1/d_monet.pth.tar\"\n\ntransformer = A.Compose(\n    [\n        A.Resize(width=256, height=256),\n        A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], max_pixel_value=255),\n        ToTensorV2(),\n     ],\n    additional_targets={\"image0\": \"image\"},\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:25:03.607583Z","iopub.execute_input":"2021-08-08T04:25:03.607904Z","iopub.status.idle":"2021-08-08T04:25:03.614393Z","shell.execute_reply.started":"2021-08-08T04:25:03.607874Z","shell.execute_reply":"2021-08-08T04:25:03.613496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import copy\n\ndef save_checkpoint(model, optimizer, filename=\"my_checkpoint.pth.tar\"):\n    print(\"=> Saving checkpoint\")\n    checkpoint = {\n        \"state_dict\": model.state_dict(),\n        \"optimizer\": optimizer.state_dict(),\n    }\n    torch.save(checkpoint, filename)\n\n\ndef load_checkpoint(checkpoint_file, model, optimizer, lr):\n    print(\"=> Loading checkpoint\")\n    checkpoint = torch.load(checkpoint_file, map_location=device)\n    model.load_state_dict(checkpoint[\"state_dict\"])\n    optimizer.load_state_dict(checkpoint[\"optimizer\"])\n\n    # If we don't do this then it will just have learning rate of old checkpoint\n    # and it will lead to many hours of debugging \\:\n    for param_group in optimizer.param_groups:\n        param_group[\"lr\"] = lr\n\n\ndef seed_everything(seed=42):\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:25:05.428669Z","iopub.execute_input":"2021-08-08T04:25:05.429031Z","iopub.status.idle":"2021-08-08T04:25:05.437981Z","shell.execute_reply.started":"2021-08-08T04:25:05.428998Z","shell.execute_reply":"2021-08-08T04:25:05.436715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir new_photos\n! mkdir new_monets","metadata":{"execution":{"iopub.status.busy":"2021-08-07T09:01:40.103239Z","iopub.execute_input":"2021-08-07T09:01:40.103608Z","iopub.status.idle":"2021-08-07T09:01:41.49805Z","shell.execute_reply.started":"2021-08-07T09:01:40.103574Z","shell.execute_reply":"2021-08-07T09:01:41.496865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Function","metadata":{}},{"cell_type":"code","source":"#photos to monet\ndef train(d_photo,d_monet, g_photo, g_monet, d_optim, g_optim, l1, mse, loader, g_scaler, d_scaler, i):\n    loop = tqdm(loader, leave = True)\n    \n    for idx, (photo, monet) in enumerate(loop):\n        #to use gpu or  cpu data should be on same device\n        monet = monet.to(device)\n        photo = photo.to(device)\n        \n        with torch.cuda.amp.autocast():\n            \n            #training d_photo to claddify a real ans fake photo(transfomrd to photo from monet)\n            real_photo = photo\n            fake_photo = g_photo(monet)\n    \n            d_photo_real = d_photo(real_photo)\n            d_photo_fake = d_photo(fake_photo.detach())\n            \n            \n            d_photo_real_loss = mse(d_photo_real, torch.ones_like(d_photo_real))\n            d_photo_fake_loss = mse(d_photo_fake, torch.zeros_like(d_photo_fake))\n            \n            d_photo_loss = d_photo_real_loss + d_photo_real_loss\n            #training d_monet to classify a real and fake monet\n            real_monet = monet\n            fake_monet = g_monet(photo)\n            \n            d_monet_real  = d_monet(real_monet)\n            d_monet_fake = d_monet(fake_monet.detach())\n            \n            d_monet_real_loss = mse(d_monet_real, torch.ones_like(d_monet_real))\n            d_monet_fake_loss = mse(d_monet_fake, torch.zeros_like(d_monet_fake))\n            \n            d_monet_loss = d_monet_fake_loss + d_monet_real_loss\n            \n            #puttinh them together\n            d_loss = (d_photo_loss + d_monet_loss)/2\n        \n        d_optim.zero_grad()\n        d_scaler.scale(d_loss).backward()\n        d_scaler.step(d_optim)\n        d_scaler.update()\n        \n        #training the generator\n        with torch.cuda.amp.autocast():\n            #adversarial loss\n            d_photo_fake = d_photo(fake_photo)\n            d_monet_fake = d_monet(fake_monet)\n            g_photo_loss = mse(d_photo_fake, torch.ones_like(d_photo_fake))\n            g_monet_loss = mse(d_monet_fake, torch.ones_like(d_monet_fake))\n            \n            #cycle loss\n            cycle_monet = g_monet(fake_photo)\n            cycle_photo = g_photo(fake_monet)\n            \n            cycle_monet_loss = l1(real_monet, cycle_monet)\n            cycle_photo_loss = l1(real_photo, cycle_photo)\n            \n            #identity loss\n            identity_monet = g_monet(real_monet)\n            identity_photo =g_photo(real_photo)\n            identity_monet_loss = l1(real_monet, identity_monet)\n            identity_photo_loss = l1(real_photo, identity_photo)\n            \n            #let put all loss togther\n            g_loss = (\n                g_monet_loss + g_photo_loss +\n                cycle_monet_loss*lambda_cycle + cycle_photo_loss*lambda_cycle +\n                identity_monet_loss*lambda_identity + identity_photo_loss*lambda_identity \n                \n            )\n            \n        g_optim.zero_grad()\n        g_scaler.scale(g_loss).backward()\n        g_scaler.step(g_optim)\n        g_scaler.update()\n        \n        if idx%100 == 0 :\n            save_image(fake_photo*0.5 + 0.5, f\"./new_photos/photos_{i}_{idx}.jpg\" )\n            save_image(fake_monet*0.5 + 0.5, f\"./new_monets/monets_{i}_{idx}.jpg\" )","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:07:55.122881Z","iopub.execute_input":"2021-08-08T04:07:55.123244Z","iopub.status.idle":"2021-08-08T04:07:55.136679Z","shell.execute_reply.started":"2021-08-08T04:07:55.123216Z","shell.execute_reply":"2021-08-08T04:07:55.135579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def main():\n    #defining d and g s\n    d_photo = Discriminator(in_channels = 3).to(device)\n    d_monet = Discriminator(in_channels = 3).to(device)\n    g_photo = Generator(img_channels = 3).to(device)\n    g_monet = Generator(img_channels = 3).to(device)\n    #defining optimizers\n    d_optim = optim.Adam( list(d_photo.parameters()) + list(d_monet.parameters()), lr = learning_rate, betas = (0.5, 0.999),  )       \n    g_optim = optim.Adam( list(g_photo.parameters()) + list(g_monet.parameters()), lr = learning_rate, betas = (0.5, 0.999),  )\n    #defining loss\n    l1 = nn.L1Loss()\n    mse = nn.MSELoss()\n    \n    data_n = mydata(photos_dir, monets_dir, transforms = transformer)\n    loader = DataLoader(data_n, batch_size = batch_size, shuffle = False, num_workers = 4, pin_memory = True)\n    \n    g_scaler = torch.cuda.amp.GradScaler()  # to run the program in float 16\n    d_scaler = torch.cuda.amp.GradScaler()\n    \n    if load_model:\n        load_checkpoint(checkpoint_g_photo, g_photo, g_optim, learning_rate,)\n        load_checkpoint(checkpoint_g_monet, g_monet, g_optim, learning_rate,)\n        load_checkpoint(checkpoint_d_photo, d_photo, d_optim, learning_rate,)\n        load_checkpoint(checkpoint_d_monet, d_monet, d_optim, learning_rate,)\n\n    for epoch in range(num_epochs):\n        train(d_photo, d_monet, g_photo, g_monet, d_optim, g_optim, l1, mse , loader, g_scaler, d_scaler, epoch)\n        \n        if save_model:\n            save_checkpoint(g_photo, g_optim, filename=checkpoint_g_photo)\n            save_checkpoint(g_monet, g_optim, filename=checkpoint_g_monet)\n            save_checkpoint(d_photo, d_optim, filename=checkpoint_d_photo)\n            save_checkpoint(d_monet, d_optim, filename=checkpoint_d_monet)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:08:00.434287Z","iopub.execute_input":"2021-08-08T04:08:00.434616Z","iopub.status.idle":"2021-08-08T04:08:00.444568Z","shell.execute_reply.started":"2021-08-08T04:08:00.434586Z","shell.execute_reply":"2021-08-08T04:08:00.443684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if __name__ == \"__main__\":\n#     main()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T09:01:47.820423Z","iopub.execute_input":"2021-08-07T09:01:47.8208Z","iopub.status.idle":"2021-08-07T09:01:47.827012Z","shell.execute_reply.started":"2021-08-07T09:01:47.820767Z","shell.execute_reply":"2021-08-07T09:01:47.826067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_photo = Generator(img_channels = 3).to(device)\ng_monet = Generator(img_channels = 3).to(device)\noptimizer = g_optim = optim.Adam( list(g_photo.parameters()) + list(g_monet.parameters()), lr = learning_rate, betas = (0.5, 0.999),  )\n# checkpoint_g_monet = \"../input/weights-painter/g_monet.pth.tar\"\ncheckpoint = torch.load(checkpoint_g_monet)\ng_monet.load_state_dict(checkpoint['state_dict'])\noptimizer.load_state_dict(checkpoint['optimizer'])\n# epoch = checkpoint['epoch']\n# loss = checkpoint['loss']","metadata":{"execution":{"iopub.status.busy":"2021-08-08T04:08:06.447716Z","iopub.execute_input":"2021-08-08T04:08:06.448027Z","iopub.status.idle":"2021-08-08T04:08:10.098837Z","shell.execute_reply.started":"2021-08-08T04:08:06.447999Z","shell.execute_reply":"2021-08-08T04:08:10.097901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(g_monet)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T09:02:03.675815Z","iopub.execute_input":"2021-08-07T09:02:03.676144Z","iopub.status.idle":"2021-08-07T09:02:03.683065Z","shell.execute_reply.started":"2021-08-07T09:02:03.676114Z","shell.execute_reply":"2021-08-07T09:02:03.68136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_n = mydata(photos_dir, monets_dir, transforms = transformer)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T09:00:06.611302Z","iopub.execute_input":"2021-08-07T09:00:06.611668Z","iopub.status.idle":"2021-08-07T09:00:06.630418Z","shell.execute_reply.started":"2021-08-07T09:00:06.611634Z","shell.execute_reply":"2021-08-07T09:00:06.629544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir ../images","metadata":{"execution":{"iopub.status.busy":"2021-08-07T09:00:08.147402Z","iopub.execute_input":"2021-08-07T09:00:08.147759Z","iopub.status.idle":"2021-08-07T09:00:08.849995Z","shell.execute_reply.started":"2021-08-07T09:00:08.147722Z","shell.execute_reply":"2021-08-07T09:00:08.84891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../images\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T09:00:15.565451Z","iopub.execute_input":"2021-08-07T09:00:15.5658Z","iopub.status.idle":"2021-08-07T09:00:15.571927Z","shell.execute_reply.started":"2021-08-07T09:00:15.565763Z","shell.execute_reply":"2021-08-07T09:00:15.57089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(data)):\n    photo_initial,_ =  data_n[i]\n    photo_initial = photo_initial.to(device)\n    photo_initial = photo_initial.unsqueeze(0)\n    monet_got = g_monet(photo_initial)\n    save_image(monet_got*0.5 + 0.5, f\"../images/monets_{i}.jpg\" )","metadata":{"execution":{"iopub.status.busy":"2021-08-07T04:03:30.416741Z","iopub.execute_input":"2021-08-07T04:03:30.417179Z","iopub.status.idle":"2021-08-07T04:08:54.969679Z","shell.execute_reply.started":"2021-08-07T04:03:30.417142Z","shell.execute_reply":"2021-08-07T04:08:54.968685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/images\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T04:08:54.971144Z","iopub.execute_input":"2021-08-07T04:08:54.971478Z","iopub.status.idle":"2021-08-07T04:08:59.170355Z","shell.execute_reply.started":"2021-08-07T04:08:54.971442Z","shell.execute_reply":"2021-08-07T04:08:59.169522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}