def buildSpImageConverter(channelOrder, img_dtype): """ Convert a imageIO byte encoded image into a image tensor suitable as input to ConvNets The name of the input must be a subset of those specified in `image.imageIO.imageSchema`. :param img_dtype: the type of data the underlying image bytes represent """ with IsolatedSession() as issn: # Flat image data -> image dimensions # This has to conform to `imageIO.imageSchema` height = tf.placeholder(tf.int32, [], name="height") width = tf.placeholder(tf.int32, [], name="width") num_channels = tf.placeholder(tf.int32, [], name="nChannels") image_buffer = tf.placeholder(tf.string, [], name="data") # The image is packed into bytes with height as leading dimension # This is the default behavior of Python Image Library shape = tf.reshape(tf.stack([height, width, num_channels], axis=0), shape=(3,), name='shape') if img_dtype == 'uint8': image_uint8 = tf.decode_raw(image_buffer, tf.uint8, name="decode_raw") image_float = tf.to_float(image_uint8) elif img_dtype == 'float32': image_float = tf.decode_raw(image_buffer, tf.float32, name="decode_raw") else: raise ValueError('''unsupported image data type "%s", currently only know how to handle uint8 and float32''' % img_dtype) image_reshaped = tf.reshape(image_float, shape, name="reshaped") image_reshaped = imageIO.fixColorChannelOrdering(channelOrder, image_reshaped) image_input = tf.expand_dims(image_reshaped, 0, name="image_input") gfn = issn.asGraphFunction([height, width, image_buffer, num_channels], [image_input]) return gfn
def _addReshapeLayers(self, tf_graph, dtype="uint8"): input_tensor_name = self.getInputTensor().name gdef = tf_graph.as_graph_def(add_shapes=True) g = tf.Graph() with g.as_default(): # Flat image data -> image dimensions height = tf.placeholder(tf.int32, [], name="height") width = tf.placeholder(tf.int32, [], name="width") num_channels = tf.placeholder(tf.int32, [], name="num_channels") image_buffer = tf.placeholder(tf.string, [], name="image_buffer") # Note: the shape argument is required for tensorframes as it uses a # slightly older version of tensorflow. shape = tf.reshape(tf.stack([height, width, num_channels], axis=0), shape=(3,), name='shape') if dtype == "uint8": image_uint8 = tf.decode_raw(image_buffer, tf.uint8, name="decode_raw") image_float = tf.to_float(image_uint8) else: assert dtype == "float32", "Unsupported dtype for image: %s" % dtype image_float = tf.decode_raw(image_buffer, tf.float32, name="decode_raw") image_reshaped = tf.reshape(image_float, shape, name="reshaped") image_reshaped = imageIO.fixColorChannelOrdering(self.channelOrder, image_reshaped) image_reshaped_expanded = tf.expand_dims(image_reshaped, 0, name="expanded") # Add on the original graph tf.import_graph_def(gdef, input_map={input_tensor_name: image_reshaped_expanded}, return_elements=[self.getOutputTensor().name], name=USER_GRAPH_NAMESPACE) # Flatten the output for tensorframes output_node = g.get_tensor_by_name(self._getOriginalOutputTensorName()) _ = tf.reshape(output_node[0], # batch-size = 1, shape=[-1], name=self._getFinalOutputOpName()) return g
def udf_impl(spimg): import numpy as np from tempfile import NamedTemporaryFile from sparkdl.image.imageIO import imageArrayToStruct img = imageIO.imageStructToPIL(spimg) # Warning: must use lossless format to guarantee consistency temp_fp = NamedTemporaryFile(suffix='.png') img.save(temp_fp, 'PNG') img_arr_reloaded = preprocessor(temp_fp.name) assert isinstance(img_arr_reloaded, np.ndarray), \ "expect preprocessor to return a numpy array" img_arr_reloaded = img_arr_reloaded.astype(np.uint8) # Keras works in RGB order, need to fix the order img_arr_reloaded = imageIO.fixColorChannelOrdering( currentOrder='RGB', imgAry=img_arr_reloaded) return imageArrayToStruct(img_arr_reloaded)
def buildSpImageConverter(channelOrder, img_dtype): """ Convert a imageIO byte encoded image into a image tensor suitable as input to ConvNets The name of the input must be a subset of those specified in `image.imageIO.imageSchema`. :param img_dtype: the type of data the underlying image bytes represent """ with IsolatedSession() as issn: # Flat image data -> image dimensions # This has to conform to `imageIO.imageSchema` height = tf.placeholder(tf.int32, [], name="height") width = tf.placeholder(tf.int32, [], name="width") num_channels = tf.placeholder(tf.int32, [], name="nChannels") image_buffer = tf.placeholder(tf.string, [], name="data") # The image is packed into bytes with height as leading dimension # This is the default behavior of Python Image Library shape = tf.reshape(tf.stack([height, width, num_channels], axis=0), shape=(3, ), name='shape') if img_dtype == 'uint8': image_uint8 = tf.decode_raw(image_buffer, tf.uint8, name="decode_raw") image_float = tf.to_float(image_uint8) elif img_dtype == 'float32': image_float = tf.decode_raw(image_buffer, tf.float32, name="decode_raw") else: raise ValueError( '''unsupported image data type "%s", currently only know how to handle uint8 and float32''' % img_dtype) image_reshaped = tf.reshape(image_float, shape, name="reshaped") image_reshaped = imageIO.fixColorChannelOrdering( channelOrder, image_reshaped) image_input = tf.expand_dims(image_reshaped, 0, name="image_input") gfn = issn.asGraphFunction([height, width, image_buffer, num_channels], [image_input]) return gfn
def _addReshapeLayers(self, tf_graph, dtype="uint8"): input_tensor_name = self.getInputTensor().name gdef = tf_graph.as_graph_def(add_shapes=True) g = tf.Graph() # pylint: disable=invalid-name with g.as_default(): # pylint: disable=not-context-manager # Flat image data -> image dimensions height = tf.placeholder(tf.int32, [], name="height") width = tf.placeholder(tf.int32, [], name="width") num_channels = tf.placeholder(tf.int32, [], name="num_channels") image_buffer = tf.placeholder(tf.string, [], name="image_buffer") # Note: the shape argument is required for tensorframes as it uses a # slightly older version of tensorflow. shape_tensor = tf.stack([height, width, num_channels], axis=0) shape = tf.reshape(shape_tensor, shape=(3, ), name='shape') if dtype == "uint8": image_uint8 = tf.decode_raw(image_buffer, tf.uint8, name="decode_raw") image_float = tf.to_float(image_uint8) else: assert dtype == "float32", "Unsupported dtype for image: %s" % dtype image_float = tf.decode_raw(image_buffer, tf.float32, name="decode_raw") image_reshaped = tf.reshape(image_float, shape, name="reshaped") image_reshaped = imageIO.fixColorChannelOrdering(self.channelOrder, image_reshaped) image_reshaped_expanded = tf.expand_dims(image_reshaped, 0, name="expanded") # Add on the original graph tf.import_graph_def( gdef, input_map={input_tensor_name: image_reshaped_expanded}, return_elements=[self.getOutputTensor().name], name=USER_GRAPH_NAMESPACE) # Flatten the output for tensorframes output_node = g.get_tensor_by_name(self._getOriginalOutputTensorName()) _ = tf.reshape(output_node[0], shape=[-1], name=self._getFinalOutputOpName()) return g