def testExisting(self): # Read some real PNGs, converting to different channel numbers prefix = 'tensorflow/core/lib/png/testdata/' inputs = (1, 'lena_gray.png'), (4, 'lena_rgba.png') for channels_in, filename in inputs: for channels in 0, 1, 3, 4: with self.test_session() as sess: png0 = io_ops.read_file(prefix + filename) image0 = image_ops.decode_png(png0, channels=channels) png0, image0 = sess.run([png0, image0]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) if channels == channels_in: image1 = image_ops.decode_png(image_ops.encode_png(image0)) self.assertAllEqual(image0, image1.eval())
def testShape(self): with self.test_session() as sess: png = constant_op.constant('nonsense') for channels in 0, 1, 3: image = image_ops.decode_png(png, channels=channels) self.assertEqual(image.get_shape().as_list(), [None, None, channels or None])
def testImageSummary(self): np.random.seed(7) with self.test_session() as sess: for depth in 1, 3, 4: shape = (4, 5, 7) + (depth,) bad_color = [255, 0, 0, 255][:depth] for positive in False, True: # Build a mostly random image with one nan const = np.random.randn(*shape).astype(np.float32) const[0, 1, 2] = 0 # Make the nan entry not the max if positive: const = 1 + np.maximum(const, 0) scale = 255 / const.reshape(4, -1).max(axis=1) offset = 0 else: scale = 127 / np.abs(const.reshape(4, -1)).max(axis=1) offset = 128 adjusted = np.floor(scale[:, None, None, None] * const + offset) const[0, 1, 2, depth // 2] = np.nan # Summarize summ = tf.image_summary("img", const) value = sess.run(summ) self.assertEqual([], summ.get_shape()) image_summ = self._AsSummary(value) # Decode the first image and check consistency image = image_ops.decode_png( image_summ.value[0].image.encoded_image_string).eval() self.assertAllEqual(image[1, 2], bad_color) image[1, 2] = adjusted[0, 1, 2] self.assertAllClose(image, adjusted[0]) # Check the rest of the proto self._CheckProto(image_summ, shape)
def testImageSummaryUint8(self): np.random.seed(7) with self.test_session() as sess: for depth in 1, 3, 4: shape = (4, 5, 7) + (depth,) # Build a random uint8 image images = np.random.randint(256, size=shape).astype(np.uint8) tf_images = tf.convert_to_tensor(images) self.assertEqual(tf_images.dtype, tf.uint8) # Summarize summ = tf.image_summary("img", tf_images) value = sess.run(summ) self.assertEqual([], summ.get_shape()) image_summ = self._AsSummary(value) # Decode the first image and check consistency. # Since we're uint8, everything should be exact. image = image_ops.decode_png( image_summ.value[0].image.encoded_image_string).eval() self.assertAllEqual(image, images[0]) # Check the rest of the proto self._CheckProto(image_summ, shape)
def testSyntheticTwoChannelUint16(self): with self.test_session() as sess: # Strip the b channel from an rgb image to get a two-channel image. gray_alpha = _SimpleColorRamp()[:, :, 0:2] image0 = constant_op.constant(gray_alpha, dtype=dtypes.uint16) png0 = image_ops.encode_png(image0, compression=7) image1 = image_ops.decode_png(png0, dtype=dtypes.uint16) png0, image0, image1 = sess.run([png0, image0, image1]) self.assertEqual(2, image0.shape[-1]) self.assertAllEqual(image0, image1)
def testEndpointsTensorAndMetadataAssets(self): g = ops.Graph() with g.as_default(): manager = plugin_asset.get_plugin_asset( projector_plugin.ProjectorPluginAsset) metadata = projector_plugin.EmbeddingMetadata(3) metadata.add_column('labels', ['a', 'b', 'c']) manager.add_metadata_for_embedding_variable('test', metadata) expected_tensor = np.array([[1, 2], [3, 4], [5, 6]]) image1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) image2 = np.array([[[10, 20, 30], [40, 50, 60]], [[70, 80, 90], [100, 110, 120]]]) manager.add_embedding('emb', expected_tensor, metadata, [image1, image2], [2, 2]) fw = writer.FileWriter(self.log_dir, graph=g) fw.close() self._SetupWSGIApp() run_json = self._GetJson('/data/plugin/projector/runs') self.assertTrue(run_json) run = run_json[0] metadata_query = '/data/plugin/projector/metadata?run=%s&name=emb' % run metadata_tsv = self._Get(metadata_query).data self.assertEqual(metadata_tsv, b'a\nb\nc\n') unk_metadata_query = '/data/plugin/projector/metadata?run=%s&name=q' % run response = self._Get(unk_metadata_query) self.assertEqual(response.status_code, 400) tensor_query = '/data/plugin/projector/tensor?run=%s&name=emb' % run tensor_bytes = self._Get(tensor_query).data self._AssertTensorResponse(tensor_bytes, expected_tensor) unk_tensor_query = '/data/plugin/projector/tensor?run=%s&name=var1' % run response = self._Get(unk_tensor_query) self.assertEqual(response.status_code, 400) image_query = '/data/plugin/projector/sprite_image?run=%s&name=emb' % run image_bytes = self._Get(image_query).data with ops.Graph().as_default(): s = session.Session() image_array = image_ops.decode_png(image_bytes).eval(session=s).tolist() expected_sprite_image = [ [[1, 2, 3], [4, 5, 6], [10, 20, 30], [40, 50, 60]], [[7, 8, 9], [10, 11, 12], [70, 80, 90], [100, 110, 120]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] ] self.assertEqual(image_array, expected_sprite_image)
def testPng(self): # Read some real PNGs, converting to different channel numbers inputs = [(1, "lena_gray.png")] for channels_in, filename in inputs: for channels in 0, 1, 3, 4: with self.cached_session(use_gpu=True) as sess: path = os.path.join(prefix_path, "png", "testdata", filename) png0 = io_ops.read_file(path) image0 = image_ops.decode_image(png0, channels=channels) image1 = image_ops.decode_png(png0, channels=channels) png0, image0, image1 = self.evaluate([png0, image0, image1]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) self.assertAllEqual(image0, image1)
def testSynthetic(self): with self.test_session() as sess: # Encode it, then decode it image0 = constant_op.constant(_SimpleColorRamp()) png0 = image_ops.encode_png(image0, compression=7) image1 = image_ops.decode_png(png0) png0, image0, image1 = sess.run([png0, image0, image1]) # PNG is lossless self.assertAllEqual(image0, image1) # Smooth ramps compress well, but not too well self.assertGreaterEqual(len(png0), 400) self.assertLessEqual(len(png0), 750)
def testPng(self): # Read some real PNGs, converting to different channel numbers inputs = [(1, "lena_gray.png")] for channels_in, filename in inputs: for channels in 0, 1, 3, 4: with self.cached_session() as sess: path = os.path.join(prefix_path, "png", "testdata", filename) png0 = io_ops.read_file(path) image0 = image_ops.decode_image(png0, channels=channels) image1 = image_ops.decode_png(png0, channels=channels) png0, image0, image1 = self.evaluate( [png0, image0, image1]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) self.assertAllEqual(image0, image1)
def testImageSummary(self): np.random.seed(7) with self.test_session() as sess: for depth in 1, 3, 4: shape = (4, 5, 7) + (depth,) bad_color = [255, 0, 0, 255][:depth] for positive in False, True: # Build a mostly random image with one nan const = np.random.randn(*shape) const[0, 1, 2] = 0 # Make the nan entry not the max if positive: const = 1 + np.maximum(const, 0) scale = 255 / const.reshape(4, -1).max(axis=1) offset = 0 else: scale = 127 / np.abs(const.reshape(4, -1)).max(axis=1) offset = 128 adjusted = np.floor(scale[:, None, None, None] * const + offset) const[0, 1, 2, depth // 2] = np.nan # Summarize summ = tf.image_summary("img", const) value = sess.run(summ) self.assertEqual([], summ.get_shape()) image_summ = self._AsSummary(value) # Decode the first image and check consistency image = image_ops.decode_png(image_summ.value[0].image.encoded_image_string).eval() self.assertAllEqual(image[1, 2], bad_color) image[1, 2] = adjusted[0, 1, 2] self.assertAllClose(image, adjusted[0]) # Check the rest of the proto # Only the first 3 images are returned. for v in image_summ.value: v.image.ClearField("encoded_image_string") expected = "\n".join( """ value { tag: "img/image/%d" image { height: %d width: %d colorspace: %d } }""" % ((i,) + shape[1:]) for i in xrange(3) ) self.assertProtoEquals(expected, image_summ)
def testImageSummary(self): np.random.seed(7) with self.test_session() as sess: for depth in 1, 3, 4: shape = (4, 5, 7) + (depth, ) bad_color = [255, 0, 0, 255][:depth] for positive in False, True: # Build a mostly random image with one nan const = np.random.randn(*shape) const[0, 1, 2] = 0 # Make the nan entry not the max if positive: const = 1 + np.maximum(const, 0) scale = 255 / const.reshape(4, -1).max(axis=1) offset = 0 else: scale = 127 / np.abs(const.reshape(4, -1)).max(axis=1) offset = 128 adjusted = np.floor(scale[:, None, None, None] * const + offset) const[0, 1, 2, depth / 2] = np.nan # Summarize summ = tf.image_summary("img", const) value = sess.run(summ) self.assertEqual([], summ.get_shape()) image_summ = self._AsSummary(value) # Decode the first image and check consistency image = image_ops.decode_png( image_summ.value[0].image.encoded_image_string).eval() self.assertAllEqual(image[1, 2], bad_color) image[1, 2] = adjusted[0, 1, 2] self.assertAllClose(image, adjusted[0]) # Check the rest of the proto # Only the first 3 images are returned. for v in image_summ.value: v.image.ClearField("encoded_image_string") expected = '\n'.join(""" value { tag: "img/image/%d" image { height: %d width: %d colorspace: %d } }""" % ((i, ) + shape[1:]) for i in xrange(3)) self.assertProtoEquals(expected, image_summ)
def test16bit(self): img_bytes = [[0, 255], [1024, 1024 + 255]] # Encoded PNG bytes resulting from encoding the above img_bytes # using go's image/png encoder. encoded_bytes = [ 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 2, 0, 0, 0, 2, 16, 0, 0, 0, 0, 7, 77, 142, 187, 0, 0, 0, 21, 73, 68, 65, 84, 120, 156, 98, 98, 96, 96, 248, 207, 194, 2, 36, 1, 1, 0, 0, 255, 255, 6, 60, 1, 10, 68, 160, 26, 131, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130 ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze( image_ops.decode_png(img_in, dtype=dtypes.uint16)) with self.cached_session(): decoded = self.evaluate(decode) self.assertAllEqual(decoded, img_bytes)
def decode(serialized_example): features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'image/name': tf.FixedLenFeature([], tf.string), 'image/height': tf.FixedLenFeature([1], tf.int64), 'image/width': tf.FixedLenFeature([1], tf.int64), 'image/channels': tf.FixedLenFeature([1], tf.int64), 'image/shape': tf.FixedLenFeature([3], tf.int64), 'image/format': tf.FixedLenFeature([], tf.string), 'image/encoded': tf.FixedLenFeature([], tf.string), 'label/format': tf.FixedLenFeature([], tf.string), 'label/encoded': tf.FixedLenFeature([], tf.string) }) features['image/encoded'] = image_ops.decode_jpeg(features['image/encoded'], channels=3) features['label/encoded'] = image_ops.decode_png(features['label/encoded'], channels=1) # image.set_shape((mnist.IMAGE_PIXELS)) return features
def test16bit(self): img_bytes = [[0, 255], [1024, 1024 + 255]] # Encoded PNG bytes resulting from encoding the above img_bytes # using go's image/png encoder. encoded_bytes = [ 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 2, 0, 0, 0, 2, 16, 0, 0, 0, 0, 7, 77, 142, 187, 0, 0, 0, 21, 73, 68, 65, 84, 120, 156, 98, 98, 96, 96, 248, 207, 194, 2, 36, 1, 1, 0, 0, 255, 255, 6, 60, 1, 10, 68, 160, 26, 131, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130 ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze( image_ops.decode_png( img_in, dtype=dtypes.uint16)) with self.cached_session(): decoded = self.evaluate(decode) self.assertAllEqual(decoded, img_bytes)
def testAddEmbeddingWithThumbnails(self): manager = plugin_asset.get_plugin_asset( projector_plugin.ProjectorPluginAsset) image1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) image2 = np.array([[[10, 20, 30], [40, 50, 60]], [[70, 80, 90], [100, 110, 120]]]) manager.add_embedding('test', np.array([[1], [2], [3]]), thumbnails=[image1, image2], thumbnail_dim=[2, 2]) assets = manager.assets() config = projector_config_pb2.ProjectorConfig() embedding = config.embeddings.add() embedding.tensor_name = 'test' embedding.tensor_shape.extend([3, 1]) embedding.tensor_path = 'test_values.tsv' embedding.sprite.image_path = 'test_sprite.png' embedding.sprite.single_image_dim.extend([2, 2]) expected_config_pbtxt = text_format.MessageToString(config) self.assertEqual(assets['projector_config.pbtxt'], expected_config_pbtxt) self.assertEqual(assets['test_values.tsv'], b'1\n2\n3\n') png_bytes = assets['test_sprite.png'] with ops.Graph().as_default(): s = session.Session() image_array = image_ops.decode_png(png_bytes).eval( session=s).tolist() expected_master_image = [[[1, 2, 3], [4, 5, 6], [10, 20, 30], [40, 50, 60]], [[7, 8, 9], [10, 11, 12], [70, 80, 90], [100, 110, 120]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]] self.assertEqual(image_array, expected_master_image)
def testImageSummary(self): np.random.seed(7) for depth in (1, 3, 4): for positive in False, True: with self.test_session(graph=ops.Graph()) as sess: shape = (4, 5, 7) + (depth, ) bad_color = [255, 0, 0, 255][:depth] # Build a mostly random image with one nan const = np.random.randn(*shape).astype(np.float32) const[0, 1, 2] = 0 # Make the nan entry not the max if positive: const = 1 + np.maximum(const, 0) scale = 255 / const.reshape(4, -1).max(axis=1) offset = 0 else: scale = 127 / np.abs(const.reshape(4, -1)).max(axis=1) offset = 128 adjusted = np.floor(scale[:, None, None, None] * const + offset) const[0, 1, 2, depth // 2] = np.nan # Summarize summ = summary.image("img", const) value = sess.run(summ) self.assertEqual([], summ.get_shape()) image_summ = self._AsSummary(value) # Decode the first image and check consistency image = image_ops.decode_png( image_summ.value[0].image.encoded_image_string).eval() self.assertAllEqual(image[1, 2], bad_color) image[1, 2] = adjusted[0, 1, 2] self.assertAllClose(image, adjusted[0], rtol=2e-5, atol=2e-5) # Check the rest of the proto self._CheckProto(image_summ, shape)
def testAddEmbeddingWithThumbnails(self): manager = plugin_asset.get_plugin_asset( projector_plugin.ProjectorPluginAsset) image1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) image2 = np.array([[[10, 20, 30], [40, 50, 60]], [[70, 80, 90], [100, 110, 120]]]) manager.add_embedding( 'test', np.array([[1], [2], [3]]), thumbnails=[image1, image2], thumbnail_dim=[2, 2]) assets = manager.assets() config = projector_config_pb2.ProjectorConfig() embedding = config.embeddings.add() embedding.tensor_name = 'test' embedding.tensor_shape.extend([3, 1]) embedding.tensor_path = 'test_values.tsv' embedding.sprite.image_path = 'test_sprite.png' embedding.sprite.single_image_dim.extend([2, 2]) expected_config_pbtxt = text_format.MessageToString(config) self.assertEqual(assets['projector_config.pbtxt'], expected_config_pbtxt) self.assertEqual(assets['test_values.tsv'], b'1\n2\n3\n') png_bytes = assets['test_sprite.png'] with ops.Graph().as_default(): s = session.Session() image_array = image_ops.decode_png(png_bytes).eval(session=s).tolist() expected_master_image = [ [[1, 2, 3], [4, 5, 6], [10, 20, 30], [40, 50, 60]], [[7, 8, 9], [10, 11, 12], [70, 80, 90], [100, 110, 120]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] ] self.assertEqual(image_array, expected_master_image)
def load_image(self, image_file, sess): image_op = image_ops.decode_png( io_ops.read_file(image_file), dtype=dtypes.uint8, channels=4)[:, :, 0:3] return sess.run(image_op)
def load_image(self, image_file, sess): image_op = image_ops.decode_png(io_ops.read_file(image_file), dtype=dtypes.uint8, channels=4)[:, :, 0:3] return sess.run(image_op)
def decode_png(): return image_ops.decode_png(image_buffer, self._channels)
def DecodePng(): return image_ops.decode_png(image_buffer, 3)