# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/", "height": 50} id="EIOlMOX7lt1R" outputId="f7e21642-a407-4852-8838-794b52129536"
from google.colab import drive
drive.mount('/content/drive')

# %cd /content/drive/My Drive/Github/Biometric

# + id="rQLTyIbVkJkU"
import os
import numpy as np
import pandas as pd
from tqdm import tqdm

import seaborn as sns
from sklearn.decomposition import PCA

import tensorflow as tf
from tensorflow.keras.models import load_model

import matplotlib.pyplot as plt
import cv2
from PIL import Image

from FUNC_script import *

# + id="JY0LMeaOl1H_"
# Load FaceNet Model
model = load_model('Template_poisoning/model/facenet_keras.h5', compile= False)
model.trainable= False


# + id="iB3r7PVemQRY"
# Read Images (160x160x3)
def load_img(path, resize=None):
    img= Image.open(path)
    img= img.convert('RGB')
    if resize is not None:
        img= img.resize((resize, resize))
    return np.asarray(img)

def Centroid(vector):
  ''' 
      After the random initialization, we first optimize the
      glasses using the adversary’s centroid in feature space(Xc)
     # Arguments
        Input: feature vector(batch_size x feature)	
    '''
  Xc= tf.math.reduce_mean(vector, axis=0)
  return tf.reshape(Xc, (1, vector.shape[-1]))


# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="4T-NbZQOn4xS" outputId="da64bf3e-5a94-4944-b54a-db22ba860aa0"
# Data  Loading
def load_data(dir):
  X=[]
  for i, sample in enumerate(os.listdir(dir)):
    image= load_img(os.path.join(dir, sample))
    image = cv2.resize(image, (160, 160))
    X.append(image/255.0)
  return np.array(X)

X= load_data('Template_poisoning/Croped_data/adversary_images')
Target_samples= load_data('Template_poisoning/Croped_data/target_images')
X_ex= X.copy() # Copy of X
print('Adversarial Batch:',X.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="MquVc5CDn40u" outputId="e20df9fe-e2e6-485d-a46d-27e430245708"
# GET Mask
mask= load_img('Template_poisoning/final_mask.png') #Sacle(0-255), RGB
mask= mask/255.0
mask.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="rl2fkdNSn4vC" outputId="5c5cb737-06fe-4b6f-c99f-32a3a78b0e57"
# img_tr= load_img('Template_poisoning/Croped_data/target_images/ben_afflek_0.jpg')
# feature_tr= model.predict(img_tr[np.newaxis, :, :, :])
#Target= Generate_target(feature_tr, batch_size= X.shape[0])

Target= Centroid(model.predict(Target_samples))
Target= Generate_target(Target, batch_size= X.shape[0])
Target.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="5hCCDwUYuAPE" outputId="6df0357f-54f0-432d-99a0-580dba33a3e8"
delta_x= np.random.uniform(low=0.0, high=1.0, size=X.shape) # Scale(0-1)
delta_x.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 169} id="VVdsjM3NRFdo" outputId="f1652b27-edd8-4f4d-955e-b6a0480952e4"
f, ax= plt.subplots(1, 5, figsize=(14, 4))
image= X*(1-mask)+ delta_x*mask
for i in range(5):
  ax[i].imshow(image[i+5])
  ax[i].set_xticks([]); ax[i].set_yticks([])
plt.show()
del image

# + colab={"base_uri": "https://localhost:8080/", "height": 50} id="jqAjO_ISRlav" outputId="be68deb4-760a-4c1e-ed55-9a6e04829d8a"
Xc= Centroid(model.predict(X)) #(1 x 128)
print(Xc.shape)
Xc= Generate_target(Xc, batch_size=24) #(24 x 128)
print(Xc.shape)


# + id="MgSGIG5PS6Rn"
def loss_object(pred, label, delta= delta_x, direction= False):
  # Loss= euclidean distance + Delta_x pixel Variance
  dist= Euclidean_dist(pred, label)
  variance= Sample_variance(delta_x)
  if direction:
    sc= tf.math.subtract(1.0, tf.math.divide(1.0, label.shape[0]))

    vector_mean= tf.math.multiply(dist, sc)
   
    target_dir= tf.math.multiply(vector_mean, dist)
    Loss= tf.math.add(target_dir, tf.cast(variance, dist.dtype))
    return Loss

  Loss= tf.math.add(tf.cast(dist, variance.dtype), variance)
  return Loss


# + id="f98mUqiwRT5h"
def back_propagate(model, X, mask, delta_x, label, direction= False):
  with tf.GradientTape() as g:
      g.watch(delta_x)
      X_batch= Generate_sample(X, delta_x, mask)
      feature= model(X_batch)
      loss= loss_object(pred= feature, label= label, delta= delta_x, direction= direction)
  # Get the gradients of the loss w.r.t to the input image.
  gradient = g.gradient(loss, delta_x)
  return gradient, tf.reduce_mean(loss).numpy()


# + id="3prov3SDUIYV"
# Tf Variables
X= tf.Variable(X, dtype=tf.float64)
delta_x= tf.Variable(delta_x, dtype=tf.float64)
mask= tf.Variable(mask, dtype=tf.float64)
Xc= tf.Variable(Xc)

# + colab={"base_uri": "https://localhost:8080/", "height": 286} id="jECDM3ejPpaJ" outputId="2b747db4-bd84-41ac-9e31-d7e3dac11d72"
epoch= 151
Lambda= 0.5
for ep  in range(epoch):
  grad, loss= back_propagate(model, X, mask, delta_x, Xc)
  # Gradient step
  delta_x= delta_x - Lambda*grad
  if ep%10 == 0:
    print('Epoch: {}  Loss: {:.3f}'.format((ep+1), loss))

# + colab={"base_uri": "https://localhost:8080/", "height": 790} id="YbRJMJw9sXAR" outputId="bb6f5b42-4308-45a3-8035-2c5f914b6aa9"
Lambda= 0.2
for ep  in range(int(3*epoch)):
  grad, loss= back_propagate(model, X, mask, delta_x, Target, direction= True)
  # Gradient step
  delta_x= delta_x - Lambda*grad
  if ep== 0:
    delta_x0= tf.identity(delta_x)
  if ep== 1:
    delta_x1= tf.identity(delta_x)
  if ep== 2:
    delta_x2= tf.identity(delta_x)
  if ep== 3:
    delta_x2= tf.identity(delta_x)
  if ep== 170:
    Lambda= 0.1
  if ep== 300:
    Lambda= 0.04
  if ep%10 == 0:
    print('Epoch: {}, Loss: {}'.format((ep+1), loss))

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="gdBAD-1Gcdyd" outputId="2dde8ee7-bb5e-4d7f-cee5-426aaa77534f"
adv_sample0=Generate_sample(X, delta_x, mask)
adv_sample0=adv_sample0.numpy()


adv_sample1=Generate_sample(X, delta_x1, mask)
adv_sample1=adv_sample1.numpy()
adv_sample1.shape

# + id="WCsgDlmilX_R"
adv_sample0=np.clip(adv_sample0, 0, 1)

adv_sample1=np.clip(adv_sample1, 0, 1)

# + colab={"base_uri": "https://localhost:8080/", "height": 169} id="eEu0NYkBf2Wj" outputId="94177076-8934-4a83-8fc8-e6d204878c20"
f, ax= plt.subplots(1, 5, figsize=(14, 4))
for i in range(5):
  ax[i].imshow(adv_sample0[i+5])
  ax[i].set_xticks([]); ax[i].set_yticks([])
plt.show()

# + id="a5uspmH0f_s4"
adv_feature= model.predict(X_ex)
df_adv= pd.DataFrame(adv_feature)

# + id="Dotupoy9gdM4"
adv_modified_feature0= model.predict(adv_sample0)
df_adv_modify0= pd.DataFrame(adv_modified_feature0)

adv_modified_feature1= model.predict(adv_sample1)
df_adv_modify1= pd.DataFrame(adv_modified_feature1)

# + id="olDOGvQ5tx2B"
target_feature= model.predict(Target_samples)
df_target= pd.DataFrame(target_feature)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="Wi6ac2lxhjOJ" outputId="f67d53ca-913d-424b-a401-6fb5c58a1f20"
df_adv['target']= 'Adversarial_sample'
df_adv_modify0['target']= 'Adversarial Pubertation initial step'
df_adv_modify1['target']= 'Adversarial Pubertation final step'
df_target['target']= 'Target_sample'

df=pd.concat([df_target, df_adv_modify0,df_adv_modify1,  df_adv], ignore_index= True)
df.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 388} id="7hDpH2Vh96jH" outputId="9f0e1b59-18ca-4711-8109-477b89ce55d8"
pca = PCA(n_components=2) 
# Fit pca to 'X'
df1= pd.DataFrame(pca.fit_transform(df.drop(['target'], 1)))
df1.shape

df1['target']= df.target

fig, ax = plt.subplots(figsize=(12, 6))
plt.grid(True)
plt.xlabel('feature-1'); plt.ylabel('feature-2')
sns.scatterplot(x=df1.iloc[:, 0] , y= df1.iloc[:, 1], hue = df1.iloc[:, 2], data= df1, palette='Set1', ax= ax)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered