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 parse_cfg(config_file): | |
file = open(config_file,'r') | |
file = file.read().split('\n') | |
file = [line for line in file if len(line)>0 and line[0] != '#'] | |
file = [line.lstrip().rstrip() for line in file] | |
final_list = [] | |
element_dict = {} | |
for line in file: |
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
import numpy as np | |
import pandas as pd | |
import torch | |
import torch.nn as nn | |
import cv2 | |
import os | |
import time | |
from torch.autograd import Variable | |
import matplotlib.pyplot as plt |
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
config_file = 'yolov3.cfg' | |
blocks = parse_cfg(config_file) | |
darknet_details = blocks[0] | |
channels = 3 | |
#list of filter numbers in each layer.It is useful while defining number of filters in routing layer | |
output_filters = [] | |
modulelist = nn.ModuleList() |
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
for i,block in enumerate(blocks[1:]): | |
seq = nn.Sequential() | |
#upcoming code comes under this loop |
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
if (block["type"] == "convolutional"): | |
activation = block["activation"] | |
filters = int(block["filters"]) | |
kernel_size = int(block["size"]) | |
strides = int(block["stride"]) | |
use_bias= False if ("batch_normalize" in block) else True | |
pad = (kernel_size - 1) // 2 | |
conv = nn.Conv2d(in_channels=channels, out_channels=filters, kernel_size=kernel_size, | |
stride=strides, padding=pad, bias = use_bias) | |
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
elif (block["type"] == 'route'): | |
# start and end is given in format (eg:-1 36 so we will find layer number from it. | |
# we will find layer number in negative format | |
# so that we can get the number of filters in that layer | |
block['layers'] = block['layers'].split(',') | |
start = int(block['layers'][0]) | |
if len(block['layers']) == 1: | |
#ie if -1 given and present layer is 20 . we have to sum filters in 19th and 20th layer | |
block['layers'][0] = int(i + start) | |
filters = output_filters[block['layers'][0]] #start layer number |
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 DummyLayer(nn.Module): | |
def __init__(self): | |
super(DummyLayer, self).__init__() |
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
elif block["type"] == "yolo": | |
mask = block["mask"].split(",") | |
mask = [int(m) for m in mask] | |
anchors = block["anchors"].split(",") | |
anchors = [(int(anchors[i]), int(anchors[i + 1])) for i in range(0, len(anchors), 2)] | |
anchors = [anchors[i] for i in mask] | |
block["anchors"] = anchors | |
detectorLayer = DetectionLayer(anchors) | |
seq.add_module("Detection_{0}".format(i),detectorLayer) |
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 DetectionLayer(nn.Module): | |
def __init__(self, anchors): | |
super(DetectionLayer, self).__init__() | |
self.anchors = anchors |
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
modulelist.append(seq) | |
output_filters.append(filters) | |
channels = filters |
OlderNewer