Python keras.models.load_model() Examples

The following are 30 code examples for showing how to use keras.models.load_model(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module keras.models , or try the search function .

Example 1
Project: vergeml   Author: mme   File: imagenet.py    License: MIT License 6 votes vote down vote up
def load(self, model_dir, architecture, image_size):
        from keras.models import load_model
        from vergeml.sources.features import get_preprocess_input
        labels_txt = os.path.join(model_dir, "labels.txt")
        if not os.path.exists(labels_txt):
            raise VergeMLError("labels.txt not found: {}".format(labels_txt))

        model_h5 = os.path.join(model_dir, "model.h5")
        if not os.path.exists(model_h5):
            raise VergeMLError("model.h5 not found: {}".format(model_h5))

        with open(labels_txt, "r") as f:
            self.labels = f.read().splitlines()

        self.model = load_model(model_h5)
        self.image_size = image_size
        self.preprocess_input = get_preprocess_input(architecture) 
Example 2
Project: vergeml   Author: mme   File: features.py    License: MIT License 6 votes vote down vote up
def get_custom_architecture(name, trainings_dir, output_layer):
    from keras.models import load_model, Model
    name = name.lstrip("@")
    model = load_model(os.path.join(trainings_dir, name, 'checkpoints', 'model.h5'))
    try:
        if isinstance(output_layer, int):
            layer = model.layers[output_layer]
        else:
            layer = model.get_layer(output_layer)
    except Exception:
        if isinstance(output_layer, int):
            raise VergeMLError(f'output-layer {output_layer} not found - model has only {len(model.layers)} layers.')
        else:
            candidates = list(map(lambda l: l.name, model.layers))
            raise VergeMLError(f'output-layer named {output_layer} not found.',
                               suggestion=did_you_mean(candidates, output_layer))
    model = Model(inputs=model.input, outputs=layer.output)
    return model 
Example 3
Project: Sound-Recognition-Tutorial   Author: JasonZhang156   File: test.py    License: Apache License 2.0 6 votes vote down vote up
def CNN_test(test_fold, feat):
    """
    Test model using test set
    :param test_fold: test fold of 5-fold cross validation
    :param feat: which feature to use
    """
    # 读取测试数据
    _, _, test_features, test_labels = esc10_input.get_data(test_fold, feat)

    # 导入训练好的模型
    model = load_model('./saved_model/cnn_{}_fold{}.h5'.format(feat, test_fold))

    # 输出训练好的模型在测试集上的表现
    score = model.evaluate(test_features, test_labels)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])

    return score[1] 
Example 4
Project: cnn-levelset   Author: wiseodd   File: localizer.py    License: MIT License 6 votes vote down vote up
def __init__(self, model_path=None):
        if model_path is not None:
            self.model = self.load_model(model_path)
        else:
            # VGG16 last conv features
            inputs = Input(shape=(7, 7, 512))
            x = Convolution2D(128, 1, 1)(inputs)
            x = Flatten()(x)

            # Cls head
            h_cls = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x)
            h_cls = Dropout(p=0.5)(h_cls)
            cls_head = Dense(20, activation='softmax', name='cls')(h_cls)

            # Reg head
            h_reg = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x)
            h_reg = Dropout(p=0.5)(h_reg)
            reg_head = Dense(4, activation='linear', name='reg')(h_reg)

            # Joint model
            self.model = Model(input=inputs, output=[cls_head, reg_head]) 
Example 5
Project: spark-deep-learning   Author: databricks   File: shared_params.py    License: Apache License 2.0 6 votes vote down vote up
def _loadTFGraph(self, sess, graph):
        """
        Loads the Keras model into memory, then uses the passed-in session to load the
        model's inference-related ops into the passed-in Tensorflow graph.

        :return: A tuple (graph, input_name, output_name) where graph is the TF graph
        corresponding to the Keras model's inference subgraph, input_name is the name of the
        Keras model's input tensor, and output_name is the name of the Keras model's output tensor.
        """
        keras_backend = K.backend()
        assert keras_backend == "tensorflow", \
            "Only tensorflow-backed Keras models are supported, tried to load Keras model " \
            "with backend %s." % (keras_backend)
        with graph.as_default():
            K.set_learning_phase(0)  # Inference phase
            model = load_model(self.getModelFile())
            out_op_name = tfx.op_name(model.output, graph)
            stripped_graph = tfx.strip_and_freeze_until([out_op_name], graph, sess,
                                                        return_graph=True)
            return stripped_graph, model.input.name, model.output.name 
Example 6
Project: multi-object-tracking   Author: jguoaj   File: yolo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example 7
Project: MELD   Author: declare-lab   File: baseline.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_model(self):

		model = load_model(self.PATH)
		intermediate_layer_model = Model(input=model.input, output=model.get_layer("utter").output)

		intermediate_output_train = intermediate_layer_model.predict(self.train_x)
		intermediate_output_val = intermediate_layer_model.predict(self.val_x)
		intermediate_output_test = intermediate_layer_model.predict(self.test_x)

		train_emb, val_emb, test_emb = {}, {}, {}
		for idx, ID in enumerate(self.train_id):
		    train_emb[ID] = intermediate_output_train[idx]
		for idx, ID in enumerate(self.val_id):
		    val_emb[ID] = intermediate_output_val[idx]
		for idx, ID in enumerate(self.test_id):
		    test_emb[ID] = intermediate_output_test[idx]
		pickle.dump([train_emb, val_emb, test_emb], open(self.OUTPUT_PATH, "wb"))

		self.calc_test_result(model.predict(self.test_x), self.test_y, self.test_mask) 
Example 8
Project: face_classification   Author: oarriaga   File: grad_cam.py    License: MIT License 6 votes vote down vote up
def modify_backprop(model, name, task):
    graph = tf.get_default_graph()
    with graph.gradient_override_map({'Relu': name}):

        # get layers that have an activation
        activation_layers = [layer for layer in model.layers
                             if hasattr(layer, 'activation')]

        # replace relu activation
        for layer in activation_layers:
            if layer.activation == keras.activations.relu:
                layer.activation = tf.nn.relu

        # re-instanciate a new model
        if task == 'gender':
            model_path = '../trained_models/gender_models/gender_mini_XCEPTION.21-0.95.hdf5'
        elif task == 'emotion':
            model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
            # model_path = '../trained_models/fer2013_mini_XCEPTION.119-0.65.hdf5'
            # model_path = '../trained_models/fer2013_big_XCEPTION.54-0.66.hdf5'
        new_model = load_model(model_path, compile=False)
    return new_model 
Example 9
Project: evo-pawness   Author: haryoa   File: reinforcement_algorithm.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self,
                 init_state,
                 max_simulation=AlphaZeroConfig.MAX_SIMULATION_AGENT,
                 MODEL_PATH=AlphaZeroConfig.DEFAULT_MODEL_AGENT):
        """
        Contructor of AlphaZero Agent
        :param init_state: the initial state of the game. It will be used continuously
        :param max_simulation: MCTS max simulation
        :param MODEL_PATH: Model Path used for AlphaZero Agent
        """
        self.max_simulation = max_simulation
        all_action_spaces = action_spaces_new()
        self.ae = ActionEncoder()
        self.ae.fit(list_all_action=all_action_spaces)
        self.stacked_state = StackedState(init_state)
        self.deepnet_model = PawnNetZero(len(all_action_spaces))
        self.deepnet_model.model = load_model(MODEL_PATH)
        self.mcts = MCTreeSearch(self.deepnet_model.model, 1, self.max_simulation, self.ae, self.stacked_state) 
Example 10
Project: shopping-classification   Author: kakao-arena   File: classifier.py    License: Apache License 2.0 6 votes vote down vote up
def predict(self, data_root, model_root, test_root, test_div, out_path, readable=False):
        meta_path = os.path.join(data_root, 'meta')
        meta = cPickle.loads(open(meta_path, 'rb').read())

        model_fname = os.path.join(model_root, 'model.h5')
        self.logger.info('# of classes(train): %s' % len(meta['y_vocab']))
        model = load_model(model_fname,
                           custom_objects={'top1_acc': top1_acc})

        test_path = os.path.join(test_root, 'data.h5py')
        test_data = h5py.File(test_path, 'r')

        test = test_data[test_div]
        batch_size = opt.batch_size
        pred_y = []
        test_gen = ThreadsafeIter(self.get_sample_generator(test, batch_size, raise_stop_event=True))
        total_test_samples = test['uni'].shape[0]
        with tqdm.tqdm(total=total_test_samples) as pbar:
            for chunk in test_gen:
                total_test_samples = test['uni'].shape[0]
                X, _ = chunk
                _pred_y = model.predict(X)
                pred_y.extend([np.argmax(y) for y in _pred_y])
                pbar.update(X[0].shape[0])
        self.write_prediction_result(test, pred_y, meta, out_path, readable=readable) 
Example 11
Project: Emotion   Author: petercunha   File: grad_cam.py    License: MIT License 6 votes vote down vote up
def modify_backprop(model, name, task):
    graph = tf.get_default_graph()
    with graph.gradient_override_map({'Relu': name}):

        # get layers that have an activation
        activation_layers = [layer for layer in model.layers
                             if hasattr(layer, 'activation')]

        # replace relu activation
        for layer in activation_layers:
            if layer.activation == keras.activations.relu:
                layer.activation = tf.nn.relu

        # re-instanciate a new model
        if task == 'gender':
            model_path = '../trained_models/gender_models/gender_mini_XCEPTION.21-0.95.hdf5'
        elif task == 'emotion':
            model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
            # model_path = '../trained_models/fer2013_mini_XCEPTION.119-0.65.hdf5'
            # model_path = '../trained_models/fer2013_big_XCEPTION.54-0.66.hdf5'
        new_model = load_model(model_path, compile=False)
    return new_model 
Example 12
Project: Face-and-Emotion-Recognition   Author: vjgpt   File: grad_cam.py    License: MIT License 6 votes vote down vote up
def modify_backprop(model, name, task):
    graph = tf.get_default_graph()
    with graph.gradient_override_map({'Relu': name}):

        # get layers that have an activation
        activation_layers = [layer for layer in model.layers
                             if hasattr(layer, 'activation')]

        # replace relu activation
        for layer in activation_layers:
            if layer.activation == keras.activations.relu:
                layer.activation = tf.nn.relu

        # re-instanciate a new model
        if task == 'gender':
            model_path = '../trained_models/gender_models/gender_mini_XCEPTION.21-0.95.hdf5'
        elif task == 'emotion':
            model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
            # model_path = '../trained_models/fer2013_mini_XCEPTION.119-0.65.hdf5'
            # model_path = '../trained_models/fer2013_big_XCEPTION.54-0.66.hdf5'
        new_model = load_model(model_path, compile=False)
    return new_model 
Example 13
Project: YOLO-3D-Box   Author: scutan90   File: yolo.py    License: MIT License 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example 14
Project: deep_sort_yolov3   Author: Qidian213   File: yolo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example 15
Project: PSPNet-Keras-tensorflow   Author: Vladkryvoruchko   File: pspnet.py    License: MIT License 6 votes vote down vote up
def __init__(self, nb_classes, resnet_layers, input_shape, weights):
        self.input_shape = input_shape
        self.num_classes = nb_classes

        json_path = join("weights", "keras", weights + ".json")
        h5_path = join("weights", "keras", weights + ".h5")
        if 'pspnet' in weights:
            if os.path.isfile(json_path) and os.path.isfile(h5_path):
                print("Keras model & weights found, loading...")
                with CustomObjectScope({'Interp': layers.Interp}):
                    with open(json_path) as file_handle:
                        self.model = model_from_json(file_handle.read())
                self.model.load_weights(h5_path)
            else:
                print("No Keras model & weights found, import from npy weights.")
                self.model = layers.build_pspnet(nb_classes=nb_classes,
                                                 resnet_layers=resnet_layers,
                                                 input_shape=self.input_shape)
                self.set_npy_weights(weights)
        else:
            print('Load pre-trained weights')
            self.model = load_model(weights) 
Example 16
Project: Coloring-greyscale-images   Author: emilwallner   File: load_trained_models.py    License: MIT License 6 votes vote down vote up
def __init__(self,
                 resource_path='./resources/',
                 learning_rate=0.0002,
                 decay_rate=2e-6,
                 gpus = 1):
        
        self.gpus = gpus
        self.learning_rate = learning_rate
        self.decay_rate = decay_rate
        

        def zero_loss(y_true, y_pred):
        	return K.zeros_like(y_true)
               
        discriminator_full = load_model(resource_path + 'discriminator_full.h5', custom_objects={'Conv2D_r': Conv2D_r, 'InstanceNormalization': InstanceNormalization, 'tf': tf, 'zero_loss': zero_loss, 'ConvSN2D': ConvSN2D, 'DenseSN': DenseSN})
        
        discriminator_full.trainable = True
        discriminator_full.name = "discriminator_full"
        
        self.model = discriminator_full
        self.save_model = discriminator_full 
Example 17
Project: Coloring-greyscale-images   Author: emilwallner   File: load_trained_models.py    License: MIT License 6 votes vote down vote up
def __init__(self,
                 resource_path='./resources/',
                 learning_rate=0.0002,
                 decay_rate=2e-6,
                 gpus = 0):
        
        self.gpus = gpus
        self.learning_rate = learning_rate
        self.decay_rate = decay_rate
        
        def zero_loss(y_true, y_pred):
        	return K.zeros_like(y_true)
       
        discriminator_low = load_model(resource_path + 'discriminator_low.h5', custom_objects={'Conv2D_r': Conv2D_r, 'InstanceNormalization': InstanceNormalization, 'tf': tf,'zero_loss': zero_loss, 'ConvSN2D': ConvSN2D, 'DenseSN': DenseSN})
        
        discriminator_low.trainable = True
        discriminator_low.name = "discriminator_low"

        self.model = discriminator_low
        self.save_model = discriminator_low 
Example 18
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example 19
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: tokenizer_mod.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, args):
        """
        Tokenizer model loading etc goes here
        """
        from keras.models import load_model
        self.model = load_model(args.model)
        with open(args.vocab,'rb') as inf:
            self.vocab = pickle.load(inf)
        self.args=args 
Example 20
Project: models   Author: kipoi   File: model.py    License: MIT License 5 votes vote down vote up
def __init__(self, weights, postproc_mean, postproc_sd):
        """Simple keras model that also runs the postprocessin
        """
        self.weights = weights
        self.model = load_model(weights)
        self.postproc_mean = postproc_mean
        self.postproc_sd = postproc_sd 
Example 21
Project: models   Author: kipoi   File: model.py    License: MIT License 5 votes vote down vote up
def __init__(self, model_file):
        self.model_file = model_file
        K.clear_session()  # restart session
        self.model = load_model(model_file, compile=False)
        self.contrib_fns = {} 
Example 22
Project: models   Author: kipoi   File: model.py    License: MIT License 5 votes vote down vote up
def __init__(self, weights):
            self.nuc_dict = {'A':[1.0,0.0,0.0,0.0],'C':[0.0,1.0,0.0,0.0],'G':[0.0,0.0,1.0,0.0], 
                             'U':[0.0,0.0,0.0,1.0], 'T':[0.0,0.0,0.0,1.0], 
                             'N':[0.0,0.0,0.0,0.0], 'X':[1/4,1/4,1/4,1/4]}
            self.weights = weights
            self.model = load_model(weights, custom_objects={'FrameSliceLayer': FrameSliceLayer})
        
        # One-hot encodes a particular sequence 
Example 23
def load_models(self, loc=None):
        if not loc:
            loc = os.path.join(os.path.expanduser('~'), '.ner_model')
        self.model = load_model(os.path.join(loc,"model.h5"))
        # loading word2Idx
        self.word2Idx = np.load(os.path.join(loc,"word2Idx.npy")).item()
        # loading idx2Label
        self.idx2Label = np.load(os.path.join(loc,"idx2Label.npy")).item() 
Example 24
Project: TradzQAI   Author: kkuette   File: DEEP.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, model_path):
        self.model = load_model(model_path)
        self.graph = tf.get_default_graph() 
Example 25
Project: steppy-toolkit   Author: minerva-ml   File: models.py    License: MIT License 5 votes vote down vote up
def load(self, filepath):
        self.model = load_model(filepath,
                                custom_objects={'AttentionWeightedAverage': AttentionWeightedAverage})
        return self 
Example 26
Project: python-esppy   Author: sassoftware   File: generators.py    License: Apache License 2.0 5 votes vote down vote up
def gen_wrap_str(self):
        if self.output_class:
            predict = 'predict_classes'
        else:
            predict = 'predict'

        wrap_str = '''
model = None
def ks_score({0}):
    "Output: {1}"
    from keras.models import load_model
    import tensorflow as tf
    import numpy as np
    global model
    global graph
    # If it is called for the first time, restore the model
    if model is None:
        model = load_model('{2}')
        model._make_predict_function()
        graph = tf.get_default_graph()

    # make prediction
    {0}_wrap = np.array([{0}])
    with graph.as_default():
        {1} = model.{3}({0}_wrap)[0]

    if isinstance({1}, np.ndarray):
        {1} = {1}.tolist()
    else:
        {1} = {1}.item()

    return {1}'''.format(self.input_name, self.output_name, self.file, predict)
        return wrap_str 
Example 27
Project: cnn-levelset   Author: wiseodd   File: localizer.py    License: MIT License 5 votes vote down vote up
def load_model(self, model_path):
        return load_model(model_path, custom_objects=self.custom_objs) 
Example 28
Project: CalibrationNN   Author: Andres-Hernandez   File: neural_network.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __fromfile(self):
        file_name = self.file_name() + '.h5'
        if isfile(file_name):
            self.model = load_model(file_name)
        else:
            self.model = None 
Example 29
Project: Jtyoui   Author: jtyoui   File: NER.py    License: MIT License 5 votes vote down vote up
def predict_model():
    x_test, original = vocab_test(test_path, vocab, length)
    ws = open(temp_path, mode='w', newline='\n')
    tags = dict(zip(tag.values(), tag.keys()))
    custom_objects = {'CRF': CRF, 'crf_loss': crf.crf_loss, 'crf_viterbi_accuracy': crf.crf_viterbi_accuracy}
    model = load_model(model_path, custom_objects=custom_objects)
    for question, tests in zip(original, x_test):
        raw = model.predict([[tests]])[0][-len(question):]
        result = [np.argmax(row) for row in raw]
        answer = tuple(map(lambda x: tags[x], result))
        ma = map(lambda x: x[0] + '\t' + x[1] + '\n', zip(question, answer))
        ws.writelines(ma)
        ws.write('\n')
    ws.flush()
    ws.close() 
Example 30
Project: Jtyoui   Author: jtyoui   File: HandWritingRecognition.py    License: MIT License 5 votes vote down vote up
def model_load(path='./model.h5'):
    model = load_model(path)
    return model