-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
61 lines (54 loc) · 1.6 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
54
55
56
57
58
59
60
61
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
from keras.datasets import cifar10
flags = tf.app.flags
FLAGS = flags.FLAGS
class BatchGenerator():
def __init__(self, images, labels):
labels_ = []
np.random.seed(0)
random.seed(0)
self.labels = labels
print("Labels: ", len(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
def get_cifar():
return cifar10.load_data()
def get_fashion():
fashion = input_data.read_data_sets('data/fashion', source_url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/')
return fashion