Skip to content

Instantly share code, notes, and snippets.

View hadifar's full-sized avatar
:octocat:

Amir Hadifar hadifar

:octocat:
View GitHub Profile
model = MyModel()
checkpoint = tf.train.Checkpoint(myModel=model)
checkpoint.save('./test/model.ckpt')
del model
model = MyModel()
checkpoint = tf.train.Checkpoint(myModel=model)
checkpoint.restore(tf.train.latest_checkpoint('./test/'))
app = Flask(__name__)
cors = CORS(app)
@app.route("/api/predict", methods=['POST'])
def predict():
start = time.time()
data = request.data.decode("utf-8")
if data == "":
DEF_SERVING_DEF_KEY = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
tensor_x = model.signature_def[DEF_SERVING_DEF_KEY].inputs['x'].name # return Placeholder:0
tensor_y = model.signature_def[DEF_SERVING_DEF_KEY].outputs['y'].name # return 'dense/BiasAdd:0'
x = tf.get_default_graph().get_tensor_by_name(tensor_x)
y = tf.get_default_graph().get_tensor_by_name(tensor_y)
import tensorflow as tf
session = tf.Session()
for item in tf.get_default_graph().get_operations():
print(item)
print(50*'-')
model = tf.saved_model.loader.load(sess=session,
tags=[tf.saved_model.tag_constants.SERVING],
export_dir='./test/')
import tensorflow as tf
session = tf.Session()
for item in tf.get_default_graph().get_operations():
print(item)
print(50*'-')
model = tf.saved_model.loader.load(sess=session,
tags=[tf.saved_model.tag_constants.SERVING],
export_dir='./test/')
model = tf.saved_model.loader.load(sess=session,
tags=[tf.saved_model.tag_constants.SERVING],
export_dir='./test/')
import numpy as np
import tensorflow as tf
# create dummy data
X_raw = np.array([2013, 2014, 2015, 2016, 2017], dtype=np.float32)
y_raw = np.array([12000, 14000, 15000, 16500, 17500], dtype=np.float32)
X_data = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min())
Y_data = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min())
X_data = np.expand_dims(X_data, axis=1)
Y_data = np.expand_dims(Y_data, axis=1)
from keras.models import load_model
model.save('my_model.h5')
del model # deletes the existing model
# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')
checkpoint = ModelCheckpoint('./test/, monitor='val_acc')
model.fit(x_train, y_train,
callbacks=[checkpoint])
with tf.Session() as sess:
tf.saved_model.loader.load(sess, [tag_constants.TRAINING], './test/')