forked from Kashu7100/Qualia2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfashion_mnist.py
92 lines (82 loc) · 3.11 KB
/
fashion_mnist.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
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
from .. import to_cpu
from ..core import *
from .dataset import *
from .transforms import Compose, ToTensor, Normalize
import matplotlib.pyplot as plt
import gzip
class FashionMNIST(Dataset):
'''FashionMNIST Dataset\n
Args:
train (bool): if True, load training dataset
transforms (transforms): transforms to apply on the features
target_transforms (transforms): transforms to apply on the labels
Shape:
- data: [N, 1, 28, 28]
'''
def __init__(self, train=True,
transforms=Compose([ToTensor(), Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])]),
target_transforms=None):
super().__init__(train, transforms, target_transforms)
def __len__(self):
if self.train:
return 60000
else:
return 10000
def state_dict(self):
return {
'label_map': fashion_mnist_labels
}
def prepare(self):
url = 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/'
files = {
'train_data.gz':'train-images-idx3-ubyte.gz',
'train_labels.gz':'train-labels-idx1-ubyte.gz',
'test_data.gz':'t10k-images-idx3-ubyte.gz',
'test_labels.gz':'t10k-labels-idx1-ubyte.gz'
}
for filename, value in files.items():
self._download(url+value, filename)
if self.train:
self.data = self._load_data(self.root+'/train_data.gz')
self.label = FashionMNIST.to_one_hot(self._load_label(self.root+'/train_labels.gz'), 10)
else:
self.data = self._load_data(self.root+'/test_data.gz')
self.label = FashionMNIST.to_one_hot(self._load_label(self.root+'/test_labels.gz'), 10)
def _load_data(self, filename):
with gzip.open(filename, 'rb') as file:
if gpu:
import numpy
data = np.asarray(numpy.frombuffer(file.read(), np.uint8, offset=16))
else:
data = np.frombuffer(file.read(), np.uint8, offset=16)
return data.reshape(-1,1,28,28)
def _load_label(self, filename):
with gzip.open(filename, 'rb') as file:
if gpu:
import numpy
labels = np.asarray(numpy.frombuffer(file.read(), np.uint8, offset=8) )
else:
labels = np.frombuffer(file.read(), np.uint8, offset=8)
return labels
def show(self, row=10, col=10):
H, W = 28, 28
img = np.zeros((H*row, W*col))
for r in range(row):
for c in range(col):
img[r*H:(r+1)*H, c*W:(c+1)*W] = self.data[random.randint(0, len(self.data)-1)].reshape(H,W)
plt.imshow(to_cpu(img) if gpu else img, cmap='gray', interpolation='nearest')
plt.axis('off')
plt.show()
fashion_mnist_labels = {
0: 'T-shirt/top',
1: 'Trouser',
2: 'Pullover',
3: 'Dress',
4: 'Coat',
5: 'Sandal',
6: 'Shirt',
7: 'Sneaker',
8: 'Bag',
9: 'Ankle boot'
}