Read the .cfg file to extract layers into `layers` as well as model-specific parameters into `meta`
(model)
| 7 | import os |
| 8 | |
| 9 | def parser(model): |
| 10 | """ |
| 11 | Read the .cfg file to extract layers into `layers` |
| 12 | as well as model-specific parameters into `meta` |
| 13 | """ |
| 14 | def _parse(l, i = 1): |
| 15 | return l.split('=')[i].strip() |
| 16 | |
| 17 | with open(model, 'rb') as f: |
| 18 | lines = f.readlines() |
| 19 | |
| 20 | lines = [line.decode() for line in lines] |
| 21 | |
| 22 | meta = dict(); layers = list() # will contains layers' info |
| 23 | h, w, c = [int()] * 3; layer = dict() |
| 24 | for line in lines: |
| 25 | line = line.strip() |
| 26 | line = line.split('#')[0] |
| 27 | if '[' in line: |
| 28 | if layer != dict(): |
| 29 | if layer['type'] == '[net]': |
| 30 | h = layer['height'] |
| 31 | w = layer['width'] |
| 32 | c = layer['channels'] |
| 33 | meta['net'] = layer |
| 34 | else: |
| 35 | if layer['type'] == '[crop]': |
| 36 | h = layer['crop_height'] |
| 37 | w = layer['crop_width'] |
| 38 | layers += [layer] |
| 39 | layer = {'type': line} |
| 40 | else: |
| 41 | try: |
| 42 | i = float(_parse(line)) |
| 43 | if i == int(i): i = int(i) |
| 44 | layer[line.split('=')[0].strip()] = i |
| 45 | except: |
| 46 | try: |
| 47 | key = _parse(line, 0) |
| 48 | val = _parse(line, 1) |
| 49 | layer[key] = val |
| 50 | except: |
| 51 | 'banana ninja yadayada' |
| 52 | |
| 53 | meta.update(layer) # last layer contains meta info |
| 54 | if 'anchors' in meta: |
| 55 | splits = meta['anchors'].split(',') |
| 56 | anchors = [float(x.strip()) for x in splits] |
| 57 | meta['anchors'] = anchors |
| 58 | meta['model'] = model # path to cfg, not model name |
| 59 | meta['inp_size'] = [h, w, c] |
| 60 | return layers, meta |
| 61 | |
| 62 | def cfg_yielder(model, binary): |
| 63 | """ |