Skip to content

Commit

Permalink
prediction from frozen (.pb)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkuse committed Jun 18, 2019
1 parent 1e428d6 commit c051c48
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
16 changes: 10 additions & 6 deletions noveou_train_netvlad_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ def on_epoch_start(self):

self.D = self.pr.step_n_times(n_samples=self.n_samples_pitts, nP=self.nP, nN=self.nN, resize=self.resize, return_gray=self.return_gray, ENABLE_IMSHOW=self.ENABLE_IMSHOW )
self.y = np.zeros( len(self.D) )
print 'len(D)=', len(self.D), '\tD[0].shape=', self.D[0].shape
print 'epoch=', self.epoch, '\tlen(D)=', len(self.D), '\tD[0].shape=', self.D[0].shape


# if self.epoch > 400:
if self.epoch > 400 and self.n_samples_pitts<0:
if self.epoch > 10:
# if self.epoch > 400 and self.n_samples_pitts<0:
# Data Augmentation after 400 epochs.
print tcolor.WARNING, 'do_typical_data_aug', tcolor.ENDC
self.D = do_typical_data_aug( self.D )
else:
print tcolor.WARNING, 'NO Data Augmentation', tcolor.ENDC


print 'returned len(self.D)=', len(self.D), 'self.D[0].shape=', self.D[0].shape
self.y = np.zeros( len(self.D) )
Expand Down Expand Up @@ -153,7 +157,7 @@ def signal_handler(sig, frame):
#TODO: somehow get access to current epoch number and write the mdoel file accordingly
print('You pressed Ctrl+C!')
print 'Save Current Model : ', int_logr.dir() + '/core_modelX.keras'
model.save( int_logr.dir() + '/core_modelX.keras' )
# model.save( int_logr.dir() + '/core_modelX.keras' )
model.save( int_logr.dir() + '/modelarch_and_weights.X.h5' )
sys.exit(0)

Expand All @@ -174,7 +178,7 @@ def signal_handler(sig, frame):
#---
image_nrows = 240
image_ncols = 320
image_nchnl = 3 #cannot make this to 1 if u want to use VGG-pretained-weights (from imagenet), bcoz imagenet was trained with color images. However this is usable (can set chanls=1) if you want to train from scratch
image_nchnl = 1 #cannot make this to 1 if u want to use VGG-pretained-weights (from imagenet), bcoz imagenet was trained with color images. However this is usable (can set chanls=1) if you want to train from scratch

#---
# note: some other thing thats need to be set
Expand All @@ -183,7 +187,7 @@ def signal_handler(sig, frame):

CNN_type = 'mobilenetv2' #'mobilenet', 'vgg16', 'mobilenetv2'
layer_name= 'block_9_add' #'conv_pw_7_relu', 'block5_pool', 'block_9_add'
init_model_weights = 'imagenet' #'imagenet', None. imagenet only nchanls=3
init_model_weights = None #'imagenet', None. imagenet only nchanls=3

nP = 6
nN = 6
Expand Down
61 changes: 61 additions & 0 deletions test_frozengraph_predictions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Load a .pb (Frozen protobuf) and do inference.
# https://www.dlology.com/blog/how-to-convert-trained-keras-model-to-tensorflow-and-make-prediction/



import TerminalColors
tcol = TerminalColors.bcolors()

import tensorflow as tf
from tensorflow.python.platform import gfile
from keras import backend as K

import numpy as np
import time

PB_PATH = 'models.keras/June2019/centeredinput-m1to1-240x320x3__mobilenetv2-block_9_add__K16__allpairloss/'
PB_FNAME = PB_PATH+'/'+'output_model.pb'


#---
# Load .pb (protobuf file)
print tcol.OKGREEN , 'READ: ', PB_FNAME, tcol.ENDC
f = gfile.FastGFile(PB_FNAME, 'rb')
graph_def = tf.GraphDef()
# Parses a serialized binary message into the current message.
graph_def.ParseFromString(f.read())
f.close()

#---
# Setup computation graph
sess = K.get_session()
sess.graph.as_default()
# Import a serialized TensorFlow `GraphDef` protocol buffer
# and place into the current default `Graph`.
tf.import_graph_def(graph_def)


#---
# Print the graph
print tcol.OKGREEN, "=== All Nodes in tf.graph ===",tcol.ENDC
for name in [n.name for n in tf.get_default_graph().as_graph_def().node]:
print name
print tcol.OKGREEN, "=== END All Nodes in tf.graph ===", tcol.ENDC
print 'note: The input/output tensors will have the name as opname:0 for example'


#---
# Prediction
print tcol.OKGREEN, "=== sess.run ===",tcol.ENDC
softmax_tensor = sess.graph.get_tensor_by_name('import/net_vlad_layer_1/l2_normalize_1:0')

x_test = np.random.random( (1,480,640,3) )

n_inference = 100
start_t = time.time()
for _ in range(n_inference): #do 10 inferences
predictions = sess.run(softmax_tensor, {'import/input_1:0': x_test})
end_t = time.time()

print tcol.BOLD, 'x_test.shape=', x_test.shape , '---->' , 'predictions.shape=', predictions.shape, tcol.ENDC
print n_inference, ' inference took: (ms)', 1000.* (end_t - start_t )
4 changes: 2 additions & 2 deletions util_keras-h5-model_to-tensorflow-pb_to-nvinfer-uff.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ def verify_generated_uff_with_tensorrt_uffparser( ufffilename ):

#-----
# Write UFF
convert_to_uff( pb_input_fname=LOG_DIR+'/output_model.pb', uff_output_fname=LOG_DIR+'/output_nvinfer.uff' )
#convert_to_uff( pb_input_fname=LOG_DIR+'/output_model.pb', uff_output_fname=LOG_DIR+'/output_nvinfer.uff' )
# convert_to_uff( pb_input_fname=LOG_DIR+'/output_model_aftersurgery.pb', uff_output_fname=LOG_DIR+'/output_nvinfer.uff' )


#-----
# Try to load UFF with tensorrt
verify_generated_uff_with_tensorrt_uffparser( ufffilename=LOG_DIR+'/output_nvinfer.uff' )
#verify_generated_uff_with_tensorrt_uffparser( ufffilename=LOG_DIR+'/output_nvinfer.uff' )
15 changes: 9 additions & 6 deletions util_kerasmodel_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import TerminalColors
tcol = TerminalColors.bcolors()

import tensorflow as tf

def load_keras_hdf5_model( kerasmodel_h5file, verbose=True ):

def load_keras_hdf5_model( kerasmodel_h5file, verbose=True, inference_only=False ):
""" Loads keras model from a HDF5 file """
assert os.path.isfile( kerasmodel_h5file ), 'The model weights file doesnot exists or there is a permission issue.'+"kerasmodel_file="+kerasmodel_h5file
# K.set_learning_phase(0)
if inference_only:
K.set_learning_phase(0)

model = keras.models.load_model(kerasmodel_h5file, custom_objects={'NetVLADLayer': NetVLADLayer, 'GhostVLADLayer': GhostVLADLayer} )

Expand Down Expand Up @@ -68,15 +71,15 @@ def load_keras_hdf5_model( kerasmodel_h5file, verbose=True ):
#---
# Change Shape
new_input_shape= ( None, nrows, ncols, model.input.shape[3].value )
new_model = change_model_inputshape( model, new_input_shape=new_input_shape )

new_model = change_model_inputshape( model, new_input_shape=new_input_shape, verbose=True )
new_model.summary()


#---
# Model Save
new_model_fname = '.'.join( kerasmodel_h5file.split( '.' )[0:-1] )+'.%dx%dx%d.h5' %(new_input_shape[1], new_input_shape[2], new_input_shape[3])
print '====\n====Save new_model to:', tcol.OKBLUE, new_model_fname, tcol.ENDC, '\n===='
import code
code.interact( local=locals() )
# import code
# code.interact( local=locals() )
new_model.save( new_model_fname )
print tcol.OKGREEN+'====DONE===='+tcol.ENDC
1 change: 1 addition & 0 deletions util_kerasmodel_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def do_inference_on_random_input( model ):
#---
# Load HDF5 Keras model
model = load_keras_hdf5_model( kerasmodel_h5file, verbose=True ) #this
model.summary()



Expand Down

0 comments on commit c051c48

Please sign in to comment.