leonelhs commited on
Commit
2454319
1 Parent(s): 29137eb

init space

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.jpg filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .idea/
2
+ __pycache__/
3
+ get.py
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from style_transfer import StyleTransfer
3
+
4
+ style = StyleTransfer()
5
+
6
+
7
+ def predict(content_image, style_image):
8
+ return style.transfer(content_image, style_image)
9
+
10
+
11
+ footer = r"""
12
+ <center>
13
+ <b>
14
+ Demo for <a href='https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization'>Style Transfer</a>
15
+ </b>
16
+ </center>
17
+ """
18
+
19
+ coffe = r"""
20
+ <center>
21
+ <a href="https://www.buymeacoffee.com/leonelhs"> <img
22
+ src="https://img.buymeacoffee.com/button-api/?text=Buy me a
23
+ coffee&emoji=&slug=leonelhs&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000
24
+ &coffee_colour=ffffff" /></a>
25
+ </center>
26
+ """
27
+
28
+ with gr.Blocks(title="Style Transfer") as app:
29
+ gr.HTML("<center><h1>Style Transfer</h1></center>")
30
+ gr.HTML("<center><h3>Fast Style Transfer for Arbitrary Styles</h3></center>")
31
+ with gr.Row(equal_height=False):
32
+ with gr.Column():
33
+ content_img = gr.Image(type="filepath", label="Content image")
34
+ style_img = gr.Image(type="filepath", label="Style image")
35
+ run_btn = gr.Button(variant="primary")
36
+ with gr.Column():
37
+ output_img = gr.Image(type="pil", label="Output image")
38
+ gr.ClearButton(components=[content_img, style_img, output_img], variant="stop")
39
+
40
+ run_btn.click(predict, [content_img, style_img], [output_img])
41
+
42
+ with gr.Row():
43
+ blobs_c = [[f"examples/contents/{x:02d}.jpg"] for x in range(1, 4)]
44
+ examples_c = gr.Dataset(components=[content_img], samples=blobs_c)
45
+ examples_c.click(lambda x: x[0], [examples_c], [content_img])
46
+ with gr.Row():
47
+ blobs_s = [[f"examples/styles/{x:02d}.jpg"] for x in range(1, 12)]
48
+ examples_s = gr.Dataset(components=[style_img], samples=blobs_s)
49
+ examples_s.click(lambda x: x[0], [examples_s], [style_img])
50
+
51
+ with gr.Row():
52
+ gr.HTML(footer)
53
+ with gr.Row():
54
+ gr.HTML(coffe)
55
+
56
+ app.launch(share=False, debug=True, show_error=True)
57
+ app.queue()
examples/contents/01.jpg ADDED

Git LFS Details

  • SHA256: c88c63f9701ea2148c6c7d8ba868eeef19146efe74a0f0ff88088920cc70284c
  • Pointer size: 130 Bytes
  • Size of remote file: 71.9 kB
examples/contents/02.jpg ADDED

Git LFS Details

  • SHA256: 94f936ca3890dd8c17148e92bdef643dfa5c22f864a67a48d349572f54fe2d65
  • Pointer size: 132 Bytes
  • Size of remote file: 3.17 MB
examples/contents/03.jpg ADDED

Git LFS Details

  • SHA256: e67d1869f01fd805385c681a80d9ebcf0f70ea4a5fe6127f0606bb1c7f4bf506
  • Pointer size: 131 Bytes
  • Size of remote file: 407 kB
examples/styles/01.jpg ADDED

Git LFS Details

  • SHA256: 44a8486abdf0330a3fe6b586e407506e738fe075fe0b5dfc43961e358fda7206
  • Pointer size: 132 Bytes
  • Size of remote file: 2.68 MB
examples/styles/02.jpg ADDED

Git LFS Details

  • SHA256: b1a03098f6c54fb697180227adaeaa16a9604e875a2407b6a330f485fdb0a4b0
  • Pointer size: 130 Bytes
  • Size of remote file: 32.4 kB
examples/styles/03.jpg ADDED

Git LFS Details

  • SHA256: ca6aa663af49ec6655fc9e1cce9bdbba68f07a8e49fbba26038462d43eeaadef
  • Pointer size: 131 Bytes
  • Size of remote file: 144 kB
examples/styles/04.jpg ADDED

Git LFS Details

  • SHA256: c05f9db0fcf412d343611e082d572058fd0858b425e743918996da620a1030e2
  • Pointer size: 131 Bytes
  • Size of remote file: 132 kB
examples/styles/05.jpg ADDED

Git LFS Details

  • SHA256: b72c83cc167ad2e35f31cb65079f4dd9ddc452a53d52334e82375a626b9782c7
  • Pointer size: 130 Bytes
  • Size of remote file: 83.4 kB
examples/styles/06.jpg ADDED

Git LFS Details

  • SHA256: 20f5590f7a9928b9da91e9a6050cf04fcfae2580a7b60843c56f36a208e24151
  • Pointer size: 131 Bytes
  • Size of remote file: 113 kB
examples/styles/07.jpg ADDED

Git LFS Details

  • SHA256: 3fd72aee2ccee24f8e9e767ab9f87236df8020c29d48c428d100c9b37b782742
  • Pointer size: 130 Bytes
  • Size of remote file: 78.8 kB
examples/styles/08.jpg ADDED

Git LFS Details

  • SHA256: a3a5012358de6d62fb24d6c57a0d6405fcd78cb142305386057283c009baf1b0
  • Pointer size: 131 Bytes
  • Size of remote file: 186 kB
examples/styles/09.jpg ADDED

Git LFS Details

  • SHA256: 4c946150547bbb2c142df03d63c35665ef7ca4b6afae03fe9cef78e19a6d30e0
  • Pointer size: 130 Bytes
  • Size of remote file: 66.3 kB
examples/styles/10.jpg ADDED

Git LFS Details

  • SHA256: d65ca553adb15eb44f36656550b283b3a653d1e87e6bf6938c483beb743f069d
  • Pointer size: 132 Bytes
  • Size of remote file: 1.91 MB
examples/styles/11.jpg ADDED

Git LFS Details

  • SHA256: a85edeab4985158e00913a7b782fdb7f16ac605ae84bb6305983d732f22f227b
  • Pointer size: 131 Bytes
  • Size of remote file: 120 kB
examples/styles/12.jpg ADDED

Git LFS Details

  • SHA256: a9bd171fe0a8da87f90a467e30542be195fb2e2a46d650f5287421869e57d390
  • Pointer size: 132 Bytes
  • Size of remote file: 4.22 MB
style_transfer.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #############################################################################
2
+ #
3
+ # Source from:
4
+ # https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization
5
+ # Forked from:
6
+ # Reimplemented by: Leonel Hernández
7
+ #
8
+ ##############################################################################
9
+
10
+ import PIL.Image
11
+ import numpy as np
12
+ import tensorflow as tf
13
+ from huggingface_hub import snapshot_download
14
+
15
+ print("TF Version: ", tf.__version__)
16
+ print("Eager mode enabled: ", tf.executing_eagerly())
17
+ print("GPU available: ", tf.config.list_physical_devices('GPU'))
18
+
19
+ STYLE_REPO_ID = "leonelhs/arbitrary-image-stylization-v1"
20
+
21
+
22
+ def crop_center(image):
23
+ """Returns a cropped square image."""
24
+ shape = image.shape
25
+ new_shape = min(shape[1], shape[2])
26
+ offset_y = max(shape[1] - shape[2], 0) // 2
27
+ offset_x = max(shape[2] - shape[1], 0) // 2
28
+ image = tf.image.crop_to_bounding_box(
29
+ image, offset_y, offset_x, new_shape, new_shape)
30
+ return image
31
+
32
+
33
+ def load_image(image_path, image_size=(256, 256)):
34
+ """Loads and preprocesses images."""
35
+ img = tf.io.decode_image(
36
+ tf.io.read_file(image_path),
37
+ channels=3, dtype=tf.float32)[tf.newaxis, ...]
38
+ img = crop_center(img)
39
+ img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)
40
+ return img
41
+
42
+
43
+ def tensor_to_image(tensor):
44
+ tensor = tensor * 255
45
+ tensor = np.array(tensor, dtype=np.uint8)
46
+ if np.ndim(tensor) > 3:
47
+ assert tensor.shape[0] == 1
48
+ tensor = tensor[0]
49
+ return PIL.Image.fromarray(tensor)
50
+
51
+
52
+ class StyleTransfer:
53
+
54
+ def __init__(self):
55
+ model_path = snapshot_download(STYLE_REPO_ID)
56
+ self.model = tf.saved_model.load(model_path)
57
+
58
+ def transfer(self, content_image, style_image):
59
+ content_image = load_image(content_image, (384, 384))
60
+ style_image = load_image(style_image, (256, 256))
61
+ style_image = tf.nn.avg_pool(style_image, ksize=[3, 3], strides=[1, 1], padding='SAME')
62
+ stylized_image = self.model(tf.constant(content_image), tf.constant(style_image))
63
+ return tensor_to_image(stylized_image[0])