Esempio n. 1
0
def _build_with_sig_def(sess, graph, sig_def):
    # pylint: disable=protected-access
    assert sig_def, 'signature_def must not be None'

    with sess.as_default(), graph.as_default():
        feed_mapping = {}
        feed_names = []
        for sigdef_key, tnsr_info in sig_def.inputs.items():
            tnsr_name = tnsr_info.name
            feed_mapping[sigdef_key] = tnsr_name
            feed_names.append(tnsr_name)

        fetch_mapping = {}
        fetch_names = []
        for sigdef_key, tnsr_info in sig_def.outputs.items():
            tnsr_name = tnsr_info.name
            fetch_mapping[sigdef_key] = tnsr_name
            fetch_names.append(tnsr_name)

        for tnsr_name in feed_names:
            assert tfx.get_op(tnsr_name, graph), \
                'requested tensor {} but found none in graph {}'.format(tnsr_name, graph)
        fetches = [tfx.get_tensor(tnsr_name, graph) for tnsr_name in fetch_names]
        graph_def = tfx.strip_and_freeze_until(fetches, graph, sess)

    return TFInputGraph(graph_def=graph_def, input_tensor_name_from_signature=feed_mapping,
                        output_tensor_name_from_signature=fetch_mapping)
Esempio n. 2
0
 def _stripGraph(self, tf_graph):
     gdef = tfx.strip_and_freeze_until([self._getFinalOutputOpName()],
                                       tf_graph)
     g = tf.Graph()
     with g.as_default():
         tf.import_graph_def(gdef, name='')
     return g
    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)
                preprocessed = preprocess_input(resized_images)
                model = InceptionV3(input_tensor=preprocessed, weights="imagenet")
                graph = tfx.strip_and_freeze_until([model.output], g, sess, return_graph=True)

        transformer = TFImageTransformer(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,
                                                                                 "filePath",
                                                                                 output_col)

        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)
Esempio n. 4
0
 def _stripGraph(self, tf_graph):
     gdef = tfx.strip_and_freeze_until([self._getFinalOutputOpName()],
                                       tf_graph)
     g = tf.Graph()  # pylint: disable=invalid-name
     with g.as_default():  # pylint: disable=not-context-manager
         tf.import_graph_def(gdef, name='')
     return g
Esempio n. 5
0
def _build_with_sig_def(sess, graph, sig_def):
    # pylint: disable=protected-access
    assert sig_def, 'signature_def must not be None'

    with sess.as_default(), graph.as_default():
        feed_mapping = {}
        feed_names = []
        for sigdef_key, tnsr_info in sig_def.inputs.items():
            tnsr_name = tnsr_info.name
            feed_mapping[sigdef_key] = tnsr_name
            feed_names.append(tnsr_name)

        fetch_mapping = {}
        fetch_names = []
        for sigdef_key, tnsr_info in sig_def.outputs.items():
            tnsr_name = tnsr_info.name
            fetch_mapping[sigdef_key] = tnsr_name
            fetch_names.append(tnsr_name)

        for tnsr_name in feed_names:
            assert tfx.get_op(tnsr_name, graph), \
                'requested tensor {} but found none in graph {}'.format(tnsr_name, graph)
        fetches = [
            tfx.get_tensor(tnsr_name, graph) for tnsr_name in fetch_names
        ]
        graph_def = tfx.strip_and_freeze_until(fetches, graph, sess)

    return TFInputGraph(graph_def=graph_def,
                        input_tensor_name_from_signature=feed_mapping,
                        output_tensor_name_from_signature=fetch_mapping)
Esempio n. 6
0
    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)
def _buildTFGraphForName(name, featurize):
    """
    Currently only supports pre-trained models from the Keras applications module.
    """
    modelData = keras_apps.getKerasApplicationModel(name).getModelData(featurize)
    sess = modelData["session"]
    outputTensorName = modelData["outputTensorName"]
    graph = tfx.strip_and_freeze_until([outputTensorName], sess.graph, sess, return_graph=True)
    modelData["graph"] = graph
    return modelData
def _buildTFGraphForName(name, featurize):
    """
    Currently only supports pre-trained models from the Keras applications module.
    """
    modelData = keras_apps.getKerasApplicationModel(name).getModelData(featurize)
    sess = modelData["session"]
    outputTensorName = modelData["outputTensorName"]
    graph = tfx.strip_and_freeze_until([outputTensorName], sess.graph, sess, return_graph=True)
    modelData["graph"] = graph
    return modelData
Esempio n. 9
0
 def _loadTFGraph(self):
     with KSessionWrap() as (sess, g):
         assert K.backend() == "tensorflow", \
             "Keras backend is not tensorflow but KerasImageTransformer only supports " + \
             "tensorflow-backed Keras models."
         with g.as_default():
             K.set_learning_phase(0)  # Testing phase
             model = load_model(self.getModelFile())
             out_op_name = tfx.op_name(g, model.output)
             self._inputTensor = model.input.name
             self._outputTensor = model.output.name
             return tfx.strip_and_freeze_until([out_op_name], g, sess, return_graph=True)
Esempio n. 10
0
 def _loadTFGraph(self):
     with KSessionWrap() as (sess, graph):
         assert K.backend() == "tensorflow", \
             "Keras backend is not tensorflow but KerasImageTransformer only supports " + \
             "tensorflow-backed Keras models."
         with graph.as_default():
             K.set_learning_phase(0)  # Testing phase
             model = load_model(self.getModelFile())
             out_op_name = tfx.op_name(model.output, graph)
             self._inputTensor = model.input.name
             self._outputTensor = model.output.name
             return tfx.strip_and_freeze_until([out_op_name], graph, sess, return_graph=True)
Esempio n. 11
0
def _buildTFGraphForName(name, featurize):
    if name == "InceptionV3":
        modelData = _buildInceptionV3Session(featurize)
    else:
        raise ValueError("%s is not a supported model. Supported models: %s" % name,
                         str(SUPPORTED_MODELS))

    sess = modelData["session"]
    outputTensorName = modelData["outputTensorName"]
    graph = tfx.strip_and_freeze_until([outputTensorName], sess.graph, sess, return_graph=True)

    modelData["graph"] = graph
    return modelData
Esempio n. 12
0
def _build_with_feeds_fetches(sess, graph, feed_names, fetch_names):
    assert feed_names is not None, "must provide feed_names"
    assert fetch_names is not None, "must provide fetch names"

    with sess.as_default(), graph.as_default():
        for tnsr_name in feed_names:
            assert tfx.get_op(tnsr_name, graph), \
                'requested tensor {} but found none in graph {}'.format(tnsr_name, graph)
        fetches = [tfx.get_tensor(tnsr_name, graph) for tnsr_name in fetch_names]
        graph_def = tfx.strip_and_freeze_until(fetches, graph, sess)

    return TFInputGraph(graph_def=graph_def, input_tensor_name_from_signature=None,
                        output_tensor_name_from_signature=None)
Esempio n. 13
0
    def asGraphFunction(self, inputs, outputs, strip_and_freeze=True):
        """
        Export the graph in this session as a :py:class:`GraphFunction` object

        :param inputs: list, graph elements representing the inputs
        :param outputs: list, graph elements representing the outputs
        :param strip_and_freeze: bool, should we remove unused part of the graph and freee its values
        """
        if strip_and_freeze:
            gdef = tfx.strip_and_freeze_until(outputs, self.graph, self.sess)
        else:
            gdef = self.graph.as_graph_def(add_shapes=True)
        return GraphFunction(graph_def=gdef,
                             input_names=[tfx.validated_input(self.graph, elem) for elem in inputs],
                             output_names=[tfx.validated_output(self.graph, elem) for elem in outputs])
Esempio n. 14
0
    def asGraphFunction(self, inputs, outputs, strip_and_freeze=True):
        """
        Export the graph in this session as a :py:class:`GraphFunction` object

        :param inputs: list, graph elements representing the inputs
        :param outputs: list, graph elements representing the outputs
        :param strip_and_freeze: bool, should we remove unused part of the graph and freee its values
        """
        if strip_and_freeze:
            gdef = tfx.strip_and_freeze_until(outputs, self.graph, self.sess)
        else:
            gdef = self.graph.as_graph_def(add_shapes=True)
        return GraphFunction(graph_def=gdef,
                             input_names=[tfx.validated_input(elem, self.graph) for elem in inputs],
                             output_names=[tfx.validated_output(elem, self.graph) for elem in outputs])
Esempio n. 15
0
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
Esempio n. 16
0
def _build_with_feeds_fetches(sess, graph, feed_names, fetch_names):
    assert feed_names is not None, "must provide feed_names"
    assert fetch_names is not None, "must provide fetch names"

    with sess.as_default(), graph.as_default():
        for tnsr_name in feed_names:
            assert tfx.get_op(tnsr_name, graph), \
                'requested tensor {} but found none in graph {}'.format(tnsr_name, graph)
        fetches = [
            tfx.get_tensor(tnsr_name, graph) for tnsr_name in fetch_names
        ]
        graph_def = tfx.strip_and_freeze_until(fetches, graph, sess)

    return TFInputGraph(graph_def=graph_def,
                        input_tensor_name_from_signature=None,
                        output_tensor_name_from_signature=None)
Esempio n. 17
0
def _buildTFGraphForName(name, featurize):
    if name == "InceptionV3":
        modelData = _buildInceptionV3Session(featurize)
    else:
        raise ValueError(
            "%s is not a supported model. Supported models: %s" % name,
            str(SUPPORTED_MODELS))

    sess = modelData["session"]
    outputTensorName = modelData["outputTensorName"]
    graph = tfx.strip_and_freeze_until([outputTensorName],
                                       sess.graph,
                                       sess,
                                       return_graph=True)

    modelData["graph"] = graph
    return modelData
Esempio n. 18
0
def _buildTFGraphForName(name, featurize):
    if name not in keras_apps.KERAS_APPLICATION_MODELS:
        raise ValueError(
            "%s is not a supported model. Supported models: %s" % name,
            str(KERAS_APPLICATION_MODELS))

    modelData = keras_apps.getKerasApplicationModel(name).getModelData(
        featurize)
    sess = modelData["session"]
    outputTensorName = modelData["outputTensorName"]
    graph = tfx.strip_and_freeze_until([outputTensorName],
                                       sess.graph,
                                       sess,
                                       return_graph=True)
    modelData["graph"] = graph

    return modelData
    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
    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
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
Esempio n. 22
0
 def _stripGraph(self, tf_graph):
     gdef = tfx.strip_and_freeze_until([self._getFinalOutputOpName()], tf_graph)
     g = tf.Graph()  # pylint: disable=invalid-name
     with g.as_default():    # pylint: disable=not-context-manager
         tf.import_graph_def(gdef, name='')
     return g
Esempio n. 23
0
# model creation

from pyspark.ml.classification import LogisticRegression
from pyspark.ml import Pipeline
from sparkdl import readImages, TFImageTransformer
import sparkdl.graph.utils as tfx  # strip_and_freeze_until was moved from sparkdl.transformers to sparkdl.graph.utils in 0.2.0
from sparkdl.transformers import utils
import tensorflow as tf

graph = tf.Graph()
with tf.Session(graph=graph) as sess:
    image_arr = utils.imageInputPlaceholder()
    resized_images = tf.image.resize_images(image_arr, (299, 299))
    # the following step is not necessary for this graph, but can be for graphs with variables, etc
    frozen_graph = tfx.strip_and_freeze_until([resized_images],
                                              graph,
                                              sess,
                                              return_graph=True)

transformer = TFImageTransformer(inputCol="image",
                                 outputCol="features",
                                 graph=frozen_graph,
                                 inputTensor=image_arr,
                                 outputTensor=resized_images)

processed_image_df = transformer.transform(test_df)
processed_image_df.show()
#model.save(model_path)
#with open(model_path, 'rb') as f:
#	f_content = f.read()
#tf.gfile.FastGFile(model_hdfs_path, 'wb').write(f_content)
print("-------------CAN TRANSFORM--------------")