def local_eval(func, model, image_size, test_path, name_path, verbose): tmp_path = os.path.join('tmp' + time.strftime("%Y%m%d%H%M", time.localtime())) if os.path.exists(tmp_path): os.remove(tmp_path) with open(test_path) as f: lines = f.readlines() paths = [line.split()[0] for line in lines] infer_time = [] with open(tmp_path, 'a+') as f: for i, path in enumerate(paths, 1): if i == 1: sys.stdout.write('\n') sys.stdout.write('\r' + keras_bar(i, len(paths))) image = read_image(path) h, w = image.shape[:2] image = preprocess_image(image, (image_size, image_size)).astype( np.float32) images = np.expand_dims(image, axis=0) tic = time.time() bboxes, scores, classes, valid_detections = model.predict(images) toc = time.time() infer_time.append(toc - tic) bboxes = bboxes[0][:valid_detections[0]] scores = scores[0][:valid_detections[0]] classes = classes[0][:valid_detections[0]] bboxes *= image_size _, bboxes = preprocess_image_inv(image, (w, h), bboxes) line = path for bbox, score, cls in zip(bboxes, scores, classes): x1, y1, x2, y2 = bbox line += " {:.2f},{:.2f},{:.2f},{:.2f},{},{:.4f}".format( x1, y1, x2, y2, int(cls), score) f.write(line + '\n') ans = func(test_path, tmp_path, name_path, verbose) # remove tmp os.remove(tmp_path) if verbose: if len(infer_time) > 5: s = np.mean(infer_time[5:]) else: s = np.mean(infer_time) print('Inference time', s * 1000, 'ms') return ans
def _getitem(self, sub_idx): path, bboxes, labels = self.annotation[sub_idx] image = read_image(path) if len(bboxes) != 0: bboxes, labels = np.array(bboxes), np.array(labels) else: bboxes, labels = np.zeros((0, 4)), np.zeros((0,)) image, bboxes = preprocess_image(image, (self._image_size, self._image_size), bboxes) labels = augment.onehot(labels, self.num_classes, self.label_smoothing) return image, bboxes, labels
def inference(image): h, w = image.shape[:2] image = preprocess_image(image, (image_size, image_size)).astype(np.float32) images = np.expand_dims(image, axis=0) tic = time.time() bboxes, scores, classes, valid_detections = model.predict(images) toc = time.time() bboxes = bboxes[0][:valid_detections[0]] scores = scores[0][:valid_detections[0]] classes = classes[0][:valid_detections[0]] # bboxes *= image_size _, bboxes = postprocess_image(image, (w, h), bboxes) return (toc - tic) * 1000, bboxes, scores, classes
def _getitem(self, sub_idx): path, bboxes, labels = self.annotation[sub_idx] image = read_image(path) bboxes, labels = np.array(bboxes), np.array(labels) if self.normal_method: image = augment.random_distort(image) image = augment.random_grayscale(image) image, bboxes = augment.random_flip_lr(image, bboxes) image, bboxes = augment.random_rotate(image, bboxes) image, bboxes, labels = augment.random_crop_and_zoom( image, bboxes, labels, (self._image_size, self._image_size)) else: image, bboxes = preprocess_image( image, (self._image_size, self._image_size), bboxes) labels = augment.onehot(labels, self.num_classes, self.label_smoothing) return image, bboxes, labels