{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\n\nimport torch\nfrom torch import nn, optim\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms\nfrom torchvision.utils import save_image\n\nfrom tqdm import tqdm_notebook as tqdm\n\nfrom fastai.vision import *\nfrom fastai.vision.gan import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-12T03:50:26.797679Z","iopub.execute_input":"2021-12-12T03:50:26.797916Z","iopub.status.idle":"2021-12-12T03:50:28.565378Z","shell.execute_reply.started":"2021-12-12T03:50:26.79787Z","shell.execute_reply":"2021-12-12T03:50:28.564679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline\nimport numpy as np\nimport torch\nfrom torch import nn, optim\nfrom torch import autograd\nimport torch.nn.functional as F\nfrom torch.nn import Parameter\nfrom torchvision import datasets, transforms\nfrom torchvision.utils import save_image\nfrom torch.utils.data import Dataset,DataLoader,Subset\nfrom PIL import Image,ImageOps,ImageEnhance\n\nimport cv2\nimport albumentations as A\nfrom albumentations.pytorch import ToTensor\n\nimport glob\nimport xml.etree.ElementTree as ET #for parsing XML\nimport shutil\nfrom tqdm import tqdm\nimport time\nimport random","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:28.568016Z","iopub.execute_input":"2021-12-12T03:50:28.568575Z","iopub.status.idle":"2021-12-12T03:50:29.308088Z","shell.execute_reply.started":"2021-12-12T03:50:28.568511Z","shell.execute_reply":"2021-12-12T03:50:29.307302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generator and Discriminator","metadata":{}},{"cell_type":"code","source":"#random seeds\nseed = 2019\nrandom.seed(seed)\ntorch.manual_seed(seed)\ntorch.cuda.manual_seed_all(seed)\nnp.random.seed(seed)\nos.environ['PYTHONHASHSEED'] = str(seed)\n\nBATCH_SIZE  = 32\nNUM_WORKERS = 4\nEMA = False\nLABEL_NOISE = False\nLABEL_NOISE_PROB = 0.1","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:29.309651Z","iopub.execute_input":"2021-12-12T03:50:29.309971Z","iopub.status.idle":"2021-12-12T03:50:29.320437Z","shell.execute_reply.started":"2021-12-12T03:50:29.309919Z","shell.execute_reply":"2021-12-12T03:50:29.319543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/Annotation.zip\n!unzip ../input/all-dogs.zip","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = './all-dogs/'\nimg_filenames = os.listdir(PATH)\nlen(img_filenames)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.407901Z","iopub.execute_input":"2021-12-12T03:50:48.408174Z","iopub.status.idle":"2021-12-12T03:50:48.432076Z","shell.execute_reply.started":"2021-12-12T03:50:48.408124Z","shell.execute_reply":"2021-12-12T03:50:48.431173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nPATH_ANNOTATION = './Annotation/'\nbreeds = glob.glob(PATH_ANNOTATION+'*')\nannotations = []\nfor breed in breeds:\n    annotations += glob.glob(breed+'/*')\nlen(annotations)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.434768Z","iopub.execute_input":"2021-12-12T03:50:48.435204Z","iopub.status.idle":"2021-12-12T03:50:48.531441Z","shell.execute_reply.started":"2021-12-12T03:50:48.435153Z","shell.execute_reply":"2021-12-12T03:50:48.530628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breed_map = {}\nfor annotation in annotations:\n    breed = annotation.split('/')[-2]\n    index = breed.split('-')[0]\n    breed_map.setdefault(index,breed)\nn_classes = len(breed_map)\nn_classes","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.533044Z","iopub.execute_input":"2021-12-12T03:50:48.533382Z","iopub.status.idle":"2021-12-12T03:50:48.566464Z","shell.execute_reply.started":"2021-12-12T03:50:48.533326Z","shell.execute_reply":"2021-12-12T03:50:48.565377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breed_map","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.568564Z","iopub.execute_input":"2021-12-12T03:50:48.5693Z","iopub.status.idle":"2021-12-12T03:50:48.581133Z","shell.execute_reply.started":"2021-12-12T03:50:48.56923Z","shell.execute_reply":"2021-12-12T03:50:48.579843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/whizzkid/crop-images-using-bounding-box\ndef bounding_box(img):\n    bpath = PATH_ANNOTATION + str(breed_map[img.split('_')[0]])+'/'+str(img.split('.')[0])\n    tree  = ET.parse(bpath)\n    root  = tree.getroot()\n    objects = root.findall('object')\n    bbxs = []\n    for o in objects:\n        bndbox = o.find('bndbox') #reading bound box\n        xmin = int(bndbox.find('xmin').text)\n        ymin = int(bndbox.find('ymin').text)\n        xmax = int(bndbox.find('xmax').text)\n        ymax = int(bndbox.find('ymax').text)\n        bbxs.append((xmin,ymin,xmax,ymax))\n    return bbxs","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.582753Z","iopub.execute_input":"2021-12-12T03:50:48.583248Z","iopub.status.idle":"2021-12-12T03:50:48.593823Z","shell.execute_reply.started":"2021-12-12T03:50:48.583046Z","shell.execute_reply":"2021-12-12T03:50:48.592638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH_ANNOTATION","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.595281Z","iopub.execute_input":"2021-12-12T03:50:48.595844Z","iopub.status.idle":"2021-12-12T03:50:48.609282Z","shell.execute_reply.started":"2021-12-12T03:50:48.595774Z","shell.execute_reply":"2021-12-12T03:50:48.608271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bounding_box_ratio(img):\n    bpath = PATH_ANNOTATION + str(breed_map[img.split('_')[0]])+'/'+str(img.split('.')[0])\n    tree  = ET.parse(bpath)\n    root  = tree.getroot()\n    objects = root.findall('object')\n    bbx_ratios = []\n    for o in objects:\n        bndbox = o.find('bndbox') #reading bound box\n        xmin = int(bndbox.find('xmin').text)\n        ymin = int(bndbox.find('ymin').text)\n        xmax = int(bndbox.find('xmax').text)\n        ymax = int(bndbox.find('ymax').text)\n        xlen = xmax - xmin\n        ylen = ymax - ymin\n        ratio = ylen / xlen\n        bbx_ratios.append((xlen,ylen,ratio))\n    return bbx_ratios","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.610952Z","iopub.execute_input":"2021-12-12T03:50:48.611607Z","iopub.status.idle":"2021-12-12T03:50:48.62337Z","shell.execute_reply.started":"2021-12-12T03:50:48.611551Z","shell.execute_reply":"2021-12-12T03:50:48.621961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n#threshold for aspect ratio, at the same time idx for each bbx\nimg_filenames_th = []\nratios_th = []\nfor img in tqdm(img_filenames):\n    bbx_ratios = bounding_box_ratio(img)\n    for i,(xlen,ylen,ratio) in enumerate(bbx_ratios):\n        if ((ratio>0.888888889)&(ratio<1.1212121212)):\n            img_filenames_th.append(img[:-4]+'_'+str(i)+'.jpg')\n            ratios_th.append(ratio)\nratios_th = np.array(ratios_th)\n\nprint('original : ', len(img_filenames))\nprint('after th : ', len(img_filenames_th))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:48.625226Z","iopub.execute_input":"2021-12-12T03:50:48.625907Z","iopub.status.idle":"2021-12-12T03:50:54.577137Z","shell.execute_reply.started":"2021-12-12T03:50:48.625847Z","shell.execute_reply":"2021-12-12T03:50:54.576328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_preprocessing(img_path,bbx_idx):\n    bbx = bounding_box(img_path)[bbx_idx]\n    img  = Image.open(os.path.join(PATH,img_path))#PILImage format\n    img_cropped  = img.crop(bbx)\n    return img_cropped","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:54.578713Z","iopub.execute_input":"2021-12-12T03:50:54.579278Z","iopub.status.idle":"2021-12-12T03:50:54.585117Z","shell.execute_reply.started":"2021-12-12T03:50:54.579224Z","shell.execute_reply":"2021-12-12T03:50:54.584156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir processed_images_ratio","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:54.586483Z","iopub.execute_input":"2021-12-12T03:50:54.587078Z","iopub.status.idle":"2021-12-12T03:50:55.27751Z","shell.execute_reply.started":"2021-12-12T03:50:54.58674Z","shell.execute_reply":"2021-12-12T03:50:55.276448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nbreed_map_2 = {}\nfor i,b in enumerate(breed_map.keys()):\n    breed_map_2[b] = i","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:55.281471Z","iopub.execute_input":"2021-12-12T03:50:55.281763Z","iopub.status.idle":"2021-12-12T03:50:55.287413Z","shell.execute_reply.started":"2021-12-12T03:50:55.281697Z","shell.execute_reply":"2021-12-12T03:50:55.286633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_ in tqdm(img_filenames_th):\n    img = data_preprocessing(img_[:-6]+\".jpg\", int(img_[-5]))\n    dir_name = os.path.join(\"processed_images_ratio\", img_.split(\"_\")[0])\n    if not os.path.exists(dir_name):\n        os.makedirs(dir_name)\n    img.save(os.path.join(\"processed_images_ratio\", img_.split(\"_\")[0], img_))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:50:55.289022Z","iopub.execute_input":"2021-12-12T03:50:55.289464Z","iopub.status.idle":"2021-12-12T03:51:31.454764Z","shell.execute_reply.started":"2021-12-12T03:50:55.289355Z","shell.execute_reply":"2021-12-12T03:51:31.453939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Parameters of GAN","metadata":{}},{"cell_type":"code","source":"path ='./processed_images_ratio'","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:51:31.456088Z","iopub.execute_input":"2021-12-12T03:51:31.456384Z","iopub.status.idle":"2021-12-12T03:51:31.460369Z","shell.execute_reply.started":"2021-12-12T03:51:31.456337Z","shell.execute_reply":"2021-12-12T03:51:31.459633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trfm = get_transforms(do_flip=False, flip_vert=False, max_rotate=8.0, \n                      max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75,\n                      xtra_tfms=[contrast(scale=(1, 2), p=0.75),rand_zoom(scale =(1.0,1.2)),crop_pad(size=128, row_pct=(0,1), col_pct=(0,1))]\n                )\ntrfm1 = get_transforms(do_flip=False, flip_vert=False,xtra_tfms=[crop_pad(size=128, row_pct=(0,1), col_pct=(0,1))])","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:51:31.461641Z","iopub.execute_input":"2021-12-12T03:51:31.462141Z","iopub.status.idle":"2021-12-12T03:51:31.471452Z","shell.execute_reply.started":"2021-12-12T03:51:31.462076Z","shell.execute_reply":"2021-12-12T03:51:31.470806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_data(bs, size):\n    return (GANItemList.from_folder(path, noise_sz=100)\n               .split_none()\n               .label_from_func(noop)\n#               .transform(tfms=[[crop_pad(size=size, row_pct=(0,1), col_pct=(0,1))], []], size=size, tfm_y=True)\n               .transform(trfm,size=size,tfm_y=True)\n               .databunch(bs=bs)\n               .normalize(stats = [torch.tensor([0.5,0.5,0.5]), torch.tensor([0.5,0.5,0.5])], do_x=False, do_y=True))\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:51:31.473111Z","iopub.execute_input":"2021-12-12T03:51:31.473625Z","iopub.status.idle":"2021-12-12T03:51:31.482757Z","shell.execute_reply.started":"2021-12-12T03:51:31.473482Z","shell.execute_reply":"2021-12-12T03:51:31.481835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = get_data(16, 128)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:51:31.484331Z","iopub.execute_input":"2021-12-12T03:51:31.484893Z","iopub.status.idle":"2021-12-12T03:51:31.761936Z","shell.execute_reply.started":"2021-12-12T03:51:31.484612Z","shell.execute_reply":"2021-12-12T03:51:31.760879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:51:31.763421Z","iopub.execute_input":"2021-12-12T03:51:31.763712Z","iopub.status.idle":"2021-12-12T03:51:39.119022Z","shell.execute_reply.started":"2021-12-12T03:51:31.763659Z","shell.execute_reply":"2021-12-12T03:51:39.116795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:51:39.120451Z","iopub.execute_input":"2021-12-12T03:51:39.120963Z","iopub.status.idle":"2021-12-12T03:51:39.126373Z","shell.execute_reply.started":"2021-12-12T03:51:39.120898Z","shell.execute_reply":"2021-12-12T03:51:39.125672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = basic_generator(in_size=128, n_channels=3, n_extra_layers=1).to(device)\ncritic    = basic_critic   (in_size=128, n_channels=3, n_extra_layers=1).to(device)\nlearn = GANLearner.wgan(data, generator, critic, switch_eval=False,\n                        opt_func = partial(optim.Adam, betas = (0.,0.99)), wd=0.)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:52:02.191414Z","iopub.execute_input":"2021-12-12T03:52:02.192007Z","iopub.status.idle":"2021-12-12T03:52:02.656389Z","shell.execute_reply.started":"2021-12-12T03:52:02.191762Z","shell.execute_reply":"2021-12-12T03:52:02.655661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit(500,5e-4)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:52:02.658281Z","iopub.execute_input":"2021-12-12T03:52:02.658595Z","iopub.status.idle":"2021-12-12T05:00:53.372249Z","shell.execute_reply.started":"2021-12-12T03:52:02.658531Z","shell.execute_reply":"2021-12-12T05:00:53.371432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.gan_trainer.switch(gen_mode=True)\nlearn.show_results(ds_type=DatasetType.Train, rows=16, figsize=(16,16))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:00:53.373985Z","iopub.execute_input":"2021-12-12T05:00:53.374491Z","iopub.status.idle":"2021-12-12T05:00:57.142986Z","shell.execute_reply.started":"2021-12-12T05:00:53.374439Z","shell.execute_reply":"2021-12-12T05:00:57.13999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds,_ = learn.get_preds(ds_type=DatasetType.Train)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:00:57.144554Z","iopub.execute_input":"2021-12-12T05:00:57.145078Z","iopub.status.idle":"2021-12-12T05:01:31.229593Z","shell.execute_reply.started":"2021-12-12T05:00:57.145027Z","shell.execute_reply":"2021-12-12T05:01:31.22869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1=preds.numpy()[1]","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:01:31.232425Z","iopub.execute_input":"2021-12-12T05:01:31.232728Z","iopub.status.idle":"2021-12-12T05:01:31.237228Z","shell.execute_reply.started":"2021-12-12T05:01:31.232679Z","shell.execute_reply":"2021-12-12T05:01:31.236239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:01:31.238666Z","iopub.execute_input":"2021-12-12T05:01:31.239126Z","iopub.status.idle":"2021-12-12T05:01:31.251326Z","shell.execute_reply.started":"2021-12-12T05:01:31.239055Z","shell.execute_reply":"2021-12-12T05:01:31.250494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:01:31.254239Z","iopub.execute_input":"2021-12-12T05:01:31.254468Z","iopub.status.idle":"2021-12-12T05:01:31.262468Z","shell.execute_reply.started":"2021-12-12T05:01:31.254426Z","shell.execute_reply":"2021-12-12T05:01:31.261751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1tran =np.transpose(img1, (2, 1, 0))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:01:31.265749Z","iopub.execute_input":"2021-12-12T05:01:31.266046Z","iopub.status.idle":"2021-12-12T05:01:31.272452Z","shell.execute_reply.started":"2021-12-12T05:01:31.265996Z","shell.execute_reply":"2021-12-12T05:01:31.271823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists('../output_images'):\n    os.mkdir('../output_images')\nim_batch_size = 50\nn_images=10000\nfor i_batch in range(0, n_images, im_batch_size):\n    gen_z = torch.randn(im_batch_size, 100, 1, 1, device=device)\n    gen_images = generator(gen_z)\n    images = gen_images.to(\"cpu\").clone().detach()\n    images = images.numpy().transpose(0, 2, 3, 1)\n    for i_image in range(gen_images.size(0)):\n        save_image(gen_images[i_image, :, :, :], os.path.join('../output_images', f'image_{i_batch+i_image:05d}.png'))\n\n\nimport shutil\nshutil.make_archive('images', 'zip', '../output_images')","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:01:31.274234Z","iopub.execute_input":"2021-12-12T05:01:31.274473Z","iopub.status.idle":"2021-12-12T05:02:13.031336Z","shell.execute_reply.started":"2021-12-12T05:01:31.274431Z","shell.execute_reply":"2021-12-12T05:02:13.030595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-12T05:02:13.032683Z","iopub.execute_input":"2021-12-12T05:02:13.033036Z","iopub.status.idle":"2021-12-12T05:02:13.046112Z","shell.execute_reply.started":"2021-12-12T05:02:13.032962Z","shell.execute_reply":"2021-12-12T05:02:13.044544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}