<jupyter_start><jupyter_text>Imports<jupyter_code>import sys 
import os
import numpy as np 
import matplotlib.pyplot as plt
from skimage import io
import math
import copy

import torch
import torchvision 
import torch.optim as optim
import torch.nn as nn
from torch.utils.data.dataset import Dataset
sys.path.insert(0, '../../Utils')

import models
from train import *
from metrics import * 
#from Gradient_Ascent import data_extraction
from skimage.restoration import denoise_nl_means, estimate_sigma

from PIL import Image
import pdb

print("Python: %s" % sys.version)
print("Pytorch: %s" % torch.__version__)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")<jupyter_output><empty_output><jupyter_text>Hyperparameters<jupyter_code>n_epochs = 40
batch_size = 32
lr = 0.01
train_percent = 0.7<jupyter_output><empty_output><jupyter_text>Load ATT Dataset<jupyter_code>class ATTdataset(Dataset): 
    def __init__(self, file_list, class_to_label, transform=None): 
        self.file_list = file_list
        self.transform = transform        
        self.people_to_idx = class_to_label
        
                
    def __len__(self): 
        return len(self.file_list)
    
    def __getitem__(self, idx): 
        image = np.empty((112, 92, 1), dtype=np.uint8)
        img_path = self.file_list[idx]
        im = io.imread(img_path)
        im = im.reshape(112,92)
        image[:,:,0]= im
        label = self.people_to_idx[img_path.split('\\')[-2]]
        if self.transform is not None: 
            image = self.transform(image)
        return image, label        
data_dir = "..\\..\\attfaces"

img_paths = []
for p in os.listdir(data_dir): 
    for i in os.listdir(os.path.join(data_dir,p)): 
        img_paths.append(os.path.join(data_dir,p,i))
        
people = []
people_to_idx = {}
idx_to_people = {}
sample_paths = []
k = 0 
for i in img_paths: 
    name = i.split('\\')[-2]
    if name not in people_to_idx: 
        people.append(name)
        people_to_idx[name] = k
        idx_to_people[k] = name
        k += 1
        sample_paths.append(i)

img_paths = np.random.permutation(img_paths)

lfw_size = len(img_paths)
lfw_train_size = int(train_percent * lfw_size)
lfw_train_list = img_paths[:lfw_train_size]
lfw_test_list = img_paths[lfw_train_size:]

# Data augmentation 
train_transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor(),
])

test_transform = torchvision.transforms.Compose([    
    torchvision.transforms.ToTensor(),
])

trainset = ATTdataset(lfw_train_list, people_to_idx, transform=train_transform)
testset = ATTdataset(lfw_test_list, people_to_idx, transform=test_transform)
sampleset = ATTdataset(sample_paths, people_to_idx, transform=torchvision.transforms.ToTensor())

trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=0)
testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False, num_workers=0)
sampleloader = torch.utils.data.DataLoader(sampleset, batch_size=40, shuffle=False, num_workers=0)
# helper function to unnormalize and plot image 
def imshow(img, name):
    print(type(img), img.shape)
    img = np.array(img)
    #img = img / 2 + 0.5
    print(type(img), img.shape)
    img = np.moveaxis(img, 0, -1)
    plt.imshow(img)
    plt.imsave('.\\'+name, img)
    
# display sample from dataset 
imgs,labels = iter(sampleloader).next()
print(type(imgs), type(torchvision.utils.make_grid(imgs)))
imshow(torchvision.utils.make_grid(imgs),'origin') 
print(labels)
print(imgs.min())
print(imgs.max())
print(imgs.shape)<jupyter_output><class 'torch.Tensor'> <class 'torch.Tensor'>
<class 'torch.Tensor'> torch.Size([3, 572, 754])
<class 'numpy.ndarray'> (3, 572, 754)
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
        18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
        36, 37, 38, 39])
tensor(0.)
tensor(0.9569)
torch.Size([40, 1, 112, 92])<jupyter_text>Model Architecture<jupyter_code>class mlp(nn.Module): 
    def __init__(self, n_in=112*92, n_out=40, n_hidden=3000): 
        super(mlp, self).__init__()
        self.hidden1 = nn.Linear(n_in, n_out)        

    def forward(self, x):
        x = x.view(-1,112*92)
        out = self.hidden1(x)
        return out<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>ww0_shape = nn.Linear(112*92, 40).weight.data.shape
print(ww0_shape)
net2 = mlp().to(device)
net2.apply(models.weights_init)
criterion = nn.CrossEntropyLoss()
optimizer2 = optim.SGD(net2.parameters(), lr=lr, momentum=0.9, weight_decay=5e-4)
ww0 = copy.deepcopy(net2.hidden1.weight.data)
bb0 = copy.deepcopy(net2.hidden1.bias.data)
train_accs, test_accs = train(net2, trainloader, testloader, optimizer2, criterion, n_epochs, verbose=False)
ww = copy.deepcopy(net2.hidden1.weight.data)
bb = copy.deepcopy(net2.hidden1.bias.data[:])
i = 39
im0 = ww0[i,:].reshape(1,1,112,92)
im_train = ww[i,:].reshape(1,1,112,92)
plt.imshow((im_train-im0).squeeze().cpu().detach().numpy(),cmap='gray')
plt.show()

real_imgs_size = torch.Size([40,1,112,92])
real_imgs = torch.zeros(real_imgs_size)
for i in range(40):
    im0 = ww0[i,:].reshape(1,1,112,92)
    im_train = ww[i,:].reshape(1,1,112,92)
    real_imgs[i] = im_train-im0<jupyter_output><empty_output><jupyter_text>DRAM Bit Error Introduction  Float Binary Conversion and Evaluation in CPU<jupyter_code># Determine the fixed point exponent
nbits = 32 # with one-bit sign
arr = copy.deepcopy(ww[39,:].reshape(1,1,112,92)).squeeze().cpu().detach().numpy().reshape(-1)
weight_max = np.absolute(arr).max()
fixed_exp = np.ceil(np.log2(weight_max/(2**(nbits-1)-1)))
def float2bin_cpu(num, fixed_exp):
    if num >= 0:
        s = '0'
        tmp = bin(int(num / (2**fixed_exp)))[2:]
    else:
        s = '1'
        tmp = bin(int(num / (2**fixed_exp)))[3:]
    s += '0'*(nbits-1-len(tmp)) + tmp
    return s
def bin2float_cpu(s, fixed_exp):
    if s[0] == '0':
        num = 1.0
    else:
        num = -1.0
    num *= int(s[1:],2) * 2**fixed_exp
    return num
# evaluation
for i in range(len(arr)):
    test = arr[i]
    rec = bin2float_cpu(float2bin_cpu(test, fixed_exp),fixed_exp)
    if np.absolute(test-rec) <= 2**fixed_exp:
        continue
    else:
        print(i, test, rec)<jupyter_output><empty_output><jupyter_text>Float Binary Conversion and Evaluation in GPU<jupyter_code>def float2bin_gpu(f, fixed_exp, nbits=8):
    s = torch.sign(f)
    f = f * s
    s = (s * (-1) + 1.) * 0.5
    s= s.unsqueeze(-1)
    f = f/(2**fixed_exp)
    m = integer2bit(f - f % 1,num_bits = nbits-1)
    dtype = f.type()
    out = torch.cat([s, m], dim=-1).type(dtype)
    return out
def integer2bit(integer, num_bits=7):
    dtype = integer.type()
    exponent_bits = -torch.arange(-(num_bits - 1), 1).type(dtype)
    exponent_bits = exponent_bits.repeat(integer.shape + (1,))
    out = integer.unsqueeze(-1) / 2 ** exponent_bits
    return (out - (out % 1)) % 2
def bin2float_gpu(b, fixed_exp, nbits=8):
    dtype = torch.float32
    s = torch.index_select(b, -1, torch.arange(0, 1).to(device))
    m = torch.index_select(b, -1, torch.arange(1,nbits).to(device))
    out = ((-1) ** s).squeeze(-1).type(dtype)
    exponents = -torch.arange(-(nbits - 2.), 1.).to(device)
    exponents = exponents.repeat(b.shape[:-1] + (1,))
    e_decimal = torch.sum(m * 2 ** (exponents), dim=-1)
    out *= e_decimal * 2 ** fixed_exp
    return out
# evaluation
nbits = 8
for i in range(40):
    f = ww[i,:]
    weight_max = torch.max(f)
    fixed_exp = torch.ceil(torch.log2(weight_max/(2**(nbits-1)-1))) + 1
    rec = bin2float_gpu(float2bin_gpu(f, fixed_exp),fixed_exp)
    if not torch.all((rec - f) <= 1 * 2**fixed_exp):
        print(i, '1 binary float conversion failed')
    if not torch.all(rec - f >= -1 * 2**fixed_exp):
        print(i, '2 binary float conversion failed')
def bin_add_bit_error(b, bit_error_percent = 0):
    cond = torch.rand(b.shape).to(device) > bit_error_percent
    a = 1 - b
    out = cond * b + torch.logical_not(cond) * a
    return out
bit_error_percent = 0.1

accuracy_list = []
testacc_list = []
similarity_list = []

for i in range(100):
    cur_imgs = torch.zeros(real_imgs.shape)
    similarity = 0
    for k in range(40):
        im0 = ww0[k,:].reshape(1,1,112,92)
        f = ww[k,:]
        f_modify = bin2float_gpu(bin_add_bit_error(float2bin_gpu(f, fixed_exp), bit_error_percent), fixed_exp)
        cur_imgs[k] = f_modify.reshape(1,1,112,92)

        data = cur_imgs[k]/100.0
        data1 = real_imgs[k]/100.0
        # ncc = torch.sum(data*data1)/torch.sqrt(torch.sum(data**2))/torch.sqrt(torch.sum(data1**2))
        # similarity += -0.5*(ncc-1)
        # Pearson Correlation Coefficient
        vdata = data - torch.mean(data)
        vdata1 = data1 - torch.mean(data1)
        pcc = torch.sum(vdata*vdata1)/torch.sqrt(torch.sum(vdata**2))/torch.sqrt(torch.sum(vdata1**2))
        similarity += pcc

        net2.hidden1.weight.data[k,:] = f_modify

    accuracy_list.append( eval_target_net(net2, trainloader) )
    testacc_list.append( eval_target_net(net2, testloader) ) 
    similarity_list.append( similarity/40 )
fig, ax = plt.subplots(1,3,figsize=(15,5))
ax[0].hist(testacc_list, density=True)
ax[0].set_title('test set acc density (bit_error_percent = 0.1)')
ax[1].hist(accuracy_list, density=True)
ax[1].set_title('train set acc density (bit_error_percent = 0.1)')
ax[2].hist(similarity_list)
ax[2].set_title('pcc similarity between the images')
accuracy_list = []
testacc_list = []
similarity_list = []
ntests = 50
bit_error_percent_list = np.logspace(-6,-2,5,base=2)
for bit_error_percent in bit_error_percent_list:
    accuracy_avg, testacc_avg, similarity_avg = 0,0,0
    for i in range(ntests):
        cur_imgs = torch.zeros(real_imgs.shape)
        similarity = 0
        for k in range(40):
            im0 = ww0[k,:].reshape(1,1,112,92)
            f = ww[k,:]
            f_modify = bin2float_gpu(bin_add_bit_error(float2bin_gpu(f, fixed_exp), bit_error_percent), fixed_exp)
            cur_imgs[k] = f_modify.reshape(1,1,112,92)

            data = cur_imgs[k]/100.0
            data1 = real_imgs[k]/100.0
            # ncc = torch.sum(data*data1)/torch.sqrt(torch.sum(data**2))/torch.sqrt(torch.sum(data1**2))
            # similarity += -0.5*(ncc-1)
            # Pearson Correlation Coefficient
            vdata = data - torch.mean(data)
            vdata1 = data1 - torch.mean(data1)
            pcc = torch.sum(vdata*vdata1)/torch.sqrt(torch.sum(vdata**2))/torch.sqrt(torch.sum(vdata1**2))
            similarity += pcc

            net2.hidden1.weight.data[k,:] = f_modify

        accuracy_avg += eval_target_net(net2, trainloader) 
        testacc_avg  += eval_target_net(net2, testloader) 
        similarity_avg += similarity/40 
    accuracy_list.append(accuracy_avg / ntests)
    testacc_list.append(testacc_avg / ntests)
    similarity_list.append(similarity_avg / ntests)  
fig, ax = plt.subplots(1,1,figsize=(5,5))
ax2 = ax.twinx()
ax.plot(bit_error_percent_list,accuracy_list)
ax.plot(bit_error_percent_list,testacc_list)
ax.set_xlabel('bit error percentage')
ax.set_ylabel('accuracy')
ax.legend(['train acc','test acc'],loc='lower left')
ax2.plot(bit_error_percent_list,similarity_list,'g-')
ax2.set_ylabel('pcc similarity')
ax2.legend(['pcc similarity'],loc='upper right')
np.logspace(-6,-2,5,base=2)
f0 = ww0[39,:]
f = ww[39,:]
bit_error_percent = 0.1
f_modify = bin2float_gpu(bin_add_bit_error(float2bin_gpu(f, fixed_exp), bit_error_percent), fixed_exp)
im0 = f0.reshape(1,1,112,92)
im = f_modify.reshape(1,1,112,92)
plt.imshow((im-im0).squeeze().cpu().detach().numpy(),cmap='gray')
plt.show()
i = 37
im0 = ww0[i,:].reshape(1,1,112,92)
im_train = ww[i,:].reshape(1,1,112,92)
plt.imshow((im_train-im0).squeeze().cpu().detach().numpy(),cmap='gray')
plt.show()
i = 37
im0 = ww0[i,:].reshape(1,1,112,92)
im_train = ww[i,:].reshape(1,1,112,92)
plt.imshow((im_train-im0).squeeze().cpu().detach().numpy(),cmap='gray')
plt.show()
plt.hist(np.absolute(tmp.reshape(-1)),bins=50)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1556181637388134, 'fraction_numerical': 0.044497845541929065, 'mean_word_length': 3.802626343016315, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '438645', 'license': 'Apache-2.0', 'n_tokens_mistral': 4974, 'n_tokens_neox': 4698, 'n_words': 830, 'path': 'Attack_baselines/Model_Inversion/ATT_faces_MI_Attacks-DRAM_bit_error-0819.ipynb', 'repo_name': 'katekemu/mia', 'size': '12068'}