-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
80 lines (55 loc) · 2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# -*- coding_utf-8 -*-
# Copy from https://blog.csdn.net/missyougoon/article/details/86549404
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import pickle
from imgaug import augmenters as iaa
import tensorflow as tf
import cv2
import os
class CifarData:
def __init__( self, filenames, need_shuffle ):
all_data = []
all_labels = []
for filename in filenames:
data, labels = self.load_data(filename)
all_data.append(data)
all_labels.append(labels)
self._data = np.vstack(all_data)
self._data = self._data / 255.
self._labels = np.hstack( all_labels )
self._num_data = self._data.shape[0]
self._need_shuffle = need_shuffle
self._indicator = 0
if self._need_shuffle:
self._shffle_data()
def load_data(self, filename):
with open(filename, 'rb') as f:
data = pickle.load(f, encoding='bytes')
return data[b'data'], data[b'labels']
def data_aug(self, img):
seq = iaa.SomeOf((1,3), [
iaa.Fliplr(1.0),
iaa.GaussianBlur(0.5),
iaa.Sharpen(alpha=0.5)
],random_order=True)
return seq.augment_image(img)
def _shffle_data( self ):
p = np.random.permutation( self._num_data )
self._data = self._data[p]
self._labels = self._labels[p]
def next_batch( self, batch_size):
'''return batch_size example as a batch'''
end_indictor = self._indicator + batch_size
if end_indictor > self._num_data:
if self._need_shuffle:
self._shffle_data()
self._indicator = 0
end_indictor = batch_size
batch_data = self._data[self._indicator:end_indictor]
batch_labels = self._labels[self._indicator:end_indictor]
self._indicator = end_indictor
return batch_data, batch_labels