def test_compare_preprocess_with_improved_gan(self, nrows, ncols): """Compares the image resizing function with that of openai/improved-gan.""" if tf.executing_eagerly(): # Eval is not supported when eager execution is enabled. return test_image = [] for j in range(nrows): test_image.append([[(i // 2 + j) % 256] * 3 for i in range(ncols)]) test_image = np.array(test_image, dtype=np.uint8) improved_image = _transform(test_image, npx=128, is_crop=True, resize_w=128) dummy_record = { 'image': tf.constant(test_image, dtype=tf.uint8), 'label': [4], } process_fn = data_provider._preprocess_dataset_record_fn(image_size=128) processed_record = process_fn(dummy_record) with self.session(): # There's a relatively large gap between the two results, mainly because # of PIL's sampling strategy. E.g., # """ # test_image = np.array([[0], [255]], dtype=np.uint8) # image_lib.fromarray(test_image).resize([2, 2], image_lib.BILINEAR) # """ # results with [[0, 0], [127, 127]] (instead of [[0, 0], [255, 255]]). self.assertLess( tf.norm(tensor=improved_image - processed_record[0], ord=np.inf).eval(), 4. / 256.)
def test_preprocess_dataset_record_shapes(self): dummy_record = { 'image': tf.zeros([123, 456, 3], dtype=tf.uint8), 'label': tf.constant([4]), } process_fn = data_provider._preprocess_dataset_record_fn(image_size=128) processed_record = process_fn(dummy_record) processed_record[0].shape.assert_is_compatible_with([128, 128, 3]) processed_record[1].shape.assert_is_compatible_with([1])
def test_preprocess_dataset_record_centering(self): """Checks that `_preprocess_dataset_record` correctly crops image.""" center_size = 4 padding_size = 5 dummy_record = { 'image': tf.concat([ tf.zeros([center_size, padding_size, 3], dtype=tf.uint8), 255 * tf.ones([center_size, center_size, 3], dtype=tf.uint8), tf.zeros([center_size, padding_size, 3], dtype=tf.uint8) ], axis=1), 'label': tf.constant([4]), } image_size = 7 process_fn = data_provider._preprocess_dataset_record_fn( image_size=image_size) processed_record = process_fn(dummy_record) processed_record[0].shape.assert_is_compatible_with( [image_size, image_size, 3]) # Test that output is all-ones (ignore the boundary in the check). self.assertAllEqual(processed_record[0][1:-1, 1:-1, :], tf.ones([image_size - 2, image_size - 2, 3]))