-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
61 lines (48 loc) · 1.83 KB
/
data.py
File metadata and controls
61 lines (48 loc) · 1.83 KB
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
import numpy as np
import os
import csv
import torch as T
# ------------------------------------------------------------------------------
DATA_DIR = '' # path to folder that contains mnist_train.csv & mnist_test.csv
# ------------------------------------------------------------------------------
def load(train_or_test, num_samples=None, num_classes=10):
""" Load MNIST data from csv.
:param train_or_test: 'train' / 'test'
:param num_samples: number of samples to get (None = all)
:param num_classes: number of classes to use (None = all)
:return:
imgs - (#samples, 28, 28, 1) float (0-1) tensor
labels - (#samples, #classes) float one hot
"""
fname = DATA_DIR + os.path.sep + 'mnist_' + train_or_test + '.csv'
assert os.path.isfile(fname), "CSV not found in data dir"
imgs, labels = [], []
with open(fname) as f:
reader = csv.reader(f, delimiter=',')
next(reader)
for row in reader:
label = int(row[0])
if label >= num_classes:
continue
img = np.array([int(x) for x in row[1:]]).reshape((28, 28, 1))
imgs.append(img)
labels.append(label)
if num_samples is not None and len(imgs) == num_samples:
break
imgs = T.tensor(imgs, dtype=T.float32) / 255
labels = T.nn.functional.one_hot(T.tensor(labels)).to(T.float32)
return imgs, labels
def _show(imgs, labels):
import matplotlib.pyplot as plt
for i, img in enumerate(imgs):
plt.imshow(img[...,0], cmap='gray')
plt.title('label=' + str(int(T.argmax(labels[i]))))
plt.draw()
try:
plt.waitforbuttonpress()
except:
break
if __name__ == "__main__":
# test
imgs, labels = load('train', num_samples=50, num_classes=10)
_show(imgs, labels)