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)
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 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())
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())
def getModelData(self, featurize): sess = tf.Session() with sess.as_default(): K.set_learning_phase(0) inputImage = imageInputPlaceholder(nChannels=3) preprocessed = self.preprocess(inputImage) model = self.model(preprocessed, featurize) return dict(inputTensorName=inputImage.name, outputTensorName=model.output.name, session=sess, inputTensorSize=self.inputShape(), outputMode="vector")
def getModelData(self, featurize): sess = tf.Session() with sess.as_default(): K.set_learning_phase(0) inputImage = imageInputPlaceholder(nChannels=3) preprocessed = self.preprocess(inputImage) model = self.model(preprocessed, featurize) return dict(inputTensorName=inputImage.name, outputTensorName=model.output.name, session=sess, inputTensorSize=self.inputShape(), outputMode="vector")
def _buildInceptionV3Session(featurize): sess = tf.Session() with sess.as_default(): K.set_learning_phase(0) inputImage = imageInputPlaceholder(nChannels=3) preprocessed = inception_v3.preprocess_input(inputImage) model = InceptionV3(input_tensor=preprocessed, weights="imagenet", include_top=(not featurize)) return dict(inputTensorName=inputImage.name, outputTensorName=model.output.name, session=sess, inputTensorSize=InceptionV3Constants.INPUT_SHAPE, outputMode="vector")
def _buildInceptionV3Session(featurize): sess = tf.Session() with sess.as_default(): K.set_learning_phase(0) inputImage = imageInputPlaceholder(nChannels=3) preprocessed = inception_v3.preprocess_input(inputImage) model = InceptionV3(input_tensor=preprocessed, weights="imagenet", include_top=(not featurize)) return dict(inputTensorName=inputImage.name, outputTensorName=model.output.name, session=sess, inputTensorSize=InceptionV3Constants.INPUT_SHAPE, outputMode="vector")
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))
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))
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))
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() )
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)
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)
#dataframe for testing the classification model test_df = jobs_test.unionAll(zuckerberg_test) test_df.show() # 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()