{"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":"**IMPORTS** ","metadata":{"id":"6tIMc0Pc5JS5"}},{"cell_type":"code","source":"import random\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten","metadata":{"id":"Zri1sTrSw-oa","execution":{"iopub.status.busy":"2022-11-20T19:52:30.130598Z","iopub.execute_input":"2022-11-20T19:52:30.131069Z","iopub.status.idle":"2022-11-20T19:52:37.054089Z","shell.execute_reply.started":"2022-11-20T19:52:30.130978Z","shell.execute_reply":"2022-11-20T19:52:37.053221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DATASET IMPORT**","metadata":{"id":"c_jQigcV5Mm3"}},{"cell_type":"markdown","source":"**DATASET FROM DIRECTORY (labels are integer encoded)**","metadata":{"id":"yaJCGHczYemg"}},{"cell_type":"code","source":"data_dir = \"../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images\"\nimg_height = 64\nimg_width = 64\n\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  labels='inferred',\n  label_mode = 'int',\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=35763)\n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  labels = 'inferred',\n  label_mode = 'int',\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=8940)","metadata":{"id":"T4oZiXFqlNRH","outputId":"a0e2ca0e-e3b0-4ab9-d153-13085cb7f48d","execution":{"iopub.status.busy":"2022-11-20T19:54:52.418768Z","iopub.execute_input":"2022-11-20T19:54:52.419172Z","iopub.status.idle":"2022-11-20T19:54:58.519416Z","shell.execute_reply.started":"2022-11-20T19:54:52.419126Z","shell.execute_reply":"2022-11-20T19:54:58.518392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)\nprint(len(class_names))","metadata":{"id":"xoLBqTavSQqM","outputId":"6d3fcf0b-87ee-47e6-ce8a-6ae3427961de","execution":{"iopub.status.busy":"2022-11-20T19:55:22.254912Z","iopub.execute_input":"2022-11-20T19:55:22.255383Z","iopub.status.idle":"2022-11-20T19:55:22.262657Z","shell.execute_reply.started":"2022-11-20T19:55:22.255344Z","shell.execute_reply":"2022-11-20T19:55:22.26146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n  for i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(class_names[labels[i]])\n    plt.axis(\"off\")","metadata":{"id":"Xhj-iHrrSVR4","outputId":"1e1cb561-a8da-4d45-fa48-89e7e34a773b"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MAKING X TRAIN, Y TRAIN, X VALID, Y VALID (for convenience of training)**","metadata":{"id":"sEseoED1Y3Q_"}},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n  X_train = image_batch\n  y_train = labels_batch\n  break","metadata":{"id":"2OiWJlRB6Dni","execution":{"iopub.status.busy":"2022-11-20T19:55:34.455651Z","iopub.execute_input":"2022-11-20T19:55:34.456089Z","iopub.status.idle":"2022-11-20T20:01:56.407467Z","shell.execute_reply.started":"2022-11-20T19:55:34.45605Z","shell.execute_reply":"2022-11-20T20:01:56.405306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_batch, labels_batch in val_ds:\n  X_valid = image_batch\n  y_valid = labels_batch\n  break","metadata":{"id":"qVZLixQuWJSK","execution":{"iopub.status.busy":"2022-11-20T20:04:59.53721Z","iopub.execute_input":"2022-11-20T20:04:59.538987Z","iopub.status.idle":"2022-11-20T20:06:21.217677Z","shell.execute_reply.started":"2022-11-20T20:04:59.538929Z","shell.execute_reply":"2022-11-20T20:06:21.21631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.numpy() #35763,64,64,3\ny_train = y_train.numpy() #35763,\nX_valid = X_valid.numpy() #8940,64,64,3\ny_valid= y_valid.numpy() #8940,64,64,3","metadata":{"id":"Oje3IkZmAk5u","execution":{"iopub.status.busy":"2022-11-20T20:09:09.367808Z","iopub.execute_input":"2022-11-20T20:09:09.368349Z","iopub.status.idle":"2022-11-20T20:09:10.465395Z","shell.execute_reply.started":"2022-11-20T20:09:09.368313Z","shell.execute_reply":"2022-11-20T20:09:10.463942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.isnan(np.min(X_train)))","metadata":{"id":"N0oqWjrX9UDb","outputId":"a60cf38e-15c9-4c51-aaf9-82563c490205","execution":{"iopub.status.busy":"2022-11-20T20:09:12.726368Z","iopub.execute_input":"2022-11-20T20:09:12.726804Z","iopub.status.idle":"2022-11-20T20:09:12.906778Z","shell.execute_reply.started":"2022-11-20T20:09:12.726771Z","shell.execute_reply":"2022-11-20T20:09:12.905868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.isnan(np.min(y_train)))","metadata":{"id":"3NRp9NTO90Pk","outputId":"d2a196c0-8b9b-48ad-d87a-ce00c34eec76","execution":{"iopub.status.busy":"2022-11-20T20:09:27.60651Z","iopub.execute_input":"2022-11-20T20:09:27.606945Z","iopub.status.idle":"2022-11-20T20:09:27.613657Z","shell.execute_reply.started":"2022-11-20T20:09:27.606911Z","shell.execute_reply":"2022-11-20T20:09:27.612624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cleaning data : REMOVING CLASSES WHICH HAVE LESS THAN 15 PHOTOS IN THE CLASS (554 classes)","metadata":{"id":"CRm_OavLhWqw"}},{"cell_type":"markdown","source":"Finding the indices in training data that need to be removed","metadata":{"id":"GCSkArrPnCIw"}},{"cell_type":"code","source":"from collections import Counter\n \ndef toberemovedElements(lst, k):\n    counted = Counter(lst)\n    return [el for el in lst if counted[el] < k]","metadata":{"id":"6yxsPSR8gFku","execution":{"iopub.status.busy":"2022-11-20T20:09:35.153541Z","iopub.execute_input":"2022-11-20T20:09:35.15398Z","iopub.status.idle":"2022-11-20T20:09:35.160892Z","shell.execute_reply.started":"2022-11-20T20:09:35.153942Z","shell.execute_reply":"2022-11-20T20:09:35.159594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_be_removed_classes = np.unique(toberemovedElements(y_train,15))","metadata":{"id":"CIsgstHWglUP","execution":{"iopub.status.busy":"2022-11-20T20:09:47.30954Z","iopub.execute_input":"2022-11-20T20:09:47.309937Z","iopub.status.idle":"2022-11-20T20:09:47.332436Z","shell.execute_reply.started":"2022-11-20T20:09:47.309906Z","shell.execute_reply":"2022-11-20T20:09:47.331381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(to_be_removed_classes)","metadata":{"id":"9Rp525XfDmzw","outputId":"398a8b69-8c44-4478-86f9-cd5088c5e28f","execution":{"iopub.status.busy":"2022-11-20T20:09:54.861532Z","iopub.execute_input":"2022-11-20T20:09:54.86236Z","iopub.status.idle":"2022-11-20T20:09:54.873612Z","shell.execute_reply.started":"2022-11-20T20:09:54.862318Z","shell.execute_reply":"2022-11-20T20:09:54.872387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reqd = []\nfor i in range(len(to_be_removed_classes)):\n  indices = list(np.where(y_train == to_be_removed_classes[i])[0])\n  for j in range(len(indices)):\n    reqd.append(indices[j]) \nreqd #index numbers","metadata":{"id":"WnxNzH3RlLv6","outputId":"e3d32bf2-40eb-45ac-9109-1efc104c723c","execution":{"iopub.status.busy":"2022-11-20T20:10:02.096098Z","iopub.execute_input":"2022-11-20T20:10:02.097184Z","iopub.status.idle":"2022-11-20T20:10:02.181603Z","shell.execute_reply.started":"2022-11-20T20:10:02.097116Z","shell.execute_reply":"2022-11-20T20:10:02.180454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices_to_remove_in_training = list(reqd)\nindices_to_remove_in_training.sort(reverse= True) #index numbers in descending order","metadata":{"id":"s5e3-C4enEaL","execution":{"iopub.status.busy":"2022-11-20T20:10:13.275852Z","iopub.execute_input":"2022-11-20T20:10:13.276264Z","iopub.status.idle":"2022-11-20T20:10:13.288568Z","shell.execute_reply.started":"2022-11-20T20:10:13.27623Z","shell.execute_reply":"2022-11-20T20:10:13.287259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"x train cleaning","metadata":{"id":"Bevoy7rMuY8x"}},{"cell_type":"code","source":"X_train = list(X_train) #removing the index numbers from X train\nfor i in range(len(indices_to_remove_in_training)):\n  X_train.pop(indices_to_remove_in_training[i])","metadata":{"id":"U-9uSz_vhYtd","execution":{"iopub.status.busy":"2022-11-20T20:10:46.044704Z","iopub.execute_input":"2022-11-20T20:10:46.045185Z","iopub.status.idle":"2022-11-20T20:10:46.099109Z","shell.execute_reply.started":"2022-11-20T20:10:46.045124Z","shell.execute_reply":"2022-11-20T20:10:46.098062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\nX_train.shape","metadata":{"id":"i8mpsGcRpkHk","outputId":"a03f1c5c-1936-42b5-885d-ee9b0f625496","execution":{"iopub.status.busy":"2022-11-20T20:10:53.883425Z","iopub.execute_input":"2022-11-20T20:10:53.883846Z","iopub.status.idle":"2022-11-20T20:10:54.275691Z","shell.execute_reply.started":"2022-11-20T20:10:53.883814Z","shell.execute_reply":"2022-11-20T20:10:54.274914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"y train cleaning","metadata":{"id":"BT-G5iyyudSN"}},{"cell_type":"code","source":"y_train = list(y_train) #removing the index numbers from y train\nfor i in range(len(indices_to_remove_in_training)):\n  y_train.pop(indices_to_remove_in_training[i])","metadata":{"id":"GHKHFN6LmXNs","execution":{"iopub.status.busy":"2022-11-20T20:10:56.66282Z","iopub.execute_input":"2022-11-20T20:10:56.663282Z","iopub.status.idle":"2022-11-20T20:10:56.706389Z","shell.execute_reply.started":"2022-11-20T20:10:56.663207Z","shell.execute_reply":"2022-11-20T20:10:56.705022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np.array(y_train)\ny_train.shape","metadata":{"id":"zIafgMWfbQH4","outputId":"ffaa9942-fe56-4e2e-c9c6-c21675990d78","execution":{"iopub.status.busy":"2022-11-20T20:11:03.624787Z","iopub.execute_input":"2022-11-20T20:11:03.625246Z","iopub.status.idle":"2022-11-20T20:11:03.634054Z","shell.execute_reply.started":"2022-11-20T20:11:03.625199Z","shell.execute_reply":"2022-11-20T20:11:03.632793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = np.unique(y_train)\nclasses = list(classes)\nlen(classes)","metadata":{"id":"RAT4-lp4n9cm","outputId":"a44a3265-41b6-4878-91f8-722655b80553","execution":{"iopub.status.busy":"2022-11-20T20:11:13.709541Z","iopub.execute_input":"2022-11-20T20:11:13.709979Z","iopub.status.idle":"2022-11-20T20:11:13.718695Z","shell.execute_reply.started":"2022-11-20T20:11:13.709942Z","shell.execute_reply":"2022-11-20T20:11:13.717362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"SCALING IMAGE DATA","metadata":{"id":"-I3zzXtEnl9k"}},{"cell_type":"code","source":"#scaling\nX_train = X_train.astype(\"float32\")/255.\nX_valid = X_valid.astype(\"float32\")/255.","metadata":{"id":"YtA_8SopA65t","execution":{"iopub.status.busy":"2022-11-20T20:11:15.672228Z","iopub.execute_input":"2022-11-20T20:11:15.672662Z","iopub.status.idle":"2022-11-20T20:11:16.364898Z","shell.execute_reply.started":"2022-11-20T20:11:15.672626Z","shell.execute_reply":"2022-11-20T20:11:16.363634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"DATA GENERATOR FOR THE TRIPLET MODEL","metadata":{"id":"uYlji60NnqF5"}},{"cell_type":"code","source":" #3 inputs of size 64,64,64,3 (anchor, positive, negative) are generated here to be fed into the network. \ndef data_generator(batch_size = 64):\n  while True:\n    a=[]\n    p=[]\n    n=[]\n    for _ in range(batch_size):\n      pos_neg = random.sample(classes,2)\n      positive_samples = np.array(random.sample(list(X_train[y_train == pos_neg[0]]), 2)) #2,64,64,3\n      negative_samples= np.array(random.sample(list(X_train[y_train == pos_neg[1]]),1)) #1,64,64,3\n      a.append(positive_samples[0]) #finally 64,64,64,3\n      p.append(positive_samples[1]) #finally 64,64,64,3\n      n.append(negative_samples[0]) #finally 64,64,64,3\n    yield ( [np.array(a), np.array(p), np.array(n)] , np.zeros((batch_size,1)).astype(\"float32\") )","metadata":{"id":"X5dknMu5CAEi","execution":{"iopub.status.busy":"2022-11-20T20:11:38.078302Z","iopub.execute_input":"2022-11-20T20:11:38.078711Z","iopub.status.idle":"2022-11-20T20:11:38.087684Z","shell.execute_reply.started":"2022-11-20T20:11:38.078678Z","shell.execute_reply":"2022-11-20T20:11:38.086436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"EFFICIENT B5 - feature extraction (5000 dimension vector)","metadata":{"id":"SEu_gzfl-JnL"}},{"cell_type":"code","source":"model = tf.keras.applications.vgg19.VGG19(\n    include_top=False,\n    weights='imagenet',\n    input_shape= (64,64,3)\n)","metadata":{"id":"X2xbfvVwoJBo","outputId":"f63b091a-0dd4-4412-c0ba-91aac0a1a63a","execution":{"iopub.status.busy":"2022-11-20T20:11:47.092924Z","iopub.execute_input":"2022-11-20T20:11:47.093344Z","iopub.status.idle":"2022-11-20T20:11:51.2869Z","shell.execute_reply.started":"2022-11-20T20:11:47.09331Z","shell.execute_reply":"2022-11-20T20:11:51.285541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.trainable = False","metadata":{"id":"ZMRXxN4sJhct","execution":{"iopub.status.busy":"2022-11-20T20:11:58.109931Z","iopub.execute_input":"2022-11-20T20:11:58.111235Z","iopub.status.idle":"2022-11-20T20:11:58.116695Z","shell.execute_reply.started":"2022-11-20T20:11:58.111181Z","shell.execute_reply":"2022-11-20T20:11:58.115606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"id":"ZE-WnRWJzvrH","outputId":"2d76b964-e381-4119-ddbe-094f3f4be1b7","execution":{"iopub.status.busy":"2022-11-20T20:12:02.294125Z","iopub.execute_input":"2022-11-20T20:12:02.294975Z","iopub.status.idle":"2022-11-20T20:12:02.304034Z","shell.execute_reply.started":"2022-11-20T20:12:02.294928Z","shell.execute_reply":"2022-11-20T20:12:02.302411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = keras.Sequential(\n       [ model, \n       keras.layers.Flatten(),\n       keras.layers.Dense(1000, activation = 'relu')]\n)","metadata":{"id":"kTWL0-C6J9jz","execution":{"iopub.status.busy":"2022-11-20T20:13:34.019208Z","iopub.execute_input":"2022-11-20T20:13:34.019627Z","iopub.status.idle":"2022-11-20T20:13:34.117453Z","shell.execute_reply.started":"2022-11-20T20:13:34.019595Z","shell.execute_reply":"2022-11-20T20:13:34.116152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary() ","metadata":{"id":"47uM6eb4cpg8","outputId":"eb1eeeb7-6fd2-453d-b7fb-602d84e4e696","execution":{"iopub.status.busy":"2022-11-20T20:13:45.891744Z","iopub.execute_input":"2022-11-20T20:13:45.892151Z","iopub.status.idle":"2022-11-20T20:13:45.89988Z","shell.execute_reply.started":"2022-11-20T20:13:45.892105Z","shell.execute_reply":"2022-11-20T20:13:45.89852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"TRIPLET LOSS","metadata":{"id":"ZTiJ0wSg2c3M"}},{"cell_type":"code","source":"def triplet_loss(y_true, y_pred):\n  anchor_out = y_pred[:,0:1000]\n  positive_out = y_pred[:,1000:2000]\n  negative_out = y_pred[:,2000:3000]\n  pos_dist = K.sum(K.abs(anchor_out - positive_out), axis =1) #l1 distance between anchor and positive image\n  neg_dist = K.sum(K.abs(anchor_out - negative_out), axis =1) #l2 distance between anchor and negative image \n  #IBM research function\n  probs = K.softmax([pos_dist, neg_dist], axis =0)\n  return K.mean(K.abs(probs[0]) + K.abs(1.-probs[1]))","metadata":{"id":"YerDGzBglF4o","execution":{"iopub.status.busy":"2022-11-20T20:13:51.202286Z","iopub.execute_input":"2022-11-20T20:13:51.202734Z","iopub.status.idle":"2022-11-20T20:13:51.211127Z","shell.execute_reply.started":"2022-11-20T20:13:51.202698Z","shell.execute_reply":"2022-11-20T20:13:51.209492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"TRIPLET MODEL ","metadata":{"id":"5XfRBbFi2viX"}},{"cell_type":"code","source":"triplet_model_a = tf.keras.Input((64,64,3))\ntriplet_model_p = tf.keras.Input((64,64,3))\ntriplet_model_n = tf.keras.Input((64,64,3))\ntriplet_model_out = keras.layers.concatenate([base_model(triplet_model_a),base_model(triplet_model_p), base_model(triplet_model_n)]) #None,3000\ntriplet_model = Model([triplet_model_a,triplet_model_p,triplet_model_n], triplet_model_out) #model inputs, output","metadata":{"id":"_f0Z4XpA2uty","execution":{"iopub.status.busy":"2022-11-20T20:13:59.527827Z","iopub.execute_input":"2022-11-20T20:13:59.528267Z","iopub.status.idle":"2022-11-20T20:13:59.744812Z","shell.execute_reply.started":"2022-11-20T20:13:59.528231Z","shell.execute_reply":"2022-11-20T20:13:59.743649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"triplet_model.summary()","metadata":{"id":"Mzh9XmiP3Zeg","outputId":"eee691de-a59f-4e00-83ff-4366dfe92d28","execution":{"iopub.status.busy":"2022-11-20T20:14:05.906208Z","iopub.execute_input":"2022-11-20T20:14:05.906614Z","iopub.status.idle":"2022-11-20T20:14:05.915742Z","shell.execute_reply.started":"2022-11-20T20:14:05.906583Z","shell.execute_reply":"2022-11-20T20:14:05.914388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"TRAINING WITH TRIPLET LOSS","metadata":{"id":"RyyH41to3q6s"}},{"cell_type":"code","source":"triplet_model.compile(loss = triplet_loss, optimizer = 'adam')","metadata":{"id":"bcdsghOe3s3t","execution":{"iopub.status.busy":"2022-11-20T20:14:12.636222Z","iopub.execute_input":"2022-11-20T20:14:12.637423Z","iopub.status.idle":"2022-11-20T20:14:12.660283Z","shell.execute_reply.started":"2022-11-20T20:14:12.637358Z","shell.execute_reply":"2022-11-20T20:14:12.659243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"triplet_model.fit(data_generator(), steps_per_epoch = 50, epochs=2)","metadata":{"id":"i592AmZA3yr2","outputId":"96532c8a-82db-4085-bf03-d0646eec6cd7","execution":{"iopub.status.busy":"2022-11-20T20:28:20.750191Z","iopub.execute_input":"2022-11-20T20:28:20.750618Z","iopub.status.idle":"2022-11-20T20:37:02.119072Z","shell.execute_reply.started":"2022-11-20T20:28:20.75058Z","shell.execute_reply":"2022-11-20T20:37:02.118199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"triplet_model.trainable = False","metadata":{"id":"l_aLOUPtFy-H","execution":{"iopub.status.busy":"2022-11-20T20:39:16.692868Z","iopub.execute_input":"2022-11-20T20:39:16.693444Z","iopub.status.idle":"2022-11-20T20:39:16.699625Z","shell.execute_reply.started":"2022-11-20T20:39:16.693398Z","shell.execute_reply":"2022-11-20T20:39:16.698692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"REPLACING LAST LAYER WITH SOFTMAX","metadata":{"id":"PIjiQTtUwkWb"}},{"cell_type":"code","source":"def softy(logits,t=1.2):\n    return tf.exp(logits/1.2) / tf.reduce_sum(tf.exp(logits/1.2), 1)","metadata":{"id":"sFp7uxg_K989","execution":{"iopub.status.busy":"2022-11-20T20:41:05.973808Z","iopub.execute_input":"2022-11-20T20:41:05.974791Z","iopub.status.idle":"2022-11-20T20:41:05.980324Z","shell.execute_reply.started":"2022-11-20T20:41:05.974749Z","shell.execute_reply":"2022-11-20T20:41:05.979213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = tf.keras.Sequential()\nlays = triplet_model.get_layer('sequential_2')\nm.add(lays)\nm.add(keras.layers.Dense(554, kernel_initializer='random_normal',activation = softy))","metadata":{"id":"BzNL5MTxwmuF","execution":{"iopub.status.busy":"2022-11-20T20:41:13.941876Z","iopub.execute_input":"2022-11-20T20:41:13.942291Z","iopub.status.idle":"2022-11-20T20:41:14.039811Z","shell.execute_reply.started":"2022-11-20T20:41:13.942256Z","shell.execute_reply":"2022-11-20T20:41:14.038831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m.summary() #model layers modified","metadata":{"id":"190OrVXxwvDT","outputId":"d37860af-712c-402e-fc1f-23b4d87622fe","execution":{"iopub.status.busy":"2022-11-20T20:41:17.367542Z","iopub.execute_input":"2022-11-20T20:41:17.367932Z","iopub.status.idle":"2022-11-20T20:41:17.374984Z","shell.execute_reply.started":"2022-11-20T20:41:17.367901Z","shell.execute_reply":"2022-11-20T20:41:17.373547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"CHANGING THE INPUT LAYER (positive,negative,anchor) into 1 input","metadata":{"id":"ckc3-kg8xdtP"}},{"cell_type":"code","source":"m_in = tf.keras.Input(shape =(64,64,3)) #input\nm_out = m(m_in)","metadata":{"id":"haWX2eFVw4GU","execution":{"iopub.status.busy":"2022-11-20T20:41:26.133421Z","iopub.execute_input":"2022-11-20T20:41:26.133889Z","iopub.status.idle":"2022-11-20T20:41:26.218618Z","shell.execute_reply.started":"2022-11-20T20:41:26.13385Z","shell.execute_reply":"2022-11-20T20:41:26.217436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = Model(m_in, m_out)\n","metadata":{"id":"O2lmXHuSxThM","execution":{"iopub.status.busy":"2022-11-20T20:41:31.122568Z","iopub.execute_input":"2022-11-20T20:41:31.123021Z","iopub.status.idle":"2022-11-20T20:41:31.132048Z","shell.execute_reply.started":"2022-11-20T20:41:31.122982Z","shell.execute_reply":"2022-11-20T20:41:31.130654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.summary()","metadata":{"id":"E5C80sIxxWAQ","outputId":"05837153-6f74-434a-9a81-9daa830edac0","execution":{"iopub.status.busy":"2022-11-20T20:41:34.652212Z","iopub.execute_input":"2022-11-20T20:41:34.652637Z","iopub.status.idle":"2022-11-20T20:41:34.66085Z","shell.execute_reply.started":"2022-11-20T20:41:34.652596Z","shell.execute_reply":"2022-11-20T20:41:34.659473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"TRAINING THIS CLASSIFIER NEURAL NETWORK","metadata":{"id":"EYItXHcMyF19"}},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(\n    learning_rate=1e-4)\nnew_model.compile(optimizer=opt, loss = 'sparse_categorical_crossentropy')","metadata":{"id":"iUip4shOyJpg","execution":{"iopub.status.busy":"2022-11-20T20:41:47.580918Z","iopub.execute_input":"2022-11-20T20:41:47.58135Z","iopub.status.idle":"2022-11-20T20:41:47.59381Z","shell.execute_reply.started":"2022-11-20T20:41:47.581313Z","shell.execute_reply":"2022-11-20T20:41:47.592929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.fit(X_train, y_train)","metadata":{"id":"_TXAWox_CwMV","outputId":"fda5cd8d-e2ce-410f-b570-08118e60c005","execution":{"iopub.status.busy":"2022-11-20T20:41:56.409118Z","iopub.execute_input":"2022-11-20T20:41:56.409608Z","iopub.status.idle":"2022-11-20T20:41:58.658313Z","shell.execute_reply.started":"2022-11-20T20:41:56.409569Z","shell.execute_reply":"2022-11-20T20:41:58.656467Z"},"trusted":true},"execution_count":null,"outputs":[]}]}