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


Python backend.set_learning_phase方法代码示例

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


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

示例1: export_h5_to_pb

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def export_h5_to_pb(path_to_h5, export_path):

    # Set the learning phase to Test since the model is already trained.
    K.set_learning_phase(0)

    # Load the Keras model
    keras_model = load_model(path_to_h5)

    # Build the Protocol Buffer SavedModel at 'export_path'
    builder = saved_model_builder.SavedModelBuilder(export_path)

    # Create prediction signature to be used by TensorFlow Serving Predict API
    signature = predict_signature_def(inputs={"images": keras_model.input},
                                      outputs={"scores": keras_model.output})

    with K.get_session() as sess:
        # Save the meta graph and the variables
        builder.add_meta_graph_and_variables(sess=sess, tags=[tag_constants.SERVING],
                                         signature_def_map={"predict": signature})

    builder.save() 
开发者ID:drscotthawley,项目名称:panotti,代码行数:23,代码来源:h5topb.py

示例2: executeKerasInceptionV3

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def executeKerasInceptionV3(image_df, uri_col="filePath"):
    """
    Apply Keras InceptionV3 Model on input DataFrame.
    :param image_df: Dataset. contains a column (uri_col) for where the image file lives.
    :param uri_col: str. name of the column indicating where each row's image file lives.
    :return: ({str => np.array[float]}, {str => (str, str, float)}).
      image file uri to prediction probability array,
      image file uri to top K predictions (class id, class description, probability).
    """
    K.set_learning_phase(0)
    model = InceptionV3(weights="imagenet")

    values = {}
    topK = {}
    for row in image_df.select(uri_col).collect():
        raw_uri = row[uri_col]
        image = loadAndPreprocessKerasInceptionV3(raw_uri)
        values[raw_uri] = model.predict(image)
        topK[raw_uri] = decode_predictions(values[raw_uri], top=5)[0]
    return values, topK 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:22,代码来源:image_utils.py

示例3: _loadTFGraph

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [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

示例4: test_save_load_all

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def test_save_load_all(self):
        for backend in self.list_backends():
            try:
                set_keras_backend(backend)
            except ModuleNotFoundError:
                continue
            K.set_learning_phase(0)  # test
            for use_attn_mask in [True, False]:
                model = self.create_small_model(use_attn_mask)
                path = '/tmp/{}.model'.format(uuid.uuid4())
                try:
                    model.save(path)
                    new_model = keras.models.load_model(path, custom_objects={'MultiHeadAttention': MultiHeadAttention,
                                                                              'LayerNormalization': LayerNormalization,
                                                                              'Gelu': Gelu})
                    TestTransformer.compare_two_models(model, new_model)
                except Exception as e:
                    raise e
                finally:
                    if os.path.exists(path):
                        os.remove(path) 
开发者ID:yyht,项目名称:BERT,代码行数:23,代码来源:test_transformer.py

示例5: test_save_load_weights

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def test_save_load_weights(self):
        for backend in self.list_backends():
            try:
                set_keras_backend(backend)
            except ModuleNotFoundError:
                continue
            K.set_learning_phase(0)  # test
            for use_attn_mask in [True, False]:
                model = self.create_small_model(use_attn_mask)
                path = '/tmp/{}.model'.format(uuid.uuid4())
                try:
                    model.save_weights(path)
                    model.load_weights(path)
                except Exception as e:
                    raise e
                finally:
                    if os.path.exists(path):
                        os.remove(path) 
开发者ID:yyht,项目名称:BERT,代码行数:20,代码来源:test_transformer.py

示例6: test_switchnorm_trainable

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def test_switchnorm_trainable():
    bn_mean = 0.5
    bn_std = 10.

    def get_model(bn_mean, bn_std):
        input = Input(shape=(1,))
        x = SwitchNormalization()(input)
        model = Model(input, x)

        model.set_weights([np.array([1.]), np.array([0.]),
                           np.array([-1e3, -1e3, 1.0]), np.array([-1e3, -1e3, 1.0]),
                           np.array([bn_mean]), np.array([bn_std ** 2])])

        return model

    # Simulates training-mode with trainable layer. Should use mini-switch statistics.
    K.set_learning_phase(1)
    model = get_model(bn_mean, bn_std)
    model.compile(loss='mse', optimizer='rmsprop')
    out = model.predict(input_4)

    assert_allclose((input_4 - np.mean(input_4)) / np.std(input_4), out, atol=1e-3) 
开发者ID:titu1994,项目名称:keras-switchnorm,代码行数:24,代码来源:test_switchnorm.py

示例7: load_cmodel_checkpoint

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def load_cmodel_checkpoint(path, summary=True):

    #this is a terrible hack
    from keras.utils.generic_utils import get_custom_objects
    # get_custom_objects().update({"tf": tf})
    get_custom_objects().update({"clipped_relu": clipped_relu})
    get_custom_objects().update({"selu": selu})
    # get_custom_objects().update({"TF_NewStatus": None})

    cfilename = path+".h5"

    K.set_learning_phase(1)
    loaded_model = load_model(cfilename)


    if(summary):
        loaded_model.summary()

    return loaded_model 
开发者ID:robmsmt,项目名称:KerasDeepSpeech,代码行数:21,代码来源:utils.py

示例8: __init__

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def __init__(self, model_path='model_data/yolo.h5', anchors_path='model_data/yolo_anchors.txt', yolo3_dir=None):
        self.yolo3_dir = yolo3_dir
        self.model_path = model_path
        self.anchors_path = anchors_path
        self.classes_path = 'model_data/coco_classes.txt'
        self.score = 0.3
        self.iou = 0.45
        self.class_names = self._get_class()
        self.anchors = self._get_anchors()
        self.sess = K.get_session()
        self.model_image_size = (416, 416)  # fixed size or (None, None), hw
        self.session = None
        self.final_model = None

        # 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))
        np.random.seed(10101)  # Fixed seed for consistent colors across runs.
        np.random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        np.random.seed(None)  # Reset seed to default.
        K.set_learning_phase(0) 
开发者ID:onnx,项目名称:keras-onnx,代码行数:27,代码来源:yolov3.py

示例9: export_h5_to_pb

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def export_h5_to_pb(path_to_h5, export_path):

    # Set the learning phase to Test since the model is already trained.
    K.set_learning_phase(0)

    # Load the Keras model
    keras_model = load_model(path_to_h5)

    # Build the Protocol Buffer SavedModel at 'export_path'
    builder = saved_model_builder.SavedModelBuilder(export_path)

    # Create prediction signature to be used by TensorFlow Serving Predict API
    signature = predict_signature_def(inputs={ "http": keras_model.input},
                                      outputs={"probability": keras_model.output})

    with K.get_session() as sess:
        # Save the meta graph and the variables
        builder.add_meta_graph_and_variables(sess=sess, tags=[tag_constants.SERVING],
                                         signature_def_map={"predict": signature})

    builder.save() 
开发者ID:PipelineAI,项目名称:models,代码行数:23,代码来源:h5tf.py

示例10: gen_model

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def gen_model(name, license, model, model_file, version=VERSION, featurize=True):
    g = tf.Graph()
    with tf.Session(graph=g) as session:
        K.set_learning_phase(0)
        inTensor = tf.placeholder(dtype=tf.string, shape=[], name="%s_input" % name)
        decoded = tf.decode_raw(inTensor, tf.uint8)
        imageTensor = tf.to_float(
            tf.reshape(
                decoded,
                shape=[
                    1,
                    model.inputShape()[0],
                    model.inputShape()[1],
                    3]))
        m = model.model(preprocessed=model.preprocess(imageTensor), featurize=featurize)
        outTensor = tf.to_double(tf.reshape(m.output, [-1]), name="%s_sparkdl_output__" % name)
        gdef = tfx.strip_and_freeze_until([outTensor], session.graph, session, False)
    g2 = tf.Graph()
    with tf.Session(graph=g2) as session:
        tf.import_graph_def(gdef, name='')
        filename = "sparkdl-%s_%s.pb" % (name, version)
        print('writing out ', filename)
        tf.train.write_graph(g2.as_graph_def(), logdir="./", name=filename, as_text=False)
        with open("./" + filename, "r") as f:
            h = sha256(f.read()).digest()
            base64_hash = b64encode(h)
            print('h', base64_hash)
    model_file.write(indent(
        scala_template % {
            "license": license,
            "name": name,
            "height": model.inputShape()[0],
            "width": model.inputShape()[1],
            "filename": filename,
            "base64": base64_hash},2))
    return g2 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:38,代码来源:generate_app_models.py

示例11: test_prediction_vs_tensorflow_inceptionV3

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def test_prediction_vs_tensorflow_inceptionV3(self):
        output_col = "prediction"
        image_df = image_utils.getSampleImageDF()

        # An example of how a pre-trained keras model can be used with TFImageTransformer
        with KSessionWrap() as (sess, g):
            with g.as_default():
                K.set_learning_phase(0)    # this is important but it's on the user to call it.
                # nChannels needed for input_tensor in the InceptionV3 call below
                image_string = utils.imageInputPlaceholder(nChannels=3)
                resized_images = tf.image.resize_images(image_string,
                                                        InceptionV3Constants.INPUT_SHAPE)
                # keras expects array in RGB order, we get it from image schema in BGR =>
                # need to flip
                preprocessed = preprocess_input(imageIO._reverseChannels(resized_images))
                model = InceptionV3(input_tensor=preprocessed, weights="imagenet")
                graph = tfx.strip_and_freeze_until([model.output], g, sess, return_graph=True)

        transformer = TFImageTransformer(channelOrder='BGR', inputCol="image", outputCol=output_col, graph=graph,
                                         inputTensor=image_string, outputTensor=model.output,
                                         outputMode="vector")
        transformed_df = transformer.transform(image_df.limit(10))
        self.assertDfHasCols(transformed_df, [output_col])
        collected = transformed_df.collect()
        transformer_values, transformer_topK = self.transformOutputToComparables(collected,
                                                                                 output_col, lambda row: row['image']['origin'])

        tf_values, tf_topK = self._executeTensorflow(graph, image_string.name, model.output.name,
                                                     image_df)
        self.compareClassSets(tf_topK, transformer_topK)
        self.compareClassOrderings(tf_topK, transformer_topK)
        self.compareArrays(tf_values, transformer_values, decimal=5) 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:34,代码来源:tf_image_test.py

示例12: test_keras_consistency

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def test_keras_consistency(self):
        """ Exported model in Keras should get same result as original """

        img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))

        def keras_load_and_preproc(fpath):
            img = load_img(fpath, target_size=(299, 299))
            img_arr = img_to_array(img)
            img_iv3_input = iv3.preprocess_input(img_arr)
            return np.expand_dims(img_iv3_input, axis=0)

        imgs_iv3_input = np.vstack([keras_load_and_preproc(fp) for fp in img_fpaths])

        model_ref = InceptionV3(weights="imagenet")
        preds_ref = model_ref.predict(imgs_iv3_input)

        with IsolatedSession(using_keras=True) as issn:
            K.set_learning_phase(0)
            model = InceptionV3(weights="imagenet")
            gfn = issn.asGraphFunction(model.inputs, model.outputs)

        with IsolatedSession(using_keras=True) as issn:
            K.set_learning_phase(0)
            feeds, fetches = issn.importGraphFunction(gfn, prefix="InceptionV3")
            preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_iv3_input})

            np.testing.assert_array_almost_equal(preds_tgt, preds_ref, decimal=5) 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:29,代码来源:test_builder.py

示例13: test_bare_keras_module

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def test_bare_keras_module(self):
        """ Keras GraphFunctions should give the same result as standard Keras models """
        img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))

        for model_gen, preproc_fn, target_size in [(InceptionV3, iv3.preprocess_input, model_sizes['InceptionV3']),
                                      (Xception, xcpt.preprocess_input, model_sizes['Xception']),
                                      (ResNet50, rsnt.preprocess_input, model_sizes['ResNet50'])]:

            keras_model = model_gen(weights="imagenet")
            _preproc_img_list = []
            for fpath in img_fpaths:
                img = load_img(fpath, target_size=target_size)
                # WARNING: must apply expand dimensions first, or ResNet50 preprocessor fails
                img_arr = np.expand_dims(img_to_array(img), axis=0)
                _preproc_img_list.append(preproc_fn(img_arr))

            imgs_input = np.vstack(_preproc_img_list)

            preds_ref = keras_model.predict(imgs_input)

            gfn_bare_keras = GraphFunction.fromKeras(keras_model)

            with IsolatedSession(using_keras=True) as issn:
                K.set_learning_phase(0)
                feeds, fetches = issn.importGraphFunction(gfn_bare_keras)
                preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_input})

            np.testing.assert_array_almost_equal(preds_tgt,
                                                 preds_ref,
                                                 decimal=self.featurizerCompareDigitsExact) 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:32,代码来源:test_pieces.py

示例14: _fromKerasModelFile

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def _fromKerasModelFile(cls, file_path):
        """
        Load a Keras model from a file path into a `GraphFunction`.

        :param file_path: the (HDF5) file path
        """
        assert file_path.endswith('.h5'), \
            'Keras model must be specified as HDF5 file'

        with IsolatedSession(using_keras=True) as issn:
            K.set_learning_phase(0)  # Testing phase
            model = load_model(file_path)
            gfn = issn.asGraphFunction(model.inputs, model.outputs)

        return gfn 
开发者ID:databricks,项目名称:spark-deep-learning,代码行数:17,代码来源:builder.py

示例15: model_fn

# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_learning_phase [as 别名]
def model_fn(input_dim,
             labels_dim,
             hidden_units=[100, 70, 50, 20],
             learning_rate=0.1):
    """Create a Keras Sequential model with layers.

    Args:
      input_dim: (int) Input dimensions for input layer.
      labels_dim: (int) Label dimensions for input layer.
      hidden_units: [int] the layer sizes of the DNN (input layer first)
      learning_rate: (float) the learning rate for the optimizer.

    Returns:
      A Keras model.
    """

    # "set_learning_phase" to False to avoid:
    # AbortionError(code=StatusCode.INVALID_ARGUMENT during online prediction.
    K.set_learning_phase(False)
    model = models.Sequential()

    for units in hidden_units:
        model.add(
            layers.Dense(units=units, input_dim=input_dim, activation=relu))
        input_dim = units

    # Add a dense final layer with sigmoid function.
    model.add(layers.Dense(labels_dim, activation='sigmoid'))
    compile_model(model, learning_rate)
    return model 
开发者ID:GoogleCloudPlatform,项目名称:cloudml-samples,代码行数:32,代码来源:model.py


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