Skip to content

Commit

Permalink
Use float16 always instead of doing 32 -> 16 -> 32 (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pbatch authored Jun 23, 2024
1 parent 73b11f2 commit eb916ac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
9 changes: 2 additions & 7 deletions clashroyalebuildabot/detectors/number_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ def _post_process(self, pred):
return clean_pred

def _preprocess(self, image):
image = self.resize(image)
image = np.array(image, dtype=np.float32)
image, padding = self.pad(image)
image = image.transpose(2, 0, 1)
image /= 255
image, padding = self.resize_pad_transpose_and_scale(image)
return image, padding

def run(self, image):
Expand All @@ -86,8 +82,7 @@ def run(self, image):
crops.append(crop)
paddings.append(padding)

crops = np.array(crops, dtype=np.float16)
preds = self._infer(crops).astype(np.float32)
preds = self._infer(crops)

for i, padding in enumerate(paddings):
preds[i] = self.fix_bboxes(
Expand Down
8 changes: 8 additions & 0 deletions clashroyalebuildabot/detectors/onnx_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def pad(self, x):
)
return x, padding

def resize_pad_transpose_and_scale(self, image):
image = self.resize(image)
image = np.array(image, dtype=np.float16)
image, padding = self.pad(image)
image = image.transpose(2, 0, 1)
image /= 255
return image, padding

def fix_bboxes(self, x, width, height, padding):
x[:, [0, 2]] -= padding[0]
x[:, [1, 3]] -= padding[2]
Expand Down
9 changes: 3 additions & 6 deletions clashroyalebuildabot/detectors/unit_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ def _preprocess(self, image):
self.UNIT_Y_END * image.height,
)
)
image = self.resize(image)
image = np.array(image, dtype=np.float32)
image, padding = self.pad(image)
image = np.expand_dims(image.transpose(2, 0, 1), axis=0)
image /= 255
image, padding = self.resize_pad_transpose_and_scale(image)
image = np.expand_dims(image, axis=0)
return image, padding

def _post_process(self, pred, height, image):
Expand Down Expand Up @@ -99,7 +96,7 @@ def _post_process(self, pred, height, image):
def run(self, image):
height, width = image.height, image.width
np_image, padding = self._preprocess(image)
pred = self._infer(np_image.astype(np.float16)).astype(np.float32)[0]
pred = self._infer(np_image)[0]
pred = pred[pred[:, 4] > self.MIN_CONF]
pred = self.fix_bboxes(pred, width, height, padding)
pred = self._post_process(pred, height, image)
Expand Down

0 comments on commit eb916ac

Please sign in to comment.