-
Notifications
You must be signed in to change notification settings - Fork 0
/
0_prepare_dataset.py
executable file
·105 lines (73 loc) · 2.45 KB
/
0_prepare_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import shutil
import torch
import torchvision
import torchvision.datasets as dset
import torchvision.transforms as transf
from PIL import Image
import mnist
def create_dir(directory):
if os.path.exists(directory):
shutil.rmtree(directory)
os.makedirs(directory)
def prepare_cifar10():
print('[CIFAR10] Preparing dataset ...')
# setting
dir_image_output = './dataset/cifar10/'
# clean and create dir if any
create_dir(dir_image_output)
# save images from pytorch dataset
dset_train = dset.CIFAR10('./dataset/others', train=True, download=True)
print('[CIFAR10] The number of images: ', len(dset_train))
cnt = 1
for img,label in dset_train:
#img = img.convert('RGB')
img.save(dir_image_output + 'img_{}.jpg'.format(cnt))
cnt += 1
print('[CIFAR10] Preparing dataset was completed.')
pass
def prepare_mnist():
print('[MNIST] Preparing dataset ...')
# setting
dir_image_output = './dataset/mnist/'
# clean and create dir if any
create_dir(dir_image_output + 'all')
create_dir(dir_image_output + 'classes')
for i in range(10):
create_dir(dir_image_output + 'classes/{}'.format(i))
train_images = mnist.train_images()
train_labels = mnist.train_labels()
for i in range(train_images.shape[0]):
img = train_images[i,:,:]
label = train_labels[i]
img = Image.fromarray(img).convert('RGB').resize((32,32))
img.save(dir_image_output + 'all/img_{}.jpg'.format(i))
img.save(dir_image_output + 'classes/{}/img_{}.jpg'.format(label, i))
print('[MNIST] Preparing dataset was completed.')
pass
def prepare_stl10():
print('[STL10] Preparing dataset ...')
# setting
dir_image_output = './dataset/stl10/'
# clean and create dir if any
create_dir(dir_image_output)
# save images from pytorch dataset
trans = transf.Compose([
transf.Resize((48, 48)),
])
dset_train = dset.STL10('./dataset/others', split='train', download=True, transform=trans)
print('[STL10] The number of images: ', len(dset_train))
cnt = 1
for img,label in dset_train:
#img = img.convert('RGB')
img.save(dir_image_output + 'img_{}.jpg'.format(cnt))
cnt += 1
print('[STL10] Preparing dataset was completed.')
pass
def prepare_cat():
pass
if __name__ == '__main__':
prepare_cifar10()
prepare_mnist()
prepare_stl10()
#prepare_cat()