-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare_cifar100.py
49 lines (43 loc) · 1.69 KB
/
prepare_cifar100.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
import tensorflow as tf
from tensorflow.keras import datasets
import argparse
from PIL import Image
import os
import pdb
from tqdm import tqdm
parser = argparse.ArgumentParser()
parser.add_argument('--save_dir', required=True, type=str)
args = parser.parse_args()
save_dir = str(args.save_dir)
if not os.path.exists(save_dir):
os.mkdir(save_dir)
if not os.path.exists(save_dir, 'cifar100'):
os.mkdir(save_dir)
if not os.path.exists(os.path.join(save_dir, 'cifar100', 'train_images')):
os.mkdir(os.path.join(save_dir, 'cifar100', 'train_images'))
if not os.path.exists(os.path.join(save_dir, 'cifar100', 'test_images')):
os.mkdir(os.path.join(save_dir, 'cifar100', 'test_images'))
if not os.path.exists(os.path.join(save_dir, 'cifar100', 'val_images')):
os.mkdir(os.path.join(save_dir, 'cifar100', 'val_images'))
(train_images, train_labels), (test_images, test_labels) = datasets.cifar100.load_data()
X_train = train_images[:45000]
Y_train = train_labels[:45000]
X_val = train_images[45000:]
Y_val = train_labels[45000:]
X_test = test_images
Y_test = test_labels
count = 0
for i in tqdm(range(X_train.shape[0])):
im = Image.fromarray(X_train[i])
im.save(os.path.join(save_dir, 'cifar100', 'train_images', "%d_%d.png" % (Y_train[i,0], count)))
count += 1
count = 0
for i in tqdm(range(X_val.shape[0])):
im = Image.fromarray(X_val[i])
im.save(os.path.join(save_dir, 'cifar100', 'val_images', "%d_%d.png" % (Y_val[i,0], count)))
count += 1
count = 0
for i in tqdm(range(X_test.shape[0])):
im = Image.fromarray(X_test[i])
im.save(os.path.join(save_dir, 'cifar100', 'test_images', "%d_%d.png" % (Y_test[i,0], count)))
count += 1