{"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":"# 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)\nimport zipfile\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import img_to_array, load_img\nimport glob\nimport imageio\nimport matplotlib.pyplot as plt\nimport PIL\nimport time\nfrom PIL import Image\nimport xml.etree.ElementTree as ET \nfrom keras.models import Model\nfrom keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Reshape, BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import LearningRateScheduler\nfrom keras.optimizers import SGD, Adam\n\nfrom IPython import display# 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\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","execution":{"iopub.status.busy":"2023-02-19T14:41:39.252386Z","iopub.execute_input":"2023-02-19T14:41:39.25317Z","iopub.status.idle":"2023-02-19T14:41:52.470934Z","shell.execute_reply.started":"2023-02-19T14:41:39.253079Z","shell.execute_reply":"2023-02-19T14:41:52.469723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists('generative-dog-images'):\n    !mkdir /kaggle/working/generative-dog-images\n!unzip /kaggle/input/generative-dog-images/all-dogs.zip -d /kaggle/working/generative-dog-images > /dev/null 2>&1\n!unzip /kaggle/input/generative-dog-images/Annotation.zip -d /kaggle/working/generative-dog-images > /dev/null 2>&1","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:41:52.472707Z","iopub.execute_input":"2023-02-19T14:41:52.473735Z","iopub.status.idle":"2023-02-19T14:42:13.11632Z","shell.execute_reply.started":"2023-02-19T14:41:52.473697Z","shell.execute_reply":"2023-02-19T14:42:13.115017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WEIGHT_INIT = tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.118297Z","iopub.execute_input":"2023-02-19T14:42:13.118751Z","iopub.status.idle":"2023-02-19T14:42:13.125855Z","shell.execute_reply.started":"2023-02-19T14:42:13.118703Z","shell.execute_reply":"2023-02-19T14:42:13.124482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#separation of list\n#set directories and paths\nall_dogs_dir = '/kaggle/working/generative-dog-images/all-dogs/'\nannotation_dir = '/kaggle/working/generative-dog-images/Annotation/'\n\nIMAGES = os.listdir(all_dogs_dir)\nBREEDS = os.listdir(annotation_dir)","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.12942Z","iopub.execute_input":"2023-02-19T14:42:13.129918Z","iopub.status.idle":"2023-02-19T14:42:13.153168Z","shell.execute_reply.started":"2023-02-19T14:42:13.129889Z","shell.execute_reply":"2023-02-19T14:42:13.152316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tpu = None\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\nexcept ValueError:\n    strategy = tf.distribute.get_strategy()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.154568Z","iopub.execute_input":"2023-02-19T14:42:13.155118Z","iopub.status.idle":"2023-02-19T14:42:13.179169Z","shell.execute_reply.started":"2023-02-19T14:42:13.155083Z","shell.execute_reply":"2023-02-19T14:42:13.17829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train dog list displayed\nprint(f'Train set dogs:{IMAGES[:10]}')","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.181215Z","iopub.execute_input":"2023-02-19T14:42:13.181778Z","iopub.status.idle":"2023-02-19T14:42:13.186994Z","shell.execute_reply.started":"2023-02-19T14:42:13.181734Z","shell.execute_reply":"2023-02-19T14:42:13.186092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Breeds list displayed\nprint(f'Train set breeds:{BREEDS[:10]}')","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.188376Z","iopub.execute_input":"2023-02-19T14:42:13.189378Z","iopub.status.idle":"2023-02-19T14:42:13.198513Z","shell.execute_reply.started":"2023-02-19T14:42:13.189343Z","shell.execute_reply":"2023-02-19T14:42:13.197574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Total breed class and dog images showed\nprint(f'total training dog images: {len(IMAGES)}')\nprint(f'total breed class: {len(BREEDS)}')","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.199996Z","iopub.execute_input":"2023-02-19T14:42:13.200418Z","iopub.status.idle":"2023-02-19T14:42:13.213391Z","shell.execute_reply.started":"2023-02-19T14:42:13.200379Z","shell.execute_reply":"2023-02-19T14:42:13.212355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Print sample of dog image\nprint(\"Sample dog image:\")\nplt.imshow(load_img(f\"{os.path.join(all_dogs_dir, os.listdir(all_dogs_dir)[0])}\"))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.215156Z","iopub.execute_input":"2023-02-19T14:42:13.215964Z","iopub.status.idle":"2023-02-19T14:42:13.569631Z","shell.execute_reply.started":"2023-02-19T14:42:13.21593Z","shell.execute_reply":"2023-02-19T14:42:13.568663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Print shape of random dog image\n# Load the first example of a dog\nsample_image  = load_img(f\"{os.path.join(all_dogs_dir, os.listdir(all_dogs_dir)[0])}\")\n\n# Convert the image into its numpy array representation\nsample_array = img_to_array(sample_image)\n\nprint(f\"Each image has shape: {sample_array.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.573863Z","iopub.execute_input":"2023-02-19T14:42:13.574934Z","iopub.status.idle":"2023-02-19T14:42:13.60138Z","shell.execute_reply.started":"2023-02-19T14:42:13.574881Z","shell.execute_reply":"2023-02-19T14:42:13.600255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dictionary of breeds of image\ndog_breed_dict = {}\nfor annotation in BREEDS:\n    annotations = annotation.split('-')\n    dog_breed_dict[annotations[0]] = annotations[1]\nprint(dog_breed_dict) ","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.603146Z","iopub.execute_input":"2023-02-19T14:42:13.603542Z","iopub.status.idle":"2023-02-19T14:42:13.609883Z","shell.execute_reply.started":"2023-02-19T14:42:13.603488Z","shell.execute_reply":"2023-02-19T14:42:13.608742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idxIn = 0; namesIn = []\nimagesIn = np.zeros((25000,64,64,3))","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.611499Z","iopub.execute_input":"2023-02-19T14:42:13.612187Z","iopub.status.idle":"2023-02-19T14:42:13.622794Z","shell.execute_reply.started":"2023-02-19T14:42:13.612151Z","shell.execute_reply":"2023-02-19T14:42:13.621742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#crop image\nfor breed in BREEDS: \n    for dog in os.listdir(annotation_dir+breed):\n        try: img = Image.open(all_dogs_dir+dog+'.jpg') #open image\n        except: continue                  \n           \n        tree = ET.parse(annotation_dir+breed+'/'+dog)\n        \n        root = tree.getroot() # <annotation>\n        objects = root.findall('object') # <object>\n        for o in objects:\n            bndbox = o.find('bndbox') # <bndbox>\n            xmin = int(bndbox.find('xmin').text) # <xmin>\n            ymin = int(bndbox.find('ymin').text) # <ymin>\n            xmax = int(bndbox.find('xmax').text) # <xmax>\n            ymax = int(bndbox.find('ymax').text) # <ymax>\n            w = np.min((xmax - xmin, ymax - ymin))\n            img2 = img.crop((xmin, ymin, xmin+w, ymin+w))\n            img2 = img2.resize((64,64),Image.Resampling.LANCZOS)\n            imagesIn[idxIn,:,:,:] = np.asarray(img2)\n            namesIn.append(breed)\n            idxIn += 1   ","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:42:13.624676Z","iopub.execute_input":"2023-02-19T14:42:13.625099Z","iopub.status.idle":"2023-02-19T14:43:45.917075Z","shell.execute_reply.started":"2023-02-19T14:42:13.625067Z","shell.execute_reply":"2023-02-19T14:43:45.916094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.random.randint(0,idxIn,25)\nfor k in range(5):\n    plt.figure(figsize=(15,3))\n    for j in range(5):\n        plt.subplot(1,5,j+1)\n        img = Image.fromarray( imagesIn[x[k*5+j],:,:,:].astype('uint8') )\n        plt.axis('off')\n        plt.title(namesIn[x[k*5+j]],fontsize=11)\n        plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:43:45.918297Z","iopub.execute_input":"2023-02-19T14:43:45.918706Z","iopub.status.idle":"2023-02-19T14:43:47.070609Z","shell.execute_reply.started":"2023-02-19T14:43:45.918666Z","shell.execute_reply":"2023-02-19T14:43:47.069024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n    model = tf.keras.Sequential([\n    tf.keras.layers.Dense(2048, activation='elu',use_bias=False, input_shape=(10000,)),\n    tf.keras.layers.Reshape((8, 8, 32)),\n    \n    tf.keras.layers.Conv2D(128, (3, 3), padding='same', use_bias=False,kernel_initializer=WEIGHT_INIT),\n    tf.keras.layers.UpSampling2D((2, 2)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.LeakyReLU(),\n    \n    tf.keras.layers.Conv2D(64, (3, 3), padding='same', use_bias=False,kernel_initializer=WEIGHT_INIT),\n    tf.keras.layers.UpSampling2D((2, 2)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.LeakyReLU(),\n    \n    tf.keras.layers.Conv2D(32, (3, 3), padding='same', use_bias=False,kernel_initializer=WEIGHT_INIT),\n    tf.keras.layers.UpSampling2D((2, 2)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.LeakyReLU(),\n\n    tf.keras.layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same', use_bias=False,kernel_initializer=WEIGHT_INIT)])\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:43:47.072181Z","iopub.execute_input":"2023-02-19T14:43:47.072582Z","iopub.status.idle":"2023-02-19T14:43:52.223582Z","shell.execute_reply.started":"2023-02-19T14:43:47.072544Z","shell.execute_reply":"2023-02-19T14:43:52.222656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\nmodel.compile( loss='binary_crossentropy',optimizer=Adam(learning_rate=0.005))","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:43:52.224952Z","iopub.execute_input":"2023-02-19T14:43:52.225307Z","iopub.status.idle":"2023-02-19T14:43:52.283386Z","shell.execute_reply.started":"2023-02-19T14:43:52.225273Z","shell.execute_reply":"2023-02-19T14:43:52.282595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TRAINING DATA\nidx = np.random.randint(0,idxIn,10000)\ntrain_y = imagesIn[idx,:,:,:]/255.\ntrain_X = np.zeros((10000,10000))\nfor i in range(10000): train_X[i,i] = 1","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:43:52.284439Z","iopub.execute_input":"2023-02-19T14:43:52.284821Z","iopub.status.idle":"2023-02-19T14:43:52.708842Z","shell.execute_reply.started":"2023-02-19T14:43:52.284783Z","shell.execute_reply":"2023-02-19T14:43:52.707717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TRAIN NETWORK\nlr = 0.005\nfor k in range(5):\n    annealer = LearningRateScheduler(lambda x: lr)\n    history = model.fit(train_X, train_y, epochs = 10, batch_size=256, callbacks=[annealer], verbose=0)\n    print('Epoch',(k+1)*10,'/50 - loss =',history.history['loss'][-1] )\n    if history.history['loss'][-1]<0.54: lr = 0.001","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:43:52.710426Z","iopub.execute_input":"2023-02-19T14:43:52.710934Z","iopub.status.idle":"2023-02-19T14:47:17.705943Z","shell.execute_reply.started":"2023-02-19T14:43:52.71088Z","shell.execute_reply":"2023-02-19T14:47:17.704788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_X, train_y, imagesIn","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:47:17.707552Z","iopub.execute_input":"2023-02-19T14:47:17.707939Z","iopub.status.idle":"2023-02-19T14:47:17.848654Z","shell.execute_reply.started":"2023-02-19T14:47:17.7079Z","shell.execute_reply":"2023-02-19T14:47:17.846635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprint('Generate Random Dogs')    \nfor k in range(5):\n    plt.figure(figsize=(15,3))\n    for j in range(5):\n        x2 = np.zeros((10000))\n        x2[np.random.randint(10000)] = 1\n        x2[np.random.randint(10000)] = 0.75\n        #x2[np.random.randint(10000)] = 0.25\n        x2 = x2/(np.sqrt(x2.dot(x2.T)))\n        plt.subplot(1,5,j+1)\n        img = model.predict(x2.reshape((-1,10000)))\n        img = Image.fromarray( (255*img).astype('uint8').reshape((64,64,3)))\n        plt.axis('off')\n        plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:47:17.850325Z","iopub.execute_input":"2023-02-19T14:47:17.850737Z","iopub.status.idle":"2023-02-19T14:47:20.504904Z","shell.execute_reply.started":"2023-02-19T14:47:17.850697Z","shell.execute_reply":"2023-02-19T14:47:20.503131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Recall from Memory Dogs')    \nfor k in range(5):\n    plt.figure(figsize=(15,3))\n    for j in range(5):\n        xx = np.zeros((10000))\n        xx[np.random.randint(10000)] = 1\n        plt.subplot(1,5,j+1)\n        img = model.predict(xx.reshape((-1,10000)))\n        img = Image.fromarray( (255*img).astype('uint8').reshape((64,64,3)))\n        plt.axis('off')\n        plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:47:20.506799Z","iopub.execute_input":"2023-02-19T14:47:20.507337Z","iopub.status.idle":"2023-02-19T14:47:23.197262Z","shell.execute_reply.started":"2023-02-19T14:47:20.507294Z","shell.execute_reply":"2023-02-19T14:47:23.195963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SAVE TO ZIP FILE NAMED IMAGES.ZIP\nz = zipfile.PyZipFile('images.zip', mode='w')\nd = DogGenerator()\nfor k in range(10000):\n    img = d.getDog(np.random.normal(0,1,100))\n    f = str(k)+'.png'\n    img.save(f,'PNG'); z.write(f); os.remove(f)\n    #if k % 1000==0: print(k)\nz.close()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T14:47:23.203568Z","iopub.execute_input":"2023-02-19T14:47:23.204131Z","iopub.status.idle":"2023-02-19T14:47:23.524439Z","shell.execute_reply.started":"2023-02-19T14:47:23.204078Z","shell.execute_reply":"2023-02-19T14:47:23.522876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculate LB Score\nfrom __future__ import absolute_import, division, print_function\nimport numpy as np\nimport os\nimport gzip, pickle\nimport tensorflow as tf\nfrom scipy import linalg\nimport pathlib\nimport urllib\nimport warnings\nfrom tqdm import tqdm\nfrom PIL import Image\n\nclass KernelEvalException(Exception):\n    pass\n\nmodel_params = {\n    'Inception': {\n        'name': 'Inception', \n        'imsize': 64,\n        'output_layer': 'Pretrained_Net/pool_3:0', \n        'input_layer': 'Pretrained_Net/ExpandDims:0',\n        'output_shape': 2048,\n        'cosine_distance_eps': 0.1\n        }\n}\n\ndef create_model_graph(pth):\n    \"\"\"Creates a graph from saved GraphDef file.\"\"\"\n    # Creates graph from saved graph_def.pb.\n    with tf.gfile.FastGFile( pth, 'rb') as f:\n        graph_def = tf.GraphDef()\n        graph_def.ParseFromString( f.read())\n        _ = tf.import_graph_def( graph_def, name='Pretrained_Net')\n\ndef _get_model_layer(sess, model_name):\n    # layername = 'Pretrained_Net/final_layer/Mean:0'\n    layername = model_params[model_name]['output_layer']\n    layer = sess.graph.get_tensor_by_name(layername)\n    ops = layer.graph.get_operations()\n    for op_idx, op in enumerate(ops):\n        for o in op.outputs:\n            shape = o.get_shape()\n            if shape._dims != []:\n              shape = [s.value for s in shape]\n              new_shape = []\n              for j, s in enumerate(shape):\n                if s == 1 and j == 0:\n                  new_shape.append(None)\n                else:\n                  new_shape.append(s)\n              o.__dict__['_shape_val'] = tf.TensorShape(new_shape)\n    return layer\n\ndef get_activations(images, sess, model_name, batch_size=50, verbose=False):\n    \"\"\"Calculates the activations of the pool_3 layer for all images.\n\n    Params:\n    -- images      : Numpy array of dimension (n_images, hi, wi, 3). The values\n                     must lie between 0 and 256.\n    -- sess        : current session\n    -- batch_size  : the images numpy array is split into batches with batch size\n                     batch_size. A reasonable batch size depends on the disposable hardware.\n    -- verbose    : If set to True and parameter out_step is given, the number of calculated\n                     batches is reported.\n    Returns:\n    -- A numpy array of dimension (num images, 2048) that contains the\n       activations of the given tensor when feeding inception with the query tensor.\n    \"\"\"\n    inception_layer = _get_model_layer(sess, model_name)\n    n_images = images.shape[0]\n    if batch_size > n_images:\n        print(\"warning: batch size is bigger than the data size. setting batch size to data size\")\n        batch_size = n_images\n    n_batches = n_images//batch_size + 1\n    pred_arr = np.empty((n_images,model_params[model_name]['output_shape']))\n    for i in tqdm(range(n_batches)):\n        if verbose:\n            print(\"\\rPropagating batch %d/%d\" % (i+1, n_batches), end=\"\", flush=True)\n        start = i*batch_size\n        if start+batch_size < n_images:\n            end = start+batch_size\n        else:\n            end = n_images\n                    \n        batch = images[start:end]\n        pred = sess.run(inception_layer, {model_params[model_name]['input_layer']: batch})\n        pred_arr[start:end] = pred.reshape(-1,model_params[model_name]['output_shape'])\n    if verbose:\n        print(\" done\")\n    return pred_arr\n\n\n# def calculate_memorization_distance(features1, features2):\n#     neigh = NearestNeighbors(n_neighbors=1, algorithm='kd_tree', metric='euclidean')\n#     neigh.fit(features2) \n#     d, _ = neigh.kneighbors(features1, return_distance=True)\n#     print('d.shape=',d.shape)\n#     return np.mean(d)\n\ndef normalize_rows(x: np.ndarray):\n    \"\"\"\n    function that normalizes each row of the matrix x to have unit length.\n\n    Args:\n     ``x``: A numpy matrix of shape (n, m)\n\n    Returns:\n     ``x``: The normalized (by row) numpy matrix.\n    \"\"\"\n    return np.nan_to_num(x/np.linalg.norm(x, ord=2, axis=1, keepdims=True))\n\n\ndef cosine_distance(features1, features2):\n    # print('rows of zeros in features1 = ',sum(np.sum(features1, axis=1) == 0))\n    # print('rows of zeros in features2 = ',sum(np.sum(features2, axis=1) == 0))\n    features1_nozero = features1[np.sum(features1, axis=1) != 0]\n    features2_nozero = features2[np.sum(features2, axis=1) != 0]\n    norm_f1 = normalize_rows(features1_nozero)\n    norm_f2 = normalize_rows(features2_nozero)\n\n    d = 1.0-np.abs(np.matmul(norm_f1, norm_f2.T))\n    print('d.shape=',d.shape)\n    print('np.min(d, axis=1).shape=',np.min(d, axis=1).shape)\n    mean_min_d = np.mean(np.min(d, axis=1))\n    print('distance=',mean_min_d)\n    return mean_min_d\n\n\ndef distance_thresholding(d, eps):\n    if d < eps:\n        return d\n    else:\n        return 1\n\ndef calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):\n    \"\"\"Numpy implementation of the Frechet Distance.\n    The Frechet distance between two multivariate Gaussians X_1 ~ N(mu_1, C_1)\n    and X_2 ~ N(mu_2, C_2) is\n            d^2 = ||mu_1 - mu_2||^2 + Tr(C_1 + C_2 - 2*sqrt(C_1*C_2)).\n            \n    Stable version by Dougal J. Sutherland.\n\n    Params:\n    -- mu1 : Numpy array containing the activations of the pool_3 layer of the\n             inception net ( like returned by the function 'get_predictions')\n             for generated samples.\n    -- mu2   : The sample mean over activations of the pool_3 layer, precalcualted\n               on an representive data set.\n    -- sigma1: The covariance matrix over activations of the pool_3 layer for\n               generated samples.\n    -- sigma2: The covariance matrix over activations of the pool_3 layer,\n               precalcualted on an representive data set.\n\n    Returns:\n    --   : The Frechet Distance.\n    \"\"\"\n\n    mu1 = np.atleast_1d(mu1)\n    mu2 = np.atleast_1d(mu2)\n\n    sigma1 = np.atleast_2d(sigma1)\n    sigma2 = np.atleast_2d(sigma2)\n\n    assert mu1.shape == mu2.shape, \"Training and test mean vectors have different lengths\"\n    assert sigma1.shape == sigma2.shape, \"Training and test covariances have different dimensions\"\n\n    diff = mu1 - mu2\n\n    # product might be almost singular\n    covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)\n    if not np.isfinite(covmean).all():\n        msg = \"fid calculation produces singular product; adding %s to diagonal of cov estimates\" % eps\n        warnings.warn(msg)\n        offset = np.eye(sigma1.shape[0]) * eps\n        # covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))\n        covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))\n    \n    # numerical error might give slight imaginary component\n    if np.iscomplexobj(covmean):\n        if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):\n            m = np.max(np.abs(covmean.imag))\n            raise ValueError(\"Imaginary component {}\".format(m))\n        covmean = covmean.real\n\n    # covmean = tf.linalg.sqrtm(tf.linalg.matmul(sigma1,sigma2))\n\n    print('covmean.shape=',covmean.shape)\n    # tr_covmean = tf.linalg.trace(covmean)\n\n    tr_covmean = np.trace(covmean)\n    return diff.dot(diff) + np.trace(sigma1) + np.trace(sigma2) - 2 * tr_covmean\n    # return diff.dot(diff) + tf.linalg.trace(sigma1) + tf.linalg.trace(sigma2) - 2 * tr_covmean\n#-------------------------------------------------------------------------------\n\n\ndef calculate_activation_statistics(images, sess, model_name, batch_size=50, verbose=False):\n    \"\"\"Calculation of the statistics used by the FID.\n    Params:\n    -- images      : Numpy array of dimension (n_images, hi, wi, 3). The values\n                     must lie between 0 and 255.\n    -- sess        : current session\n    -- batch_size  : the images numpy array is split into batches with batch size\n                     batch_size. A reasonable batch size depends on the available hardware.\n    -- verbose     : If set to True and parameter out_step is given, the number of calculated\n                     batches is reported.\n    Returns:\n    -- mu    : The mean over samples of the activations of the pool_3 layer of\n               the incption model.\n    -- sigma : The covariance matrix of the activations of the pool_3 layer of\n               the incption model.\n    \"\"\"\n    act = get_activations(images, sess, model_name, batch_size, verbose)\n    mu = np.mean(act, axis=0)\n    sigma = np.cov(act, rowvar=False)\n    return mu, sigma, act\n    \ndef _handle_path_memorization(path, sess, model_name, is_checksize, is_check_png):\n    path = pathlib.Path(path)\n    files = list(path.glob('*.jpg')) + list(path.glob('*.png'))\n    imsize = model_params[model_name]['imsize']\n\n    # In production we don't resize input images. This is just for demo purpose. \n    x = np.array([np.array(img_read_checks(fn, imsize, is_checksize, imsize, is_check_png)) for fn in files])\n    m, s, features = calculate_activation_statistics(x, sess, model_name)\n    del x #clean up memory\n    return m, s, features\n\n# check for image size\ndef img_read_checks(filename, resize_to, is_checksize=False, check_imsize = 64, is_check_png = False):\n    im = Image.open(str(filename))\n    if is_checksize and im.size != (check_imsize,check_imsize):\n        raise KernelEvalException('The images are not of size '+str(check_imsize))\n    \n    if is_check_png and im.format != 'PNG':\n        raise KernelEvalException('Only PNG images should be submitted.')\n\n    if resize_to is None:\n        return im\n    else:\n        return im.resize((resize_to,resize_to),Image.ANTIALIAS)\n\ndef calculate_kid_given_paths(paths, model_name, model_path, feature_path=None, mm=[], ss=[], ff=[]):\n    ''' Calculates the KID of two paths. '''\n    tf.reset_default_graph()\n    create_model_graph(str(model_path))\n    with tf.Session() as sess:\n        sess.run(tf.global_variables_initializer())\n        m1, s1, features1 = _handle_path_memorization(paths[0], sess, model_name, is_checksize = True, is_check_png = True)\n        if len(mm) != 0:\n            m2 = mm\n            s2 = ss\n            features2 = ff\n        elif feature_path is None:\n            m2, s2, features2 = _handle_path_memorization(paths[1], sess, model_name, is_checksize = False, is_check_png = False)\n        else:\n            with np.load(feature_path) as f:\n                m2, s2, features2 = f['m'], f['s'], f['features']\n\n        print('m1,m2 shape=',(m1.shape,m2.shape),'s1,s2=',(s1.shape,s2.shape))\n        print('starting calculating FID')\n        fid_value = calculate_frechet_distance(m1, s1, m2, s2)\n        print('done with FID, starting distance calculation')\n        distance = cosine_distance(features1, features2)        \n        return fid_value, distance, m2, s2, features2","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-19T14:47:23.526158Z","iopub.status.idle":"2023-02-19T14:47:23.526693Z","shell.execute_reply.started":"2023-02-19T14:47:23.526414Z","shell.execute_reply":"2023-02-19T14:47:23.52644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if ComputeLB:\n    \n    # UNCOMPRESS OUR IMGAES\n    with zipfile.ZipFile(\"../working/images.zip\",\"r\") as z:\n        z.extractall(\"../tmp/images2/\")\n\n    # COMPUTE LB SCORE\n    m2 = []; s2 =[]; f2 = []\n    user_images_unzipped_path = '../tmp/images2/'\n    images_path = [user_images_unzipped_path,'../input/generative-dog-images/all-dogs/all-dogs/']\n    public_path = '../input/dog-face-generation-competition-kid-metric-input/classify_image_graph_def.pb'\n\n    fid_epsilon = 10e-15\n\n    fid_value_public, distance_public, m2, s2, f2 = calculate_kid_given_paths(images_path, 'Inception', public_path, mm=m2, ss=s2, ff=f2)\n    distance_public = distance_thresholding(distance_public, model_params['Inception']['cosine_distance_eps'])\n    print(\"FID_public: \", fid_value_public, \"distance_public: \", distance_public, \"multiplied_public: \",\n            fid_value_public /(distance_public + fid_epsilon))\n    \n    # REMOVE FILES TO PREVENT KERNEL ERROR OF TOO MANY FILES\n    ! rm -r ../tmp","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-19T14:47:23.528741Z","iopub.status.idle":"2023-02-19T14:47:23.529269Z","shell.execute_reply.started":"2023-02-19T14:47:23.528993Z","shell.execute_reply":"2023-02-19T14:47:23.529019Z"},"trusted":true},"execution_count":null,"outputs":[]}]}