-
Notifications
You must be signed in to change notification settings - Fork 1
/
hops-v01-built_in.py
92 lines (74 loc) · 2.43 KB
/
hops-v01-built_in.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from typing import Literal
from flask import Flask
import ghhops_server as hs
import rhino3dm
import numpy as np
import os
import sys
import tensorflow as tf
from PIL import Image
from matplotlib import pyplot as plt
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
app = Flask(__name__)
hops = hs.Hops(app)
@hops.component(
"/runml",
name="runml",
description="Run Machine Learning prediction",
icon="examples/pointat.png",
inputs=[
hs.HopsCurve("Curve", "C", "Curve to evaluate"),
hs.HopsNumber("t", "t", "Parameter on Curve to evaluate"),
],
outputs=[
hs.HopsPoint("P", "P", "Point on curve at t"),
]
)
def run_ml():
new_model = tf.keras.models.load_model("D:\German Profile\Desktop\PREDICTION MODEL\pattern_model.h5")
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")
if __name__ == "__main__":
app.run()