2019-12-11 02:04:24 +00:00
|
|
|
import os
|
|
|
|
|
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-12-11 02:04:24 +00:00
|
|
|
# Parse the yolo *.cfg file and return module definitions path may be 'cfg/yolov3.cfg', 'yolov3.cfg', or 'yolov3'
|
|
|
|
if not path.endswith('.cfg'): # add .cfg suffix if omitted
|
|
|
|
path += '.cfg'
|
2019-12-11 02:25:14 +00:00
|
|
|
if not os.path.exists(path) and os.path.exists('cfg' + os.sep + path): # add cfg/ prefix if omitted
|
2019-12-11 02:04:24 +00:00
|
|
|
path = 'cfg' + os.sep + path
|
|
|
|
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
lines = f.read().split('\n')
|
2018-08-26 08:51:39 +00:00
|
|
|
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()
|
|
|
|
|
2020-02-20 01:08:03 +00:00
|
|
|
if key == 'anchors': # return nparray
|
2019-08-15 16:15:27 +00:00
|
|
|
mdefs[-1][key] = np.array([float(x) for x in val.split(',')]).reshape((-1, 2)) # np anchors
|
2020-02-20 01:08:03 +00:00
|
|
|
elif key in ['from', 'layers', 'mask']: # return array
|
|
|
|
mdefs[-1][key] = [int(x) for x in val.split(',')]
|
2019-08-15 16:15:27 +00:00
|
|
|
else:
|
2020-02-20 01:08:03 +00:00
|
|
|
val = val.strip()
|
|
|
|
if val.isnumeric(): # return int or float
|
|
|
|
mdefs[-1][key] = int(val) if (int(val) - float(val)) == 0 else float(val)
|
|
|
|
else:
|
|
|
|
mdefs[-1][key] = val # return string
|
2018-08-26 08:51:39 +00:00
|
|
|
|
2019-12-09 21:49:50 +00:00
|
|
|
# Check all fields are supported
|
|
|
|
supported = ['type', 'batch_normalize', 'filters', 'size', 'stride', 'pad', 'activation', 'layers', 'groups',
|
|
|
|
'from', 'mask', 'anchors', 'classes', 'num', 'jitter', 'ignore_thresh', 'truth_thresh', 'random',
|
2020-02-19 05:04:58 +00:00
|
|
|
'stride_x', 'stride_y', 'weights_type', 'weights_normalization', 'scale_x_y', 'beta_nms', 'nms_kind',
|
|
|
|
'iou_loss', 'iou_normalizer', 'cls_normalizer', 'iou_thresh']
|
2019-12-09 21:49:50 +00:00
|
|
|
|
2019-12-09 23:54:46 +00:00
|
|
|
f = [] # fields
|
2019-12-09 21:49:50 +00:00
|
|
|
for x in mdefs[1:]:
|
|
|
|
[f.append(k) for k in x if k not in f]
|
2019-12-09 23:54:46 +00:00
|
|
|
u = [x for x in f if x not in supported] # unsupported fields
|
|
|
|
assert not any(u), "Unsupported fields %s in %s. See https://github.com/ultralytics/yolov3/issues/631" % (u, path)
|
2019-12-09 21:37:58 +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
|
2019-12-11 02:25:14 +00:00
|
|
|
if not os.path.exists(path) and os.path.exists('data' + os.sep + path): # add data/ prefix if omitted
|
|
|
|
path = 'data' + os.sep + path
|
|
|
|
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
lines = f.readlines()
|
2019-08-15 16:15:27 +00:00
|
|
|
|
2019-12-11 02:25:14 +00:00
|
|
|
options = dict()
|
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
|