Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -16,98 +16,7 @@ width, height = keras.preprocessing.image.load_img(base_image_path).size
|
|
16 |
img_nrows = 400
|
17 |
img_ncols = int(width * img_nrows / height)
|
18 |
|
19 |
-
def preprocess_image(image_path):
|
20 |
-
# Util function to open, resize and format pictures into appropriate tensors
|
21 |
-
img = keras.preprocessing.image.load_img(
|
22 |
-
image_path, target_size=(img_nrows, img_ncols)
|
23 |
-
)
|
24 |
-
img = keras.preprocessing.image.img_to_array(img)
|
25 |
-
img = np.expand_dims(img, axis=0)
|
26 |
-
img = vgg19.preprocess_input(img)
|
27 |
-
return tf.convert_to_tensor(img)
|
28 |
-
|
29 |
-
def deprocess_image(x):
|
30 |
-
# Util function to convert a tensor into a valid image
|
31 |
-
x = x.reshape((img_nrows, img_ncols, 3))
|
32 |
-
# Remove zero-center by mean pixel
|
33 |
-
x[:, :, 0] += 103.939
|
34 |
-
x[:, :, 1] += 116.779
|
35 |
-
x[:, :, 2] += 123.68
|
36 |
-
# 'BGR'->'RGB'
|
37 |
-
x = x[:, :, ::-1]
|
38 |
-
x = np.clip(x, 0, 255).astype("uint8")
|
39 |
-
return x
|
40 |
-
|
41 |
-
# The gram matrix of an image tensor (feature-wise outer product)
|
42 |
-
|
43 |
-
def gram_matrix(x):
|
44 |
-
x = tf.transpose(x, (2, 0, 1))
|
45 |
-
features = tf.reshape(x, (tf.shape(x)[0], -1))
|
46 |
-
gram = tf.matmul(features, tf.transpose(features))
|
47 |
-
return gram
|
48 |
-
|
49 |
-
# The "style loss" is designed to maintain
|
50 |
-
# the style of the reference image in the generated image.
|
51 |
-
# It is based on the gram matrices (which capture style) of
|
52 |
-
# feature maps from the style reference image
|
53 |
-
# and from the generated image
|
54 |
-
|
55 |
-
def style_loss(style, combination):
|
56 |
-
S = gram_matrix(style)
|
57 |
-
C = gram_matrix(combination)
|
58 |
-
channels = 3
|
59 |
-
size = img_nrows * img_ncols
|
60 |
-
return tf.reduce_sum(tf.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2))
|
61 |
-
|
62 |
-
# An auxiliary loss function
|
63 |
-
# designed to maintain the "content" of the
|
64 |
-
# base image in the generated image
|
65 |
-
|
66 |
-
def content_loss(base, combination):
|
67 |
-
return tf.reduce_sum(tf.square(combination - base))
|
68 |
-
|
69 |
-
# The 3rd loss function, total variation loss,
|
70 |
-
# designed to keep the generated image locally coherent
|
71 |
-
|
72 |
-
def total_variation_loss(x):
|
73 |
-
a = tf.square(
|
74 |
-
x[:, : img_nrows - 1, : img_ncols - 1, :] - x[:, 1:, : img_ncols - 1, :]
|
75 |
-
)
|
76 |
-
b = tf.square(
|
77 |
-
x[:, : img_nrows - 1, : img_ncols - 1, :] - x[:, : img_nrows - 1, 1:, :]
|
78 |
-
)
|
79 |
-
return tf.reduce_sum(tf.pow(a + b, 1.25))
|
80 |
-
|
81 |
-
def compute_loss(combination_image, base_image, style_reference_image):
|
82 |
-
input_tensor = tf.concat(
|
83 |
-
[base_image, style_reference_image, combination_image], axis=0
|
84 |
-
)
|
85 |
-
features = feature_extractor(input_tensor)
|
86 |
-
|
87 |
-
# Initialize the loss
|
88 |
-
loss = tf.zeros(shape=())
|
89 |
-
|
90 |
-
# Add content loss
|
91 |
-
layer_features = features[content_layer_name]
|
92 |
-
base_image_features = layer_features[0, :, :, :]
|
93 |
-
combination_features = layer_features[2, :, :, :]
|
94 |
-
loss = loss + content_weight * content_loss(
|
95 |
-
base_image_features, combination_features
|
96 |
-
)
|
97 |
-
# Add style loss
|
98 |
-
for layer_name in style_layer_names:
|
99 |
-
layer_features = features[layer_name]
|
100 |
-
style_reference_features = layer_features[1, :, :, :]
|
101 |
-
combination_features = layer_features[2, :, :, :]
|
102 |
-
sl = style_loss(style_reference_features, combination_features)
|
103 |
-
loss += (style_weight / len(style_layer_names)) * sl
|
104 |
-
|
105 |
-
# Add total variation loss
|
106 |
-
loss += total_variation_weight * total_variation_loss(combination_image)
|
107 |
-
return loss
|
108 |
-
|
109 |
# Build a VGG19 model loaded with pre-trained ImageNet weights
|
110 |
-
# model = vgg19.VGG19(weights="imagenet", include_top=False)
|
111 |
model = from_pretrained_keras("rushic24/keras-VGG19")
|
112 |
|
113 |
# Get the symbolic outputs of each "key" layer (we gave them unique names).
|
|
|
16 |
img_nrows = 400
|
17 |
img_ncols = int(width * img_nrows / height)
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Build a VGG19 model loaded with pre-trained ImageNet weights
|
|
|
20 |
model = from_pretrained_keras("rushic24/keras-VGG19")
|
21 |
|
22 |
# Get the symbolic outputs of each "key" layer (we gave them unique names).
|