Exemple #1
0
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 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())
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
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
Exemple #5
0
    # 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',
                                      activation='relu')(cnn_dwn)
        cnn_dwn = keras.layers.normalization.BatchNormalization()(cnn_dwn)
        cnn = cnn_dwn

    # out, out_amap = NetVLADLayer(num_clusters = 32)( cnn )
    out = NetVLADLayer(num_clusters=netvlad_num_clusters)(cnn)
    # out = GhostVLADLayer(num_clusters = netvlad_num_clusters, num_ghost_clusters=1)( cnn )
    model = keras.models.Model(inputs=input_img, outputs=out)

    # Plot
    model.summary()
    keras.utils.plot_model(model,
                           to_file=int_logr.dir() + '/core.png',
                           show_shapes=True)
    int_logr.save_model_as_json('model.json', model)
    # look at test_loadjsonmodel.py for demo on how to load model with json file.

    if initial_epoch > 0:
        # Load Previous Weights
        model.load_weights(int_logr.dir() + '/core_model.%d.keras' %
                           (initial_epoch))
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=64)(cnn)
    model = keras.models.Model(inputs=input_img, outputs=out)

    ##---------- Print Model Info, Memory Usage, FLOPS
    if False:  # 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.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))
    # 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)

    # Plot
    model.summary()
    keras.utils.plot_model(model,
                           to_file=int_logr.dir() + '/core.png',
                           show_shapes=True)
    int_logr.add_file('model.json', model.to_json())

    initial_epoch = 600
    if initial_epoch > 0:
        # Load Previous Weights
        model.load_weights(int_logr.dir() + '/core_model.%d.keras' %
                           (initial_epoch))
import keras
import numpy as np
import code
from CustomNets import NetVLADLayer, GhostVLADLayer

input_img = keras.layers.Input(shape=(60, 80, 256))
# out = GhostVLADLayer(num_clusters = 16, num_ghost_clusters = 1)( input_img )
out = NetVLADLayer(num_clusters=16)(input_img)
model = keras.models.Model(inputs=input_img, outputs=out)

model.predict(np.random.rand(1, 60, 80, 256).astype('float32'))