forked from ando-khachatryan/HiDDeN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
147 lines (125 loc) · 7.54 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import pprint
import argparse
import torch
import pickle
import utils
import logging
import sys
from options import *
from model.hidden import Hidden
from noise_layers.noiser import Noiser
from noise_argparser import NoiseArgParser
from train import train
def main():
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
parent_parser = argparse.ArgumentParser(description='Training of HiDDeN nets')
subparsers = parent_parser.add_subparsers(dest='command', help='Sub-parser for commands')
new_run_parser = subparsers.add_parser('new', help='starts a new run')
new_run_parser.add_argument('--data-dir', '-d', required=True, type=str,
help='The directory where the data is stored.')
new_run_parser.add_argument('--batch-size', '-b', required=True, type=int, help='The batch size.')
new_run_parser.add_argument('--epochs', '-e', default=300, type=int, help='Number of epochs to run the simulation.')
new_run_parser.add_argument('--name', required=True, type=str, help='The name of the experiment.')
new_run_parser.add_argument('--size', '-s', default=128, type=int,
help='The size of the images (images are square so this is height and width).')
new_run_parser.add_argument('--message', '-m', default=30, type=int, help='The length in bits of the watermark.')
new_run_parser.add_argument('--continue-from-folder', '-c', default='', type=str,
help='The folder from where to continue a previous run. Leave blank if you are starting a new experiment.')
# parser.add_argument('--tensorboard', dest='tensorboard', action='store_true',
# help='If specified, use adds a Tensorboard log. On by default')
new_run_parser.add_argument('--tensorboard', action='store_true',
help='Use to switch on Tensorboard logging.')
new_run_parser.add_argument('--enable-fp16', dest='enable_fp16', action='store_true',
help='Enable mixed-precision training.')
new_run_parser.add_argument('--noise', nargs='*', action=NoiseArgParser,
help="Noise layers configuration. Use quotes when specifying configuration, e.g. 'cropout((0.55, 0.6), (0.55, 0.6))'")
new_run_parser.set_defaults(tensorboard=False)
new_run_parser.set_defaults(enable_fp16=False)
continue_parser = subparsers.add_parser('continue', help='Continue a previous run')
continue_parser.add_argument('--folder', '-f', required=True, type=str,
help='Continue from the last checkpoint in this folder.')
continue_parser.add_argument('--data-dir', '-d', required=False, type=str,
help='The directory where the data is stored. Specify a value only if you want to override the previous value.')
continue_parser.add_argument('--epochs', '-e', required=False, type=int,
help='Number of epochs to run the simulation. Specify a value only if you want to override the previous value.')
# continue_parser.add_argument('--tensorboard', action='store_true',
# help='Override the previous setting regarding tensorboard logging.')
args = parent_parser.parse_args()
checkpoint = None
loaded_checkpoint_file_name = None
if args.command == 'continue':
this_run_folder = args.folder
options_file = os.path.join(this_run_folder, 'options-and-config.pickle')
train_options, hidden_config, noise_config = utils.load_options(options_file)
checkpoint, loaded_checkpoint_file_name = utils.load_last_checkpoint(os.path.join(this_run_folder, 'checkpoints'))
train_options.start_epoch = checkpoint['epoch'] + 1
if args.data_dir is not None:
train_options.train_folder = os.path.join(args.data_dir, 'train')
train_options.validation_folder = os.path.join(args.data_dir, 'val')
if args.epochs is not None:
if train_options.start_epoch < args.epochs:
train_options.number_of_epochs = args.epochs
else:
print(f'Command-line specifies of number of epochs = {args.epochs}, but folder={args.folder} '
f'already contains checkpoint for epoch = {train_options.start_epoch}.')
exit(1)
else:
assert args.command == 'new'
start_epoch = 1
train_options = TrainingOptions(
batch_size=args.batch_size,
number_of_epochs=args.epochs,
train_folder=os.path.join(args.data_dir, 'train'),
validation_folder=os.path.join(args.data_dir, 'val'),
runs_folder=os.path.join('.', 'runs'),
start_epoch=start_epoch,
experiment_name=args.name)
noise_config = args.noise if args.noise is not None else []
hidden_config = HiDDenConfiguration(H=args.size, W=args.size,
message_length=args.message,
encoder_blocks=4, encoder_channels=64,
decoder_blocks=7, decoder_channels=64,
use_discriminator=True,
use_vgg=False,
discriminator_blocks=3, discriminator_channels=64,
decoder_loss=1,
encoder_loss=0.7,
adversarial_loss=1e-3,
enable_fp16=args.enable_fp16
)
this_run_folder = utils.create_folder_for_run(train_options.runs_folder, args.name)
with open(os.path.join(this_run_folder, 'options-and-config.pickle'), 'wb+') as f:
pickle.dump(train_options, f)
pickle.dump(noise_config, f)
pickle.dump(hidden_config, f)
logging.basicConfig(level=logging.INFO,
format='%(message)s',
handlers=[
logging.FileHandler(os.path.join(this_run_folder, f'{train_options.experiment_name}.log')),
logging.StreamHandler(sys.stdout)
])
if (args.command == 'new' and args.tensorboard) or \
(args.command == 'continue' and os.path.isdir(os.path.join(this_run_folder, 'tb-logs'))):
logging.info('Tensorboard is enabled. Creating logger.')
from tensorboard_logger import TensorBoardLogger
tb_logger = TensorBoardLogger(os.path.join(this_run_folder, 'tb-logs'))
else:
tb_logger = None
noiser = Noiser(noise_config, device)
model = Hidden(hidden_config, device, noiser, tb_logger)
if args.command == 'continue':
# if we are continuing, we have to load the model params
assert checkpoint is not None
logging.info(f'Loading checkpoint from file {loaded_checkpoint_file_name}')
utils.model_from_checkpoint(model, checkpoint)
logging.info('HiDDeN model: {}\n'.format(model.to_stirng()))
logging.info('Model Configuration:\n')
logging.info(pprint.pformat(vars(hidden_config)))
logging.info('\nNoise configuration:\n')
logging.info(pprint.pformat(str(noise_config)))
logging.info('\nTraining train_options:\n')
logging.info(pprint.pformat(vars(train_options)))
train(model, device, hidden_config, train_options, this_run_folder, tb_logger)
if __name__ == '__main__':
main()