forked from ardiya/siamesenetwork-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
53 lines (46 loc) · 1.32 KB
/
dataset.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
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import tensorflow.contrib.slim as slim
import random
from tensorflow.examples.tutorials.mnist import input_data
from numpy.random import choice, permutation
from itertools import combinations
flags = tf.app.flags
FLAGS = flags.FLAGS
class BatchGenerator():
def __init__(self, images, labels):
np.random.seed(0)
random.seed(0)
self.labels = labels
print images.shape
self.images = images.reshape((55000, 28, 28, 1))
self.tot = len(labels)
self.i = 5
self.num_idx = dict()
for idx, num in enumerate(self.labels):
if num in self.num_idx:
self.num_idx[num].append(idx)
else:
self.num_idx[num] = [idx]
self.to_img = lambda x: self.images[x]
def next_batch(self, batch_size):
left = []
right = []
sim = []
# genuine
for i in range(10):
n = 45
l = choice(self.num_idx[i], n*2, replace=False).tolist()
left.append(self.to_img(l.pop()))
right.append(self.to_img(l.pop()))
sim.append([1])
#impostor
for i,j in combinations(range(10), 2):
left.append(self.to_img(choice(self.num_idx[i])))
right.append(self.to_img(choice(self.num_idx[j])))
sim.append([0])
return np.array(left), np.array(right), np.array(sim)
def get_mnist():
mnist = input_data.read_data_sets("MNIST_data/")
return mnist