def testNoOp(self): x_shape = [13, 9, 3] x_np = np.ones(x_shape, dtype=np.float32) with self.test_session(): x = constant_op.constant(x_np, shape=x_shape) target_height = x_shape[0] target_width = x_shape[1] y = image_ops.crop_to_bounding_box(x, 0, 0, target_height, target_width) y_tf = y.eval() self.assertAllEqual(y_tf, x_np)
def testNoOp(self): x_shape = [13, 9, 3] x_np = np.ones(x_shape, dtype=np.float32) with self.test_session(): x = constant_op.constant(x_np, shape=x_shape) target_height = x_shape[0] target_width = x_shape[1] y = image_ops.crop_to_bounding_box(x, 0, 0, target_height, target_width) y_tf = y.eval() self.assertAllEqual(y_tf, x_np)
def testCropping(self): x_np = np.arange(0, 30, dtype=np.int32).reshape([6, 5, 1]) offset_height = 1 after_height = 2 offset_width = 0 after_width = 3 target_height = x_np.shape[0] - offset_height - after_height target_width = x_np.shape[1] - offset_width - after_width y_np = x_np[offset_height : offset_height + target_height, offset_width : offset_width + target_width, :] with self.test_session(): x = constant_op.constant(x_np, shape=x_np.shape) y = image_ops.crop_to_bounding_box(x, offset_height, offset_width, target_height, target_width) y_tf = y.eval() self.assertAllEqual(y_tf.flatten(), y_np.flatten())
def testCropping(self): x_np = np.arange(0, 30, dtype=np.int32).reshape([6, 5, 1]) offset_height = 1 after_height = 2 offset_width = 0 after_width = 3 target_height = x_np.shape[0] - offset_height - after_height target_width = x_np.shape[1] - offset_width - after_width y_np = x_np[offset_height:offset_height + target_height, offset_width:offset_width + target_width, :] with self.test_session(): x = constant_op.constant(x_np, shape=x_np.shape) y = image_ops.crop_to_bounding_box(x, offset_height, offset_width, target_height, target_width) y_tf = y.eval() self.assertAllEqual(y_tf.flatten(), y_np.flatten())
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