Skip to content

Commit d840b81

Browse files
committed
Always handle input scaling
1 parent 29598c3 commit d840b81

1 file changed

Lines changed: 13 additions & 34 deletions

File tree

‎src/modules/ObjectDetectionCoral/tpu_runner.py‎

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from datetime import datetime
2626

2727
import numpy as np
28-
from PIL import Image
28+
from PIL import Image, ImageOps
2929

3030
try:
3131
from pycoral.utils.dataset import read_label_file
@@ -1145,58 +1145,37 @@ def _resize_and_chop_tiles(self,
11451145

11461146
logging.debug("Resizing to {} x {} for tiling".format(resamp_x, resamp_y))
11471147

1148-
# From: https://uploadcare.com/blog/fast-import-of-pillow-images-to-numpy-opencv-arrays/
1149-
def to_numpy(im, shape, typestr):
1150-
# unpack data
1151-
e = Image._getencoder(im.mode, 'raw', im.mode)
1152-
e.setimage(im.im)
1153-
1154-
# NumPy buffer for the result
1155-
data = np.empty(shape, dtype=typestr)
1156-
mem = data.data.cast('B', (data.data.nbytes,))
1157-
1158-
bufsize, s, offset = 65536, 0, 0
1159-
while not s:
1160-
l, s, d = e.encode(bufsize)
1161-
mem[offset:offset + len(d)] = d
1162-
offset += len(d)
1163-
if s < 0:
1164-
raise RuntimeError("encoder error %d in tobytes" % s)
1165-
return data
1166-
11671148
# Chop & resize image piece
11681149
if image.mode != 'RGB':
11691150
image = image.convert('RGB')
11701151
image.thumbnail((resamp_x, resamp_y), Image.LANCZOS)
1171-
1152+
1153+
# Rescale the input from uint8
1154+
input_zero = self.input_details['quantization'][1]
1155+
input_scale = 1.0 / (255.0 * self.input_details['quantization'][0])
1156+
11721157
# It'd be useful to print this once at the beginning of the run
11731158
key = "{} {}".format(*image.size)
11741159
if key not in self.printed_shape_map:
11751160
logging.info(
11761161
"Mapping {} image to {}x{} tiles".format(image.size, tiles_x, tiles_y))
11771162
self.printed_shape_map[key] = True
11781163

1179-
input_zero = self.input_details['quantization'][1]
1180-
input_scale = 1.0 / (256.0 * self.input_details['quantization'][0])
1181-
11821164
# Do chunking
11831165
tiles = []
11841166
for x_off in range(0, resamp_x - options.tile_overlap, m_width - options.tile_overlap):
11851167
for y_off in range(0, resamp_y - options.tile_overlap, m_height - options.tile_overlap):
1186-
cropped_arr = to_numpy(image.crop((x_off,
1187-
y_off,
1188-
x_off + m_width,
1189-
y_off + m_height)), self.input_details['shape'][1:], np.uint8)
1168+
image_chunk = ImageOps.autocontrast(image.crop((x_off,
1169+
y_off,
1170+
x_off + m_width,
1171+
y_off + m_height)), 1)
1172+
# Normalize to whatever the input is
1173+
cropped_arr = np.asarray(image_chunk, np.float32) * input_scale + input_zero
11901174

11911175
logging.debug("Resampled image tile {} at offset {}, {}".format(cropped_arr.shape, x_off, y_off))
11921176
resamp_info = (x_off, y_off, i_width/image.width, i_height/image.height)
11931177

1194-
# Normalize from 8-bit image to whatever the input is
1195-
if self.input_details['dtype'] == np.int8:
1196-
cropped_arr = cropped_arr * input_scale + input_zero
1197-
tiles.append((cropped_arr.astype(np.int8), resamp_info))
1198-
else:
1199-
tiles.append((cropped_arr, resamp_info))
1178+
tiles.append((cropped_arr.astype(self.input_details['dtype']), resamp_info))
12001179

12011180
return tiles
12021181

0 commit comments

Comments
 (0)