def styleTransfer(sourcepath, stylepath): path = 'static/uploads' base_image_path = os.path.join(path, "source.png") style_reference_image_path = os.path.join(path, "style.png") result_prefix = "static/results/result" global content_weight global total_variation_weight global style_weight total_variation_weight = 1e-6 style_weight = 3e-6 content_weight = 5e-7 width, height = image.load_img(base_image_path).size global img_nrows global img_ncols img_nrows = 400 img_ncols = int(width * img_nrows / height) model = VGG19(weights="imagenet", include_top=False) outputs_dict = dict([(layer.name, layer.output) for layer in model.layers]) global feature_extractor feature_extractor = Model(inputs=model.inputs, outputs=outputs_dict) global style_layer_names style_layer_names = [ "block1_conv1", "block2_conv1", "block3_conv1", "block4_conv1", "block5_conv1", ] global content_layer_name content_layer_name = "block5_conv2" optimizer = SGD(schedules.ExponentialDecay( initial_learning_rate=100.0, decay_steps=100, decay_rate=0.96)) base_image = preprocess_image(base_image_path) style_reference_image = preprocess_image(style_reference_image_path) combination_image = tf.Variable(preprocess_image(base_image_path)) iterations = 100 for i in range(1, iterations + 1): loss, grads = compute_loss_and_grads( combination_image, base_image, style_reference_image) optimizer.apply_gradients([(grads, combination_image)]) print("Iteration %d: loss=%.2f" % (i, (loss))) print(combination_image) img = deprocess_image(combination_image.numpy()) fname = result_prefix + ".png" image.save_img(fname, img)
img_nrows = 400 img_ncols = int(width * img_nrows / height) optimizer = SGD( keras.optimizers.schedules.ExponentialDecay( initial_learning_rate=100.0, decay_steps=100, decay_rate=0.96 ) ) base_image = preprocess_image(base_image_path) style_reference_image = preprocess_image(style_image_path) combination_image = tf.Variable(preprocess_image(base_image_path)) iterations = 4000 for i in tqdm(range(1, iterations + 1)): loss, grads = compute_loss_and_grads( combination_image, base_image, style_reference_image ) optimizer.apply_gradients([(grads, combination_image)]) if i % 10 == 0: print("Iteration get_ipython().run_line_magic("d:", " loss=%.2f\" % (i, loss))") result_saver(i)