sayakpaul HF staff commited on
Commit
a2944d5
1 Parent(s): e3d848e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +63 -6
README.md CHANGED
@@ -6,14 +6,71 @@ tags:
6
  ---
7
  # Whitebox Cartoonizer
8
 
9
- Whitebox Cartoonizer [1] model in the `SavedModel` format. The model was exported to the SavedModel format using this notebook. Original model repository can be found [here](https://github.com/SystemErrorWang/White-box-Cartoonization).
 
 
10
 
11
  ## Inference code
12
 
13
  ```py
14
- from huggingface_hub import hf_hub_download
15
- import tensorflow as tf
 
 
 
 
16
 
17
- root = tf.saved_model.load("/content/saved_model_dir")
18
- concrete_func = root.signatures["serving_default"]
19
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  ---
7
  # Whitebox Cartoonizer
8
 
9
+ Whitebox Cartoonizer [1] model in the `SavedModel` format. The model was exported to the SavedModel format using
10
+ [this notebook](https://huggingface.co/sayakpaul/whitebox-cartoonizer/blob/main/export-saved-model.ipynb). Original model
11
+ repository can be found [here](https://github.com/SystemErrorWang/White-box-Cartoonization).
12
 
13
  ## Inference code
14
 
15
  ```py
16
+ import cv2
17
+ import numpy as np
18
+ import requests
19
+ import tensorflow as tf
20
+ from huggingface_hub import snapshot_download
21
+ from PIL import Image
22
 
23
+
24
+ def resize_crop(image):
25
+ h, w, c = np.shape(image)
26
+ if min(h, w) > 720:
27
+ if h > w:
28
+ h, w = int(720 * h / w), 720
29
+ else:
30
+ h, w = 720, int(720 * w / h)
31
+ image = cv2.resize(image, (w, h), interpolation=cv2.INTER_AREA)
32
+ h, w = (h // 8) * 8, (w // 8) * 8
33
+ image = image[:h, :w, :]
34
+ return image
35
+
36
+
37
+ def download_image(url):
38
+ image = Image.open(requests.get(url, stream=True).raw)
39
+ image = image.convert("RGB")
40
+ image = np.array(image)
41
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
42
+ return image
43
+
44
+
45
+ def preprocess_image(image):
46
+ image = resize_crop(image)
47
+ image = image.astype(np.float32) / 127.5 - 1
48
+ image = np.expand_dims(image, axis=0)
49
+ image = tf.constant(image)
50
+ return image
51
+
52
+
53
+ # Load the model and extract concrete function.
54
+ model_path = snapshot_download("sayakpaul/whitebox-cartoonizer")
55
+ loaded_model = tf.saved_model.load(model_path)
56
+ concrete_func = loaded_model.signatures["serving_default"]
57
+
58
+ # Download and preprocess image.
59
+ image_url = "https://huggingface.co/spaces/sayakpaul/cartoonizer-demo-onnx/resolve/main/mountain.jpeg"
60
+ image = download_image(image_url)
61
+ preprocessed_image = preprocess_image(image)
62
+
63
+ # Run inference.
64
+ result = concrete_func(preprocessed_image)["final_output:0"]
65
+
66
+ # Post-process the result and serialize it.
67
+ output = (result[0].numpy() + 1.0) * 127.5
68
+ output = np.clip(output, 0, 255).astype(np.uint8)
69
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
70
+ output_image = Image.fromarray(output)
71
+ output_image.save("result.png")
72
+ ```
73
+
74
+ ## References
75
+
76
+ [1] Learning to Cartoonize Using White-box Cartoon Representations; Xinrui Wang and Jinze Yu; CVPR 2020.