def _encoder(image, image_format): assert image_format in ['jpeg', 'png'] if image_format == 'jpeg': tf_image = constant_op.constant(image, dtype=dtypes.uint8) return image_ops.encode_jpeg(tf_image) if image_format == 'png': tf_image = constant_op.constant(image, dtype=dtypes.uint8) return image_ops.encode_png(tf_image)
def testSynthetic(self): with self.test_session() as sess: # Encode it, then decode it, then encode it image0 = constant_op.constant(_SimpleColorRamp()) jpeg0 = image_ops.encode_jpeg(image0) image1 = image_ops.decode_jpeg(jpeg0) image2 = image_ops.decode_jpeg(image_ops.encode_jpeg(image1)) jpeg0, image0, image1, image2 = sess.run([jpeg0, image0, image1, image2]) # The decoded-encoded image should be similar to the input self.assertLess(self.averageError(image0, image1), 0.6) # We should be very close to a fixpoint self.assertLess(self.averageError(image1, image2), 0.02) # Smooth ramps compress well (input size is 153600) self.assertGreaterEqual(len(jpeg0), 5000) self.assertLessEqual(len(jpeg0), 6000)
def _Encoder(self, image, image_format): assert image_format in ['jpeg', 'JPEG', 'png', 'PNG', 'raw', 'RAW'] if image_format in ['jpeg', 'JPEG']: tf_image = constant_op.constant(image, dtype=dtypes.uint8) return image_ops.encode_jpeg(tf_image) if image_format in ['png', 'PNG']: tf_image = constant_op.constant(image, dtype=dtypes.uint8) return image_ops.encode_png(tf_image) if image_format in ['raw', 'RAW']: return constant_op.constant(image.tostring(), dtype=dtypes.string)
def testExisting(self): # Read a real jpeg and verify shape path = ('tensorflow/core/lib/jpeg/testdata/' 'jpeg_merge_test1.jpg') with self.test_session() as sess: jpeg0 = io_ops.read_file(path) image0 = image_ops.decode_jpeg(jpeg0) image1 = image_ops.decode_jpeg(image_ops.encode_jpeg(image0)) jpeg0, image0, image1 = sess.run([jpeg0, image0, image1]) self.assertEqual(len(jpeg0), 3771) self.assertEqual(image0.shape, (256, 128, 3)) self.assertLess(self.averageError(image0, image1), 0.8)
def testExisting(self): # Read a real jpeg and verify shape path = "tensorflow/core/lib/jpeg/testdata/" "jpeg_merge_test1.jpg" with self.test_session() as sess: jpeg0 = io_ops.read_file(path) image0 = image_ops.decode_jpeg(jpeg0) image1 = image_ops.decode_jpeg(image_ops.encode_jpeg(image0)) jpeg0, image0, image1 = sess.run([jpeg0, image0, image1]) self.assertEqual(len(jpeg0), 3771) self.assertEqual(image0.shape, (256, 128, 3)) self.assertLess(self.averageError(image0, image1), 0.8)
def _evalDecodeJpeg(self, image_name, parallelism, num_iters, tile=None): """Evaluate DecodeJpegOp for the given image. TODO(tanmingxing): add decoding+cropping as well. Args: image_name: a string of image file name (without suffix). parallelism: the number of concurrent decode_jpeg ops to be run. num_iters: number of iterations for evaluation. tile: if not None, tile the image to composite a larger fake image. Returns: The duration of the run in seconds. """ ops.reset_default_graph() image_file_path = os.path.join(prefix_path, image_name) if tile is None: image_content = variable_scope.get_variable( 'image_%s' % image_name, initializer=io_ops.read_file(image_file_path)) else: single_image = image_ops.decode_jpeg( io_ops.read_file(image_file_path), channels=3, name='single_image') # Tile the image to composite a new larger image. tiled_image = array_ops.tile(single_image, tile) image_content = variable_scope.get_variable( 'tiled_image_%s' % image_name, initializer=image_ops.encode_jpeg(tiled_image)) with session.Session() as sess: sess.run(variables.global_variables_initializer()) images = [] for i in xrange(parallelism): images.append( image_ops.decode_jpeg(image_content, channels=3, name='image_%d' % (i))) r = control_flow_ops.group(*images) for _ in xrange(3): # Skip warm up time. sess.run(r) start_time = time.time() for _ in xrange(num_iters): sess.run(r) return time.time() - start_time
def _Encoder(self, image, image_format): assert image_format in ['jpeg', 'JPEG', 'png', 'PNG', 'raw', 'RAW'] if image_format in ['jpeg', 'JPEG']: tf_image = tf.constant(image, dtype=tf.uint8) return image_ops.encode_jpeg(tf_image) if image_format in ['png', 'PNG']: tf_image = tf.constant(image, dtype=tf.uint8) return image_ops.encode_png(tf_image) if image_format in ['raw', 'RAW']: # If machine is big endian, change the byte ordering in case of dtype # float32 so that it should be interpreted correctly. if image.dtype == np.float32 and sys.byteorder == 'big': image = image.astype('<f4') return tf.constant(image.tostring(), dtype=tf.string)
def _evalDecodeJpeg(self, image_name, parallelism, num_iters, tile=None): """Evaluate DecodeJpegOp for the given image. TODO(tanmingxing): add decoding+cropping as well. Args: image_name: a string of image file name (without suffix). parallelism: the number of concurrent decode_jpeg ops to be run. num_iters: number of iterations for evaluation. tile: if not None, tile the image to composite a larger fake image. Returns: The duration of the run in seconds. """ ops.reset_default_graph() image_file_path = os.path.join(prefix_path, image_name) if tile is None: image_content = variable_scope.get_variable( 'image_%s' % image_name, initializer=io_ops.read_file(image_file_path)) else: single_image = image_ops.decode_jpeg( io_ops.read_file(image_file_path), channels=3, name='single_image') # Tile the image to composite a new larger image. tiled_image = array_ops.tile(single_image, tile) image_content = variable_scope.get_variable( 'tiled_image_%s' % image_name, initializer=image_ops.encode_jpeg(tiled_image)) with session.Session() as sess: sess.run(variables.global_variables_initializer()) images = [] for i in xrange(parallelism): images.append( image_ops.decode_jpeg( image_content, channels=3, name='image_%d' % (i))) r = control_flow_ops.group(*images) for _ in xrange(3): # Skip warm up time. sess.run(r) start_time = time.time() for _ in xrange(num_iters): sess.run(r) return time.time() - start_time
def _evalDecodeJpeg(self, image_name, parallelism, num_iters, crop_during_decode=None, crop_window=None, tile=None): """Evaluate DecodeJpegOp for the given image. TODO(tanmingxing): add decoding+cropping as well. Args: image_name: a string of image file name (without suffix). parallelism: the number of concurrent decode_jpeg ops to be run. num_iters: number of iterations for evaluation. crop_during_decode: If true, use fused DecodeAndCropJpeg instead of separate decode and crop ops. It is ignored if crop_window is None. crop_window: if not None, crop the decoded image. Depending on crop_during_decode, cropping could happen during or after decoding. tile: if not None, tile the image to composite a larger fake image. Returns: The duration of the run in seconds. """ ops.reset_default_graph() image_file_path = resource_loader.get_path_to_datafile( os.path.join('core', 'lib', 'jpeg', 'testdata', image_name)) if tile is None: image_content = variable_scope.get_variable( 'image_%s' % image_name, initializer=io_ops.read_file(image_file_path)) else: single_image = image_ops.decode_jpeg( io_ops.read_file(image_file_path), channels=3, name='single_image') # Tile the image to composite a new larger image. tiled_image = array_ops.tile(single_image, tile) image_content = variable_scope.get_variable( 'tiled_image_%s' % image_name, initializer=image_ops.encode_jpeg(tiled_image)) with session.Session() as sess: self.evaluate(variables.global_variables_initializer()) images = [] for _ in xrange(parallelism): if crop_window is None: # No crop. image = image_ops.decode_jpeg(image_content, channels=3) elif crop_during_decode: # combined decode and crop. image = image_ops.decode_and_crop_jpeg(image_content, crop_window, channels=3) else: # separate decode and crop. image = image_ops.decode_jpeg(image_content, channels=3) image = image_ops.crop_to_bounding_box( image, offset_height=crop_window[0], offset_width=crop_window[1], target_height=crop_window[2], target_width=crop_window[3]) images.append(image) r = control_flow_ops.group(*images) for _ in xrange(3): # Skip warm up time. self.evaluate(r) start_time = time.time() for _ in xrange(num_iters): self.evaluate(r) end_time = time.time() return end_time - start_time
def _evalDecodeJpeg(self, image_name, parallelism, num_iters, crop_during_decode=None, crop_window=None, tile=None): """Evaluate DecodeJpegOp for the given image. TODO(tanmingxing): add decoding+cropping as well. Args: image_name: a string of image file name (without suffix). parallelism: the number of concurrent decode_jpeg ops to be run. num_iters: number of iterations for evaluation. crop_during_decode: If true, use fused DecodeAndCropJpeg instead of separate decode and crop ops. It is ignored if crop_window is None. crop_window: if not None, crop the decoded image. Depending on crop_during_decode, cropping could happen during or after decoding. tile: if not None, tile the image to composite a larger fake image. Returns: The duration of the run in seconds. """ ops.reset_default_graph() image_file_path = os.path.join(prefix_path, image_name) if tile is None: image_content = variable_scope.get_variable( 'image_%s' % image_name, initializer=io_ops.read_file(image_file_path)) else: single_image = image_ops.decode_jpeg( io_ops.read_file(image_file_path), channels=3, name='single_image') # Tile the image to composite a new larger image. tiled_image = array_ops.tile(single_image, tile) image_content = variable_scope.get_variable( 'tiled_image_%s' % image_name, initializer=image_ops.encode_jpeg(tiled_image)) with session.Session() as sess: sess.run(variables.global_variables_initializer()) images = [] for _ in xrange(parallelism): if crop_window is None: # No crop. image = image_ops.decode_jpeg(image_content, channels=3) elif crop_during_decode: # combined decode and crop. image = image_ops.decode_and_crop_jpeg( image_content, crop_window, channels=3) else: # separate decode and crop. image = image_ops.decode_jpeg(image_content, channels=3) image = image_ops.crop_to_bounding_box( image, offset_height=crop_window[0], offset_width=crop_window[1], target_height=crop_window[2], target_width=crop_window[3]) images.append(image) r = control_flow_ops.group(*images) for _ in xrange(3): # Skip warm up time. sess.run(r) start_time = time.time() for _ in xrange(num_iters): sess.run(r) return time.time() - start_time