|
| 1 | +""" |
| 2 | +Contains all code related to the configuration of experiments. |
| 3 | +""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import random |
| 7 | +import time |
| 8 | +from configparser import SectionProxy, ConfigParser |
| 9 | +from enum import Enum, auto |
| 10 | +from pathlib import Path |
| 11 | +from typing import Tuple |
| 12 | + |
| 13 | +import torch |
| 14 | + |
| 15 | + |
| 16 | +class ModelName(Enum): |
| 17 | + """ |
| 18 | + Encodes the names of supported models. |
| 19 | + """ |
| 20 | + DENSE = auto() |
| 21 | + RESNET = auto() |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def getByName(name: str) -> "ModelName": |
| 25 | + """ |
| 26 | + Returns the ModelName corresponding to the given string. Returns ModelName.RESNET in case an unknown name is |
| 27 | + provided. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + name : str |
| 32 | + string representation that should be converted to a ModelName |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + ModelName representation of the provided string, default: ModelName.RESNET |
| 37 | + """ |
| 38 | + if name.upper() in [model.name for model in ModelName]: |
| 39 | + return ModelName[name.upper()] |
| 40 | + else: |
| 41 | + return ModelName.RESNET |
| 42 | + |
| 43 | + |
| 44 | +class Configuration: |
| 45 | + """ |
| 46 | + Holds the configuration for the current experiment. |
| 47 | + """ |
| 48 | + |
| 49 | + def __init__(self, parsedConfig: SectionProxy, test: bool = False, fileSection: str = "DEFAULT"): |
| 50 | + self.fileSection = fileSection |
| 51 | + self.outDir = Path(parsedConfig.get('outdir')) / '{}_{}_{}'.format(fileSection, str(int(time.time())), |
| 52 | + random.randint(0, 100000)) |
| 53 | + if not self.outDir.exists() and not test: |
| 54 | + self.outDir.mkdir(parents=True, exist_ok=True) |
| 55 | + if torch.cuda.is_available(): |
| 56 | + self.device = 'cuda' |
| 57 | + else: |
| 58 | + self.device = 'cpu' |
| 59 | + |
| 60 | + self.epochs = parsedConfig.getint('epochs', 100) |
| 61 | + self.learningRate = parsedConfig.getfloat('learning_rate', 0.0002) |
| 62 | + self.betas = self.parseBetas(parsedConfig.get("betas", "0.5,0.999")) |
| 63 | + |
| 64 | + self.batchSize = parsedConfig.getint('batchsize', 4) |
| 65 | + self.imageHeight = parsedConfig.getint('imageheight', 128) |
| 66 | + self.imageWidth = parsedConfig.getint('imagewidth', 256) |
| 67 | + self.modelSaveEpoch = parsedConfig.getint('modelsaveepoch', 10) |
| 68 | + self.validationEpoch = parsedConfig.getint('validationEpochInterval', 10) |
| 69 | + self.trainImageDir = Path(parsedConfig.get('trainimgagebasedir')) |
| 70 | + self.testImageDir = Path(parsedConfig.get('testimagedir')) |
| 71 | + self.invertImages = parsedConfig.getboolean('invertImages', False) |
| 72 | + self.padScale = parsedConfig.getboolean('padscale', False) |
| 73 | + self.padWidth = parsedConfig.getint('padwidth', 512) |
| 74 | + self.padHeight = parsedConfig.getint('padheight', 256) |
| 75 | + |
| 76 | + self.modelName = ModelName.getByName(parsedConfig.get("model", "RESNET")) |
| 77 | + |
| 78 | + if not test: |
| 79 | + configOut = self.outDir / 'config.cfg' |
| 80 | + with configOut.open('w+') as cfile: |
| 81 | + parsedConfig.parser.write(cfile) |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def parseBetas(betaString: str) -> Tuple[float, float]: |
| 85 | + """ |
| 86 | + Parses a comma-separated string to a list of floats. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + betaString: str |
| 91 | + String to be parsed. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + Tuple of floats. |
| 96 | +
|
| 97 | + Raises |
| 98 | + ------ |
| 99 | + ValueError |
| 100 | + if fewer than two values are specified |
| 101 | + """ |
| 102 | + betas = betaString.split(',') |
| 103 | + if len(betas) < 2: |
| 104 | + raise ValueError("found fewer than two values for betas") |
| 105 | + return float(betas[0]), float(betas[1]) |
| 106 | + |
| 107 | + |
| 108 | +def getConfiguration() -> Configuration: |
| 109 | + """ |
| 110 | + Reads the required arguments from command line and parse the respective configuration file/section. |
| 111 | +
|
| 112 | + Returns |
| 113 | + ------- |
| 114 | + parsed :class:`Configuration` |
| 115 | + """ |
| 116 | + cmdParser = argparse.ArgumentParser() |
| 117 | + cmdParser.add_argument("-config", required=False, help="section of config-file to use") |
| 118 | + cmdParser.add_argument("-configfile", required=False, help="path to config-file") |
| 119 | + args = vars(cmdParser.parse_args()) |
| 120 | + fileSection = 'DEFAULT' |
| 121 | + fileName = 'config.cfg' |
| 122 | + if args["config"]: |
| 123 | + fileSection = args["config"] |
| 124 | + |
| 125 | + if args['configfile']: |
| 126 | + fileName = args['configfile'] |
| 127 | + configParser = ConfigParser() |
| 128 | + configParser.read(fileName) |
| 129 | + parsedConfig = configParser[fileSection] |
| 130 | + sections = configParser.sections() |
| 131 | + for s in sections: |
| 132 | + if s != fileSection: |
| 133 | + configParser.remove_section(s) |
| 134 | + return Configuration(parsedConfig, fileSection=fileSection) |
0 commit comments