car-detection-bayes/utils/parse_config.py

43 lines
1.4 KiB
Python
Raw Normal View History

2019-08-15 16:15:27 +00:00
import numpy as np
2019-02-12 15:58:07 +00:00
def parse_model_cfg(path):
2019-08-15 16:15:27 +00:00
# Parses the yolo-v3 layer configuration file and returns module definitions
2018-08-26 08:51:39 +00:00
file = open(path, 'r')
lines = file.read().split('\n')
lines = [x for x in lines if x and not x.startswith('#')]
2019-02-11 17:15:51 +00:00
lines = [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespaces
2019-08-15 16:15:27 +00:00
mdefs = [] # module definitions
2018-08-26 08:51:39 +00:00
for line in lines:
2019-02-11 17:15:51 +00:00
if line.startswith('['): # This marks the start of a new block
2019-08-15 16:15:27 +00:00
mdefs.append({})
mdefs[-1]['type'] = line[1:-1].rstrip()
if mdefs[-1]['type'] == 'convolutional':
mdefs[-1]['batch_normalize'] = 0 # pre-populate with zeros (may be overwritten later)
2018-08-26 08:51:39 +00:00
else:
2019-08-15 16:15:27 +00:00
key, val = line.split("=")
key = key.rstrip()
if 'anchors' in key:
mdefs[-1][key] = np.array([float(x) for x in val.split(',')]).reshape((-1, 2)) # np anchors
else:
mdefs[-1][key] = val.strip()
2018-08-26 08:51:39 +00:00
2019-08-15 16:15:27 +00:00
return mdefs
2018-08-26 08:51:39 +00:00
2019-02-11 17:15:51 +00:00
2019-02-08 21:43:05 +00:00
def parse_data_cfg(path):
2019-08-15 16:15:27 +00:00
# Parses the data configuration file
2018-08-26 08:51:39 +00:00
options = dict()
with open(path, 'r') as fp:
lines = fp.readlines()
2019-08-15 16:15:27 +00:00
2018-08-26 08:51:39 +00:00
for line in lines:
line = line.strip()
if line == '' or line.startswith('#'):
continue
2019-08-15 16:15:27 +00:00
key, val = line.split('=')
options[key.strip()] = val.strip()
2018-08-26 08:51:39 +00:00
return options