当前位置: 首页>>代码示例>>Python>>正文


Python models.load_model方法代码示例

本文整理汇总了Python中keras.models.load_model方法的典型用法代码示例。如果您正苦于以下问题:Python models.load_model方法的具体用法?Python models.load_model怎么用?Python models.load_model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在keras.models的用法示例。


在下文中一共展示了models.load_model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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) 
开发者ID:mme,项目名称:vergeml,代码行数:19,代码来源:imagenet.py

示例2: CNN_test

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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] 
开发者ID:JasonZhang156,项目名称:Sound-Recognition-Tutorial,代码行数:20,代码来源:test.py

示例3: generate

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:jguoaj,项目名称:multi-object-tracking,代码行数:26,代码来源:yolo.py

示例4: get_custom_architecture

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:mme,项目名称:vergeml,代码行数:20,代码来源:features.py

示例5: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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]) 
开发者ID:wiseodd,项目名称:cnn-levelset,代码行数:23,代码来源:localizer.py

示例6: _loadTFGraph

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:22,代码来源:shared_params.py

示例7: test_model

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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) 
开发者ID:declare-lab,项目名称:MELD,代码行数:21,代码来源:baseline.py

示例8: modify_backprop

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:oarriaga,项目名称:face_classification,代码行数:24,代码来源:grad_cam.py

示例9: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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) 
开发者ID:haryoa,项目名称:evo-pawness,代码行数:20,代码来源:reinforcement_algorithm.py

示例10: predict

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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) 
开发者ID:kakao-arena,项目名称:shopping-classification,代码行数:27,代码来源:classifier.py

示例11: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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) 
开发者ID:Vladkryvoruchko,项目名称:PSPNet-Keras-tensorflow,代码行数:24,代码来源:pspnet.py

示例12: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:emilwallner,项目名称:Coloring-greyscale-images,代码行数:23,代码来源:load_trained_models.py

示例13: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:TurkuNLP,项目名称:Turku-neural-parser-pipeline,代码行数:11,代码来源:tokenizer_mod.py

示例14: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 
开发者ID:kipoi,项目名称:models,代码行数:9,代码来源:model.py

示例15: __init__

# 需要导入模块: from keras import models [as 别名]
# 或者: from keras.models import load_model [as 别名]
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 = {} 
开发者ID:kipoi,项目名称:models,代码行数:7,代码来源:model.py


注:本文中的keras.models.load_model方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。