def init_net(): """ The way to a)load json-formatted models b)loading weights c) sample prediction """ # LOG_DIR = 'models.keras/Apr2019/K16_gray_training/' LOG_DIR = '/app/datasets/models.keras/color_conv6_K16Ghost1__centeredinput/' # core_model.1000.keras # Load JSON formatted model json_string = open_json_file(LOG_DIR + '/model.json') print '=======================' pprint.pprint(json_string, indent=4) print '=======================' model = keras.models.model_from_json(str(json_string), custom_objects={ 'NetVLADLayer': NetVLADLayer, 'GhostVLADLayer': GhostVLADLayer }) print 'OLD MODEL: ' model.summary() # quit() # Load Weights from model-file model_fname = LOG_DIR + '/core_model.%d.keras' % (2000) print 'Load model: ', model_fname model.load_weights(model_fname) return model
def simple_load_demo(): """ The way to a)load json-formatted models b)loading weights c) sample prediction """ # LOG_DIR = 'models.keras/Apr2019/K16_gray_training/' LOG_DIR = 'models.keras/Apr2019/gray_conv6_K16Ghost1__centeredinput/' # Load JSON formatted model json_string = open_json_file( LOG_DIR+'/model.json' ) print '=======================' pprint.pprint( json_string, indent=4 ) print '=======================' model = keras.models.model_from_json(str(json_string), custom_objects={'NetVLADLayer': NetVLADLayer, 'GhostVLADLayer': GhostVLADLayer} ) print 'OLD MODEL: ' model.summary() quit() # Load Weights from model-file model_fname = LOG_DIR+'/core_model.%d.keras' %(100) print 'Load model: ', model_fname model.load_weights( model_fname ) # Replace Input Layer new_model = change_model_inputshape( model, new_input_shape=(1,1500,500,1) ) # Sample Predict # test new model on a random input image X = np.random.rand(new_input_shape[0], new_input_shape[1], new_input_shape[2], new_input_shape[3] ) y_pred = new_model.predict(X) print('try predict with a random input_img with shape='+str(X.shape)+ str(y_pred) )
def render_and_predict(): #---------- Setup model LOG_DIR = 'models.keras/Apr2019/gray_conv6_K16__centeredinput/' # Load json-model json_model_fname = LOG_DIR + '/model.json' print 'Load json-model: ', json_model_fname json_string = open_json_file(json_model_fname) model = keras.models.model_from_json( str(json_string), custom_objects={'NetVLADLayer': NetVLADLayer}) # Load Weights model_fname = LOG_DIR + '/core_model.%d.keras' % (400) print 'Load model: ', model_fname model.load_weights(model_fname) # change_model_inputshape new_model = change_model_inputshape(model, new_input_shape=(11, 480, 640, 1)) #--------------- Setup renderer PITS_VAL_PATH = '/Bulk_Data/data_Akihiko_Torii/Pitssburg_validation/' pr = PittsburgRenderer(PITS_VAL_PATH) for _ in range(100): print '---' a, b = pr.step(nP=5, nN=5, ENABLE_IMSHOW=False, return_gray=True, resize=(640, 480)) a = np.copy(a) #if you dont do a copy, keras.predict gives sigsegv # from CustomNets import do_typical_data_aug # a = do_typical_data_aug( a ) # Predict print 'a.shape=', a.shape, '\ta.dtype=', a.dtype a_pred = new_model.predict((a.astype('float32') - 128.) / 255.) # a0_pred = new_model.predict( np.expand_dims(a[0],0).astype('float32') ) # a_p0_pred = new_model.predict( np.expand_dims(a[1],0).astype('float32')) # a_n0_pred = new_model.predict(np.expand_dims(a[6],0).astype('float32') ) DOT = np.matmul(a_pred, a_pred[0]) print '<a, a0> ', DOT # imshow cv2.imshow('query', a[0].astype('uint8')) imshow_set('sim', a[1:6], str(DOT[1:6])) imshow_set('dif', a[6:], str(DOT[6:])) key = cv2.waitKey(0) if key == ord('q'): print 'Quit...' break code.interact(local=locals())
def compare_jsonload_with_staticload(): # Load Image # im = cv2.resize( cv2.imread( '/app/lena.jpg', 0 ), (320,240) ) im = cv2.imread('/app/lena.jpg', 0) im_rows, im_cols = im.shape[0:2] im_centered = (im - 128.) / 255. im_centered = np.expand_dims(np.expand_dims(im_centered, 0), -1) LOG_DIR = './models.keras/Apr2019/centeredinput-gray__mobilenet-conv6__K16__allpairloss/' # LOG_DIR = './models.keras/Apr2019/default_model/' # LOG_DIR = 'models.keras/Apr2019/gray_conv6_K16Ghost1__centeredinput/' model_fname = LOG_DIR + '/core_model.%d.keras' % (1000) # model_fname = LOG_DIR+'/mobilenet_conv7_allpairloss.keras' # Load JSON if True: json_string = open_json_file(LOG_DIR + '/model.json') model_json = keras.models.model_from_json(str(json_string), custom_objects={ 'NetVLADLayer': NetVLADLayer, 'GhostVLADLayer': GhostVLADLayer }) print 'Load model into `model_json`: ', model_fname model_json.load_weights(model_fname) model_json1 = change_model_inputshape(model_json, new_input_shape=(1, im_rows, im_cols, 1), verbose=True) # Load Static if True: image_nrows = im_rows #240 image_ncols = im_cols #320 image_nchnl = 1 netvlad_num_clusters = 16 input_img = keras.layers.Input(shape=(image_nrows, image_ncols, image_nchnl)) cnn = make_from_mobilenet(input_img, layer_name='conv_pw_7_relu', weights=None, kernel_regularizer=None, trainable=False) out = NetVLADLayer(num_clusters=netvlad_num_clusters)(cnn) model_static = keras.models.Model(inputs=input_img, outputs=out) print 'Load model into `model_json`: ', model_fname model_static.load_weights(model_fname) # a = model_json.predict( im_centered ) # b = model_static.predict( im_centered ) code.interact(local=locals())
from CustomNets import NetVLADLayer, GhostVLADLayer from CustomNets import make_from_mobilenet, make_from_vgg16 from predict_utils import open_json_file, change_model_inputshape LOG_DIR = 'models.keras/May2019/centeredinput-m1to1-240x320x1__mobilenet-conv_pw_7_relu__K16__allpairloss/' LOG_DIR = 'models.keras/' ##----------------------------------------------------------------------------## ## LOAD KERAS MODEL ##----------------------------------------------------------------------------## if False: # Load JSON formatted model model_json_fname = LOG_DIR + '/model.json' print 'Load model_json_fname:', model_json_fname json_string = open_json_file(model_json_fname) model = keras.models.model_from_json(str(json_string), custom_objects={ 'NetVLADLayer': NetVLADLayer, 'GhostVLADLayer': GhostVLADLayer }) if False: # printing print '=======================' pprint.pprint(json_string, indent=4) print '=======================' print 'OLD MODEL: ' model.summary() # Load Weights model_fname = LOG_DIR + '/core_model.%d.keras' % (1000) print 'Load Weights: ', model_fname
def __init__(self, kerasmodel_file, im_rows=600, im_cols=960, im_chnls=3): ## Build net from keras.backend.tensorflow_backend import set_session import tensorflow as tf config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.1 # config.gpu_options.visible_device_list = "0" set_session(tf.Session(config=config)) # Blackbox 4 # self.im_rows = 512 # self.im_cols = 640 # self.im_chnls = 3 # point grey # self.im_rows = 600 # self.im_cols = 960 # self.im_chnls = 3 # EuroC # self.im_rows = 480 # self.im_cols = 752 # self.im_chnls = 3 self.im_rows = int(im_rows) self.im_cols = int(im_cols) self.im_chnls = int(im_chnls) LOG_DIR = '/'.join(kerasmodel_file.split('/')[0:-1]) model_type = LOG_DIR.split('/')[-1] # code.interact( local=locals() ) assert os.path.isdir( LOG_DIR ), "The LOG_DIR doesnot exist of there is a permission issue. LOG_DIR=" + LOG_DIR assert os.path.isfile( LOG_DIR + '/model.json' ), "model.json does not exist in LOG_DIR=" + LOG_DIR + '. This file is needed to load the network architecture from json format. This file should have been created when you learned the model using script in github.com/mpkuse/cartwheel_train' #----- @ Load Model Structure from JSON # Load JSON formatted model json_string = open_json_file(LOG_DIR + '/model.json') # print '=======================' # pprint.pprint( json_string, indent=4 ) # print '=======================' model = keras.models.model_from_json(str(json_string), custom_objects={ 'NetVLADLayer': NetVLADLayer, 'GhostVLADLayer': GhostVLADLayer }) old_input_shape = model._layers[0].input_shape model._layers[0] print 'OLD MODEL: ', 'input_shape=', str(old_input_shape) model.summary() model_visual_fname = '/app/core.png' print 'Writing Model Visual to: ', model_visual_fname keras.utils.plot_model(model, to_file=model_visual_fname, show_shapes=True) #----- @ Load Weights assert os.path.isfile( kerasmodel_file ), 'The model weights file doesnot exists or there is a permission issue.' + "kerasmodel_file=" + kerasmodel_file model_fname = kerasmodel_file print tcol.OKGREEN, 'Load model: ', model_fname, tcol.ENDC model.load_weights(model_fname) # Replace Input Layer new_model = change_model_inputshape(model, new_input_shape=(1, self.im_rows, self.im_cols, self.im_chnls)) new_input_shape = new_model._layers[0].input_shape print 'OLD MODEL: ', 'input_shape=', str(old_input_shape) print 'NEW MODEL: input_shape=', str(new_input_shape) self.model = new_model self.model_type = model_type # Doing this is a hack to force keras to allocate GPU memory. Don't comment this, tmp_zer = np.zeros((1, self.im_rows, self.im_cols, self.im_chnls), dtype='float32') tmp_zer_out = self.model.predict(tmp_zer) print 'model input.shape=', tmp_zer.shape, '\toutput.shape=', tmp_zer_out.shape print 'model_type=', self.model_type print '-----' print '\tinput_image.shape=', tmp_zer.shape print '\toutput.shape=', tmp_zer_out.shape print '\tminmax=', np.min(tmp_zer_out), np.max(tmp_zer_out) print '\tnorm=', np.linalg.norm(tmp_zer_out) print '\tdtype=', tmp_zer_out.dtype print '-----'