-
Notifications
You must be signed in to change notification settings - Fork 1
/
runPython.py
70 lines (56 loc) · 1.75 KB
/
runPython.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
62
63
64
65
66
67
68
69
70
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import sys
import tensorflow as tf
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
#FIND DEPENDENCIES
path_model = "pattern_model.h5"
print("Dependencies OK")
#LOAD MODEL
new_model = tf.keras.models.load_model(path_model)
#new_model.summary()
print("Model OK")
#LOAD QUERY
query_flat = np.loadtxt("query.txt") #load
query = np.reshape(query_flat,(128,128,3))
query = query.astype(np.float32)
query_norm = np.multiply(query, 2)
query_norm = np.subtract(query_norm , 1) #normalize -1 to 1, it is already 0 to 1
query_t = tf.convert_to_tensor(query_norm)
query_t = tf.expand_dims(query_t, 0)
#MAKE PREDICTION
IMG_WIDTH = 128
IMG_HEIGHT = 128
CHANNELS = 3
def plot(tensor):
array = np.asarray(tensor)
array = array.astype('float64')
array = array.reshape(IMG_HEIGHT, IMG_WIDTH,CHANNELS)
return array
def generate_images(model, test_input):
prediction = model(test_input, training=True)
# remap to [0,1]
test_input = test_input * 0.5 + 0.5
prediction = prediction * 0.5 + 0.5
display_list = [plot(test_input), plot(prediction)]
title = ['Input Image', 'Predicted Image']
for i in range(2):
plt.subplot(1, 2, i + 1)
plt.title(title[i])
# getting the pixel values between [0, 1] to plot it.
plt.imshow(display_list[i])
plt.axis('off')
plt.draw()
plt.savefig("D:\German Profile\Desktop\PREDICTION MODEL\log.png")
return prediction
pred = generate_images(new_model,query_t)
print("Prediction OK")
#SAVE TO FOLDER
pred = np.reshape(pred,(128,128,3))
np.savetxt("prediction.txt", pred.flatten())
print("text Saved")
img = Image.fromarray(np.uint8(pred * 255), 'RGB')
img.save("pred_image.png")
print("imagesaved")