-
Notifications
You must be signed in to change notification settings - Fork 2
/
input_generator.py
72 lines (61 loc) · 2.25 KB
/
input_generator.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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 26 14:45:46 2016
@author: lenovo
"""
import numpy as np
import pickle
import random
from prepare_dataset import image_to_array
from keras.utils import np_utils
import pdb
dir_path = '/home/ubuntu/dataset/market1501/boundingboxtrain'
the_filename = 'data_by_path.pkl'
def load_from_file(filename):
with open(filename, 'rb') as f:
var = pickle.load(f)
return var
class DataSet(object):
def __init__(self, data, batch_size=128, random_shuffle=True):
self._data = data
self.batch_size = batch_size
if random_shuffle:
random.shuffle(self._data)
self._index_in_epoch = 0
self._data_num = len(self._data)
self._epochs_completed = 0
self._num_batch = self._data_num // self.batch_size
@property
def data(self):
return self._data
@property
def data_num(self):
return self._data_num
@property
def epochs_completed(self):
return self._epochs_completed
def next_batch(self):
while(1):
start = self._index_in_epoch
self._index_in_epoch += self.batch_size
if self._index_in_epoch > self._data_num:
# Finished epoch
self._epochs_completed += 1
# Shuffle the data
random.shuffle(self._data)
# Start next epoch
start = 0
self._index_in_epoch = self.batch_size
assert self.batch_size <= self._num_examples
end = self._index_in_epoch
in_path1 = [c[0] for c in self._data[start:end]]
in_path2 = [c[1] for c in self._data[start:end]]
in_put1 = np.array([image_to_array(dir_path+'/'+c) for c in in_path1])
in_put2 = np.array([image_to_array(dir_path+'/'+c) for c in in_path2])
label = np_utils.to_categorical(np.array([c[2] for c in self._data[start:end]]))
yield ([in_put1, in_put2], label)
if __name__ == '__main__':
dataset = load_from_file(the_filename)
market = DataSet(dataset, 10)
a, b = market.next_batch()
pdb.set_trace()