BillyAggarwal commited on
Commit
51e1870
·
verified ·
1 Parent(s): cc00226

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py CHANGED
@@ -10,6 +10,33 @@ vgg.trainable = False
10
  STYLE_LAYERS = [...] # same layers as in your notebook
11
  CONTENT_LAYER = [...] # e.g. [('block5_conv4', 1)]
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def preprocess(img):
14
  img = Image.fromarray(img).resize((256, 256))
15
  arr = np.expand_dims(np.array(img) / 255.0, axis=0).astype(np.float32)
 
10
  STYLE_LAYERS = [...] # same layers as in your notebook
11
  CONTENT_LAYER = [...] # e.g. [('block5_conv4', 1)]
12
 
13
+
14
+ def gram_matrix(A):
15
+ """Compute Gram matrix for style representation."""
16
+ A = tf.transpose(A, (0, 3, 1, 2)) # (batch, channels, height, width)
17
+ features = tf.reshape(A, (A.shape[0], A.shape[1], -1))
18
+ gram = tf.matmul(features, features, transpose_b=True)
19
+ return gram / tf.cast(tf.shape(features)[-1], tf.float32)
20
+
21
+ def compute_content_cost(a_C, a_G):
22
+ """Content cost between content and generated image features."""
23
+ return tf.reduce_mean(tf.square(a_C - a_G))
24
+
25
+ def compute_style_cost(a_S, a_G):
26
+ """Style cost using Gram matrices."""
27
+ J_style = 0
28
+ for s, g in zip(a_S, a_G):
29
+ J_style += tf.reduce_mean(tf.square(gram_matrix(s) - gram_matrix(g)))
30
+ return J_style / len(a_S)
31
+
32
+ def total_cost(J_content, J_style, alpha=10, beta=40):
33
+ """Weighted sum of content + style."""
34
+ return alpha * J_content + beta * J_style
35
+
36
+ def clip_0_1(img):
37
+ """Keep pixel values in [0,1]."""
38
+ return tf.clip_by_value(img, 0.0, 1.0)
39
+
40
  def preprocess(img):
41
  img = Image.fromarray(img).resize((256, 256))
42
  arr = np.expand_dims(np.array(img) / 255.0, axis=0).astype(np.float32)