-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmnist6.py
63 lines (51 loc) · 1.64 KB
/
fmnist6.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
import numpy as np
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data.dataset import Dataset
import matplotlib.pyplot as plt
import numpy as np
def load_data():
train_dataset = datasets.FashionMNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = datasets.FashionMNIST(root='./data',
train=False,
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=100,
shuffle=False)
test_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=1,
shuffle=False)
return train_loader, test_loader
def sort_by_first(lst):
'''
Sort a list lst of triples by the first element x of it, such as (x, i, j).
Return a set of index tuple (i, j) in the triple lst.
'''
sort_lst = lst.sort()
result = []
for x in sort_lst:
result.append(x[0]
return result
def main():
train_loader, test_loader = load_data()
image = train_loader.dataset[200][0]
mask = torch.zeros(image.size())
mask[:,20,:] = 1
mask[:,21,:] = 1
mask[:,22,:] = 1
print(mask.size())
image = image*mask
show_img = image.numpy().reshape(28, 28)
plt.imshow(show_img, cmap='gray')
plt.show()
if __name__ == '__main__':
# main()