This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SelfAttention(nn.Module): | |
def __init__(self, dim, num_heads=8): | |
super().__init__() | |
self.Wq = nn.Linear(dim, num_heads * dim, bias=False) | |
self.Wk = nn.Linear(dim, num_heads * dim, bias=False) | |
self.Wv = nn.Linear(dim, num_heads * dim, bias=False) | |
self.Wo = nn.Linear(num_heads * dim, dim) | |
def forward(self, x): | |
# X is of shape (Batch size, number of tokens, embedding dimension) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import the necessary packages | |
import numpy as np | |
# Malisiewicz et al. | |
def non_max_suppression_fast(boxes, overlapThresh): | |
# if there are no boxes, return an empty list | |
if len(boxes) == 0: | |
return [] | |
# if the bounding boxes integers, convert them to floats -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_metrics(true_pos, false_pos, false_neg): | |
"""Compute the precision, recall, and f1 score.""" | |
precision = true_pos / (true_pos + false_pos) | |
recall = true_pos / (true_pos + false_neg) | |
if precision == 0 or recall == 0: | |
return precision, recall, 0 | |
f1 = 2 / (1 / precision + 1 / recall) | |
return precision, recall, f1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def load_retinanet(weights, n_classes, freeze=True): | |
modifier = freeze_model if freeze else None | |
model = resnet50_retinanet(num_classes=num_classes, modifier=modifier) | |
model.load_weights(weights, by_name=True, skip_mismatch=True) | |
return model | |
def compile(model): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DfGenerator(CSVGenerator): | |
"""Custom generator intented to work with in-memory Pandas' dataframe.""" | |
def __init__(self, df, class_mapping, cols, base_dir='', **kwargs): | |
"""Initialization method. | |
Arguments: | |
df: Pandas DataFrame containing paths, labels, and bounding boxes. | |
class_mapping: Dict mapping label_str to id_int. | |
cols: Dict Mapping 'col_{filename/label/x1/y1/x2/y2} to corresponding df col. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def jaccard(box_a, box_b): | |
"""box_X is a tuple of the shape (x1, y1, x2, y2).""" | |
side1 = max(0, min(a[2], b[2]) - max(a[0], b[0])) | |
side2 = max(0, min(a[3], b[3]) - max(a[1], b[1])) | |
inter = side1 * side2 | |
area_a = (a[2] - a[0]) * (a[3] - a[1]) | |
area_b = (b[2] - b[0]) * (b[3] - b[1]) | |
union = area_a + area_b - inter | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import clear_output, Image, display, HTML | |
def strip_consts(graph_def, max_const_size=32): | |
"""Strip large constant values from graph_def.""" | |
strip_def = tf.GraphDef() | |
for n0 in graph_def.node: | |
n = strip_def.node.add() | |
n.MergeFrom(n0) | |
if n.op == 'Const': | |
tensor = n.attr['value'].tensor |