-
Notifications
You must be signed in to change notification settings - Fork 82
/
noise_argparser.py
107 lines (92 loc) · 3.81 KB
/
noise_argparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import argparse
import re
from noise_layers.cropout import Cropout
from noise_layers.crop import Crop
from noise_layers.identity import Identity
from noise_layers.dropout import Dropout
from noise_layers.resize import Resize
from noise_layers.quantization import Quantization
from noise_layers.jpeg_compression import JpegCompression
def parse_pair(match_groups):
heights = match_groups[0].split(',')
hmin = float(heights[0])
hmax = float(heights[1])
widths = match_groups[1].split(',')
wmin = float(widths[0])
wmax = float(widths[1])
return (hmin, hmax), (wmin, wmax)
def parse_crop(crop_command):
matches = re.match(r'crop\(\((\d+\.*\d*,\d+\.*\d*)\),\((\d+\.*\d*,\d+\.*\d*)\)\)', crop_command)
(hmin, hmax), (wmin, wmax) = parse_pair(matches.groups())
return Crop((hmin, hmax), (wmin, wmax))
def parse_cropout(cropout_command):
matches = re.match(r'cropout\(\((\d+\.*\d*,\d+\.*\d*)\),\((\d+\.*\d*,\d+\.*\d*)\)\)', cropout_command)
(hmin, hmax), (wmin, wmax) = parse_pair(matches.groups())
return Cropout((hmin, hmax), (wmin, wmax))
def parse_dropout(dropout_command):
matches = re.match(r'dropout\((\d+\.*\d*,\d+\.*\d*)\)', dropout_command)
ratios = matches.groups()[0].split(',')
keep_min = float(ratios[0])
keep_max = float(ratios[1])
return Dropout((keep_min, keep_max))
def parse_resize(resize_command):
matches = re.match(r'resize\((\d+\.*\d*,\d+\.*\d*)\)', resize_command)
ratios = matches.groups()[0].split(',')
min_ratio = float(ratios[0])
max_ratio = float(ratios[1])
return Resize((min_ratio, max_ratio))
class NoiseArgParser(argparse.Action):
def __init__(self,
option_strings,
dest,
nargs=None,
const=None,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None):
argparse.Action.__init__(self,
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=const,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar,
)
@staticmethod
def parse_cropout_args(cropout_args):
pass
@staticmethod
def parse_dropout_args(dropout_args):
pass
def __call__(self, parser, namespace, values,
option_string=None):
layers = []
split_commands = values[0].split('+')
for command in split_commands:
# remove all whitespace
command = command.replace(' ', '')
if command[:len('cropout')] == 'cropout':
layers.append(parse_cropout(command))
elif command[:len('crop')] == 'crop':
layers.append(parse_crop(command))
elif command[:len('dropout')] == 'dropout':
layers.append(parse_dropout(command))
elif command[:len('resize')] == 'resize':
layers.append(parse_resize(command))
elif command[:len('jpeg')] == 'jpeg':
layers.append('JpegPlaceholder')
elif command[:len('quant')] == 'quant':
layers.append('QuantizationPlaceholder')
elif command[:len('identity')] == 'identity':
# We are adding one Identity() layer in Noiser anyway
pass
else:
raise ValueError('Command not recognized: \n{}'.format(command))
setattr(namespace, self.dest, layers)