-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize.py
49 lines (37 loc) · 1.34 KB
/
resize.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
import os
import time
from torchvision import transforms as T
from torchvision.datasets import ImageFolder
from multiprocessing import pool
from multiprocessing.dummy import Pool as ThreadPool
from util import pil_loader
in_root = r"C:\Users\LUKE_SARGEN\projects\classifier\data\subset"
out_root = r"C:\Users\LUKE_SARGEN\projects\classifier\data\subset320"
out_size = int(320)
threads = 4
train_ds = ImageFolder(root=in_root)
# Make output folders
if not os.path.exists(out_root):
os.mkdir(out_root)
out_folders = [] # Path to destination folder
for c in train_ds.classes:
c_path = os.path.join(out_root, c)
out_folders.append(c_path)
if not os.path.exists(c_path):
os.mkdir(c_path)
def resize_save(idx):
path, target = train_ds.samples[idx]
name = os.path.split(path)[1] # get the file path, split, get filename w extension
out = os.path.join(out_root, train_ds.classes[target], name)
if not os.path.exists(out):
img = pil_loader(path)
x = T.Resize(out_size)(img)
x.save(out)
start_time = time.time()
print(" * Starting resize...")
pool = ThreadPool(threads)
pool.map(resize_save, list(range(len(train_ds))))
duration = time.time() - start_time
print(" * Resize Complete")
print(" * Duration {:.2f} Seconds".format(duration))
print(" * {:.2f} Images per Second".format(len(train_ds)/duration))