{"cells":[{"metadata":{"id":"AMhtZHM0d8Rv","colab_type":"code","outputId":"0061c201-a676-4a23-d265-a419af2c6b06","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"import os\nroot_dir = \"\"\nproject_dir =  \"\"","execution_count":null,"outputs":[]},{"metadata":{"id":"Wd6ZsgkRYJMT","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#!mkdir gdrive/'My Drive'/ML/APTOS2019/\n#!ls {project_dir}","execution_count":null,"outputs":[]},{"metadata":{"id":"AdScyi1pYf_b","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#!cp {project_dir}/dogs_gan.ipynb gdrive/'My Drive'/ML/APTOS2019/","execution_count":null,"outputs":[]},{"metadata":{"id":"2Eay5B3KqEKp","colab_type":"text"},"cell_type":"markdown","source":"## Create Necessry directories in order to structure the project in your google drive\n##our directory structure will be \n\n\n              gdrive/'My Drive'/ML/\n                                  |\n                                  DOGS_GAN\n                                  |-- README.md\n                                  |-- data\n                                  |   |-- all-dogs.zip\n                                  |   |-- Annotations.zip\n                                  |   \n                                  |-- dogs_gan.ipynb\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_dir = \"../input/\"","execution_count":null,"outputs":[]},{"metadata":{"id":"1qQUqIU-XbkQ","colab_type":"code","outputId":"947240b8-222b-41fd-8218-f8550122efbb","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"!ls {data_dir}\n#!unzip {data_dir}/all-dogs.zip -d {data_dir}\n#!unzip {data_dir}/Annotation.zip -d {data_dir}\n#!ls","execution_count":null,"outputs":[]},{"metadata":{"id":"7f7OwT_KuYGI","colab_type":"text"},"cell_type":"markdown","source":"## Install kaggle commands on system to download the dataset\n\n\n*   Make sure you have registered for the kaggle from the same comptetion\n*   Download the  kaggle.json credential file into your ML folder which will be needed to authorize  your download requests for the datasets.\n* copy the kaggle.json credential file ./kaggle directory  and change permissions\n\n"},{"metadata":{"id":"4K2SB7qAgSua","colab_type":"text"},"cell_type":"markdown","source":"### Download the data set from Kaggle to the data directory directly using command below"},{"metadata":{"id":"2i_RxcIMxl6G","colab_type":"text"},"cell_type":"markdown","source":"## Kickstart the project\n\n#### Now that we have our working directory setup we can start working with our project\n#### Step 1 is to append the current root directory to python path so that we can import  necessary libraries and python functions from our script\n\n#### Step 2 Below we are importing  kerasutil which is in our root directory"},{"metadata":{"id":"6I5Bt3y1nPWp","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import sys\n#sys.path.append(\"gdrive/My Drive/ML/\")\n#import download_utils","execution_count":null,"outputs":[]},{"metadata":{"id":"G7CS0bS-opOA","colab_type":"code","outputId":"caaac4f5-b5a5-4d80-f874-4f67f007abc5","colab":{"base_uri":"https://localhost:8080/","height":68},"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom keras import backend as K\nimport numpy as np\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport cv2  # for image processing\nimport scipy.io\nimport os\n#import keras_utils\n#from keras_utils import reset_tf_session \nprint(tf.__version__)\nprint(keras.__version__)","execution_count":null,"outputs":[]},{"metadata":{"id":"dxsszeJsUTR4","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"plt.rcParams.update({'axes.titlesize': 'small'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def reset_tf_session():\n    curr_session = tf.get_default_session()\n    # close current session\n    if curr_session is not None:\n        curr_session.close()\n    # reset graph\n    K.clear_session()\n    # create new session\n    config = tf.ConfigProto()\n    config.gpu_options.allow_growth = True\n    s = tf.InteractiveSession(config=config)\n    K.set_session(s)\n    return s","execution_count":null,"outputs":[]},{"metadata":{"id":"ZU9m219coqHy","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"IMG_SIZE = 128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#!ls ../input/all-dogs/all-dogs","execution_count":null,"outputs":[]},{"metadata":{"id":"pudD-BIUoTAM","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"dogs_imgages = \"../input/all-dogs/all-dogs/\"\nannotations = \"../input/annotation/\"","execution_count":null,"outputs":[]},{"metadata":{"id":"ZfNMd0rZ8gZc","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#!ls {data_dir}/all-dogs","execution_count":null,"outputs":[]},{"metadata":{"id":"LLIu2qo6hdTs","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import glob","execution_count":null,"outputs":[]},{"metadata":{"id":"YaqlsZhnoRDt","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"images = [f for f in glob.glob(dogs_imgages + \"*.jpg\")]\nannotations = [f for f in glob.glob(annotations + \"*/n*\")]","execution_count":null,"outputs":[]},{"metadata":{"id":"pxLkun7hhwLa","colab_type":"code","outputId":"d3d94800-3d64-406c-9c88-8dcb747bac33","colab":{"base_uri":"https://localhost:8080/","height":71},"trusted":true},"cell_type":"code","source":"print (images[:5])\nprint (annotations[:2])","execution_count":null,"outputs":[]},{"metadata":{"id":"-eWp4IjlqMfl","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"from PIL import Image\nfrom matplotlib import pyplot as plt\nimport xml.etree.ElementTree as ET\nimport random\n","execution_count":null,"outputs":[]},{"metadata":{"id":"vMRQswsmqNoH","colab_type":"code","outputId":"b3991e74-041a-4845-894e-f18c0e59a0ca","colab":{"base_uri":"https://localhost:8080/","height":908},"trusted":true},"cell_type":"code","source":"images_to_display = random.choices(images, k=64)\n\nfig = plt.figure(figsize=(25, 16))\nfor ii, img in enumerate(images_to_display):\n    ax = fig.add_subplot(8, 8, ii + 1, xticks=[], yticks=[])\n    img = cv2.imread(img)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    #img = Image.open(img_byte)\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"id":"7ga5KCy3yk9S","colab_type":"code","outputId":"b35b7474-0333-4113-bc88-b37ab576575a","colab":{"base_uri":"https://localhost:8080/","height":217},"trusted":true},"cell_type":"code","source":"img = cv2.imread(images[100])\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nprint(img.shape)\n#cv2_imshow(img)\nfig = plt.figure(figsize=(4,8))\nplt.imshow(img)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"3WZVdq1Dnqhe","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def get_bbox(dog_id):\n  breed_folder = [x.rsplit('/', 1)[0] for x in annotations if dog_id in x.split('/')[-1] ]\n  #print (breed_folder)\n  if len(breed_folder) != 1:\n    return None\n  breed_folder = breed_folder[0]\n  file_name = \"{}/{}\".format(breed_folder, dog_id)\n  #print (file_name)\n  root = ET.parse(file_name).getroot()\n  objects = root.findall('object')\n  for obj in objects:\n      bndbox = obj.find('bndbox')\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  bbox = (xmin, ymin, xmax, ymax)\n  #print(\"Bounding Box: \", bbox)\n  return bbox\n  \n  ","execution_count":null,"outputs":[]},{"metadata":{"id":"7Szn2ISdjl-6","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"dog_id = 'n02109961_16718'","execution_count":null,"outputs":[]},{"metadata":{"id":"6RUaW4vj2BIr","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"box = get_bbox(dog_id)","execution_count":null,"outputs":[]},{"metadata":{"id":"sZHgLNXl4gtC","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def get_annotated_img(img_file):\n  dog_id = img_file.split('/')[-1].split('.')[0]\n  #raw_bytes = read_raw_from_zip(zip_dogs,\"all-dogs/{}.jpg\".format(dog_id))\n  #img = decode_image_from_raw_bytes(raw_bytes)\n  img = cv2.imread(img_file)\n  bbox = get_bbox(dog_id)\n  if bbox:\n    xmin, ymin, xmax, ymax = bbox\n    img = img[ymin:ymax,xmin:xmax]\n  img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n  img = img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n  return img\n  ","execution_count":null,"outputs":[]},{"metadata":{"id":"F5ttez5DdOis","colab_type":"code","outputId":"089688cf-9be8-4874-b99e-8d47ba6ce118","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"img = get_annotated_img(images[4])\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{"id":"08EvDTVh50FY","colab_type":"code","outputId":"c0fdb87a-0a90-4aa1-cf2a-0bc85faaa83c","colab":{"base_uri":"https://localhost:8080/","height":269},"trusted":true},"cell_type":"code","source":"fig = plt.figure()\nplt.imshow(img)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"B3P3aNEi1Uxg","colab_type":"code","outputId":"419cecfe-105e-4201-e90e-dc21f625904b","colab":{"base_uri":"https://localhost:8080/","height":224},"trusted":true},"cell_type":"code","source":"import tensorflow as tf\n#from keras_utils import reset_tf_session\ns = reset_tf_session()\n\nimport keras\nfrom keras.models import Sequential\nfrom keras import layers as L","execution_count":null,"outputs":[]},{"metadata":{"id":"AoPwiNZFBpdf","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import random\nfrom scipy import ndarray\nimport skimage as sk\nfrom skimage import transform\nfrom skimage import util\nfrom copy import deepcopy\ndef random_rotation(image_array: ndarray):\n    # pick a random degree of rotation between 25% on the left and 25% on the right\n    random_degree = random.uniform(-25, 25)\n    return sk.transform.rotate(image_array, random_degree)\n\ndef random_noise(image_array: ndarray):\n    # add random noise to the image\n    return sk.util.random_noise(image_array)\n\ndef horizontal_flip(image_array: ndarray):\n    # horizontal flip doesn't need skimage, it's easy as flipping the image array of pixels !\n    return image_array[:, ::-1]","execution_count":null,"outputs":[]},{"metadata":{"id":"bR827VP2_UYd","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def generate_training_images(images, batch_size=100):\n  cur_batch = []\n  for image in images:\n    img = get_annotated_img(image)\n    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE) ) \n    cur_batch.append(img)\n    if len(cur_batch) == batch_size:\n      yield cur_batch\n      cur_batch = []\n    cur_batch.append(random_rotation(deepcopy(img) ))\n    if len(cur_batch) == batch_size:\n      yield cur_batch\n      cur_batch = []\n    cur_batch.append(random_noise(deepcopy(img) ))\n    if len(cur_batch) == batch_size:\n      yield cur_batch\n      cur_batch = []\n    cur_batch.append(horizontal_flip(deepcopy(img) ))\n    if len(cur_batch) == batch_size:\n      yield cur_batch\n      cur_batch = []\n  return cur_batch","execution_count":null,"outputs":[]},{"metadata":{"id":"C8Zry2Z18YLa","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)","execution_count":null,"outputs":[]},{"metadata":{"id":"yYx8KIwI8s2P","colab_type":"code","outputId":"72ef8b30-2281-4421-d692-4a0d0b177277","colab":{"base_uri":"https://localhost:8080/","height":156},"trusted":true},"cell_type":"code","source":"CODE_SIZE =256\ndim = 8\ndepth = 64*4\ndropout = 0.4\n\ngenerator = Sequential()\n\n#layer 1  : Dense Layer with BatchNormalization and relu activation\ngenerator.add(L.Dense(dim * dim * depth, \n                      input_shape=(CODE_SIZE,)))\ngenerator.add(L.LeakyReLU(alpha=0.2))\ngenerator.add(L.BatchNormalization(momentum=0.8))\n\n\n#layer2 :  Reshape and Dropout\ngenerator.add(L.Reshape((dim, dim, depth)))\n#generator.add(L.Dropout(dropout))\n\n\n#layer 3 : Transpose conv layer\ngenerator.add(L.Deconv2D(int(depth/2), \n                         kernel_size=(5,5)))\ngenerator.add(L.BatchNormalization(momentum=0.8))\ngenerator.add(L.LeakyReLU(alpha=0.2))\n\n\n\n\n#layer 4 : upsample conv layer\ngenerator.add(L.UpSampling2D())\ngenerator.add(L.Deconv2D(int(depth/4), \n                         kernel_size=(5,5)))\ngenerator.add(L.Dropout(dropout))\ngenerator.add(L.BatchNormalization(momentum=0.8))\ngenerator.add(L.LeakyReLU(alpha=0.2))\n\n\ngenerator.add(L.Deconv2D(int(depth/4), \n                         kernel_size=(5,5)))\ngenerator.add(L.Dropout(dropout))\ngenerator.add(L.BatchNormalization(momentum=0.8))\ngenerator.add(L.LeakyReLU(alpha=0.2))\n\n\n#layer 5 : upsample conv layer\ngenerator.add(L.UpSampling2D(size=(2,2)))\ngenerator.add(L.Deconv2D(int(depth/4), \n                         kernel_size=(5,5)))\ngenerator.add(L.Dropout(dropout))\ngenerator.add(L.BatchNormalization(momentum=0.8))\ngenerator.add(L.LeakyReLU(alpha=0.2))\n\n#layer 5 : Image generation Layer\ngenerator.add(L.Conv2D(3, kernel_size=5, activation=None))\ngenerator.add(L.UpSampling2D())","execution_count":null,"outputs":[]},{"metadata":{"id":"1dphMMd97Th7","colab_type":"code","outputId":"2628571f-48fa-4980-aff9-4f8d18d4f943","colab":{"base_uri":"https://localhost:8080/","height":884},"trusted":true},"cell_type":"code","source":"#generator = load_model('./G')\ngenerator.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"CRvr20q2ZkdZ","colab_type":"code","outputId":"9e78197c-7071-428a-e62c-e992fb1f0b73","colab":{"base_uri":"https://localhost:8080/","height":71},"trusted":true},"cell_type":"code","source":"discriminator = Sequential()\ndepth = 64\ndropout = 0.4\n\ndiscriminator.add(L.InputLayer(IMG_SHAPE))\n\ndiscriminator.add(L.Conv2D(depth, (3, 3),padding='same') )\ndiscriminator.add(L.LeakyReLU(0.1))\n\ndiscriminator.add(L.Conv2D(depth*2, (3, 3)))\ndiscriminator.add(L.LeakyReLU(0.1))\ndiscriminator.add(L.Dropout(dropout))\n\ndiscriminator.add(L.MaxPool2D())\n\ndiscriminator.add(L.Conv2D(depth*3, (3, 3)))\ndiscriminator.add(L.LeakyReLU(0.1))\n\ndiscriminator.add(L.MaxPool2D())\n\ndiscriminator.add(L.Conv2D(depth, (3, 3)))\ndiscriminator.add(L.LeakyReLU(0.1))\ndiscriminator.add(L.Dropout(dropout))\n\ndiscriminator.add(L.MaxPool2D())\ndiscriminator.add(L.Flatten())\ndiscriminator.add(L.Dense(512,activation='tanh'))\ndiscriminator.add(L.Dense(CODE_SIZE,activation='tanh'))\ndiscriminator.add(L.Dense(2, activation=tf.nn.log_softmax))","execution_count":null,"outputs":[]},{"metadata":{"id":"h-VALo7R7jWx","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"dCiGTgwM7aHD","colab_type":"code","outputId":"f52c1c49-ffa1-4098-b697-dbdf346f5978","colab":{"base_uri":"https://localhost:8080/","height":714},"trusted":true},"cell_type":"code","source":"#discriminator = load_model('./D')\ndiscriminator.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"ZoVKJim2D500","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"noise = tf.placeholder('float32',[None,CODE_SIZE])\nreal_data = tf.placeholder('float32',[None,]+list(IMG_SHAPE))\n\nlogp_real = discriminator(real_data)\n\ngenerated_data = generator(noise)\n\nlogp_gen = discriminator(generated_data)","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"code","id":"zyAlntZrED-1","outputId":"744e323d-7132-40a9-d8c4-355cf3a1a84c","colab":{"base_uri":"https://localhost:8080/","height":88},"trusted":true},"cell_type":"code","source":"########################\n#discriminator training#\n########################\n\nd_loss = -tf.reduce_mean(logp_real[:,1] + logp_gen[:,0])\n\n#regularize\nd_loss += tf.reduce_mean(discriminator.layers[-1].kernel**2)\n\n#optimize\ndisc_optimizer =  tf.train.GradientDescentOptimizer(1e-3).minimize(d_loss, var_list=discriminator.trainable_weights)","execution_count":null,"outputs":[]},{"metadata":{"id":"AiIcwXmuESOm","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"########################\n###generator training###\n########################\n\ng_loss = -tf.log(1-logp_gen)\n\ngen_optimizer = tf.train.AdamOptimizer(1e-4).minimize(g_loss,var_list=generator.trainable_weights)\n\n    ","execution_count":null,"outputs":[]},{"metadata":{"id":"3iBq6CNxEajW","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"bwxQAm3eEeCm","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def sample_noise_batch(bsize):\n    return np.random.normal(size=(bsize, CODE_SIZE)).astype('float32')\n\ndef sample_data_batch(bsize):\n    idxs = np.random.choice(np.arange(len(images)), size = int(bsize/2) )\n    cur_batch = []\n    for idx in idxs:\n        img = get_annotated_img(images[idx]) \n        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE) )\n        #print (img.shape)\n        cur_batch.append( img)\n        \n    idxs = np.random.choice(np.arange(len(images)), size= bsize-int(bsize/2) )\n    for idx in idxs:\n        img = get_annotated_img(images[idx]) \n        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE) )\n        if idxs[0]%3 == 0:\n          cur_batch.append( random_rotation(img))\n        elif idxs[0]%3 == 1:\n          cur_batch.append( random_noise(img))\n        else:\n          cur_batch.append( horizontal_flip(img))\n        #print (img.shape)\n    return np.array(cur_batch)\n\ndef sample_images(nrow,ncol, sharp=False):\n    images = generator.predict(sample_noise_batch(bsize=nrow*ncol))\n    for i in range(nrow*ncol):\n        plt.subplot(nrow,ncol,i+1)\n        if sharp:\n            plt.imshow(images[i].reshape(IMG_SHAPE),cmap=\"gray\", interpolation=\"none\")\n        else:\n            plt.imshow(images[i].reshape(IMG_SHAPE),cmap=\"gray\")\n            \n    plt.show()\n\ndef sample_probas(bsize):\n    plt.title('Generated vs real data')\n    plt.hist(np.exp(discriminator.predict(sample_data_batch(bsize)))[:,1],\n             label='D(x)', alpha=0.5,range=[0,1])\n    plt.hist(np.exp(discriminator.predict(generator.predict(sample_noise_batch(bsize))))[:,1],\n             label='D(G(z))',alpha=0.5,range=[0,1])\n    plt.legend(loc='best')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"LVTTJF_ZE4mU","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#import tqdm_utils","execution_count":null,"outputs":[]},{"metadata":{"id":"K3ufveNE_GS7","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#!ls {data_dir}\ns.run(tf.global_variables_initializer())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from time import time\ninit_time = time()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm_notebook as tqdm","execution_count":null,"outputs":[]},{"metadata":{"id":"ruKyn1hhEncD","colab_type":"code","outputId":"08f60f08-4ce2-404c-8c10-918ab2ea4a08","colab":{"base_uri":"https://localhost:8080/","height":1000},"trusted":true},"cell_type":"code","source":"from IPython import display\n\nfor epoch in tqdm(range(50000)):\n    \n    feed_dict = {\n        real_data:sample_data_batch(100),\n        noise:sample_noise_batch(100)\n    }\n    \n    for i in range(5):\n        s.run(disc_optimizer, feed_dict)\n    \n    s.run(gen_optimizer,feed_dict)\n    fl = 0\n    if epoch %100==0 and epoch > 0:\n        display.clear_output(wait=True)\n        sample_images(2,3,True)\n        sample_probas(1000)\n        if epoch%500 ==0 and epoch > 0:\n            generator.save('G_{}'.format(epoch))\n            discriminator.save('D_{}'.format(epoch))\n            if int((time() - init_time)/3600) > 6:\n                fl = 1\n                break\n    if fl:\n        break\n        \n        ","execution_count":null,"outputs":[]},{"metadata":{"id":"QDhtoc6bn89k","colab_type":"text"},"cell_type":"markdown","source":""},{"metadata":{"id":"OLg8QWK4CBJ7","colab_type":"code","colab":{},"trusted":false},"cell_type":"code","source":"","execution_count":0,"outputs":[]}],"metadata":{"colab":{"name":"dogs_gan.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}