python - Using two different models in tensorflow -
im trying use 2 different mobilenet models. following code of how initialize model.
def initialsetup(): os.environ['tf_cpp_min_log_level'] = '2' start_time = timeit.default_timer() # takes 2-5 seconds run # unpersists graph file tf.gfile.fastgfile('age/output_graph.pb', 'rb') f: age_graph_def = tf.graphdef() age_graph_def.parsefromstring(f.read()) tf.import_graph_def(age_graph_def, name='') tf.gfile.fastgfile('output_graph.pb', 'rb') f: gender_graph_def = tf.graphdef() gender_graph_def.parsefromstring(f.read()) tf.import_graph_def(gender_graph_def, name='') print ('took {} seconds unpersist graph'.format(timeit.default_timer() - start_time))
since both 2 different models, how use predictions?
update
initialsetup() age_session = tf.session(graph=age_graph_def) gender_session = tf.session(graph=gender_graph_def) tf.session() sess: start_time = timeit.default_timer() # feed image_data input graph , first prediction softmax_tensor = age_session.graph.get_tensor_by_name('final_result:0') print ('took {} seconds feed data graph'.format(timeit.default_timer() - start_time)) while true: # capture frame-by-frame ret, frame = video_capture.read()
error
traceback (most recent call last): file "c:/users/desktop/untitled/testimg/testimg/combo.py", line 48, in age_session = tf.session(graph=age_graph_def) file "c:\program files\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1292, in init super(session, self).init(target, graph, config=config) file "c:\program files\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 529, in init raise typeerror('graph must tf.graph, got %s' % type(graph)) typeerror: graph must tf.graph, got exception ignored in: > traceback (most recent call last): file "c:\program files\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 587, in del if self._session not none: attributeerror: 'session' object has no attribute '_session'
when working multiple models in same graph, use name scoping give individual tensors predictable names. example, rewrite initial_setup()
follows:
def initialsetup(): os.environ['tf_cpp_min_log_level'] = '2' start_time = timeit.default_timer() # takes 2-5 seconds run # unpersists graph file tf.gfile.fastgfile('age/output_graph.pb', 'rb') f: age_graph_def = tf.graphdef() age_graph_def.parsefromstring(f.read()) tf.import_graph_def(age_graph_def, name='age_model') tf.gfile.fastgfile('output_graph.pb', 'rb') f: gender_graph_def = tf.graphdef() gender_graph_def.parsefromstring(f.read()) tf.import_graph_def(gender_graph_def, name='gender_model') print ('took {} seconds unpersist graph'.format(timeit.default_timer() - start_time))
now names of of nodes age_graph_def
prefixed "age_model/"
, names of of nodes gender_graph_def
prefixed "gender_model/"
. part of same default graph, can use single tf.session
no graph
argument access either model.
initialsetup() tf.session() sess: start_time = timeit.default_timer() # feed image_data input graph , first prediction softmax_tensor = sess.graph.get_tensor_by_name('age_model/final_result:0') # alternatively, tensor gender model: # tensor = sess.graph.get_tensor_by_name('gender_model/...') print ('took {} seconds feed data graph'.format(timeit.default_timer() - start_time)) while true: # capture frame-by-frame ret, frame = video_capture.read()
Comments
Post a Comment