Python keras.backend.tensorflow_backend.get_session() Examples

The following are code examples for showing how to use keras.backend.tensorflow_backend.get_session(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Intelligent_Arm_Project   Author: TeamLimb   File: tiny_yolo2.py    MIT License 6 votes vote down vote up
def yolo_eval(graph, yolo_outputs, image_shape, max_boxes=10, score_threshold=.6, iou_threshold=.5):
    with graph.as_default():
        box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs
        boxes = yolo_boxes_to_corners(graph, box_xy, box_wh)
        boxes, scores, classes = yolo_filter_boxes(graph, boxes, box_confidence, box_class_probs, threshold=score_threshold)

        # Scale boxes back to original image shape.
        height = image_shape[0]
        width = image_shape[1]
        image_dims = K.stack([height, width, height, width])
        image_dims = K.reshape(image_dims, [1, 4])
        boxes = boxes * image_dims

        # TODO: Something must be done about this ugly hack!
        max_boxes_tensor = K.variable(max_boxes, dtype='int32')
        K.get_session().run(tf.variables_initializer([max_boxes_tensor]))
        nms_index = tf.image.non_max_suppression(boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold)
        boxes = K.gather(boxes, nms_index)
        scores = K.gather(scores, nms_index)
        classes = K.gather(classes, nms_index)

        return boxes, scores, classes 
Example 2
Project: applications   Author: geomstats   File: backend_test.py    MIT License 6 votes vote down vote up
def test_function_tf_fetches(self):
        # Additional operations can be passed to tf.Session().run() via its
        # `fetches` arguments. In contrast to `updates` argument of
        # KTF.function() these do not have control dependency on `outputs`, so
        # they can run in parallel. Also they should not contribute to output of
        # KTF.function().

        x = KTF.variable(0.)
        y = KTF.variable(0.)
        x_placeholder = KTF.placeholder(shape=())
        y_placeholder = KTF.placeholder(shape=())

        f = KTF.function(inputs=[x_placeholder, y_placeholder],
                         outputs=[x_placeholder + y_placeholder],
                         updates=[(x, x_placeholder + 1.)],
                         fetches=[KTF.update(y, 5.)])
        output = f([10., 20.])
        assert output == [30.]
        assert KTF.get_session().run(fetches=[x, y]) == [11., 5.] 
Example 3
Project: applications   Author: geomstats   File: backend_test.py    MIT License 5 votes vote down vote up
def test_function_tf_feed_dict(self):
        # Additional substitutions can be passed to `tf.Session().run()` via its
        # `feed_dict` arguments. Note that the feed_dict is passed once in the
        # constructor but we can modify the values in the dictionary. Through
        # this feed_dict we can provide additional substitutions besides Keras
        # inputs.

        x = KTF.variable(0.)
        y = KTF.variable(0.)
        x_placeholder = KTF.placeholder(shape=())
        y_placeholder = KTF.placeholder(shape=())

        feed_dict = {y_placeholder: 3.}

        f = KTF.function(inputs=[x_placeholder],
                         outputs=[x_placeholder + 1.],
                         updates=[(x, x_placeholder + 10.)],
                         feed_dict=feed_dict,
                         fetches=[KTF.update(y, y_placeholder * 10.)])
        output = f([10.])
        assert output == [11.]
        assert KTF.get_session().run(fetches=[x, y]) == [20., 30.]

        # updated value in feed_dict will be modified within the K.function()
        feed_dict[y_placeholder] = 4.
        output = f([20.])
        assert output == [21.]
        assert KTF.get_session().run(fetches=[x, y]) == [30., 40.]