Exemple #1
0
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) )
Exemple #2
0
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())
Exemple #4
0
def model_config(model, im_w, im_h):  # Replace Input Layer
    new_input_shape = (1, im_w, im_h, 3)  #(1,480,640,1)
    new_model = change_model_inputshape(model, new_input_shape=new_input_shape)
    return new_model
    # model = load_basic_model()
    # quit()

    #-----
    # Replace Input Layer's Dimensions
    im_rows = None  #480
    im_cols = 752
    im_chnls = 3
    if im_rows == None or im_cols == None or im_chnls == None:
        print tcol.WARNING, 'NOT doing `change_model_inputshape`', tcol.ENDC
        new_model = model
    else:
        # change_model_inputshape uses model_from_json internally, I feel a bit uncomfortable about this.
        new_model = change_model_inputshape(model,
                                            new_input_shape=(1, im_rows,
                                                             im_cols,
                                                             im_chnls),
                                            verbose=True)
    print 'OLD MODEL: ', 'input_shape=', str(model.inputs)
    print 'NEW MODEL: input_shape=', str(new_model.inputs)

    #-----
    # Write Tensorflow (atleast 1.12) proto-binary (.pb)
    write_kerasmodel_as_tensorflow_pb(new_model,
                                      LOG_DIR=LOG_DIR,
                                      output_model_name='output_model.pb')
    # write_kerasmodel_as_tensorflow_pb( new_model, LOG_DIR=LOG_DIR, output_model_name=kerasmodel_h5file.split('/')[-1]+'.pb' )

    #-----
    # Clean up graph with Nvidia's graphsurgeon
    # currently not in use but might come in handly later...maybe
Exemple #6
0
    print '## kerasmodel_h5file = ', kerasmodel_h5file
    print '## LOG_DIR = ', LOG_DIR
    print '##------------------------------------------------------------##'
    print tcol.ENDC

    #---
    # Load HDF5 Keras model
    model = load_keras_hdf5_model(kerasmodel_h5file, verbose=True)  #this
    model.summary()

    #---
    # FLOPS
    from CustomNets import print_model_memory_usage, print_flops_report
    print tcol.OKGREEN + '====\n==== print_model_memory_usage\n====', tcol.ENDC
    print_model_memory_usage(1, model)
    print_flops_report(model)

    #---
    # Change Model Shape and do flops computation
    print tcol.OKGREEN + '====\n==== Memory and FLOPS for various Input Shapes\n====', tcol.ENDC
    for m in [0.5, 1.0, 2.0, 4.0]:
        from predict_utils import change_model_inputshape
        new_input_shape = (None, int(model.input.shape[1].value * m),
                           int(model.input.shape[2].value * m),
                           model.input.shape[3].value)
        new_model = change_model_inputshape(model,
                                            new_input_shape=new_input_shape)

        print_model_memory_usage(1, new_model)
        do_inference_on_random_input(new_model)
Exemple #7
0
    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 '-----'
Exemple #8
0
    LOG_DIR = '/'.join( kerasmodel_h5file.split('/')[0:-1] )
    print tcol.HEADER
    print '##------------------------------------------------------------##'
    print '## kerasmodel_h5file = ', kerasmodel_h5file
    print '## LOG_DIR = ', LOG_DIR
    print '## nrows = ', nrows, '\tncols = ', ncols
    print '##------------------------------------------------------------##'
    print tcol.ENDC


    #---
    # Load HDF5 Keras model
    model = load_keras_hdf5_model( kerasmodel_h5file, verbose=True ) #this


    #---
    # 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, 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() )
    new_model.save( new_model_fname )
    print tcol.OKGREEN+'====DONE===='+tcol.ENDC