-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8-working_example.py
30 lines (28 loc) · 937 Bytes
/
8-working_example.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
# example of using saved cyclegan models for image translation
from numpy import load
from numpy import expand_dims
from keras.models import load_model
from matplotlib import pyplot
# load an image to the preferred size
def load_image(filename, size=(256,256)):
# load and resize the image
pixels = load_img(filename, target_size=size)
# convert to numpy array
pixels = img_to_array(pixels)
# transform in a sample
pixels = expand_dims(pixels, 0)
# scale from [0,255] to [-1,1]
pixels = (pixels - 127.5) / 127.5
return pixels
# load the image
image_src = load_image('../dicaprio/licensed-image.jpeg')
# load the model
cust = {'InstanceNormalization': InstanceNormalization}
model_AtoB = load_model('../g_model_AtoB_000700.h5', cust)
# translate image
image_tar = model_AtoB.predict(image_src)
# scale from [-1,1] to [0,1]
image_tar = (image_tar + 1) / 2.0
# plot the translated image
pyplot.imshow(image_tar[0])
pyplot.show()