def make_keras_model_org_netvlad(im_rows=240, im_cols=320, im_chnls=3): # im_rows = 240 # im_cols = 320 # im_chnls = 3 input_img = keras.layers.Input(batch_shape=(1, im_rows, im_cols, im_chnls)) cnn = make_from_vgg16(input_img, weights=None, layer_name='block5_pool') out, out_amap = NetVLADLayer(num_clusters=64)(cnn) model = keras.models.Model(inputs=input_img, outputs=out) return model
def make_keras_model_relja_netvlad(DATA_DIR, im_rows=240, im_cols=320, im_chnls=3): # im_rows = 240 # im_cols = 320 # im_chnls = 3 input_img = keras.layers.Input(batch_shape=(1, im_rows, im_cols, im_chnls)) cnn = make_from_vgg16(input_img, weights=None, layer_name='block5_pool') out, out_amap = NetVLADLayer(num_clusters=64)(cnn) model = keras.models.Model(inputs=input_img, outputs=out) model.load_weights(DATA_DIR + '/matlab_model.keras') WPCA_M = scipy.io.loadmat(DATA_DIR + '/WPCA_1.mat')['the_mat'] # 1x1x32768x4096 WPCA_b = scipy.io.loadmat(DATA_DIR + '/WPCA_2.mat')['the_mat'] # 4096x1 WPCA_M = WPCA_M[0, 0] # 32768x4096 WPCA_b = np.transpose(WPCA_b) #1x4096 return model, WPCA_M, WPCA_b
def load_basic_model(): K.set_learning_phase(0) from CustomNets import make_from_mobilenet, make_from_vgg16 from CustomNets import NetVLADLayer, GhostVLADLayer # Please choose only one of these. if False: # VGG input_img = keras.layers.Input(shape=(240, 320, 3)) cnn = make_from_vgg16(input_img, weights=None, layer_name='block5_pool', kernel_regularizer=keras.regularizers.l2(0.01)) model = keras.models.Model(inputs=input_img, outputs=cnn) if True: #mobilenet input_img = keras.layers.Input(shape=(240, 320, 3)) cnn = make_from_mobilenet( input_img, layer_name='conv_pw_5_relu', weights=None, kernel_regularizer=keras.regularizers.l2(0.01)) model = keras.models.Model(inputs=input_img, outputs=cnn) if False: #mobilenet+netvlad input_img = keras.layers.Input(shape=(240, 320, 3)) cnn = make_from_mobilenet( input_img, layer_name='conv_pw_5_relu', weights=None, kernel_regularizer=keras.regularizers.l2(0.01)) # cnn = make_from_vgg16( input_img, weights=None, layer_name='block5_pool', kernel_regularizer=keras.regularizers.l2(0.01) ) out = NetVLADLayer(num_clusters=16)(cnn) model = keras.models.Model(inputs=input_img, outputs=out) if False: #netvlad only input_img = keras.layers.Input(shape=(60, 80, 256)) out = NetVLADLayer(num_clusters=16)(input_img) model = keras.models.Model(inputs=input_img, outputs=out) model.summary() return model
image_nchnl)) if CNN_type == 'mobilenet' or CNN_type == 'vgg16' or CNN_type == 'mobilenetv2': pass else: assert (False) # weights=imagenet will only work with nchanls=3 cnn = None if CNN_type == 'mobilenet': cnn = make_from_mobilenet( input_img, layer_name=layer_name,\ weights=init_model_weights, kernel_regularizer=keras.regularizers.l2(0.01) ) if CNN_type == 'vgg16': cnn = make_from_vgg16(input_img, weights=init_model_weights, layer_name=layer_name, kernel_regularizer=keras.regularizers.l2(0.001)) if CNN_type == 'mobilenetv2': cnn = make_from_mobilenetv2( input_img, layer_name=layer_name,\ weights=init_model_weights, kernel_regularizer=keras.regularizers.l2(0.01) ) # Reduce nChannels of the output. # @ Downsample (Optional) if False: #Downsample last layer (Reduce nChannels of the output.) cnn_dwn = keras.layers.Conv2D(256, (1, 1), padding='same', activation='relu')(cnn) cnn_dwn = keras.layers.normalization.BatchNormalization()(cnn_dwn) cnn_dwn = keras.layers.Conv2D(64, (1, 1), padding='same',
# int_logr = InteractiveLogger( './models.keras/mobilenet_conv7_quash_chnls_allpairloss/' ) # int_logr = InteractiveLogger( './models.keras/mobilenet_conv7_quash_chnls_tripletloss2_K64/' ) # int_logr = InteractiveLogger( './models.keras/vgg16/block5_pool_k48_tripletloss2' ) int_logr = InteractiveLogger( './models.keras/vgg16_new/block5_pool_k16_allpairloss') #-------------------------------------------------------------------------- # Core Model Setup #-------------------------------------------------------------------------- # Build input_img = keras.layers.Input(shape=(image_nrows, image_ncols, image_nchnl)) # cnn = make_from_mobilenet( input_img ) cnn = make_from_vgg16(input_img, layer_name='block5_pool') # Reduce nChannels of the output. # @ Downsample (Optional) if False: #Downsample last layer (Reduce nChannels of the output.) cnn_dwn = keras.layers.Conv2D(256, (1, 1), padding='same', activation='relu')(cnn) cnn_dwn = keras.layers.normalization.BatchNormalization()(cnn_dwn) cnn_dwn = keras.layers.Conv2D(32, (1, 1), padding='same', activation='relu')(cnn_dwn) cnn_dwn = keras.layers.normalization.BatchNormalization()(cnn_dwn) cnn = cnn_dwn out, out_amap = NetVLADLayer(num_clusters=16)(cnn) model = keras.models.Model(inputs=input_img, outputs=out)
def do_demo(): im_rows = 240 im_cols = 320 im_chnls = 3 im_rows = 480 im_cols = 640 im_chnls = 3 ##------ Create Model (from json file) # [[[[[Option-A]]]]] (load from json) this doesn't work. seem like i need to implement get_config in my custom layer which I am not able to do correctly, TODO fix in the future # json_fname = base_path+'/model.json' # print 'Read model json: ', json_fname # with open(json_fname, 'r') as myfile: # model_json_string=myfile.read() # model = keras.models.model_from_json( model_json_string, custom_objects={'NetVLADLayer': NetVLADLayer} ) # [[[[[Option-B]]]]] (from h5 files). Apparently h5 stores the model config and the weights. # h5_fname = base_path+'/model.h5' # model = keras.models.load_model( h5_fname, custom_objects={'NetVLADLayer': NetVLADLayer} ) # [[[[[Option-C]]]]] : Construct manually # @ Input # input_img = keras.layers.Input( shape=(480, 640, 3 ) ) # input_img = keras.layers.Input( shape=(240, 320, 3 ) ) # input_img = keras.layers.Input( batch_shape=(1,480, 640, 3 ) ) input_img = keras.layers.Input(batch_shape=(1, im_rows, im_cols, im_chnls)) # @ CNN cnn = make_from_vgg16(input_img, weights=None, layer_name='block5_pool') # cnn = make_from_mobilenet( input_img, weights=None, layer_name='conv_pw_13_relu' ) # @ Downsample (Optional) if False: #Downsample last layer (Reduce nChannels of the output.) cnn_dwn = keras.layers.Conv2D(256, (1, 1), padding='same', activation='relu')(cnn) cnn_dwn = keras.layers.normalization.BatchNormalization()(cnn_dwn) cnn_dwn = keras.layers.Conv2D(32, (1, 1), padding='same', activation='relu')(cnn_dwn) cnn_dwn = keras.layers.normalization.BatchNormalization()(cnn_dwn) cnn = cnn_dwn # @ NetVLADLayer out, out_amap = NetVLADLayer(num_clusters=16)(cnn) model = keras.models.Model(inputs=input_img, outputs=out) ##---------- Print Model Info, Memory Usage, FLOPS if True: # Set this to `True` to display FLOPs, memory etc . model.summary() # print_flops_report( model ) # print_model_memory_usage( 1, model ) print 'input_shape=%s\toutput_shape=%s' % (model.input_shape, model.output_shape) ##------------ Load Weights model_file = './models.keras/vgg16_new/block5_pool_k16_allpairloss/core_model.1000.keras' print 'load_weights=', model_file model.load_weights(model_file) code.interact(local=locals()) ##------------model.predict # Load your image here. tmp_zer = np.random.rand(1, im_rows, im_cols, im_chnls).astype('float32') start = time.time() tmp_zer_out = model.predict(tmp_zer) print 'Exec in %4.2fms' % (1000. * (time.time() - start)) tmp_zer = np.random.rand(1, im_rows, im_cols, im_chnls).astype('float32') start = time.time() tmp_zer_out = model.predict(tmp_zer) print 'Exec in %4.2fms' % (1000. * (time.time() - start))