[5a7589]: / scripts / subsample.py

Download this file

159 lines (128 with data), 4.5 kB

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
import os
import sys
import json
import numpy as np
from PIL import Image
from pycocotools.mask import encode, decode
def get_outfile(infile, outdir):
outfile = os.path.basename(infile)
outsubdir = os.path.basename(os.path.dirname(infile))
outfile = os.path.join(outdir, outsubdir, outfile)
return outfile
def filegen(indir, ext='png'):
for dd in os.scandir(indir):
if not os.path.isdir(dd.path):
continue
for ff in os.scandir(dd.path):
if not ff.name.endswith(ext):
continue
yield ff.path
def subsample_coco_mask(mask, factor):
mask = Image.fromarray(mask)
sz = np.asarray(mask.size)
out_sz = (sz / factor).astype(int)
mask.thumbnail(size, Image.ANTIALIAS)
mask = np.asarray(mask, order='F')
return mask
def subsample_verts(verts, factor):
verts = (np.asarray(verts)//factor).tolist()
vvprev = [None, None]
newverts = []
for vv in verts:
if np.all(vv == vvprev):
continue
else:
newverts.append(vv)
vvprev = vv.copy()
return newverts
def subsample_roi(fn, fnout, factor):
with open(fn) as fh:
rois = json.load(fh)
if len(rois) == 0:
print("NO ROIS IN\t%s" % fn)
for roi in rois:
mask = decode(roi)
mask = subsample_coco_mask(mask, factor)
cocomask = encode(mask)
cocomask['counts'] = cocomask['counts'].decode()
roi.update(cocomask)
verts = roi["vertices"]
roi.update({"vertices": subsample_verts(verts, factor)})
if 'zoom' in roi:
roi.pop('zoom')
with open(fnout, 'w+') as fh:
json.dump(rois, fh)
####################################################################
if __name__ == '__main__':
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'indir', type=str, help = 'input directory')
parser.add_argument(
'--outdir',
default='',
type=str,
help = 'output directory')
parser.add_argument(
'--original-side',
type=int,
default=1024,
help='Original side (in pixels) of the image patches')
parser.add_argument('--img', dest='img', action='store_true')
parser.add_argument('--no-img', dest='img', action='store_false')
parser.set_defaults(img=True)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--target-side',
type=int,
default=512,
help='Requested side (in pixels) of the image patches')
group.add_argument(
'--factor',
type=int,
default=None,
help='Requested subsampling rate')
prms = parser.parse_args()
if prms.factor is None:
factor = prms.original_side // prms.target_side
side = prms.target_side
else:
factor = prms.factor
side = prms.original_side // prms.factor
size = side, side
if len(prms.outdir)==0:
basedir, set_ = os.path.split(prms.indir.rstrip('/'))
basedir = os.path.dirname(basedir)
#basedir = os.path.dirname(basedir)
outdir = "{}/data_{}_subsample_{:d}x/fullsplit/all".format(basedir, side, factor)
else:
outdir = prms.outdir
print("SAVING TO:", outdir, sep='\t')
os.makedirs(outdir, exist_ok = True)
if prms.img:
print("SUBSAMPLING IMAGES")
for infile in filegen(prms.indir):
outfile = get_outfile(infile, outdir=outdir)
os.makedirs(os.path.dirname(outfile), exist_ok=True)
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "png")
except IOError as ee:
print( "cannot create thumbnail for '%s'" % infile)
print(ee)
print("SUBSAMPLING MASKS")
for infile in filegen(prms.indir, ext = '.json'):
outfile = get_outfile(infile, outdir=outdir)
if infile != outfile:
try:
subsample_roi(infile, outfile, factor)
except IOError as ee:
print( "cannot create thumbnail for '%s'" % infile)
print(ee)
except Exception as ee:
print( "cannot create thumbnail for '%s'" % infile)
raise ee