def evaluate_dataset(self, image_dataset, frame_ids, out_dir): total_predictions = total_annotations = total_matches = 0 for idx in frame_ids: frame = image_dataset.all_frames[idx] predicted_bboxes = self.predict_complete(frame.img_data) predicted_bboxes = image_utils.group_bboxes(predicted_bboxes) predicted_annotations = [ image_utils.get_cell_center(r) for r in predicted_bboxes ] gt_annotations = [(ann[0], ann[1]) for ann in frame.annotations] matches = metric_utils.get_matches(predicted_annotations, gt_annotations) total_matches += len(matches) total_predictions += len(predicted_annotations) total_annotations += len(gt_annotations) recall_f, precision_f, f1_f = metric_utils.score_detections( predicted_annotations, frame.annotations, matches) logger.info('Processed frame:{}, precision:{}, recall:{}, f1:{}', frame.img_id, precision_f, recall_f, f1_f) annotated_img = image_utils.get_annotated_img( frame.img_data, predicted_annotations, (15, 15)) cv2.imwrite(os.path.join(out_dir, frame.img_id), image_utils.normalize(annotated_img)) # plt.imshow(image_utils.get_annotated_img(frame.img_data, predicted_annotations,(15,15))) # plt.show() precision = total_matches * 1.0 / total_predictions recall = total_matches * 1.0 / total_annotations f1 = 2 * precision * recall / (precision + recall) return precision, recall, f1
def predict_all(model, input_dir, out_dir, file_pattern): batch_size = 1 if model == 'resnet': detector = FCNResnet50([batch_size, 224, 224, 3], 1e-3, 1, weight_file='weights/fcn_resnet.hdf5') step_size = (160, 160) elif model == 'unet': detector = UNet([batch_size, 252, 252, 3], 1e-3, 1, weight_file='weights/unet.hdf5') step_size = (180, 180) all_files = glob.glob(input_dir + file_pattern) for fn in all_files: logger.info('Processing file:{}', fn) base_filename = os.path.basename(fn) image = cv2.imread(fn) response_map = detector.predict_complete(image, step_size) rm_norm = normalize(response_map) out_path = out_dir + 'response_map_{}'.format(base_filename) logger.info('Writing response map at :{}', out_path) cv2.imwrite(out_path, rm_norm)
dataset = ImageDataset( '/data/lrz/hm-cell-tracking/sequences_A549/annotations/', '.png') fcn_mask_gen = GridDatasetGenerator(dataset, 0, 0).grid_dataset_generator( 1, (512, 512), (16, 16)) for data in fcn_mask_gen: input, output = data input_img = np.squeeze(input['input_1'], axis=0) plt.figure(1), plt.imshow(input_img) class_score, bb_score = output['class_out'], output['bb_out'] annotations = image_utils.feature_to_annotations( input_img, np.squeeze(class_score), np.squeeze(bb_score)) ann_img = image_utils.draw_bboxes(input_img, annotations) plt.figure(2), plt.imshow(ann_img) plt.figure(3), plt.imshow(np.squeeze(class_score)) zoom_class_score = interpolation.zoom( np.squeeze(image_utils.normalize(class_score)), (16, 16)) zoom_class_score = np.repeat(np.expand_dims( zoom_class_score, axis=-1).astype(np.float32), 3, axis=-1) c = np.array((1, 0, 0), dtype=np.float32) zoom_class_score = zoom_class_score * c.reshape(1, 1, 3) out1 = cv2.addWeighted(input_img, 0.5, zoom_class_score, 0.5, 0) for i in range(32): out1 = cv2.line(out1, (16 * i, 0), (16 * i, 511), (0, 0, 255), 1) out1 = cv2.line(out1, (0, 16 * i), (511, 16 * i), (0, 0, 255), 1) cv2.imwrite('/home/sanjeev/overlay.jpg', out1[:300, :300]) cv2.imwrite('/home/sanjeev/original.jpg', input_img[:300, :300]) # plt.show() print(class_score, bb_score)
def get_predictions(self, dataset, frame_idx, base_dir): total_predictions = total_annotations = total_matches = 0 frame_metrics = [] all_predictions = [] all_fp = [] all_fn = [] for idx in frame_idx: frame = dataset.all_frames[idx] response_map = self.predict_complete(frame.img_data) actual_annotations = np.array([(ann[0], ann[1]) for ann in frame.annotations]) predicted_annotations = local_maxima(response_map, 20, 0.4) matches = get_matches(predicted_annotations, actual_annotations) logger.info('Frame:{} Predicted:{}, actual:{}, matches:{}', frame.img_id, len(predicted_annotations), len(frame.annotations), len(matches)) total_annotations += len(frame.annotations) total_matches += len(matches) total_predictions += len(predicted_annotations) false_pos = set(range( len(predicted_annotations))) - {match[0] for match in matches} false_neg = set(range( len(actual_annotations))) - {match[1] for match in matches} false_neg_ann = [frame.annotations[idx] for idx in false_neg] false_pos_ann = [predicted_annotations[idx] for idx in false_pos] all_predictions.extend([[frame.img_id] + ann.tolist() for ann in predicted_annotations]) all_fp.extend([[frame.img_id] + ann.tolist() for ann in false_pos_ann]) all_fn.extend([[frame.img_id] + list(ann) for ann in false_neg_ann]) rm_norm = normalize(response_map) cv2.imwrite( os.path.join(base_dir, 'response_map_{}'.format(frame.img_id)), rm_norm) # predicted_img = get_annotated_img(frame.img_data, predicted_annotations, (15, 15)) # false_neg_img = get_annotated_img(frame.img_data, false_neg_ann, (15, 15)) # fals_pos_img = get_annotated_img(frame.img_data, false_pos_ann, (15, 15)) # cv2.imwrite(os.path.join(base_dir, 'fn_{}'.format(frame.img_id)), false_neg_img) # cv2.imwrite(os.path.join(base_dir, 'fp_{}'.format(frame.img_id)), fals_pos_img) # cv2.imwrite(os.path.join(base_dir, 'prediction_{}'.format(frame.img_id)), predicted_img) recall_f, precision_f, f1_f = score_detections( predicted_annotations, frame.annotations, matches) # img_id, total_ann, total_predictions, total_matches, recall, precision, f1 frame_metric = [ frame.img_id, len(frame.annotations), len(predicted_annotations), len(matches), recall_f, precision_f, f1_f ] frame_metrics.append(frame_metric) write_to_file(all_predictions, os.path.join(base_dir, 'predictions.csv')) write_to_file(all_fp, os.path.join(base_dir, 'fp.csv')) write_to_file(all_fn, os.path.join(base_dir, 'fn.csv')) write_to_file(frame_metrics, os.path.join(base_dir, 'score.csv')) logger.info('Total matches:{}, predictions:{}, actual:{}', total_matches, total_predictions, total_annotations) precision = total_matches * 1.0 / total_predictions recall = total_matches * 1.0 / total_annotations f1 = 2 * precision * recall / (precision + recall) logger.info('Precision:{}, recall:{}, F1:{}', precision, recall, f1)