-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize_inat.py
80 lines (51 loc) · 1.91 KB
/
resize_inat.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
import torch
# import cv2
import matplotlib.pyplot as plt
import numpy as np
# import lmdb
import os
import json
import math
import PIL
from config import dset_root
from shutil import copyfile
from random import shuffle
'''
base_file_list = ['./filelists/metaiNat/base.json',
'./filelists/metaiNat/val.json',
'./filelists/metaiNat/novel.json',
'./filelists/metaiNat/novel_train.json',
'./filelists/metaiNat/novel_test.json']
'''
all_json = ['train2018.json', 'val2018.json', 'test2018.json']
to_size = 448
output_root = dset_root['inat'].replace('inat_2018', 'inat_2018_%d'%to_size)
for json_file in all_json:
# for base_file in base_file_list:
base_file = os.path.join(dset_root['inat'], json_file)
with open(base_file, 'r') as f:
meta = json.load(f)
folder_name = set([os.path.dirname(x['file_name']) for x in meta['images']])
for x in folder_name:
fpath = os.path.join(output_root, x)
if not os.path.isdir(fpath):
os.makedirs(fpath)
'''
lmdb_file, _ = os.path.splitext(base_file)
base_name = os.path.basename(lmdb_file)
dataset = SimpleDataset(base_file, None)
'''
num_img = len(meta['images'])
shuffle(meta['images'])
for idx, x in enumerate(meta['images']):
if (idx + 1) % 1000 == 0:
print('%s: %d / %d'%(json_file.split('.')[0], idx+1, num_img))
out_name = os.path.join(output_root, x['file_name'])
if os.path.isfile(out_name):
continue
im = PIL.Image.open(os.path.join(dset_root['inat'], x['file_name']))
ratio = to_size / min(im.size)
resize_to = tuple([math.ceil(y*ratio) for y in im.size])
resizeImg = im.resize(resize_to, resample=PIL.Image.BILINEAR)
resizeImg.save(out_name)
# copyfile(base_file, os.path.join(output_root, json_file))