-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathempty_frames_utilities.py
77 lines (63 loc) · 2.89 KB
/
empty_frames_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pandas as pd
import numpy as np
import cv2
import tempfile
import os
def plot_recall_curve(precision_curve, invert=False):
"""Plot recall at fixed interval 0:1"""
recalls = {}
for i in np.linspace(0,1,11):
recalls[i] = empty_image(precision_curve=precision_curve, threshold=i)
recalls = pd.DataFrame(list(recalls.items()), columns=["threshold","recall"])
if invert:
recalls["recall"] = 1 - recalls["recall"].astype(float)
ax1 = recalls.plot.scatter("threshold","recall")
return ax1
def predict_empty_frames(model, empty_images, comet_logger, invert=False):
"""Optionally read a set of empty frames and predict
Args:
invert: whether the recall should be relative to empty images (default) or non-empty images (1-value)"""
#Create PR curve
precision_curve = [ ]
for path in empty_images:
boxes = model.predict_image(path = path, return_plot=False)
if boxes is not None:
boxes["image"] = path
else:
boxes = pd.DataFrame({"image_path":[path], "xmin":[None],"ymin":[None],"xmax":[None],"ymax":[None],"label":[None],"image":[path], "score":[None]})
precision_curve.append(boxes)
precision_curve = pd.concat(precision_curve)
recall_plot = plot_recall_curve(precision_curve, invert=invert)
recall_at_threshold = empty_image(precision_curve, threshold=0.3)
if invert:
recall_at_threshold = 1 - recall_at_threshold
metric_name = "BirdRecall_at_0.3"
recall_plot.set_title("Atleast One Bird Recall")
else:
metric_name = "EmptyRecall_at_0.3"
recall_plot.set_title("Empty Recall")
comet_logger.experiment.log_metric(metric_name,recall_at_threshold)
comet_logger.experiment.log_figure(recall_plot)
def is_empty(precision_curve, threshold):
precision_curve.score = precision_curve.score.astype(float)
precision_curve = precision_curve[precision_curve.score > threshold]
return precision_curve.empty
def empty_image(precision_curve, threshold):
empty_true_positives = 0
empty_false_negatives = 0
for name, group in precision_curve.groupby('image'):
if is_empty(group, threshold):
empty_true_positives +=1
else:
empty_false_negatives+=1
empty_recall = empty_true_positives/float(empty_true_positives + empty_false_negatives)
return empty_recall
def upload_empty_images(model, comet_logger, empty_images):
tmpdir = tempfile.gettempdir()
for x in empty_images:
img = model.predict_image(path=x, return_plot=True)
if img is not None:
cv2.imwrite("{}/false_positive_{}.png".format(tmpdir,os.path.basename(x)), img)
comet_logger.experiment.log_image("{}/false_positive_{}.png".format(tmpdir,os.path.basename(x)), image_scale=0.5)
else:
continue