def testInvertOp(self): dtype_list = [ dtypes.int8, dtypes.int16, dtypes.int32, dtypes.int64, dtypes.uint8, dtypes.uint16, dtypes.uint32, dtypes.uint64 ] inputs = [0, 5, 3, 14] with self.test_session(use_gpu=True) as sess: for dtype in dtype_list: # Because of issues with negative numbers, let's test this indirectly. # 1. invert(a) and a = 0 # 2. invert(a) or a = invert(0) input_tensor = constant_op.constant(inputs, dtype=dtype) not_a_and_a, not_a_or_a, not_0 = sess.run([ bitwise_ops.bitwise_and(input_tensor, bitwise_ops.invert(input_tensor)), bitwise_ops.bitwise_or(input_tensor, bitwise_ops.invert(input_tensor)), bitwise_ops.invert(constant_op.constant(0, dtype=dtype)) ]) self.assertAllEqual(not_a_and_a, [0, 0, 0, 0]) self.assertAllEqual(not_a_or_a, [not_0] * 4) # For unsigned dtypes let's also check the result directly. if dtype.is_unsigned: inverted = sess.run(bitwise_ops.invert(input_tensor)) expected = [dtype.max - x for x in inputs] self.assertAllEqual(inverted, expected)
def testDispatchForUnaryElementwiseAPIs(self): @dispatch.dispatch_for_unary_elementwise_apis(MaskedTensor) def unary_elementwise_api_handler(api_func, x): return MaskedTensor(api_func(x.values), x.mask) try: x = MaskedTensor([1, -2, -3], [True, True, False]) # Test calls with positional & keyword argument (& combinations) abs_x = math_ops.abs(x) sign_x = math_ops.sign(x=x) neg_x = math_ops.negative(x, "neg_x") invert_x = bitwise_ops.invert(x, name="invert_x") ones_like_x = array_ops.ones_like(x, name="ones_like_x") ones_like_x_float = array_ops.ones_like( x, dtypes.float32, name="ones_like_x_float") self.assertAllEqual(abs_x.values, [1, 2, 3]) self.assertAllEqual(sign_x.values, [1, -1, -1]) self.assertAllEqual(neg_x.values, [-1, 2, 3]) self.assertAllEqual(invert_x.values, [-2, 1, 2]) self.assertAllEqual(ones_like_x.values, [1, 1, 1]) self.assertAllEqual(ones_like_x_float.values, [1., 1., 1.]) for result in [ abs_x, sign_x, neg_x, invert_x, ones_like_x, ones_like_x_float ]: self.assertAllEqual(result.mask, [True, True, False]) if not context.executing_eagerly(): # names not defined in eager mode. self.assertRegex(neg_x.values.name, r"^neg_x/Neg:.*") self.assertRegex(invert_x.values.name, r"^invert_x/.*") self.assertRegex(ones_like_x.values.name, r"^ones_like_x/.*") self.assertRegex(ones_like_x_float.values.name, r"^ones_like_x_float/.*") finally: dispatch.unregister_elementwise_api_handler(unary_elementwise_api_handler)
def testInvertOp(self): dtype_list = [dtypes.int8, dtypes.int16, dtypes.int32, dtypes.int64, dtypes.uint8, dtypes.uint16] inputs = [0, 5, 3, 14] with self.test_session(use_gpu=True) as sess: for dtype in dtype_list: # Because of issues with negative numbers, let's test this indirectly. # 1. invert(a) and a = 0 # 2. invert(a) or a = invert(0) input_tensor = constant_op.constant(inputs, dtype=dtype) not_a_and_a, not_a_or_a, not_0 = sess.run( [bitwise_ops.bitwise_and( input_tensor, bitwise_ops.invert(input_tensor)), bitwise_ops.bitwise_or( input_tensor, bitwise_ops.invert(input_tensor)), bitwise_ops.invert(constant_op.constant(0, dtype=dtype))]) self.assertAllEqual(not_a_and_a, [0, 0, 0, 0]) self.assertAllEqual(not_a_or_a, [not_0] * 4) # For unsigned dtypes let's also check the result directly. if dtype.is_unsigned: inverted = sess.run(bitwise_ops.invert(input_tensor)) expected = [dtype.max - x for x in inputs] self.assertAllEqual(inverted, expected)
def process_chinese_path(image_path, image_name): # Convert the dataset as: # (path, filename) --> (image, label [str], input_len, label_len), 0 # input_len is always img_width // reduction_factor, should be changed depending on the model. # The last 0 is there only for compatibility w.r.t. .fit(). It is ignored afterwards. # Load the image and resize img = tf.io.read_file("." + os.sep + image_path + os.sep + image_name) img = tf.image.decode_jpeg(img, channels=3) img = tf.image.resize(img, [img_height, img_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) img = tf.dtypes.cast(img, tf.int32) img = bitwise_ops.invert(img) # chinese plates bitwise flip img = tf.cast(img[:, :, 0], tf.float32) / 255.0 img = img[:, :, tf.newaxis] # Get the label and its length label = tf.strings.split(image_name, '.jpg')[0] label = tf.strings.split(label, '_')[0] label = tf.strings.split(label, ' ')[0] label = tf.strings.upper(label) label_len = tf.strings.length(label) return (img, tf.strings.bytes_split(label), img_width // reduction_factor - 2, label_len), tf.zeros(1)
def f(x): if x.dtype == dtypes.bool: return math_ops.logical_not(x) return bitwise_ops.invert(x)
def bitwise_xnor(a, b): # Need to do some dim expanding to handle batches. a = tf.expand_dims(a, axis=1) b = tf.expand_dims(b, axis=0) ab = bitwise_ops.invert(bitwise_ops.bitwise_xor(a, b)) return ab