def testNoOp(self): img_shape = [1, 6, 4, 1] single_shape = [6, 4, 1] # This test is also conducted with int8, so 127 is the maximum # value that can be used. data = [127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127, 50, 50, 100, 100, 50, 50, 100, 100] target_height = 6 target_width = 4 for nptype in self.TYPES: img_np = np.array(data, dtype=nptype).reshape(img_shape) for opt in self.OPTIONS: with self.test_session() as sess: image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) yshape = array_ops.shape(y) resized, newshape = sess.run([y, yshape]) self.assertAllEqual(img_shape, newshape) self.assertAllClose(resized, img_np, atol=1e-5) # Resizing with a single image must leave the shape unchanged also. with self.test_session(): img_single = img_np.reshape(single_shape) image = constant_op.constant(img_single, shape=single_shape) y = image_ops.resize_images(image, target_height, target_width, self.OPTIONS[0]) yshape = array_ops.shape(y) newshape = yshape.eval() self.assertAllEqual(single_shape, newshape)
def testNoOp(self): img_shape = [1, 6, 4, 1] single_shape = [6, 4, 1] # This test is also conducted with int8, so 127 is the maximum # value that can be used. data = [ 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127, 50, 50, 100, 100, 50, 50, 100, 100 ] target_height = 6 target_width = 4 for nptype in self.TYPES: img_np = np.array(data, dtype=nptype).reshape(img_shape) for opt in self.OPTIONS: with self.test_session() as sess: image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) yshape = array_ops.shape(y) resized, newshape = sess.run([y, yshape]) self.assertAllEqual(img_shape, newshape) self.assertAllClose(resized, img_np, atol=1e-5) # Resizing with a single image must leave the shape unchanged also. with self.test_session(): img_single = img_np.reshape(single_shape) image = constant_op.constant(img_single, shape=single_shape) y = image_ops.resize_images(image, target_height, target_width, self.OPTIONS[0]) yshape = array_ops.shape(y) newshape = yshape.eval() self.assertAllEqual(single_shape, newshape)
def testNoOp(self): img_shape = [1, 6, 4, 1] single_shape = [6, 4, 1] data = [128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 6 target_width = 4 for opt in self.OPTIONS: with self.test_session() as sess: image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) yshape = array_ops.shape(y) resized, newshape = sess.run([y, yshape]) self.assertAllEqual(img_shape, newshape) self.assertAllClose(resized, img_np, atol=1e-5) # Resizing with a single image must leave the shape unchanged also. with self.test_session(): img_single = img_np.reshape(single_shape) image = constant_op.constant(img_single, shape=single_shape) y = image_ops.resize_images(image, target_height, target_width, self.OPTIONS[0]) yshape = array_ops.shape(y) newshape = yshape.eval() self.assertAllEqual(single_shape, newshape)
def testResizeDownArea(self): img_shape = [1, 6, 6, 1] data = [128, 64, 32, 16, 8, 4, 4, 8, 16, 32, 64, 128, 128, 64, 32, 16, 8, 4, 5, 10, 15, 20, 25, 30, 30, 25, 20, 15, 10, 5, 5, 10, 15, 20, 25, 30] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 4 target_width = 4 expected_data = [73, 33, 23, 39, 73, 33, 23, 39, 14, 16, 19, 21, 14, 16, 19, 21] with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, image_ops.ResizeMethod.AREA) expected = np.array(expected_data).reshape( [1, target_height, target_width, 1]) resized = y.eval() self.assertAllClose(resized, expected, atol=1)
def _make_sprite_image(thumbnails, thumbnail_dim): """Constructs a sprite image from thumbnails and returns the png bytes.""" if len(thumbnails) < 1: raise ValueError('The length of "thumbnails" must be >= 1') if isinstance(thumbnails, np.ndarray) and thumbnails.ndim != 4: raise ValueError('"thumbnails" should be of rank 4, ' 'but is of rank %d' % thumbnails.ndim) if isinstance(thumbnails, list): if not isinstance(thumbnails[0], np.ndarray) or thumbnails[0].ndim != 3: raise ValueError('Each element of "thumbnails" must be a 3D `ndarray`') thumbnails = np.array(thumbnails) with ops.Graph().as_default(): s = session.Session() resized_images = image_ops.resize_images(thumbnails, thumbnail_dim).eval( session=s) images_per_row = int(math.ceil(math.sqrt(len(thumbnails)))) thumb_height = thumbnail_dim[0] thumb_width = thumbnail_dim[1] master_height = images_per_row * thumb_height master_width = images_per_row * thumb_width num_channels = thumbnails.shape[3] master = np.zeros([master_height, master_width, num_channels]) for idx, image in enumerate(resized_images): left_idx = idx % images_per_row top_idx = int(math.floor(idx / images_per_row)) left_start = left_idx * thumb_width left_end = left_start + thumb_width top_start = top_idx * thumb_height top_end = top_start + thumb_height master[top_start:top_end, left_start:left_end, :] = image return image_ops.encode_png(master).eval(session=s)
def model(x): x = var * x x = image_ops.resize_images( x, size=[img_width, img_width], method=image_ops.ResizeMethod.BILINEAR) return x
def testResizeDown(self): data = [128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100] expected_data = [128, 64, 64, 128, 50, 100] target_height = 3 target_width = 2 # Test out 3-D and 4-D image shapes. img_shapes = [[1, 6, 4, 1], [6, 4, 1]] target_shapes = [[1, target_height, target_width, 1], [target_height, target_width, 1]] for target_shape, img_shape in zip(target_shapes, img_shapes): img_np = np.array(data, dtype=np.uint8).reshape(img_shape) for opt in self.OPTIONS: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) expected = np.array(expected_data).reshape(target_shape) resized = y.eval() self.assertAllClose(resized, expected, atol=1e-5)
def testResizeDown(self): # This test is also conducted with int8, so 127 is the maximum # value that can be used. data = [127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127, 50, 50, 100, 100, 50, 50, 100, 100] expected_data = [127, 64, 64, 127, 50, 100] target_height = 3 target_width = 2 # Test out 3-D and 4-D image shapes. img_shapes = [[1, 6, 4, 1], [6, 4, 1]] target_shapes = [[1, target_height, target_width, 1], [target_height, target_width, 1]] for target_shape, img_shape in zip(target_shapes, img_shapes): for nptype in self.TYPES: img_np = np.array(data, dtype=nptype).reshape(img_shape) for opt in self.OPTIONS: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) expected = np.array(expected_data).reshape(target_shape) resized = y.eval() self.assertAllClose(resized, expected, atol=1e-5)
def testResizeUpBicubic(self): img_shape = [1, 6, 6, 1] data = [ 128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100 ] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 8 target_width = 8 expected_data = [ 128, 135, 96, 55, 64, 114, 134, 128, 78, 81, 68, 52, 57, 118, 144, 136, 55, 49, 79, 109, 103, 89, 83, 84, 74, 70, 95, 122, 115, 69, 49, 55, 100, 105, 75, 43, 50, 89, 105, 100, 57, 54, 74, 96, 91, 65, 55, 58, 70, 69, 75, 81, 80, 72, 69, 70, 105, 112, 75, 36, 45, 92, 111, 105 ] with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, image_ops.ResizeMethod.BICUBIC) resized = y.eval() expected = np.array(expected_data).reshape( [1, target_height, target_width, 1]) self.assertAllClose(resized, expected, atol=1)
def testResizeDown(self): # This test is also conducted with int8, so 127 is the maximum # value that can be used. data = [ 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127, 50, 50, 100, 100, 50, 50, 100, 100 ] expected_data = [127, 64, 64, 127, 50, 100] target_height = 3 target_width = 2 # Test out 3-D and 4-D image shapes. img_shapes = [[1, 6, 4, 1], [6, 4, 1]] target_shapes = [[1, target_height, target_width, 1], [target_height, target_width, 1]] for target_shape, img_shape in zip(target_shapes, img_shapes): for nptype in self.TYPES: img_np = np.array(data, dtype=nptype).reshape(img_shape) for opt in self.OPTIONS: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) expected = np.array(expected_data).reshape( target_shape) resized = y.eval() self.assertAllClose(resized, expected, atol=1e-5)
def testResizeDown(self): data = [ 128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100 ] expected_data = [128, 64, 64, 128, 50, 100] target_height = 3 target_width = 2 # Test out 3-D and 4-D image shapes. img_shapes = [[1, 6, 4, 1], [6, 4, 1]] target_shapes = [[1, target_height, target_width, 1], [target_height, target_width, 1]] for target_shape, img_shape in zip(target_shapes, img_shapes): img_np = np.array(data, dtype=np.uint8).reshape(img_shape) for opt in self.OPTIONS: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) expected = np.array(expected_data).reshape(target_shape) resized = y.eval() self.assertAllClose(resized, expected, atol=1e-5)
def testResizeUpBicubic(self): img_shape = [1, 6, 6, 1] data = [128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 8 target_width = 8 expected_data = [128, 135, 96, 55, 64, 114, 134, 128, 78, 81, 68, 52, 57, 118, 144, 136, 55, 49, 79, 109, 103, 89, 83, 84, 74, 70, 95, 122, 115, 69, 49, 55, 100, 105, 75, 43, 50, 89, 105, 100, 57, 54, 74, 96, 91, 65, 55, 58, 70, 69, 75, 81, 80, 72, 69, 70, 105, 112, 75, 36, 45, 92, 111, 105] with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, image_ops.ResizeMethod.BICUBIC) resized = y.eval() expected = np.array(expected_data).reshape( [1, target_height, target_width, 1]) self.assertAllClose(resized, expected, atol=1)
def testCompareNearestNeighbor(self): input_shape = [1, 5, 5, 3] target_height = target_width = 10 for nptype in [np.float]: for align_corners in [True, False]: img_np = np.arange(0, np.prod(input_shape), dtype=nptype).reshape(input_shape) with self.test_session(use_gpu=True): image = constant_op.constant(img_np, shape=input_shape) out_op = image_ops.resize_images(image, target_height, target_width, image_ops.ResizeMethod.NEAREST_NEIGHBOR, align_corners=align_corners) gpu_val = out_op.eval() with self.test_session(use_gpu=False): image = constant_op.constant(img_np, shape=input_shape) out_op = image_ops.resize_images(image, target_height, target_width, image_ops.ResizeMethod.NEAREST_NEIGHBOR, align_corners=align_corners) cpu_val = out_op.eval() self.assertAllClose(cpu_val, gpu_val, rtol=1e-5, atol=1e-5)
def testTensorArguments(self): img_shape = [1, 6, 4, 1] single_shape = [6, 4, 1] # This test is also conducted with int8, so 127 is the maximum # value that can be used. data = [ 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127, 50, 50, 100, 100, 50, 50, 100, 100 ] target_height = array_ops.placeholder(dtypes.int32) target_width = array_ops.placeholder(dtypes.int32) img_np = np.array(data, dtype=np.uint8).reshape(img_shape) for opt in self.OPTIONS: with self.test_session() as sess: image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) yshape = array_ops.shape(y) resized, newshape = sess.run([y, yshape], { target_height: 6, target_width: 4 }) self.assertAllEqual(img_shape, newshape) self.assertAllClose(resized, img_np, atol=1e-5) # Resizing with a single image must leave the shape unchanged also. with self.test_session(): img_single = img_np.reshape(single_shape) image = constant_op.constant(img_single, shape=single_shape) y = image_ops.resize_images(image, target_height, target_width, self.OPTIONS[0]) yshape = array_ops.shape(y) newshape = yshape.eval(feed_dict={ target_height: 6, target_width: 4 }) self.assertAllEqual(single_shape, newshape) # Incorrect shape. with self.assertRaises(ValueError): _ = image_ops.resize_images(image, [12, 32], 4, image_ops.ResizeMethod.BILINEAR) with self.assertRaises(ValueError): _ = image_ops.resize_images(image, 6, [12, 32], image_ops.ResizeMethod.BILINEAR) # Incorrect dtypes. with self.assertRaises(ValueError): _ = image_ops.resize_images(image, 6.0, 4, image_ops.ResizeMethod.BILINEAR) with self.assertRaises(ValueError): _ = image_ops.resize_images(image, 6, 4.0, image_ops.ResizeMethod.BILINEAR)
def testNoOp(self): img_shape = [1, 6, 4, 1] data = [ 128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100 ] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 6 target_width = 4 for opt in self.OPTIONS: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) resized = y.eval() self.assertAllClose(resized, img_np, atol=1e-5)
def testResizeUp(self): img_shape = [1, 3, 2, 1] data = [64, 32, 32, 64, 50, 100] target_height = 6 target_width = 4 expected_data = {} expected_data[image_ops.ResizeMethod.BILINEAR] = [ 64.0, 48.0, 32.0, 32.0, 48.0, 48.0, 48.0, 48.0, 32.0, 48.0, 64.0, 64.0, 41.0, 61.5, 82.0, 82.0, 50.0, 75.0, 100.0, 100.0, 50.0, 75.0, 100.0, 100.0] expected_data[image_ops.ResizeMethod.NEAREST_NEIGHBOR] = [ 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0, 100.0] expected_data[image_ops.ResizeMethod.AREA] = [ 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0, 100.0] for nptype in self.TYPES: for opt in [ image_ops.ResizeMethod.BILINEAR, image_ops.ResizeMethod.NEAREST_NEIGHBOR, image_ops.ResizeMethod.AREA]: for use_gpu in self.availableGPUModes(opt, nptype): with self.test_session(use_gpu=use_gpu): img_np = np.array(data, dtype=nptype).reshape(img_shape) image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) resized = y.eval() expected = np.array(expected_data[opt]).reshape( [1, target_height, target_width, 1]) self.assertAllClose(resized, expected, atol=1e-05)
def testTensorArguments(self): img_shape = [1, 6, 4, 1] single_shape = [6, 4, 1] # This test is also conducted with int8, so 127 is the maximum # value that can be used. data = [127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127, 50, 50, 100, 100, 50, 50, 100, 100] target_height = array_ops.placeholder(dtypes.int32) target_width = array_ops.placeholder(dtypes.int32) img_np = np.array(data, dtype=np.uint8).reshape(img_shape) for opt in self.OPTIONS: with self.test_session() as sess: image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) yshape = array_ops.shape(y) resized, newshape = sess.run([y, yshape], {target_height: 6, target_width: 4}) self.assertAllEqual(img_shape, newshape) self.assertAllClose(resized, img_np, atol=1e-5) # Resizing with a single image must leave the shape unchanged also. with self.test_session(): img_single = img_np.reshape(single_shape) image = constant_op.constant(img_single, shape=single_shape) y = image_ops.resize_images(image, target_height, target_width, self.OPTIONS[0]) yshape = array_ops.shape(y) newshape = yshape.eval(feed_dict={target_height: 6, target_width: 4}) self.assertAllEqual(single_shape, newshape) # Incorrect shape. with self.assertRaises(ValueError): _ = image_ops.resize_images( image, [12, 32], 4, image_ops.ResizeMethod.BILINEAR) with self.assertRaises(ValueError): _ = image_ops.resize_images( image, 6, [12, 32], image_ops.ResizeMethod.BILINEAR) # Incorrect dtypes. with self.assertRaises(ValueError): _ = image_ops.resize_images( image, 6.0, 4, image_ops.ResizeMethod.BILINEAR) with self.assertRaises(ValueError): _ = image_ops.resize_images( image, 6, 4.0, image_ops.ResizeMethod.BILINEAR)
def testResizeUp(self): img_shape = [1, 3, 2, 1] data = [64, 32, 32, 64, 50, 100] target_height = 6 target_width = 4 expected_data = {} expected_data[image_ops.ResizeMethod.BILINEAR] = [ 64.0, 48.0, 32.0, 32.0, 48.0, 48.0, 48.0, 48.0, 32.0, 48.0, 64.0, 64.0, 41.0, 61.5, 82.0, 82.0, 50.0, 75.0, 100.0, 100.0, 50.0, 75.0, 100.0, 100.0] expected_data[image_ops.ResizeMethod.NEAREST_NEIGHBOR] = [ 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0, 100.0] expected_data[image_ops.ResizeMethod.AREA] = [ 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0, 100.0] for nptype in self.TYPES: for opt in [ image_ops.ResizeMethod.BILINEAR, image_ops.ResizeMethod.NEAREST_NEIGHBOR, image_ops.ResizeMethod.AREA]: with self.test_session(): img_np = np.array(data, dtype=nptype).reshape(img_shape) image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) resized = y.eval() expected = np.array(expected_data[opt]).reshape( [1, target_height, target_width, 1]) self.assertAllClose(resized, expected, atol=1e-05)
def testResizeUp(self): img_shape = [1, 3, 2, 1] data = [128, 64, 64, 128, 50, 100] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 6 target_width = 4 expected_data = {} expected_data[image_ops.ResizeMethod.BILINEAR] = [ 128.0, 96.0, 64.0, 64.0, 96.0, 96.0, 96.0, 96.0, 64.0, 96.0, 128.0, 128.0, 57.0, 85.5, 114.0, 114.0, 50.0, 75.0, 100.0, 100.0, 50.0, 75.0, 100.0, 100.0] expected_data[image_ops.ResizeMethod.NEAREST_NEIGHBOR] = [ 128.0, 128.0, 64.0, 64.0, 128.0, 128.0, 64.0, 64.0, 64.0, 64.0, 128.0, 128.0, 64.0, 64.0, 128.0, 128.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0, 100.0] expected_data[image_ops.ResizeMethod.AREA] = [ 128.0, 128.0, 64.0, 64.0, 128.0, 128.0, 64.0, 64.0, 64.0, 64.0, 128.0, 128.0, 64.0, 64.0, 128.0, 128.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0, 100.0] for opt in [ image_ops.ResizeMethod.BILINEAR, image_ops.ResizeMethod.NEAREST_NEIGHBOR, image_ops.ResizeMethod.AREA]: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) resized = y.eval() expected = np.array(expected_data[opt]).reshape( [1, target_height, target_width, 1]) self.assertAllClose(resized, expected, atol=1e-05)
def testNoOp(self): img_shape = [1, 6, 4, 1] data = [128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128, 50, 50, 100, 100, 50, 50, 100, 100] img_np = np.array(data, dtype=np.uint8).reshape(img_shape) target_height = 6 target_width = 4 for opt in self.OPTIONS: with self.test_session(): image = constant_op.constant(img_np, shape=img_shape) y = image_ops.resize_images(image, target_height, target_width, opt) resized = y.eval() self.assertAllClose(resized, img_np, atol=1e-5)