Spaces:
Runtime error
Runtime error
File size: 10,935 Bytes
7fab858 |
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import sys
import argparse
import os
from util import util
import torch
import models
import data
import pickle
class BaseOptions:
def __init__(self):
self.initialized = False
def initialize(self, parser):
# experiment specifics
parser.add_argument(
"--name",
type=str,
default="label2coco",
help="name of the experiment. It decides where to store samples and models",
)
parser.add_argument(
"--gpu_ids", type=str, default="0", help="gpu ids: e.g. 0 0,1,2, 0,2. use -1 for CPU"
)
parser.add_argument(
"--checkpoints_dir", type=str, default="./checkpoints", help="models are saved here"
)
parser.add_argument("--model", type=str, default="pix2pix", help="which model to use")
parser.add_argument(
"--norm_G",
type=str,
default="spectralinstance",
help="instance normalization or batch normalization",
)
parser.add_argument(
"--norm_D",
type=str,
default="spectralinstance",
help="instance normalization or batch normalization",
)
parser.add_argument(
"--norm_E",
type=str,
default="spectralinstance",
help="instance normalization or batch normalization",
)
parser.add_argument("--phase", type=str, default="train", help="train, val, test, etc")
# input/output sizes
parser.add_argument("--batchSize", type=int, default=1, help="input batch size")
parser.add_argument(
"--preprocess_mode",
type=str,
default="scale_width_and_crop",
help="scaling and cropping of images at load time.",
choices=(
"resize_and_crop",
"crop",
"scale_width",
"scale_width_and_crop",
"scale_shortside",
"scale_shortside_and_crop",
"fixed",
"none",
"resize",
),
)
parser.add_argument(
"--load_size",
type=int,
default=1024,
help="Scale images to this size. The final image will be cropped to --crop_size.",
)
parser.add_argument(
"--crop_size",
type=int,
default=512,
help="Crop to the width of crop_size (after initially scaling the images to load_size.)",
)
parser.add_argument(
"--aspect_ratio",
type=float,
default=1.0,
help="The ratio width/height. The final height of the load image will be crop_size/aspect_ratio",
)
parser.add_argument(
"--label_nc",
type=int,
default=182,
help="# of input label classes without unknown class. If you have unknown class as class label, specify --contain_dopntcare_label.",
)
parser.add_argument(
"--contain_dontcare_label",
action="store_true",
help="if the label map contains dontcare label (dontcare=255)",
)
parser.add_argument("--output_nc", type=int, default=3, help="# of output image channels")
# for setting inputs
parser.add_argument("--dataroot", type=str, default="./datasets/cityscapes/")
parser.add_argument("--dataset_mode", type=str, default="coco")
parser.add_argument(
"--serial_batches",
action="store_true",
help="if true, takes images in order to make batches, otherwise takes them randomly",
)
parser.add_argument(
"--no_flip",
action="store_true",
help="if specified, do not flip the images for data argumentation",
)
parser.add_argument("--nThreads", default=0, type=int, help="# threads for loading data")
parser.add_argument(
"--max_dataset_size",
type=int,
default=sys.maxsize,
help="Maximum number of samples allowed per dataset. If the dataset directory contains more than max_dataset_size, only a subset is loaded.",
)
parser.add_argument(
"--load_from_opt_file",
action="store_true",
help="load the options from checkpoints and use that as default",
)
parser.add_argument(
"--cache_filelist_write",
action="store_true",
help="saves the current filelist into a text file, so that it loads faster",
)
parser.add_argument(
"--cache_filelist_read", action="store_true", help="reads from the file list cache"
)
# for displays
parser.add_argument("--display_winsize", type=int, default=400, help="display window size")
# for generator
parser.add_argument(
"--netG", type=str, default="spade", help="selects model to use for netG (pix2pixhd | spade)"
)
parser.add_argument("--ngf", type=int, default=64, help="# of gen filters in first conv layer")
parser.add_argument(
"--init_type",
type=str,
default="xavier",
help="network initialization [normal|xavier|kaiming|orthogonal]",
)
parser.add_argument(
"--init_variance", type=float, default=0.02, help="variance of the initialization distribution"
)
parser.add_argument("--z_dim", type=int, default=256, help="dimension of the latent z vector")
parser.add_argument(
"--no_parsing_map", action="store_true", help="During training, we do not use the parsing map"
)
# for instance-wise features
parser.add_argument(
"--no_instance", action="store_true", help="if specified, do *not* add instance map as input"
)
parser.add_argument(
"--nef", type=int, default=16, help="# of encoder filters in the first conv layer"
)
parser.add_argument("--use_vae", action="store_true", help="enable training with an image encoder.")
parser.add_argument(
"--tensorboard_log", action="store_true", help="use tensorboard to record the resutls"
)
# parser.add_argument('--img_dir',)
parser.add_argument(
"--old_face_folder", type=str, default="", help="The folder name of input old face"
)
parser.add_argument(
"--old_face_label_folder", type=str, default="", help="The folder name of input old face label"
)
parser.add_argument("--injection_layer", type=str, default="all", help="")
self.initialized = True
return parser
def gather_options(self):
# initialize parser with basic options
if not self.initialized:
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser = self.initialize(parser)
# get the basic options
opt, unknown = parser.parse_known_args()
# modify model-related parser options
model_name = opt.model
model_option_setter = models.get_option_setter(model_name)
parser = model_option_setter(parser, self.isTrain)
# modify dataset-related parser options
# dataset_mode = opt.dataset_mode
# dataset_option_setter = data.get_option_setter(dataset_mode)
# parser = dataset_option_setter(parser, self.isTrain)
opt, unknown = parser.parse_known_args()
# if there is opt_file, load it.
# The previous default options will be overwritten
if opt.load_from_opt_file:
parser = self.update_options_from_file(parser, opt)
opt = parser.parse_args()
self.parser = parser
return opt
def print_options(self, opt):
message = ""
message += "----------------- Options ---------------\n"
for k, v in sorted(vars(opt).items()):
comment = ""
default = self.parser.get_default(k)
if v != default:
comment = "\t[default: %s]" % str(default)
message += "{:>25}: {:<30}{}\n".format(str(k), str(v), comment)
message += "----------------- End -------------------"
# print(message)
def option_file_path(self, opt, makedir=False):
expr_dir = os.path.join(opt.checkpoints_dir, opt.name)
if makedir:
util.mkdirs(expr_dir)
file_name = os.path.join(expr_dir, "opt")
return file_name
def save_options(self, opt):
file_name = self.option_file_path(opt, makedir=True)
with open(file_name + ".txt", "wt") as opt_file:
for k, v in sorted(vars(opt).items()):
comment = ""
default = self.parser.get_default(k)
if v != default:
comment = "\t[default: %s]" % str(default)
opt_file.write("{:>25}: {:<30}{}\n".format(str(k), str(v), comment))
with open(file_name + ".pkl", "wb") as opt_file:
pickle.dump(opt, opt_file)
def update_options_from_file(self, parser, opt):
new_opt = self.load_options(opt)
for k, v in sorted(vars(opt).items()):
if hasattr(new_opt, k) and v != getattr(new_opt, k):
new_val = getattr(new_opt, k)
parser.set_defaults(**{k: new_val})
return parser
def load_options(self, opt):
file_name = self.option_file_path(opt, makedir=False)
new_opt = pickle.load(open(file_name + ".pkl", "rb"))
return new_opt
def parse(self, save=False):
opt = self.gather_options()
opt.isTrain = self.isTrain # train or test
opt.contain_dontcare_label = False
self.print_options(opt)
if opt.isTrain:
self.save_options(opt)
# Set semantic_nc based on the option.
# This will be convenient in many places
opt.semantic_nc = (
opt.label_nc + (1 if opt.contain_dontcare_label else 0) + (0 if opt.no_instance else 1)
)
# set gpu ids
str_ids = opt.gpu_ids.split(",")
opt.gpu_ids = []
for str_id in str_ids:
int_id = int(str_id)
if int_id >= 0:
opt.gpu_ids.append(int_id)
if len(opt.gpu_ids) > 0:
print("The main GPU is ")
print(opt.gpu_ids[0])
torch.cuda.set_device(opt.gpu_ids[0])
assert (
len(opt.gpu_ids) == 0 or opt.batchSize % len(opt.gpu_ids) == 0
), "Batch size %d is wrong. It must be a multiple of # GPUs %d." % (opt.batchSize, len(opt.gpu_ids))
self.opt = opt
return self.opt
|