This file contains hidden or 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
self.neck = nn.ModuleList() | |
self.neck += [ | |
CBL(in_channels=first_out*16, out_channels=first_out*8, kernel_size=1, stride=1, padding=0), | |
C3(in_channels=first_out*16, out_channels=first_out*8, width_multiple=0.25, depth=2, backbone=False), | |
CBL(in_channels=first_out*8, out_channels=first_out*4, kernel_size=1, stride=1, padding=0), | |
C3(in_channels=first_out*8, out_channels=first_out*4, width_multiple=0.25, depth=2, backbone=False), | |
CBL(in_channels=first_out*4, out_channels=first_out*4, kernel_size=3, stride=2, padding=1), | |
C3(in_channels=first_out*8, out_channels=first_out*8, width_multiple=0.5, depth=2, backbone=False), | |
CBL(in_channels=first_out*8, out_channels=first_out*8, kernel_size=3, stride=2, padding=1), | |
C3(in_channels=first_out*16, out_channels=first_out*16, width_multiple=0.5, depth=2, backbone=False) |
This file contains hidden or 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 forward(self, x): | |
assert x.shape[2] % 32 == 0 and x.shape[3] % 32 == 0, "Width and Height aren't divisible by 32!" | |
backbone_connection = [] | |
neck_connection = [] | |
outputs = [] | |
for idx, layer in enumerate(self.backbone): | |
# takes the out of the 2nd and 3rd C3 block and stores it | |
x = layer(x) | |
if idx in [4, 6]: | |
backbone_connection.append(x) |
This file contains hidden or 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 HEADS(nn.Module): | |
def __init__(self, nc=80, anchors=(), ch=()): # detection layer | |
super(HEADS, self).__init__() | |
self.nc = nc # number of classes | |
self.nl = len(anchors) # number of detection layers | |
self.naxs = len(anchors[0]) # number of anchors per scale | |
self.stride = [8, 16, 32] | |
# anchors are divided by the stride (anchors_for_head_1/8, anchors_for_head_1/16 etc.) | |
anchors_ = torch.tensor(anchors).float().view(self.nl, -1, 2) / torch.tensor(self.stride).repeat(6, 1).T.reshape(3, 3, 2) |
This file contains hidden or 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 cells_to_bboxes(predictions, anchors, strides): | |
num_out_layers = len(predictions) | |
grid = [torch.empty(0) for _ in range(num_out_layers)] # initialize | |
anchor_grid = [torch.empty(0) for _ in range(num_out_layers)] # initialize | |
all_bboxes = [] | |
for i in range(num_out_layers): | |
bs, naxs, ny, nx, _ = predictions[i].shape | |
stride = strides[i] | |
grid[i], anchor_grid[i] = make_grids(anchors, naxs, ny=ny, nx=nx, stride=stride, i=i) |
This file contains hidden or 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
S = [8, 16, 32] | |
with torch.no_grad(): | |
out = model(img) | |
boxes = cells_to_bboxes(out, model.head.anchors, S, list_output=False, is_pred=True) | |
boxes = non_max_suppression(boxes, iou_threshold=0.6, threshold=.25, max_detections=300) | |
plot_image(img[0].permute(1, 2, 0).to("cpu"), boxes[0]) |
This file contains hidden or 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
gist = "indentation error" |
This file contains hidden or 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 Training_Dataset(Dataset): | |
"""COCO 2017 dataset constructed using the PyTorch built-in functionalities""" | |
def __init__(self, | |
num_classes, | |
root_directory=config.ROOT_DIR, | |
transform=None, | |
train=True, | |
rect_training=False, | |
default_size=640, |
This file contains hidden or 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 Training_Dataset(Dataset): | |
def __init__(self, | |
root_directory=config.ROOT_DIR, | |
transform=None, | |
train=True, | |
rect_training=False, | |
default_size=640, | |
bs=64, | |
bboxes_format="coco", |
This file contains hidden or 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
try: | |
self.annotations = pd.read_csv(os.path.join(root_directory, "labels", annot_file), | |
header=None, index_col=0).sort_values(by=[0]) | |
self.annotations = self.annotations.head((len(self.annotations)-1)) # just removes last line | |
except FileNotFoundError: | |
annotations = [] | |
for img_txt in os.listdir(os.path.join(self.root_directory, "labels", self.annot_folder)): | |
img = img_txt.split(".txt")[0] | |
try: | |
w, h = imagesize.get(os.path.join(self.root_directory, "images", self.annot_folder, f"{img}.jpg")) |
This file contains hidden or 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 adaptive_shape(self, annotations): | |
name = "train" if self.train else "val" | |
path = os.path.join( | |
self.root_directory, "labels", | |
"adaptive_ann_{}_{}_br_{}.csv".format(name, self.len_ann, int(self.batch_range)) | |
) | |
if os.path.isfile(path): | |
print(f"==> Loading cached annotations for rectangular training on {self.annot_folder}") |