Upload with huggingface_hub
Browse files- Dockerfile +11 -0
- app.py +72 -0
- requirements.txt +4 -0
- test.py +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.8-slim-buster
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip3 install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
CMD ["python3", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.applications import MobileNetV2
|
| 6 |
+
from tensorflow.keras.layers import Conv2DTranspose, Concatenate, Activation
|
| 7 |
+
from tensorflow.keras.models import Model
|
| 8 |
+
|
| 9 |
+
def load_model():
|
| 10 |
+
base_model = MobileNetV2(input_shape=(None, None, 3), include_top=False)
|
| 11 |
+
layer_names = [
|
| 12 |
+
'block_1_expand_relu', # 64x64
|
| 13 |
+
'block_3_expand_relu', # 32x32
|
| 14 |
+
'block_6_expand_relu', # 16x16
|
| 15 |
+
'block_13_expand_relu', # 8x8
|
| 16 |
+
'block_16_project', # 4x4
|
| 17 |
+
]
|
| 18 |
+
layers = [base_model.get_layer(name).output for name in layer_names]
|
| 19 |
+
down_stack = Model(inputs=base_model.input, outputs=layers)
|
| 20 |
+
down_stack.trainable = False
|
| 21 |
+
|
| 22 |
+
up_stack = [
|
| 23 |
+
Conv2DTranspose(512, 3, strides=2, padding='same'),
|
| 24 |
+
Concatenate(),
|
| 25 |
+
Activation('relu'),
|
| 26 |
+
Conv2DTranspose(256, 3, strides=2, padding='same'),
|
| 27 |
+
Concatenate(),
|
| 28 |
+
Activation('relu'),
|
| 29 |
+
Conv2DTranspose(128, 3, strides=2, padding='same'),
|
| 30 |
+
Concatenate(),
|
| 31 |
+
Activation('relu'),
|
| 32 |
+
Conv2DTranspose(64, 3, strides=2, padding='same'),
|
| 33 |
+
Concatenate(),
|
| 34 |
+
Activation('relu'),
|
| 35 |
+
Conv2DTranspose(3, 3, strides=2, padding='same'),
|
| 36 |
+
Activation('sigmoid'),
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
inputs = tf.keras.layers.Input(shape=[None, None, 3])
|
| 40 |
+
x = inputs
|
| 41 |
+
|
| 42 |
+
skips = down_stack(x)
|
| 43 |
+
skips = reversed(skips[:-1])
|
| 44 |
+
|
| 45 |
+
for up, skip in zip(up_stack, skips):
|
| 46 |
+
x = up(x)
|
| 47 |
+
concat = Concatenate()
|
| 48 |
+
x = concat([x, skip])
|
| 49 |
+
|
| 50 |
+
model = Model(inputs=inputs, outputs=x)
|
| 51 |
+
|
| 52 |
+
return model
|
| 53 |
+
|
| 54 |
+
model = load_model()
|
| 55 |
+
|
| 56 |
+
def sketch_image(img):
|
| 57 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 58 |
+
img = img / 255.0
|
| 59 |
+
img = cv2.resize(img, (256, 256))
|
| 60 |
+
img = np.expand_dims(img, axis=0)
|
| 61 |
+
output = model.predict(img)
|
| 62 |
+
output = np.squeeze(output, axis=0)
|
| 63 |
+
output = cv2.resize(output, (img.shape[2], img.shape[1]))
|
| 64 |
+
output = np.clip(output * 255, 0, 255).astype('uint8')
|
| 65 |
+
return output
|
| 66 |
+
|
| 67 |
+
title = "Picture to Drawing"
|
| 68 |
+
description = "Turn your pictures into beautiful drawings!"
|
| 69 |
+
inputs = gr.inputs.Image(label="Input Image")
|
| 70 |
+
outputs = gr.outputs.Image(label="Output Image")
|
| 71 |
+
examples = [['examples/1.jpg'], ['examples/2.jpg'], ['examples/3.jpg']]
|
| 72 |
+
gr.Interface(sketch_image, inputs, outputs, title=title, description=description, examples=examples).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python-headless
|
| 3 |
+
numpy
|
| 4 |
+
tensorflow==2.5.0
|
test.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import app
|
| 2 |
+
|
| 3 |
+
def test_sketch_image():
|
| 4 |
+
img = cv2.imread('examples/1.jpg')
|
| 5 |
+
output = app.sketch_image(img)
|
| 6 |
+
assert output.shape == img.shape
|
| 7 |
+
|
| 8 |
+
test_sketch_image()
|