Spaces:
Running
Running
Initial commits
Browse files- app.py +129 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install gradio
|
| 2 |
+
# Import necessary libs
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import keras
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load the VGG19 model
|
| 8 |
+
from keras.applications.vgg19 import VGG19
|
| 9 |
+
from keras.applications.vgg19 import preprocess_input, decode_predictions
|
| 10 |
+
|
| 11 |
+
model = VGG19(include_top=False, weights='imagenet')
|
| 12 |
+
|
| 13 |
+
# Define style layers and content layers for VGG19
|
| 14 |
+
content_layers = 'block5_conv2'
|
| 15 |
+
|
| 16 |
+
style_layers = ['block1_conv1',
|
| 17 |
+
'block2_conv1',
|
| 18 |
+
'block3_conv1',
|
| 19 |
+
'block4_conv1',
|
| 20 |
+
'block5_conv1']
|
| 21 |
+
|
| 22 |
+
num_content_layers = len(content_layers)
|
| 23 |
+
num_style_layers = len(style_layers)
|
| 24 |
+
|
| 25 |
+
# Model for extracting style feature
|
| 26 |
+
style_models = [
|
| 27 |
+
keras.Model(inputs=model.input, outputs=model.get_layer(layer).output) for layer in style_layers
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Model for extracting content feature
|
| 31 |
+
content_model = keras.Model(inputs=model.input, outputs=model.get_layer(content_layers).output, name='Content_model')
|
| 32 |
+
|
| 33 |
+
# Style models summary
|
| 34 |
+
for m in style_models:
|
| 35 |
+
print(f'Style model: {m.name}')
|
| 36 |
+
print(m.summary())
|
| 37 |
+
|
| 38 |
+
# Content model summary
|
| 39 |
+
print(f'Content model: {content_model.name}')
|
| 40 |
+
content_model.summary()
|
| 41 |
+
|
| 42 |
+
# Compute content loss
|
| 43 |
+
def content_loss(gen, content_img):
|
| 44 |
+
return tf.reduce_sum(tf.square(content_img - gen))
|
| 45 |
+
|
| 46 |
+
# Gram matrix
|
| 47 |
+
def gram_matrix(image):
|
| 48 |
+
# Reshape image to 2-D array
|
| 49 |
+
channel = image.shape[-1] # Number of channels
|
| 50 |
+
a = tf.reshape(image, [-1, channel])
|
| 51 |
+
# Compute Gram matrix corresponding to the input image
|
| 52 |
+
dimension = a.shape[0] # Dimension M*N of an MxNxc image
|
| 53 |
+
return tf.matmul(a, a, transpose_a=True) / tf.cast(dimension, tf.float32)
|
| 54 |
+
|
| 55 |
+
# Style loss
|
| 56 |
+
def style_loss(gen, style_img):
|
| 57 |
+
# Compute Gram matrix of generated image and style image
|
| 58 |
+
gen_gram = gram_matrix(gen)
|
| 59 |
+
style_gram = gram_matrix(style_img)
|
| 60 |
+
# Compute style loss
|
| 61 |
+
return tf.reduce_sum(tf.square(gen_gram-style_gram))/4
|
| 62 |
+
|
| 63 |
+
# Training parameter
|
| 64 |
+
lr = 10.
|
| 65 |
+
optimizer = keras.optimizers.Adam(lr)
|
| 66 |
+
alpha = 1e-7
|
| 67 |
+
beta = 1e-10
|
| 68 |
+
style_weight = len(style_layers)
|
| 69 |
+
|
| 70 |
+
# Train step
|
| 71 |
+
@tf.function(reduce_retracing=True)
|
| 72 |
+
def train_step(image, process_content_image, process_style_image):
|
| 73 |
+
with tf.GradientTape() as tape:
|
| 74 |
+
# Calculate content loss
|
| 75 |
+
gen_content_feature = content_model(image)
|
| 76 |
+
content_feature = content_model(process_content_image)
|
| 77 |
+
loss_content = content_loss(gen_content_feature, content_feature)
|
| 78 |
+
# Calculate style loss
|
| 79 |
+
loss_style = 0
|
| 80 |
+
for style_model in style_models:
|
| 81 |
+
gen_style_feature = style_model(image)
|
| 82 |
+
style_feature = style_model(process_style_image)
|
| 83 |
+
loss_style += style_loss(gen_style_feature, style_feature)
|
| 84 |
+
# Calculate total loss
|
| 85 |
+
total_loss = alpha*loss_content + beta*loss_style/style_weight
|
| 86 |
+
# Calculate gradient
|
| 87 |
+
grad = tape.gradient(total_loss, image)
|
| 88 |
+
# Apply gradient
|
| 89 |
+
optimizer.apply_gradients([(grad, image)])
|
| 90 |
+
# Return loss for visualize
|
| 91 |
+
return total_loss
|
| 92 |
+
|
| 93 |
+
# Deprocess image
|
| 94 |
+
def deprocess_image(x):
|
| 95 |
+
# Util function to convert a tensor into a valid image
|
| 96 |
+
x = x.reshape((256,256,3))
|
| 97 |
+
# Remove zero-center by mean pixel
|
| 98 |
+
x[:, :, 0] += 103.939
|
| 99 |
+
x[:, :, 1] += 116.779
|
| 100 |
+
x[:, :, 2] += 123.68
|
| 101 |
+
# 'BGR'->'RGB'
|
| 102 |
+
x = x[:, :, ::-1]
|
| 103 |
+
x = np.clip(x, 0, 255).astype("uint8")
|
| 104 |
+
return x
|
| 105 |
+
|
| 106 |
+
def load_image(image, RESO=256):
|
| 107 |
+
image = tf.image.convert_image_dtype(image, tf.float32) * 255
|
| 108 |
+
image = tf.image.resize(image,[RESO, RESO],method=tf.image.ResizeMethod.BICUBIC)
|
| 109 |
+
image = tf.expand_dims(preprocess_input(image), 0)
|
| 110 |
+
return image
|
| 111 |
+
|
| 112 |
+
def style_transfer(content_image, style_image, step=100):
|
| 113 |
+
process_content_image = load_image(content_image)
|
| 114 |
+
process_style_image = load_image(style_image)
|
| 115 |
+
generate_image = tf.Variable(process_content_image, trainable=True)
|
| 116 |
+
for step in range(1, step+1):
|
| 117 |
+
# Train step
|
| 118 |
+
loss = train_step(generate_image, process_content_image, process_style_image)
|
| 119 |
+
return deprocess_image(generate_image[0].numpy())
|
| 120 |
+
|
| 121 |
+
import gradio as gr
|
| 122 |
+
|
| 123 |
+
demo = gr.Interface(
|
| 124 |
+
fn=style_transfer,
|
| 125 |
+
inputs=[gr.Image(label='Input Image'), gr.Image(label='Style Image'), gr.Slider(10, 200, 50, step=10, label='Step', show_label=True)],
|
| 126 |
+
outputs=gr.Image(label='Style Transfer Image'),
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
demo.launch(debug=True, share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
keras
|