Initialize a tensorflow model in main(), pass it to __init__ and execute inside another method
up vote
0
down vote
favorite
I want to build a web service with flask where multiple deep learning models will be applied to certain types of data to give back a result. Currently, I want to load them locally on main() once at start, pass them to init to just initialize them once when the execution of the script starts and then call it every time it is needed to perform a forward pass to return something. So far that's what I ve done with the rest but I don't know how to handle a pure tensorflow model initialization. The below code works fine. Any Suggestions, alterations are appreciated:
def evaluate_sample(numpy_array, no_of_frames):
_IMAGE_SIZE = 224
_SAMPLE_VIDEO_FRAMES = no_of_frames
_CHECKPOINT_PATHS = {'flow': 'data/checkpoints/flow_scratch/model.ckpt'}
NUM_CLASSES = 400
flow_input = tf.placeholder(
tf.float32,
shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
with tf.variable_scope('Flow', reuse=tf.AUTO_REUSE):
flow_model = i3d.InceptionI3d(NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
flow_logits, _ = flow_model(flow_input, is_training=False, dropout_keep_prob=1.0)
flow_variable_map = {}
for variable in tf.global_variables():
if variable.name.split('/')[0] == 'Flow':
flow_variable_map[variable.name.replace(':0', '')] = variable
flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)
model_logits = flow_logits
model_predictions = tf.nn.softmax(model_logits)
with tf.Session() as sess:
feed_dict = {}
flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
flow_sample = numpy_array
feed_dict[flow_input] = flow_sample
out_logits, out_predictions = sess.run(
[model_logits, model_predictions],
feed_dict=feed_dict)
logits2=np.asarray(out_logits)
return logits2
def get_flow_features(video_path):
.....
aggregated_flow_vector = evaluate_sample(final_np_cropped_flow, len(all_frames_flow))
.....
class GetOutOfContext:
def __init__(self, keras_model, pytorch_model, word2vec_model, max_pooling):
self.keras_model = keras_model
self.pytorch_model = pytorch_model
self.word2vec_model = word2vec_model
self.max_pooling = max_pooling
#self.kineticsi3d = kineticsi3d
print("Similarity Between Video and Text Service Initialized...")
def get(self):
dirpath = tempfile.mkdtemp()+"/"
video_path = download_video(url,dirpath)
aggregated_audio = get_audio_features(video_path)
aggregated_flow = get_flow_features(video_path)
aggregated_video = get_visual_features(video_path, dirpath)
aggregated_text = get_word_features(text)
.......
if __name__ == "__main__":
"""Loading Prediction Model"""
video_modality_dim = {'face': (128,128), 'audio': (128*16,128),'visual': (2048,2048), 'motion': (1024,1024)}
the_model = Net(video_modality_dim, 300, audio_cluster=16)
the_model.load_state_dict(torch.load('/home/estathop/Desktop/journalmodel/msrvttjournal.pt', map_location=lambda storage, loc: storage))
the_model.eval()
"""Loading Image Feature Extraction Model"""
model = ResNet152(include_top=False, weights='imagenet', pooling = 'avg') #cons
"""Loading Word2Vec Model"""
model2 = api.load("word2vec-google-news-300")
maxpoolingmodel = keras.layers.pooling.GlobalMaxPooling1D()
word_vectors = model2.wv
nltk.download('stopwords')
x = GetOutOfContext(model,the_model,model2, maxpoolingmodel)
y = x.get()
python web-services tensorflow initialization multiple-models
add a comment |
up vote
0
down vote
favorite
I want to build a web service with flask where multiple deep learning models will be applied to certain types of data to give back a result. Currently, I want to load them locally on main() once at start, pass them to init to just initialize them once when the execution of the script starts and then call it every time it is needed to perform a forward pass to return something. So far that's what I ve done with the rest but I don't know how to handle a pure tensorflow model initialization. The below code works fine. Any Suggestions, alterations are appreciated:
def evaluate_sample(numpy_array, no_of_frames):
_IMAGE_SIZE = 224
_SAMPLE_VIDEO_FRAMES = no_of_frames
_CHECKPOINT_PATHS = {'flow': 'data/checkpoints/flow_scratch/model.ckpt'}
NUM_CLASSES = 400
flow_input = tf.placeholder(
tf.float32,
shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
with tf.variable_scope('Flow', reuse=tf.AUTO_REUSE):
flow_model = i3d.InceptionI3d(NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
flow_logits, _ = flow_model(flow_input, is_training=False, dropout_keep_prob=1.0)
flow_variable_map = {}
for variable in tf.global_variables():
if variable.name.split('/')[0] == 'Flow':
flow_variable_map[variable.name.replace(':0', '')] = variable
flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)
model_logits = flow_logits
model_predictions = tf.nn.softmax(model_logits)
with tf.Session() as sess:
feed_dict = {}
flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
flow_sample = numpy_array
feed_dict[flow_input] = flow_sample
out_logits, out_predictions = sess.run(
[model_logits, model_predictions],
feed_dict=feed_dict)
logits2=np.asarray(out_logits)
return logits2
def get_flow_features(video_path):
.....
aggregated_flow_vector = evaluate_sample(final_np_cropped_flow, len(all_frames_flow))
.....
class GetOutOfContext:
def __init__(self, keras_model, pytorch_model, word2vec_model, max_pooling):
self.keras_model = keras_model
self.pytorch_model = pytorch_model
self.word2vec_model = word2vec_model
self.max_pooling = max_pooling
#self.kineticsi3d = kineticsi3d
print("Similarity Between Video and Text Service Initialized...")
def get(self):
dirpath = tempfile.mkdtemp()+"/"
video_path = download_video(url,dirpath)
aggregated_audio = get_audio_features(video_path)
aggregated_flow = get_flow_features(video_path)
aggregated_video = get_visual_features(video_path, dirpath)
aggregated_text = get_word_features(text)
.......
if __name__ == "__main__":
"""Loading Prediction Model"""
video_modality_dim = {'face': (128,128), 'audio': (128*16,128),'visual': (2048,2048), 'motion': (1024,1024)}
the_model = Net(video_modality_dim, 300, audio_cluster=16)
the_model.load_state_dict(torch.load('/home/estathop/Desktop/journalmodel/msrvttjournal.pt', map_location=lambda storage, loc: storage))
the_model.eval()
"""Loading Image Feature Extraction Model"""
model = ResNet152(include_top=False, weights='imagenet', pooling = 'avg') #cons
"""Loading Word2Vec Model"""
model2 = api.load("word2vec-google-news-300")
maxpoolingmodel = keras.layers.pooling.GlobalMaxPooling1D()
word_vectors = model2.wv
nltk.download('stopwords')
x = GetOutOfContext(model,the_model,model2, maxpoolingmodel)
y = x.get()
python web-services tensorflow initialization multiple-models
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to build a web service with flask where multiple deep learning models will be applied to certain types of data to give back a result. Currently, I want to load them locally on main() once at start, pass them to init to just initialize them once when the execution of the script starts and then call it every time it is needed to perform a forward pass to return something. So far that's what I ve done with the rest but I don't know how to handle a pure tensorflow model initialization. The below code works fine. Any Suggestions, alterations are appreciated:
def evaluate_sample(numpy_array, no_of_frames):
_IMAGE_SIZE = 224
_SAMPLE_VIDEO_FRAMES = no_of_frames
_CHECKPOINT_PATHS = {'flow': 'data/checkpoints/flow_scratch/model.ckpt'}
NUM_CLASSES = 400
flow_input = tf.placeholder(
tf.float32,
shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
with tf.variable_scope('Flow', reuse=tf.AUTO_REUSE):
flow_model = i3d.InceptionI3d(NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
flow_logits, _ = flow_model(flow_input, is_training=False, dropout_keep_prob=1.0)
flow_variable_map = {}
for variable in tf.global_variables():
if variable.name.split('/')[0] == 'Flow':
flow_variable_map[variable.name.replace(':0', '')] = variable
flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)
model_logits = flow_logits
model_predictions = tf.nn.softmax(model_logits)
with tf.Session() as sess:
feed_dict = {}
flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
flow_sample = numpy_array
feed_dict[flow_input] = flow_sample
out_logits, out_predictions = sess.run(
[model_logits, model_predictions],
feed_dict=feed_dict)
logits2=np.asarray(out_logits)
return logits2
def get_flow_features(video_path):
.....
aggregated_flow_vector = evaluate_sample(final_np_cropped_flow, len(all_frames_flow))
.....
class GetOutOfContext:
def __init__(self, keras_model, pytorch_model, word2vec_model, max_pooling):
self.keras_model = keras_model
self.pytorch_model = pytorch_model
self.word2vec_model = word2vec_model
self.max_pooling = max_pooling
#self.kineticsi3d = kineticsi3d
print("Similarity Between Video and Text Service Initialized...")
def get(self):
dirpath = tempfile.mkdtemp()+"/"
video_path = download_video(url,dirpath)
aggregated_audio = get_audio_features(video_path)
aggregated_flow = get_flow_features(video_path)
aggregated_video = get_visual_features(video_path, dirpath)
aggregated_text = get_word_features(text)
.......
if __name__ == "__main__":
"""Loading Prediction Model"""
video_modality_dim = {'face': (128,128), 'audio': (128*16,128),'visual': (2048,2048), 'motion': (1024,1024)}
the_model = Net(video_modality_dim, 300, audio_cluster=16)
the_model.load_state_dict(torch.load('/home/estathop/Desktop/journalmodel/msrvttjournal.pt', map_location=lambda storage, loc: storage))
the_model.eval()
"""Loading Image Feature Extraction Model"""
model = ResNet152(include_top=False, weights='imagenet', pooling = 'avg') #cons
"""Loading Word2Vec Model"""
model2 = api.load("word2vec-google-news-300")
maxpoolingmodel = keras.layers.pooling.GlobalMaxPooling1D()
word_vectors = model2.wv
nltk.download('stopwords')
x = GetOutOfContext(model,the_model,model2, maxpoolingmodel)
y = x.get()
python web-services tensorflow initialization multiple-models
I want to build a web service with flask where multiple deep learning models will be applied to certain types of data to give back a result. Currently, I want to load them locally on main() once at start, pass them to init to just initialize them once when the execution of the script starts and then call it every time it is needed to perform a forward pass to return something. So far that's what I ve done with the rest but I don't know how to handle a pure tensorflow model initialization. The below code works fine. Any Suggestions, alterations are appreciated:
def evaluate_sample(numpy_array, no_of_frames):
_IMAGE_SIZE = 224
_SAMPLE_VIDEO_FRAMES = no_of_frames
_CHECKPOINT_PATHS = {'flow': 'data/checkpoints/flow_scratch/model.ckpt'}
NUM_CLASSES = 400
flow_input = tf.placeholder(
tf.float32,
shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
with tf.variable_scope('Flow', reuse=tf.AUTO_REUSE):
flow_model = i3d.InceptionI3d(NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
flow_logits, _ = flow_model(flow_input, is_training=False, dropout_keep_prob=1.0)
flow_variable_map = {}
for variable in tf.global_variables():
if variable.name.split('/')[0] == 'Flow':
flow_variable_map[variable.name.replace(':0', '')] = variable
flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)
model_logits = flow_logits
model_predictions = tf.nn.softmax(model_logits)
with tf.Session() as sess:
feed_dict = {}
flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
flow_sample = numpy_array
feed_dict[flow_input] = flow_sample
out_logits, out_predictions = sess.run(
[model_logits, model_predictions],
feed_dict=feed_dict)
logits2=np.asarray(out_logits)
return logits2
def get_flow_features(video_path):
.....
aggregated_flow_vector = evaluate_sample(final_np_cropped_flow, len(all_frames_flow))
.....
class GetOutOfContext:
def __init__(self, keras_model, pytorch_model, word2vec_model, max_pooling):
self.keras_model = keras_model
self.pytorch_model = pytorch_model
self.word2vec_model = word2vec_model
self.max_pooling = max_pooling
#self.kineticsi3d = kineticsi3d
print("Similarity Between Video and Text Service Initialized...")
def get(self):
dirpath = tempfile.mkdtemp()+"/"
video_path = download_video(url,dirpath)
aggregated_audio = get_audio_features(video_path)
aggregated_flow = get_flow_features(video_path)
aggregated_video = get_visual_features(video_path, dirpath)
aggregated_text = get_word_features(text)
.......
if __name__ == "__main__":
"""Loading Prediction Model"""
video_modality_dim = {'face': (128,128), 'audio': (128*16,128),'visual': (2048,2048), 'motion': (1024,1024)}
the_model = Net(video_modality_dim, 300, audio_cluster=16)
the_model.load_state_dict(torch.load('/home/estathop/Desktop/journalmodel/msrvttjournal.pt', map_location=lambda storage, loc: storage))
the_model.eval()
"""Loading Image Feature Extraction Model"""
model = ResNet152(include_top=False, weights='imagenet', pooling = 'avg') #cons
"""Loading Word2Vec Model"""
model2 = api.load("word2vec-google-news-300")
maxpoolingmodel = keras.layers.pooling.GlobalMaxPooling1D()
word_vectors = model2.wv
nltk.download('stopwords')
x = GetOutOfContext(model,the_model,model2, maxpoolingmodel)
y = x.get()
python web-services tensorflow initialization multiple-models
python web-services tensorflow initialization multiple-models
asked Nov 7 at 15:42
Evan
277
277
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
In your current code, the model is defined and loaded in evaluate_sample
, you can simply move the majority of the code from evaluate_sample
to main or init and pass the tf.Session object and the graph to evaluate_sample
.
Here is a clumsy example:
import tensorflow as tf
def main():
sess=tf.Session()
a=tf.placeholder(tf.float32)
b=tf.constant(5.0)
c=a+b
evaluate_sample(sess,c,a)
def evaluate_sample(session,graph,input):
print(session.run(graph,feed_dict={input:3}))
if __name__ == "__main__":
main()
add a comment |
up vote
1
down vote
I'd hold on to the Session
and just run
multiple times. saver.restore
should happen just once. For error checking you can tf.get_default_graph().finalize()
after you specify the model to make sure the graph isn't changing each request, which would slow things down.
add a comment |
up vote
0
down vote
Are you trying to load a pretrained model and run an inference? By initializing are you referring to loading a model or initializing new weights for each instance this is executed?
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In your current code, the model is defined and loaded in evaluate_sample
, you can simply move the majority of the code from evaluate_sample
to main or init and pass the tf.Session object and the graph to evaluate_sample
.
Here is a clumsy example:
import tensorflow as tf
def main():
sess=tf.Session()
a=tf.placeholder(tf.float32)
b=tf.constant(5.0)
c=a+b
evaluate_sample(sess,c,a)
def evaluate_sample(session,graph,input):
print(session.run(graph,feed_dict={input:3}))
if __name__ == "__main__":
main()
add a comment |
up vote
1
down vote
accepted
In your current code, the model is defined and loaded in evaluate_sample
, you can simply move the majority of the code from evaluate_sample
to main or init and pass the tf.Session object and the graph to evaluate_sample
.
Here is a clumsy example:
import tensorflow as tf
def main():
sess=tf.Session()
a=tf.placeholder(tf.float32)
b=tf.constant(5.0)
c=a+b
evaluate_sample(sess,c,a)
def evaluate_sample(session,graph,input):
print(session.run(graph,feed_dict={input:3}))
if __name__ == "__main__":
main()
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In your current code, the model is defined and loaded in evaluate_sample
, you can simply move the majority of the code from evaluate_sample
to main or init and pass the tf.Session object and the graph to evaluate_sample
.
Here is a clumsy example:
import tensorflow as tf
def main():
sess=tf.Session()
a=tf.placeholder(tf.float32)
b=tf.constant(5.0)
c=a+b
evaluate_sample(sess,c,a)
def evaluate_sample(session,graph,input):
print(session.run(graph,feed_dict={input:3}))
if __name__ == "__main__":
main()
In your current code, the model is defined and loaded in evaluate_sample
, you can simply move the majority of the code from evaluate_sample
to main or init and pass the tf.Session object and the graph to evaluate_sample
.
Here is a clumsy example:
import tensorflow as tf
def main():
sess=tf.Session()
a=tf.placeholder(tf.float32)
b=tf.constant(5.0)
c=a+b
evaluate_sample(sess,c,a)
def evaluate_sample(session,graph,input):
print(session.run(graph,feed_dict={input:3}))
if __name__ == "__main__":
main()
answered Nov 17 at 0:49
Qin Heyang
963
963
add a comment |
add a comment |
up vote
1
down vote
I'd hold on to the Session
and just run
multiple times. saver.restore
should happen just once. For error checking you can tf.get_default_graph().finalize()
after you specify the model to make sure the graph isn't changing each request, which would slow things down.
add a comment |
up vote
1
down vote
I'd hold on to the Session
and just run
multiple times. saver.restore
should happen just once. For error checking you can tf.get_default_graph().finalize()
after you specify the model to make sure the graph isn't changing each request, which would slow things down.
add a comment |
up vote
1
down vote
up vote
1
down vote
I'd hold on to the Session
and just run
multiple times. saver.restore
should happen just once. For error checking you can tf.get_default_graph().finalize()
after you specify the model to make sure the graph isn't changing each request, which would slow things down.
I'd hold on to the Session
and just run
multiple times. saver.restore
should happen just once. For error checking you can tf.get_default_graph().finalize()
after you specify the model to make sure the graph isn't changing each request, which would slow things down.
answered Nov 17 at 0:22
Allen Lavoie
4,3511721
4,3511721
add a comment |
add a comment |
up vote
0
down vote
Are you trying to load a pretrained model and run an inference? By initializing are you referring to loading a model or initializing new weights for each instance this is executed?
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
add a comment |
up vote
0
down vote
Are you trying to load a pretrained model and run an inference? By initializing are you referring to loading a model or initializing new weights for each instance this is executed?
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
add a comment |
up vote
0
down vote
up vote
0
down vote
Are you trying to load a pretrained model and run an inference? By initializing are you referring to loading a model or initializing new weights for each instance this is executed?
Are you trying to load a pretrained model and run an inference? By initializing are you referring to loading a model or initializing new weights for each instance this is executed?
answered Nov 7 at 16:36
vr9494
516
516
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
add a comment |
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
I am trying to load a pre-trained model once at the start and run inference many times on demand.But I want this to happen in a structure way within main() and init , just like with keras_model, pytorch_model, max_pooling and word2vec
– Evan
Nov 8 at 7:53
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192824%2finitialize-a-tensorflow-model-in-main-pass-it-to-init-and-execute-inside%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown