-
Notifications
You must be signed in to change notification settings - Fork 8
/
Privacy_vs_Utility_check.py
147 lines (127 loc) · 4.69 KB
/
Privacy_vs_Utility_check.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from __future__ import print_function
from keras.layers import Dense, Dropout, Flatten,GlobalAveragePooling2D, Activation, Input, Reshape
from keras.optimizers import SGD,Adagrad,RMSprop, Adam
from keras.models import Model
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.callbacks import ModelCheckpoint, EarlyStopping,History, LearningRateScheduler
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import plot_model
from keras.layers.advanced_activations import LeakyReLU
import time
######################################### dataset parameters ###########################################
data_augmentation = False
# #
batch_size = 256
num_ep = 7
num_pp = 6
epochs = 10
img_rows, img_cols = 64, 64
clipvalue = 20
noise_dim = 10
c_dim = num_pp
n_dim = 10
date = 1205
####### Choose your task #######
# Task = 'ExpRec'
Task = 'Identification'
###############################
### Load your data, either f(I) or I ###
########################################
############ CNN model(for I) #################
d0 = Input((x_train.shape[1:]))
# x0 = Dense(img_rows*img_cols*1, activation = 'relu')(d0)
# x0 = Reshape((img_rows,img_cols,1))(x0)
x = Conv2D(32, (5,5), padding = 'same', name = 'id_conv1')(d0)
x = LeakyReLU(0.2)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((3, 3), padding='same', strides = (2,2))(x)
x = Dropout(0)(x)
x = Conv2D(32, (5,5), padding = 'same', name = 'id_conv2')(x)
x = LeakyReLU(0.2)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((3, 3), padding='same', strides = (2,2))(x)
x = Dropout(0)(x)
x = Conv2D(32, (3,3), padding = 'same', name = 'id_conv3')(x)
x = LeakyReLU(0.2)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((3, 3), padding='same', strides = (2,2))(x)
x = Dropout(0)(x)
x = Conv2D(32, (3,3), padding = 'same', name = 'id_conv4')(x)
x = LeakyReLU(0.2)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((3, 3), padding='same', strides = (2,2))(x)
x = Dropout(0)(x)
x = Flatten()(x)
x = Dense(32, name = 'id_dense1')(x)
x = LeakyReLU(0.2)(x)
x = Dropout(0.5)(x)
if Task == 'Identification':
output_main = Dense(num_pp, activation = 'softmax', name = 'id_dense2')(x)
elif Task == 'ExpRec':
output_main = Dense(num_ep, activation = 'softmax', name = 'id_dense2')(x)
model = Model(d0,output_main)
#### ANN model(for f(I))####
d0 = Input((x_train.shape[1:]))
# d0 = Flatten()(d0)
x = Dense(128, name = 'id_dense1')(d0)
x = LeakyReLU(0.2)(x)
x = Dropout(0.25)(x)
x = Dense(128, name = 'id_dense2')(x)
x = LeakyReLU(0.2)(x)
x = Dropout(0.25)(x)
x = Dense(128, name = 'id_dense3')(x)
x = LeakyReLU(0.2)(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
if Task == 'Identification':
output_main = Dense(num_pp, activation = 'softmax', name = 'id_dense4')(x)
elif Task == 'ExpRec':
output_main=Dense(num_ep, activation = 'softmax', name = 'id_dense4')(x)
model = Model(d0,output_main)
#############################################
# initiate optimizer
# opt = keras.optimizers.SGD(lr=0.001, decay=1e-6)
adam = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=1e-8)
sgd = SGD(lr=0.005, momentum=0.9, decay=1e-7, nesterov=True)
model.compile(loss= ['categorical_crossentropy'], #'categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
model.summary()
# plot_model(model, to_file = 'ANN_model.png')
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# model_filename = '/home/vivo/Dropbox/Encrypted_video_classification/ImageSharing-master/DES_encrypted_ixmas/model/0721.{epoch:02d}-{val_loss:.2f}.h5'
callbacks = [
EarlyStopping(monitor='loss',
patience=100,
verbose=1,
mode='auto'),
ModelCheckpoint(filepath='/loc.h5',
monitor='val_acc',
save_best_only=True),
History(),
]
# plot_model(model, to_file='de_id_mse_model.png',show_shapes = True)
start = time.time()
count0 = 0
history = model.fit(x_train, y_train1,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test ,y_test1),
shuffle=True, callbacks = callbacks)
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title(Task)
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
# plt.show()
plt.savefig('/media/vivo/New Volume/FERG_DB_256/VAE_GAN_stats/vaegan_FERG_color_train_on_ori_'+Task+'_'+str(date)+'.png')
plt.close()
finish_time = time.time()
print("Elapsed: %s " % (finish_time - start) )