def build(args): vgg = model_vgg.build(args) vgg = extract_layers(vgg, args["mrf_layers"]) octave_model = model_octave.build(args["octaves"], args["octave_a"]) model = attach_models(octave_model, vgg) targets = [] losses = [] layers_num = len(model.outputs) // args["octaves"] mix_shape = [args["size"][1], args["size"][0]] tar_shape = args["style_shape"] for o in range(args["octaves"]): for l in range(layers_num): i = layers_num * o + l targets.append(Input(model.outputs[i].shape[1:])) layer_weight = 1 / len(args["style_layers"]) mrf_model = mrf_m(args["mrf_patch_size"], args["mrf_patch_stride"], args["mrf_layers"][l], o, mix_shape, tar_shape) layer_loss = mrf_model([model.outputs[i], targets[i]]) layer_loss = Lambda(lambda x: x * layer_weight)(layer_loss) losses.append(layer_loss) loss = Lambda(lambda x: K.expand_dims(K.sum(x)) * args["mrf_w"])(losses) loss_model = Model(model.inputs + targets, loss) return loss_model, model, targets
def build(args): vgg = model_vgg.build(args) model = extract_layers(vgg, args["layers"]) content_layer = content_l() model = attach_models(model, content_layer) targets = [] masks = [] losses = [] for i in range(len(model.outputs)): targets.append( Input(model.outputs[i].shape[1:], name="content_target_input_{}".format(i))) masks.append( Input(model.outputs[i].shape[1:-1], name="content_mask_input_{}".format(i))) layer_weight = 1 layer_loss = mask_loss_l(layer_weight)( [targets[i], model.outputs[i], masks[i]]) losses.append(layer_loss) loss = Lambda(lambda x: K.expand_dims(K.sum(x)) / len(losses) * args[ "weight"])(losses) loss_model = Model(model.inputs + targets + masks, loss) return loss_model, model, targets + masks
def build(args): vgg = model_vgg.build(args) vgg = extract_layers(vgg, args["layers"]) model = vgg targets = [] losses = [] layers_num = len(model.outputs) mix_shape = args["shape"] tar_shape = args["target_shape"] for l in range(layers_num): i = layers_num + l targets.append(Input(model.outputs[l].shape[1:])) layer_weight = 1 / len(args["layers"]) mrf_model = mrf_m(args["patch_size"], args["patch_stride"], args["layers"][l], mix_shape, tar_shape) layer_loss = mrf_model([model.outputs[l], targets[l]]) layer_loss = Lambda(lambda x: x * layer_weight)(layer_loss) losses.append(layer_loss) loss = Lambda(lambda x: K.expand_dims(K.sum(x)) * args["weight"])(losses) loss_model = Model(model.inputs + targets, loss) return loss_model, model, targets
def build(args): with tf.device("/cpu:0"): cpu_vgg = model_vgg.build(args) cpu_model = extract_layers(cpu_vgg, args["layers"]) cpu_mask_model, cpu_mask_inputs = build_mask_model(cpu_model, args) vgg = model_vgg.build(args) model = extract_layers(vgg, args["layers"]) mask_model, mask_inputs = build_mask_model(model, args) targets = [] losses = [] for i in range(len(mask_model.outputs)): targets.append( Input(mask_model.outputs[i].shape[1:], name=args["name"] + "_" + str(i))) layer_weight = args["layers_weights"][i] / len(args["layers"]) layer_loss = loss_l(layer_weight)([targets[i], mask_model.outputs[i]]) losses.append(layer_loss) loss = Lambda(lambda x: K.expand_dims(K.sum(x)) * args["weight"])(losses) loss_model = Model(mask_model.inputs + targets, loss) return loss_model, cpu_mask_model, mask_inputs + targets
def build(args): vgg = model_vgg.build(args) model = extract_layers(vgg, args["style_layers"]) mask_model, mask_inputs = build_mask_model(model) targets = [] losses = [] for i in range(len(mask_model.outputs)): targets.append( Input(mask_model.outputs[i].shape[1:], name="wtf" + str(i))) layer_weight = args["style_lw"][i] / len(args["style_layers"]) layer_loss = loss_l(layer_weight)([targets[i], mask_model.outputs[i]]) losses.append(layer_loss) loss = Lambda(lambda x: K.expand_dims(K.sum(x)) * args["style_w"])(losses) loss_model = Model(mask_model.inputs + targets, loss) return loss_model, mask_model, mask_inputs + targets
def build(args): vgg = model_vgg.build(args) model = extract_layers(vgg, args["content_layers"]) content_layer = content_l() model = attach_models(model, content_layer) targets = [] losses = [] for i in range(len(model.outputs)): targets.append(Input(model.outputs[i].shape[1:])) layer_weight = 1 layer_loss = loss_l(layer_weight)([targets[i], model.outputs[i]]) losses.append(layer_loss) loss = Lambda(lambda x: K.expand_dims(K.sum(x)) / len(losses) * args[ "content_w"])(losses) loss_model = Model(model.inputs + targets, loss) return loss_model, model, targets