-
Notifications
You must be signed in to change notification settings - Fork 11
/
serving.py
61 lines (48 loc) · 1.75 KB
/
serving.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
from keras.preprocessing import image
from keras.preprocessing.image import array_to_img
from skimage.color import rgb2lab, lab2rgb, rgb2gray, gray2rgb
from support import (
load_pretrained_model,
create_inception_embedding,
)
INCEPTION_PATH = ('/colornet/models/'
'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5')
MODEL_PATH = '/colornet/models/color_tensorflow_real_mode_300.h5'
ALLOWED_EXTENSIONS = set(['jpg', 'png', 'jpeg'])
# MODELS
model = None
inception = None
def load_model():
"""Load the model"""
global model, inception
(model, inception) = load_pretrained_model(INCEPTION_PATH, MODEL_PATH)
def evaluate_input(input: str):
global model
(color_me, color_me_embed) = _data_preprocessing(input)
output = model.predict([color_me, color_me_embed])
# Rescale the output from [-1,1] to [-128, 128]
output = output * 128
# Output colorizations
for i in range(len(output)):
cur = np.zeros((256, 256, 3))
# LAB representation
cur[:, :, 0] = color_me[i][:, :, 0]
cur[:, :, 1:] = output[i]
img = array_to_img(lab2rgb(cur))
return img
def _data_preprocessing(input_filepath):
"""From RGB image to L(grayscale)"""
global inception
img = image.load_img(input_filepath, target_size=(256, 256))
img = image.img_to_array(img)
color_me = [img]
#################
# Preprocessing #
#################
# From RGB to B&W and embedding
color_me = np.array(color_me, dtype=float)
color_me_embed = create_inception_embedding(inception, gray2rgb(rgb2gray(1.0/255*color_me)))
color_me = rgb2lab(1.0/255*color_me)[:, :, :, 0]
color_me = color_me.reshape(color_me.shape+(1,))
return (color_me, color_me_embed)