Exemplo n.º 1
0
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
Exemplo n.º 3
0
    # Core Model Setup
    #--------------------------------------------------------------------------
    # Build
    global model
    input_img = keras.layers.Input(shape=(image_nrows, image_ncols,
                                          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),
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))