def main(argv): config_path = args.conf num_anchors = args.anchors with open(config_path) as config_buffer: config = json.loads(config_buffer.read()) train_imgs, train_labels = parse_annotation( config['train']['train_annot_folder'], config['train']['train_image_folder'], config['model']['labels']) grid_w = config['model']['input_size'] / 32 grid_h = config['model']['input_size'] / 32 # run k_mean to find the anchors annotation_dims = [] for image in train_imgs: cell_w = image['width'] / grid_w cell_h = image['height'] / grid_h for obj in image['object']: relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h annotation_dims.append(tuple(map(float, (relative_w, relatice_h)))) annotation_dims = np.array(annotation_dims) centroids = run_kmeans(annotation_dims, num_anchors) # write anchors to file print('\naverage IOU for', num_anchors, 'anchors:', '%0.2f' % avg_IOU(annotation_dims, centroids)) print_anchors(centroids)
def setValidationDataSet(self, data_path): img_dir = os.path.join(data_path, 'train_97c/') annotation_dir = os.path.join(data_path, 'annotation_97c') img_dir += "/" annotation_dir += "/" self.valid_imgs, self.valid_labels = parse_annotation( annotation_dir, img_dir, self.labels) ocrdf = pd.read_csv(os.path.join(data_path, 'number_label.txt')) self.ocr_valid_root_dir = data_path self.ocr_valid_annotation = ocrdf.as_matrix() self.ocr.setValidationData(self.ocr_valid_annotation, self.ocr_valid_root_dir)
def test(self, data_path): img_dir = os.path.join(data_path, 'train_97c/') annotation_dir = os.path.join(data_path, 'annotation_97c') img_dir += "/" annotation_dir += "/" test_imgs, test_labels = parse_annotation(annotation_dir, img_dir, self.labels) mAP = self.yolo.test(test_imgs) print('Yolo Test Result, mAP:', mAP) ocrdf = pd.read_csv(os.path.join(data_path, 'number_label.txt')) test_annotation = ocrdf.as_matrix() test_ds = self.ocr.getTestDataSet(test_annotation, data_path) test_loss = self.ocr.test_model(test_ds) print('OCR Test Loss:', test_loss) return mAP[0], test_loss