MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / SVHNDigit

Class SVHNDigit

tensorpack/dataflow/dataset/svhn.py:17–76  ·  view source on GitHub ↗

`SVHN `_ Cropped Digit Dataset. Produces [img, label], img of 32x32x3 in range [0,255], label of 0-9

Source from the content-addressed store, hash-verified

15
16
17class SVHNDigit(RNGDataFlow):
18 """
19 `SVHN <http://ufldl.stanford.edu/housenumbers/>`_ Cropped Digit Dataset.
20 Produces [img, label], img of 32x32x3 in range [0,255], label of 0-9
21 """
22 _Cache = {}
23
24 def __init__(self, name, data_dir=None, shuffle=True):
25 """
26 Args:
27 name (str): 'train', 'test', or 'extra'.
28 data_dir (str): a directory containing the original {train,test,extra}_32x32.mat.
29 shuffle (bool): shuffle the dataset.
30 """
31 self.shuffle = shuffle
32
33 if name in SVHNDigit._Cache:
34 self.X, self.Y = SVHNDigit._Cache[name]
35 return
36 if data_dir is None:
37 data_dir = get_dataset_path('svhn_data')
38 assert name in ['train', 'test', 'extra'], name
39 filename = os.path.join(data_dir, name + '_32x32.mat')
40 if not os.path.isfile(filename):
41 url = SVHN_URL + os.path.basename(filename)
42 logger.info("File {} not found!".format(filename))
43 logger.info("Downloading from {} ...".format(url))
44 download(url, os.path.dirname(filename))
45 logger.info("Loading {} ...".format(filename))
46 data = scipy.io.loadmat(filename)
47 self.X = data['X'].transpose(3, 0, 1, 2)
48 self.Y = data['y'].reshape((-1))
49 self.Y[self.Y == 10] = 0
50 SVHNDigit._Cache[name] = (self.X, self.Y)
51
52 def __len__(self):
53 return self.X.shape[0]
54
55 def __iter__(self):
56 n = self.X.shape[0]
57 idxs = np.arange(n)
58 if self.shuffle:
59 self.rng.shuffle(idxs)
60 for k in idxs:
61 # since svhn is quite small, just do it for safety
62 yield [self.X[k], self.Y[k]]
63
64 @staticmethod
65 def get_per_pixel_mean(names=('train', 'test', 'extra')):
66 """
67 Args:
68 names (tuple[str]): names of the dataset split
69
70 Returns:
71 a 32x32x3 image, the mean of all images in the given datasets
72 """
73 for name in names:
74 assert name in ['train', 'test', 'extra'], name

Callers 2

get_per_pixel_meanMethod · 0.85
svhn.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…