コード例 #1
0
ファイル: tf_image_test.py プロジェクト: SichengZhang/Spark
    def test_load_image_vs_keras_RGB(self):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            # keras expects array in RGB order, we get it from image schema in BGR => need to flip
            preprocessed = preprocess_input(image_arr)

        output_col = "transformed_image"
        transformer = TFImageTransformer(channelOrder='RGB',
                                         inputCol="image",
                                         outputCol=output_col,
                                         graph=g,
                                         inputTensor=image_arr,
                                         outputTensor=preprocessed.name,
                                         outputMode="vector")

        image_df = image_utils.getSampleImageDF()
        df = transformer.transform(image_df.limit(5))

        for row in df.collect():
            processed = np.array(row[output_col]).astype(np.float32)
            # compare to keras loading
            images = self._loadImageViaKeras(row["image"]['origin'])
            image = images[0]
            image.shape = (1, image.shape[0] * image.shape[1] * image.shape[2])
            keras_processed = image[0]
            self.assertTrue((processed == keras_processed).all())
コード例 #2
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)
コード例 #3
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)
                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)
コード例 #4
0
    def test_load_image_vs_keras(self):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            preprocessed = preprocess_input(image_arr)

        output_col = "transformed_image"
        transformer = TFImageTransformer(inputCol="image",
                                         outputCol=output_col,
                                         graph=g,
                                         inputTensor=image_arr,
                                         outputTensor=preprocessed.name,
                                         outputMode="vector")

        image_df = image_utils.getSampleImageDF()
        df = transformer.transform(image_df.limit(5))

        for row in df.collect():
            processed = np.array(row[output_col]).astype(np.float32)
            # compare to keras loading
            images = self._loadImageViaKeras(row["filePath"])
            image = images[0]
            image.shape = (1, image.shape[0] * image.shape[1] * image.shape[2])
            keras_processed = image[0]
            self.assertTrue((processed == keras_processed).all())
コード例 #5
0
 def _transform(self, dataset):
     with KSessionWrap() as (sess, keras_graph):
         graph, inputTensorName, outputTensorName = self._loadTFGraph(sess=sess,
                                                                      graph=keras_graph)
         image_df = self.loadImagesInternal(dataset, self.getInputCol())
         transformer = TFImageTransformer(channelOrder='RGB', inputCol=self._loadedImageCol(),
                                          outputCol=self.getOutputCol(), graph=graph,
                                          inputTensor=inputTensorName,
                                          outputTensor=outputTensorName,
                                          outputMode=self.getOrDefault(self.outputMode))
         return transformer.transform(image_df).drop(self._loadedImageCol())
コード例 #6
0
    def _transform(self, dataset):
        graph = self._loadTFGraph()
        image_df = self._loadImages(dataset)

        assert self._inputTensor is not None, "self._inputTensor must be set"
        assert self._outputTensor is not None, "self._outputTensor must be set"

        transformer = TFImageTransformer(inputCol=self._loadedImageCol(),
                                         outputCol=self.getOutputCol(), graph=graph,
                                         inputTensor=self._inputTensor,
                                         outputTensor=self._outputTensor,
                                         outputMode=self.getOrDefault(self.outputMode))
        return transformer.transform(image_df).drop(self._loadedImageCol())
コード例 #7
0
 def _transform(self, dataset):
     modelGraphSpec = _buildTFGraphForName(self.getModelName(), self.getFeaturize())
     inputCol = self.getInputCol()
     resizedCol = "__sdl_imagesResized"
     tfTransformer = TFImageTransformer(inputCol=resizedCol,
                                        outputCol=self.getOutputCol(),
                                        graph=modelGraphSpec["graph"],
                                        inputTensor=modelGraphSpec["inputTensorName"],
                                        outputTensor=modelGraphSpec["outputTensorName"],
                                        outputMode=modelGraphSpec["outputMode"])
     resizeUdf = resizeImage(modelGraphSpec["inputTensorSize"])
     result = tfTransformer.transform(dataset.withColumn(resizedCol, resizeUdf(inputCol)))
     return result.drop(resizedCol)
コード例 #8
0
    def _transform(self, dataset):
        graph = self._loadTFGraph()
        image_df = self.loadImagesInternal(dataset, self.getInputCol())

        assert self._inputTensor is not None, "self._inputTensor must be set"
        assert self._outputTensor is not None, "self._outputTensor must be set"

        transformer = TFImageTransformer(inputCol=self._loadedImageCol(),
                                         outputCol=self.getOutputCol(), graph=graph,
                                         inputTensor=self._inputTensor,
                                         outputTensor=self._outputTensor,
                                         outputMode=self.getOrDefault(self.outputMode))
        return transformer.transform(image_df).drop(self._loadedImageCol())
コード例 #9
0
    def _preprocessingInceptionV3Transformed(self, outputMode, outputCol):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            resized_images = tf.image.resize_images(image_arr, InceptionV3Constants.INPUT_SHAPE)
            processed_images = preprocess_input(resized_images)
        self.assertEqual(processed_images.shape[1], InceptionV3Constants.INPUT_SHAPE[0])
        self.assertEqual(processed_images.shape[2], InceptionV3Constants.INPUT_SHAPE[1])

        transformer = TFImageTransformer(inputCol="image", outputCol=outputCol, graph=g,
                                         inputTensor=image_arr.name, outputTensor=processed_images,
                                         outputMode=outputMode)
        image_df = image_utils.getSampleImageDF()
        return transformer.transform(image_df.limit(5))
コード例 #10
0
    def _preprocessingInceptionV3Transformed(self, outputMode, outputCol):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            resized_images = tf.image.resize_images(image_arr, InceptionV3Constants.INPUT_SHAPE)
            # keras expects array in RGB order, we get it from image schema in BGR => need to flip
            processed_images = preprocess_input(imageIO._reverseChannels(resized_images))
        self.assertEqual(processed_images.shape[1], InceptionV3Constants.INPUT_SHAPE[0])
        self.assertEqual(processed_images.shape[2], InceptionV3Constants.INPUT_SHAPE[1])

        transformer = TFImageTransformer(channelOrder='BGR', inputCol="image", outputCol=outputCol, graph=g,
                                         inputTensor=image_arr.name, outputTensor=processed_images,
                                         outputMode=outputMode)
        image_df = image_utils.getSampleImageDF()
        return transformer.transform(image_df.limit(5))
コード例 #11
0
 def _transform(self, dataset):
     modelGraphSpec = _buildTFGraphForName(self.getModelName(), self.getFeaturize())
     inputCol = self.getInputCol()
     resizedCol = "__sdl_imagesResized"
     tfTransformer = TFImageTransformer(
         channelOrder='BGR',
         inputCol=resizedCol,
         outputCol=self.getOutputCol(),
         graph=modelGraphSpec["graph"],
         inputTensor=modelGraphSpec["inputTensorName"],
         outputTensor=modelGraphSpec["outputTensorName"],
         outputMode=modelGraphSpec["outputMode"])
     resizeUdf = createResizeImageUDF(modelGraphSpec["inputTensorSize"])
     result = tfTransformer.transform(dataset.withColumn(resizedCol, resizeUdf(inputCol)))
     return result.drop(resizedCol)
コード例 #12
0
    def _preprocessingInceptionV3Transformed(self, outputMode, outputCol):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            resized_images = tf.image.resize_images(image_arr, InceptionV3Constants.INPUT_SHAPE)
            # keras expects array in RGB order, we get it from image schema in BGR => need to flip
            processed_images = preprocess_input(imageIO._reverseChannels(resized_images))
        self.assertEqual(processed_images.shape[1], InceptionV3Constants.INPUT_SHAPE[0])
        self.assertEqual(processed_images.shape[2], InceptionV3Constants.INPUT_SHAPE[1])

        transformer = TFImageTransformer(channelOrder='BGR', inputCol="image", outputCol=outputCol, graph=g,
                                         inputTensor=image_arr.name, outputTensor=processed_images,
                                         outputMode=outputMode)
        image_df = image_utils.getSampleImageDF()
        return transformer.transform(image_df.limit(5))
コード例 #13
0
    def test_load_image_vs_keras(self):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            preprocessed = preprocess_input(image_arr)

        output_col = "transformed_image"
        transformer = TFImageTransformer(inputCol="image", outputCol=output_col, graph=g,
                                         inputTensor=image_arr, outputTensor=preprocessed.name,
                                         outputMode="vector")

        image_df = image_utils.getSampleImageDF()
        df = transformer.transform(image_df.limit(5))

        for row in df.collect():
            processed = np.array(row[output_col]).astype(np.float32)
            # compare to keras loading
            images = self._loadImageViaKeras(row["filePath"])
            image = images[0]
            image.shape = (1, image.shape[0] * image.shape[1] * image.shape[2])
            keras_processed = image[0]
            self.assertTrue( (processed == keras_processed).all() )
コード例 #14
0
    def test_load_image_vs_keras_RGB(self):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            # keras expects array in RGB order, we get it from image schema in BGR => need to flip
            preprocessed = preprocess_input(image_arr)

        output_col = "transformed_image"
        transformer = TFImageTransformer(channelOrder='RGB', inputCol="image", outputCol=output_col, graph=g,
                                         inputTensor=image_arr, outputTensor=preprocessed.name,
                                         outputMode="vector")

        image_df = image_utils.getSampleImageDF()
        df = transformer.transform(image_df.limit(5))

        for row in df.collect():
            processed = np.array(row[output_col], dtype = np.float32)
            # compare to keras loading
            images = self._loadImageViaKeras(row["image"]['origin'])
            image = images[0]
            image.shape = (1, image.shape[0] * image.shape[1] * image.shape[2])
            keras_processed = image[0]
            np.testing.assert_array_almost_equal(keras_processed, processed, decimal = 6)
コード例 #15
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)
                preprocessed = preprocess_input(resized_images)
                model = InceptionV3(input_tensor=preprocessed,
                                    weights="imagenet")
                graph = utils.stripAndFreezeGraph(
                    g.as_graph_def(add_shapes=True), sess, [model.output])

        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)