forked from aarathimuppalla/CV_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsn.py
171 lines (136 loc) · 5.56 KB
/
dsn.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Dropout, Activation, Flatten, merge
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D
from keras.utils import np_utils
from keras.constraints import maxnorm
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.optimizers import SGD, Adam
from keras import backend as K
#from keras.utils import np_utils
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo,encoding='latin1')
return dict
def get_similarities(N,labels):
out = [];
for i in range(0,N):
a=np.floor(np.random.rand(2)*len(data))
b=np.array_equal(labels[int(a[1])],labels[int(a[0])])
out.append([int(a[1]),int(a[0]),b])
return out
def generate_batch(out,batch_size,shuffle=False):
while True:
if shuffle:
indices = np.random.permutation(np.arange(len(out)))
else:
indices = np.arange(len(out))
shuffled_triples = [out[ix] for ix in indices]
num_batches = len(shuffled_triples) // batch_size
for j in range(num_batches):
i1, i2, label = [], [], []
batch = out[j * batch_size : (j + 1) * batch_size]
for i in range(0,len(batch)):
i1.append(data[batch[i][0]]);
i2.append(data[batch[i][1]]);
label.append(batch[i][2]);
X1 = np.array(i1)
X2 = np.array(i2)
Y = np_utils.to_categorical(np.array(label), num_classes=2)
yield ([X1, X2], Y)
data_folder = "F:\Aarathi\IIIT\Computer Vision\Project\cifar-10-batches-py"
test_file = "F:\Aarathi\IIIT\Computer Vision\Project\cifar-10-batches-py\test_batch"
#Read image data
for file in os.listdir(data_folder):
if file.endswith(".meta"):
meta_file = os.path.join(data_folder, file)
elif "data_batch_1" in file:
data_batch = os.path.join(data_folder, file)
a = unpickle(data_batch)
data = a["data"]
#print(data.shape)
labels = a["labels"]
elif "data_batch" in file:
data_batch = os.path.join(data_folder, file)
a = unpickle(data_batch)
data = np.concatenate((data,a["data"]),axis=0)
labels = np.concatenate((labels,a['labels']),axis=0)
#print(data.shape)
#print(labels.shape)
#Read Labels
b = unpickle(meta_file)
label_names = b["label_names"]
data=data.reshape((data.shape[0],3,32,32))/255.0;
labels = np_utils.to_categorical(labels, 10);
##for visualization
#print(label_names)
#print(labels[1]);
#im=data[1,:];
#im=im.reshape((3,32,32));
#plt.imshow(im.T)
#plt.show()
#####
# Create the model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(3, 32, 32), padding='same', activation='relu', kernel_constraint=maxnorm(3), strides=1))
#model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=(3, 3), strides=2))
model.add(Conv2D(32, (5, 5), activation='relu', padding='same', kernel_constraint=maxnorm(3),strides=1))
model.add(AveragePooling2D(pool_size=(3, 3), strides=2,dim_ordering="th"))
model.add(Conv2D(64, (5, 5), activation='relu', padding='same', kernel_constraint=maxnorm(3),strides=1))
model.add(AveragePooling2D(pool_size=(3, 3), strides=2,dim_ordering="th"))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
BatchNormalization()
#model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
input_shape=(3, 32, 32);
i1 = Input(input_shape)
i2 = Input(input_shape)
b1 = model(i1);
b2 = model(i2);
L1_distance = lambda x: K.abs(x[0]-x[1])
merge = merge([b1,b2], mode = L1_distance, output_shape=lambda x: x[0])
pred = Dense(2,activation='sigmoid')(merge)
sim_model = Model(input=[i1,i2],output=pred)
def loss_function(b1,b2,y):
alpha = 0.5;
m=0.5;
#b1=X[0];
#b2=X[1];
euc_dist=(K.sqrt(K.dot(b1,b2)))
Loss = 0.5*(1-y)*euc_dist+0.5*y*K.max(m-euc_dist,0)+alpha*K.abs(K.subtract(b1,1))+alpha*K.abs(K.subtract(b2,1))
return Loss
sim_model.compile(loss='binary_crossentropy',optimizer=Adam(0.0006),metrics=["accuracy"])
##Loss function given in Paper
#sim_model.compile(loss=loss_function,optimizer=Adam(),metrics=["accuracy"])
data_sim = get_similarities(20000,labels) #increase for better results
BATCH_SIZE = 30
split_point = int(len(data_sim) * 0.7)
data_sim_train, data_sim_test = data_sim[0:split_point], data_sim[split_point:]
NUM_EPOCHS=10 #increase for better results
train_gen = generate_batch(data_sim_train, BATCH_SIZE,shuffle=True)
val_gen = generate_batch(data_sim_test, BATCH_SIZE,shuffle=False)
num_train_steps = len(data_sim_train) // BATCH_SIZE
num_val_steps = len(data_sim_test) // BATCH_SIZE
model_out = sim_model.fit_generator(train_gen,
steps_per_epoch=num_train_steps,
epochs=NUM_EPOCHS,
validation_data=val_gen,
validation_steps=num_val_steps)
plt.title("Loss")
plt.plot(model_out.history["loss"], color="r", label="train")
plt.plot(model_out.history["val_loss"], color="b", label="validation")
plt.legend(loc="best")
plt.tight_layout()
plt.show()
plt.title("Accuracy")
plt.plot(model_out.history["acc"], color="r", label="train")
plt.plot(model_out.history["val_acc"], color="b", label="validation")
plt.legend(loc="best")
plt.tight_layout()
plt.show()