Skip to content

Instantly share code, notes, and snippets.

View arunm8489's full-sized avatar

Arun Mohan arunm8489

View GitHub Profile
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:
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
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()
@arunm8489
arunm8489 / 4
Last active June 3, 2020 14:21
for i,block in enumerate(blocks[1:]):
seq = nn.Sequential()
#upcoming code comes under this loop
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)
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
class DummyLayer(nn.Module):
def __init__(self):
super(DummyLayer, self).__init__()
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)
class DetectionLayer(nn.Module):
def __init__(self, anchors):
super(DetectionLayer, self).__init__()
self.anchors = anchors
modulelist.append(seq)
output_filters.append(filters)
channels = filters