Python tensorflow.int8() Examples

The following are 30 code examples of tensorflow.int8(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: test_forward.py    From incubator-tvm with Apache License 2.0 6 votes vote down vote up
def test_tensor_array_split():
    def run(dtype_str, infer_shape):
        with tf.Graph().as_default():
            dtype =  tf_dtypes[dtype_str]
            t = tf.constant(np.array([[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0]]).astype(dtype_str), dtype=dtype)
            split_length = tf.constant([2, 2, 2, 2], dtype=tf.int32)
            ta1 = tf.TensorArray(dtype=dtype, size=4, infer_shape=infer_shape)
            ta2 = ta1.split(t, split_length)
            out0 = ta2.read(0)
            out1 = ta2.read(1)
            out2 = ta2.read(2)
            out3 = ta2.read(3)
            g = tf.get_default_graph()
            compare_tf_with_tvm([], [], ['TensorArrayReadV3:0'], mode='debug')
            compare_tf_with_tvm([], [], ['TensorArrayReadV3_1:0'], mode='debug')
            compare_tf_with_tvm([], [], ['TensorArrayReadV3_2:0'], mode='debug')
            compare_tf_with_tvm([], [], ['TensorArrayReadV3_3:0'], mode='debug')
    for dtype in ["float32", "int8"]:
        run(dtype, False)
        run(dtype, True) 
Example #2
Source File: constant_op_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testOnesLike(self):
    for dtype in [tf.float32, tf.float64, tf.int32,
                  tf.uint8, tf.int16, tf.int8,
                  tf.complex64, tf.complex128, tf.int64]:
      numpy_dtype = dtype.as_numpy_dtype
      with self.test_session():
        # Creates a tensor of non-zero values with shape 2 x 3.
        d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype)
        # Constructs a tensor of zeros of the same dimensions and type as "d".
        z_var = tf.ones_like(d)
        # Test that the type is correct
        self.assertEqual(z_var.dtype, dtype)
        z_value = z_var.eval()

      # Test that the value is correct
      self.assertTrue(np.array_equal(z_value, np.array([[1] * 3] * 2)))
      self.assertEqual([2, 3], z_var.get_shape()) 
Example #3
Source File: constant_op_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testDtype(self):
    with self.test_session():
      d = tf.fill([2, 3], 12., name="fill")
      self.assertEqual(d.get_shape(), [2, 3])
      # Test default type for both constant size and dynamic size
      z = tf.ones([2, 3])
      self.assertEqual(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      self.assertAllEqual(z.eval(), np.ones([2, 3]))
      z = tf.ones(tf.shape(d))
      self.assertEqual(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      self.assertAllEqual(z.eval(), np.ones([2, 3]))
      # Test explicit type control
      for dtype in (tf.float32, tf.float64, tf.int32,
                    tf.uint8, tf.int16, tf.int8,
                    tf.complex64, tf.complex128, tf.int64, tf.bool):
        z = tf.ones([2, 3], dtype=dtype)
        self.assertEqual(z.dtype, dtype)
        self.assertEqual([2, 3], z.get_shape())
        self.assertAllEqual(z.eval(), np.ones([2, 3]))
        z = tf.ones(tf.shape(d), dtype=dtype)
        self.assertEqual(z.dtype, dtype)
        self.assertEqual([2, 3], z.get_shape())
        self.assertAllEqual(z.eval(), np.ones([2, 3])) 
Example #4
Source File: constant_op_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testDtype(self):
    with self.test_session():
      d = tf.fill([2, 3], 12., name="fill")
      self.assertEqual(d.get_shape(), [2, 3])
      # Test default type for both constant size and dynamic size
      z = tf.zeros([2, 3])
      self.assertEqual(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      self.assertAllEqual(z.eval(), np.zeros([2, 3]))
      z = tf.zeros(tf.shape(d))
      self.assertEqual(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      self.assertAllEqual(z.eval(), np.zeros([2, 3]))
      # Test explicit type control
      for dtype in [tf.float32, tf.float64, tf.int32,
                    tf.uint8, tf.int16, tf.int8,
                    tf.complex64, tf.complex128, tf.int64, tf.bool]:
        z = tf.zeros([2, 3], dtype=dtype)
        self.assertEqual(z.dtype, dtype)
        self.assertEqual([2, 3], z.get_shape())
        self.assertAllEqual(z.eval(), np.zeros([2, 3]))
        z = tf.zeros(tf.shape(d), dtype=dtype)
        self.assertEqual(z.dtype, dtype)
        self.assertEqual([2, 3], z.get_shape())
        self.assertAllEqual(z.eval(), np.zeros([2, 3])) 
Example #5
Source File: recommender.py    From openrec with Apache License 2.0 6 votes vote down vote up
def _input(self, dtype='float32', shape=None, name=None):
        
        """Define an input for the recommender.

        Parameters
        ----------
        dtype: str
            Data type: "float16", "float32", "float64", "int8", "int16", "int32", "int64", "bool", or "string".
        shape: list or tuple
            Input shape.
        name: str
            Name of the input.

        Returns
        -------
        Tensorflow placeholder
            Defined tensorflow placeholder.
        """
        if dtype not in self._str_to_dtype:
            raise ValueError
        else:
            return tf.placeholder(self._str_to_dtype[dtype], shape=shape, name=name) 
Example #6
Source File: tfrecord_test.py    From nobrainer with Apache License 2.0 6 votes vote down vote up
def test__dtype_to_bytes():
    np_tf_dt = [
        (np.uint8, tf.uint8, b"uint8"),
        (np.uint16, tf.uint16, b"uint16"),
        (np.uint32, tf.uint32, b"uint32"),
        (np.uint64, tf.uint64, b"uint64"),
        (np.int8, tf.int8, b"int8"),
        (np.int16, tf.int16, b"int16"),
        (np.int32, tf.int32, b"int32"),
        (np.int64, tf.int64, b"int64"),
        (np.float16, tf.float16, b"float16"),
        (np.float32, tf.float32, b"float32"),
        (np.float64, tf.float64, b"float64"),
    ]

    for npd, tfd, dt in np_tf_dt:
        npd = np.dtype(npd)
        assert tfrecord._dtype_to_bytes(npd) == dt
        assert tfrecord._dtype_to_bytes(tfd) == dt

    assert tfrecord._dtype_to_bytes("float32") == b"float32"
    assert tfrecord._dtype_to_bytes("foobar") == b"foobar" 
Example #7
Source File: tensorflow_backend.py    From KerasNeuralFingerprint with MIT License 6 votes vote down vote up
def _convert_string_dtype(dtype):
    if dtype == 'float16':
        return tf.float16
    if dtype == 'float32':
        return tf.float32
    elif dtype == 'float64':
        return tf.float64
    elif dtype == 'int16':
        return tf.int16
    elif dtype == 'int32':
        return tf.int32
    elif dtype == 'int64':
        return tf.int64
    elif dtype == 'uint8':
        return tf.int8
    elif dtype == 'uint16':
        return tf.uint16
    else:
        raise ValueError('Unsupported dtype:', dtype) 
Example #8
Source File: test_node.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
def test_quantize_linear(self):
    node_def = helper.make_node("QuantizeLinear",
                                ["x", "y_scale", "y_zero_point"], ["y"])
    for x in [
        self._get_rnd_float32(-512., 512., [2, 6]),
        self._get_rnd_int(-512, 512, [2, 6])
    ]:
      y_scale = self._get_rnd_float32(-10., 10.)
      for y_zero_point in [
          self._get_rnd_int(-128, 127, dtype=np.int8),
          self._get_rnd_int(0, 255, dtype=np.uint8)
      ]:
        y = np.divide(x, y_scale)
        y = np.round(y)
        y = np.add(y, y_zero_point)
        if y_zero_point.dtype.type is np.int8:
          y = np.clip(y, -128, 127).astype(np.int8)
        else:
          y = np.clip(y, 0, 255).astype(np.uint8)
        output = run_node(node_def, [x, y_scale, y_zero_point])
        np.testing.assert_almost_equal(output["y"], y) 
Example #9
Source File: test_node.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
def test_max_pool_2d_dilations_ceil_pads_int8(self):
    if legacy_opset_pre_ver(12):
      raise unittest.SkipTest(
          "ONNX version {} does not support int8 input type.".format(
              defs.onnx_opset_version()))

    kernel_shape = [3, 3]
    strides = [2, 2]
    dilations = [3, 3]
    pads = [1, 1, 2, 2]
    ceil_mode = 1

    input_shape = [10, 3, 23, 23]
    self._test_pooling(input_shape=input_shape,
                       kernel_shape=kernel_shape,
                       strides=strides,
                       dilations=dilations,
                       pads=pads,
                       ceil_mode=ceil_mode,
                       input_dtype=np.int8) 
Example #10
Source File: test_node.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
def test_dequantize_linear(self):
    node_def = helper.make_node("DequantizeLinear",
                                ["x", "x_scale", "x_zero_point"], ["y"])
    for x, x_zero_point in [[
        self._get_rnd_int(-128, 127, [2, 6], np.int8),
        self._get_rnd_int(-128, 127, dtype=np.int8)
    ],
                            [
                                self._get_rnd_int(0, 255, [2, 6], np.uint8),
                                self._get_rnd_int(0, 255, dtype=np.uint8)
                            ],
                            [self._get_rnd_int(-512, 512, [2, 6]),
                             np.int32(0)]]:
      x_scale = self._get_rnd_float32(-10., 10)
      y = np.subtract(np.float32(x), np.float32(x_zero_point))
      y = np.multiply(y, x_scale)
      output = run_node(node_def, [x, x_scale, x_zero_point])
      np.testing.assert_almost_equal(output["y"], y) 
Example #11
Source File: tensorflow_util.py    From MedicalDataAugmentationTool with GNU General Public License v3.0 6 votes vote down vote up
def reduce_mean_support_empty(input, keepdims=False):
    return tf.cond(tf.size(input) > 0, lambda: tf.reduce_mean(input, keepdims=keepdims), lambda: tf.zeros_like(input))


# def bit_tensor_list(input):
#     assert input.dtype in [tf.uint8, tf.uint16, tf.uint32, tf.uint64], 'unsupported data type, must be uint*'
#     num_bits = 0
#     if input.dtype == tf.int8:
#         num_bits = 8
#     elif input.dtype == tf.int16:
#         num_bits = 16
#     elif input.dtype == tf.uint32:
#         num_bits = 32
#     elif input.dtype == tf.uint64:
#         num_bits = 64
#     bit_tensors = []
#     for i in range(num_bits):
#         current_bit = 1 << i
#         current_bit_tensor = tf.bitwise.bitwise_and(input, current_bit) == 1
#         bit_tensors.append(current_bit_tensor)
#     print(bit_tensors)
#     return bit_tensors 
Example #12
Source File: as_string_op_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testLargeInt(self):
    # Cannot use values outside -128..127 for test, because we're also
    # testing int8
    s = lambda strs: [x.decode("ascii") for x in strs]

    with self.test_session():
      input_ = tf.placeholder(tf.int32)
      int_inputs_ = [np.iinfo(np.int32).min, np.iinfo(np.int32).max]
      output = tf.as_string(input_)
      result = output.eval(feed_dict={input_: int_inputs_})
      self.assertAllEqual(s(result), ["%d" % x for x in int_inputs_])

      input_ = tf.placeholder(tf.int64)
      int_inputs_ = [np.iinfo(np.int64).min, np.iinfo(np.int64).max]
      output = tf.as_string(input_)
      result = output.eval(feed_dict={input_: int_inputs_})
      self.assertAllEqual(s(result), ["%d" % x for x in int_inputs_]) 
Example #13
Source File: tensor_util_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testIntTypes(self):
    for dtype, nptype in [
        (tf.int32, np.int32),
        (tf.uint8, np.uint8),
        (tf.uint16, np.uint16),
        (tf.int16, np.int16),
        (tf.int8, np.int8)]:
      # Test with array.
      t = tensor_util.make_tensor_proto([10, 20, 30], dtype=dtype)
      self.assertEquals(dtype, t.dtype)
      self.assertProtoEquals("dim { size: 3 }", t.tensor_shape)
      a = tensor_util.MakeNdarray(t)
      self.assertEquals(nptype, a.dtype)
      self.assertAllClose(np.array([10, 20, 30], dtype=nptype), a)
      # Test with ndarray.
      t = tensor_util.make_tensor_proto(np.array([10, 20, 30], dtype=nptype))
      self.assertEquals(dtype, t.dtype)
      self.assertProtoEquals("dim { size: 3 }", t.tensor_shape)
      a = tensor_util.MakeNdarray(t)
      self.assertEquals(nptype, a.dtype)
      self.assertAllClose(np.array([10, 20, 30], dtype=nptype), a) 
Example #14
Source File: dtypes_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testNumpyConversion(self):
    self.assertIs(tf.float32, tf.as_dtype(np.float32))
    self.assertIs(tf.float64, tf.as_dtype(np.float64))
    self.assertIs(tf.int32, tf.as_dtype(np.int32))
    self.assertIs(tf.int64, tf.as_dtype(np.int64))
    self.assertIs(tf.uint8, tf.as_dtype(np.uint8))
    self.assertIs(tf.uint16, tf.as_dtype(np.uint16))
    self.assertIs(tf.int16, tf.as_dtype(np.int16))
    self.assertIs(tf.int8, tf.as_dtype(np.int8))
    self.assertIs(tf.complex64, tf.as_dtype(np.complex64))
    self.assertIs(tf.complex128, tf.as_dtype(np.complex128))
    self.assertIs(tf.string, tf.as_dtype(np.object))
    self.assertIs(tf.string, tf.as_dtype(np.array(["foo", "bar"]).dtype))
    self.assertIs(tf.bool, tf.as_dtype(np.bool))
    with self.assertRaises(TypeError):
      tf.as_dtype(np.dtype([("f1", np.uint), ("f2", np.int32)])) 
Example #15
Source File: tf_utils.py    From deepsignal with GNU General Public License v3.0 6 votes vote down vote up
def parse_a_line_b(value, base_num, signal_num):
    vec = tf.decode_raw(value, tf.int8)

    bases = tf.cast(tf.reshape(tf.strided_slice(vec, [0], [base_num]), [base_num]), dtype=tf.int32)
    means = tf.bitcast(
        tf.reshape(tf.strided_slice(vec, [base_num], [base_num + base_num * 4]), [base_num, 4]),
        type=tf.float32)
    stds = tf.bitcast(
        tf.reshape(tf.strided_slice(vec, [base_num * 5], [base_num * 5 + base_num * 4]), [base_num, 4]),
        type=tf.float32)
    sanum = tf.cast(tf.bitcast(
        tf.reshape(tf.strided_slice(vec, [base_num * 9], [base_num * 9 + base_num * 2]), [base_num, 2]),
        type=tf.int16), dtype=tf.int32)
    signals = tf.bitcast(
        tf.reshape(tf.strided_slice(vec, [base_num * 11], [base_num * 11 + 4 * signal_num]),
                   [signal_num, 4]), type=tf.float32)
    labels = tf.cast(
        tf.reshape(tf.strided_slice(vec, [base_num * 11 + signal_num * 4], [base_num * 11 + signal_num * 4 + 1]),
                   [1]),
        dtype=tf.int32)

    return bases, means, stds, sanum, signals, labels 
Example #16
Source File: test_forward.py    From incubator-tvm with Apache License 2.0 6 votes vote down vote up
def test_tensor_array_scatter():
    def run(dtype_str, infer_shape):
        with tf.Graph().as_default():
            dtype =  tf_dtypes[dtype_str]
            if infer_shape:
                element_shape = tf.TensorShape([tf.Dimension(None)])
            else:
                element_shape = None
            t = tf.constant(np.array([[1.0], [2.0], [3.0]]).astype(dtype_str), dtype=dtype)
            indices = tf.constant([2, 1, 0])
            ta1 = tf.TensorArray(dtype=dtype, size=3,
                                 infer_shape=infer_shape,
                                 element_shape=element_shape)
            ta2 = ta1.scatter(indices, t)
            out0 = ta2.read(0)
            out1 = ta2.read(1)
            out2 = ta2.read(2)
            g = tf.get_default_graph()
            compare_tf_with_tvm([], [], ['TensorArrayReadV3:0'], mode='vm')
            compare_tf_with_tvm([], [], ['TensorArrayReadV3_1:0'], mode='vm')
            compare_tf_with_tvm([], [], ['TensorArrayReadV3_2:0'], mode='vm')
    for dtype in ["float32", "int8"]:
        run(dtype, False)
        run(dtype, True) 
Example #17
Source File: test_forward.py    From incubator-tvm with Apache License 2.0 6 votes vote down vote up
def test_tensor_array_write_read():
    def run(dtype_str, infer_shape, element_shape):
        with tf.Graph().as_default():
            dtype = tf_dtypes[dtype_str]
            np_data = np.array([[1.0, 2.0], [3.0, 4.0]]).astype(dtype_str)
            in_data = [np_data, np_data]
            t1 = tf.constant(np_data, dtype=dtype)
            t2 = tf.constant(np_data, dtype=dtype)
            ta1 = tf.TensorArray(dtype=dtype, size=2, infer_shape=infer_shape,
                                 element_shape=element_shape)
            ta2 = ta1.write(0, t1)
            ta3 = ta2.write(1, t2)
            out = ta3.read(0)
            g = tf.get_default_graph()
            compare_tf_with_tvm([], [], 'TensorArrayReadV3:0', mode='vm')

    for dtype in ["float32", "int8"]:
        run(dtype, False, None)
        run(dtype, False, tf.TensorShape([None, 2]))
        run(dtype, True, None) 
Example #18
Source File: sharpmask.py    From sharpmask with Apache License 2.0 5 votes vote down vote up
def transform_ds(x):
    keys_to_features = {'score': tf.FixedLenFeature([], tf.int64),
                        'mask': tf.FixedLenFeature([], tf.string),
                        'image': tf.FixedLenFeature([], tf.string)}
    parsed_features = tf.parse_single_example(x, keys_to_features)
    image = transform_image(parsed_features['image'])
    masks = tf.reshape(tf.decode_raw(parsed_features['mask'], tf.int8), shape=[224, 224])
    return {'score': tf.cast(parsed_features['score'], tf.float32), 'mask': masks, 'image': image} 
Example #19
Source File: main.py    From Implementation-CVPR2015-CNN-for-ReID with MIT License 5 votes vote down vote up
def train_input_fn():
    dataset = tf.data.Dataset.from_generator(
                     get_data_generator(),
                     ((tf.float32, tf.float32), tf.int8),
                     ((tf.TensorShape([*cfg.DATA.ARRAY_SIZE, 3]),
                       tf.TensorShape([*cfg.DATA.ARRAY_SIZE, 3])),
                      tf.TensorShape(None))
                    )

    dataset = dataset.batch(batch_size=cfg.TRAIN.BATCHSIZE)
    dataset = dataset.prefetch(buffer_size=cfg.TRAIN.BATCHSIZE)

    return dataset 
Example #20
Source File: object_detection_tf_multiprocessing.py    From object_detection_with_tensorflow with MIT License 5 votes vote down vote up
def detect_object(detection_graph, sess, image, category_index):
    with detection_graph.as_default():
        with sess.as_default() as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')
            #   image = Image.open(image_path)
              # the array based representation of the image will be used later in order to prepare the
              # result image with boxes and labels on it.
            # image_np = load_image_into_numpy_array(image)
            image_np = image
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            test_var = tf.placeholder(dtype=tf.int8, shape=[])
            # Actual detection.
            (boxes, scores, classes, num) = sess.run(
              [detection_boxes, detection_scores, detection_classes, num_detections],
              feed_dict={image_tensor: image_np_expanded})
            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
              image_np,
              np.squeeze(boxes),
              np.squeeze(classes).astype(np.int32),
              np.squeeze(scores),
              category_index,
              use_normalized_coordinates=True,
              line_thickness=8,
              min_score_thresh = 0.7)
            return image_np 
Example #21
Source File: batch_generator.py    From 3d-semantic-segmentation with MIT License 5 votes vote down vote up
def _wrapped_generate_train_blob(self, index):
        return tf.py_func(func=self._generate_blob,
                          #     pos_id,  train, aug_trans
                          inp=[index, True, self._augmentation],
                          #       data        labels   mask
                          Tout=(tf.float32, tf.int8, tf.int8, tf.int32, tf.int64),

                          name='generate_train_blob') 
Example #22
Source File: dtypes_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testIsComplex(self):
    self.assertEqual(tf.as_dtype("int8").is_complex, False)
    self.assertEqual(tf.as_dtype("int16").is_complex, False)
    self.assertEqual(tf.as_dtype("int32").is_complex, False)
    self.assertEqual(tf.as_dtype("int64").is_complex, False)
    self.assertEqual(tf.as_dtype("uint8").is_complex, False)
    self.assertEqual(tf.as_dtype("uint16").is_complex, False)
    self.assertEqual(tf.as_dtype("complex64").is_complex, True)
    self.assertEqual(tf.as_dtype("complex128").is_complex, True)
    self.assertEqual(tf.as_dtype("float32").is_complex, False)
    self.assertEqual(tf.as_dtype("float64").is_complex, False)
    self.assertEqual(tf.as_dtype("string").is_complex, False)
    self.assertEqual(tf.as_dtype("bool").is_complex, False)
    self.assertEqual(tf.as_dtype("bfloat16").is_integer, False) 
Example #23
Source File: dtypes_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testStringConversion(self):
    self.assertIs(tf.float32, tf.as_dtype("float32"))
    self.assertIs(tf.float64, tf.as_dtype("float64"))
    self.assertIs(tf.int32, tf.as_dtype("int32"))
    self.assertIs(tf.uint8, tf.as_dtype("uint8"))
    self.assertIs(tf.uint16, tf.as_dtype("uint16"))
    self.assertIs(tf.int16, tf.as_dtype("int16"))
    self.assertIs(tf.int8, tf.as_dtype("int8"))
    self.assertIs(tf.string, tf.as_dtype("string"))
    self.assertIs(tf.complex64, tf.as_dtype("complex64"))
    self.assertIs(tf.complex128, tf.as_dtype("complex128"))
    self.assertIs(tf.int64, tf.as_dtype("int64"))
    self.assertIs(tf.bool, tf.as_dtype("bool"))
    self.assertIs(tf.qint8, tf.as_dtype("qint8"))
    self.assertIs(tf.quint8, tf.as_dtype("quint8"))
    self.assertIs(tf.qint32, tf.as_dtype("qint32"))
    self.assertIs(tf.bfloat16, tf.as_dtype("bfloat16"))
    self.assertIs(tf.float32_ref, tf.as_dtype("float32_ref"))
    self.assertIs(tf.float64_ref, tf.as_dtype("float64_ref"))
    self.assertIs(tf.int32_ref, tf.as_dtype("int32_ref"))
    self.assertIs(tf.uint8_ref, tf.as_dtype("uint8_ref"))
    self.assertIs(tf.int16_ref, tf.as_dtype("int16_ref"))
    self.assertIs(tf.int8_ref, tf.as_dtype("int8_ref"))
    self.assertIs(tf.string_ref, tf.as_dtype("string_ref"))
    self.assertIs(tf.complex64_ref, tf.as_dtype("complex64_ref"))
    self.assertIs(tf.complex128_ref, tf.as_dtype("complex128_ref"))
    self.assertIs(tf.int64_ref, tf.as_dtype("int64_ref"))
    self.assertIs(tf.bool_ref, tf.as_dtype("bool_ref"))
    self.assertIs(tf.qint8_ref, tf.as_dtype("qint8_ref"))
    self.assertIs(tf.quint8_ref, tf.as_dtype("quint8_ref"))
    self.assertIs(tf.qint32_ref, tf.as_dtype("qint32_ref"))
    self.assertIs(tf.bfloat16_ref, tf.as_dtype("bfloat16_ref"))
    with self.assertRaises(TypeError):
      tf.as_dtype("not_a_type") 
Example #24
Source File: builtin_quantizers.py    From nni with MIT License 5 votes vote down vote up
def quantize_weight(self, weight, config, op_name, **kwargs):
        new_scale = tf.reduce_max(tf.abs(weight)) / 127
        scale = tf.maximum(self.layer_scale.get(op_name, tf.constant(0.0)), new_scale)
        self.layer_scale[op_name] = scale
        orig_type = weight.dtype
        return tf.cast(tf.cast(weight / scale, tf.int8), orig_type) * scale 
Example #25
Source File: data_pipeline.py    From ml-on-gcp with Apache License 2.0 5 votes vote down vote up
def _deserialize(self, serialized_data, batch_size):
    """Convert serialized TFRecords into tensors.

    Args:
      serialized_data: A tensor containing serialized records.
      batch_size: The data arrives pre-batched, so batch size is needed to
        deserialize the data.
    """
    feature_map = _TRAIN_FEATURE_MAP if self._is_training else _EVAL_FEATURE_MAP
    features = tf.parse_single_example(serialized_data, feature_map)

    users = tf.reshape(tf.decode_raw(
        features[movielens.USER_COLUMN], rconst.USER_DTYPE), (batch_size,))
    items = tf.reshape(tf.decode_raw(
        features[movielens.ITEM_COLUMN], rconst.ITEM_DTYPE), (batch_size,))

    def decode_binary(data_bytes):
      # tf.decode_raw does not support bool as a decode type. As a result it is
      # necessary to decode to int8 (7 of the bits will be ignored) and then
      # cast to bool.
      return tf.reshape(tf.cast(tf.decode_raw(data_bytes, tf.int8), tf.bool),
                        (batch_size,))

    if self._is_training:
      mask_start_index = tf.decode_raw(
          features[rconst.MASK_START_INDEX], tf.int32)[0]
      valid_point_mask = tf.less(tf.range(batch_size), mask_start_index)

      return {
          movielens.USER_COLUMN: users,
          movielens.ITEM_COLUMN: items,
          rconst.VALID_POINT_MASK: valid_point_mask,
      }, decode_binary(features["labels"])

    return {
        movielens.USER_COLUMN: users,
        movielens.ITEM_COLUMN: items,
        rconst.DUPLICATE_MASK: decode_binary(features[rconst.DUPLICATE_MASK]),
    } 
Example #26
Source File: _parser_tf.py    From keras-onnx with MIT License 5 votes vote down vote up
def infer_variable_type(tensor, opset, inbound_node_shape=None):
    tensor_shape = []
    if inbound_node_shape is None:
        if tensor.shape not in (tf.TensorShape(None), tf.TensorShape([])):
            if opset > 8:
                tensor_shape = normalize_tensor_shape(tensor.shape)
            else:  # most inference engine has problem with unset dim param if they released around opset 8 publish
                tensor_shape = ['None' if d is None else d for d in normalize_tensor_shape(tensor.shape)]
    else:
        tensor_shape = list(inbound_node_shape)

    # Determine the tensor's element type
    tensor_type = tensor.dtype.base_dtype
    if tensor.dtype == 'resource':
        node_attr = tensor.op.node_def.attr
        tensor_type = node_attr['dtype'].type
        tensor_shape = ['None' if d.size is None else d.size for d in node_attr['shape'].shape.dim]
    if tensor_type in [tf.int8, tf.int16, tf.int32]:
        return Int32TensorType(shape=tensor_shape)
    elif tensor_type == tf.int64:
        return Int64TensorType(shape=tensor_shape)
    elif tensor_type in [tf.float16, tf.float32]:
        return FloatTensorType(shape=tensor_shape)
    elif tensor_type == tf.float64:
        return DoubleTensorType(shape=tensor_shape)
    elif tensor_type == tf.bool:
        return BooleanTensorType(shape=tensor_shape)
    else:
        raise ValueError(
            "Unable to find out a correct type for tensor type = {} of {}".format(tensor_type, tensor.name)) 
Example #27
Source File: embeddings.py    From mead-baseline with Apache License 2.0 5 votes vote down vote up
def _mean_pool(inputs, embeddings):
    mask = tf.not_equal(inputs, 0)
    seq_lengths = tf.reduce_sum(tf.cast(mask, tf.int8), axis=1, keepdims=True)
    embeddings = tf.where(tf.expand_dims(mask, -1), embeddings, 0.)
    return tf.reduce_sum(embeddings, 1, False) / tf.cast(seq_lengths, embeddings.dtype) 
Example #28
Source File: dtypes_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testIsUnsigned(self):
    self.assertEqual(tf.as_dtype("int8").is_unsigned, False)
    self.assertEqual(tf.as_dtype("int16").is_unsigned, False)
    self.assertEqual(tf.as_dtype("int32").is_unsigned, False)
    self.assertEqual(tf.as_dtype("int64").is_unsigned, False)
    self.assertEqual(tf.as_dtype("uint8").is_unsigned, True)
    self.assertEqual(tf.as_dtype("uint16").is_unsigned, True)
    self.assertEqual(tf.as_dtype("float32").is_unsigned, False)
    self.assertEqual(tf.as_dtype("float64").is_unsigned, False)
    self.assertEqual(tf.as_dtype("bool").is_unsigned, False)
    self.assertEqual(tf.as_dtype("string").is_unsigned, False)
    self.assertEqual(tf.as_dtype("complex64").is_unsigned, False)
    self.assertEqual(tf.as_dtype("complex128").is_unsigned, False)
    self.assertEqual(tf.as_dtype("bfloat16").is_integer, False) 
Example #29
Source File: main.py    From Implementation-CVPR2015-CNN-for-ReID with MIT License 5 votes vote down vote up
def valid_input_fn():
    dataset = tf.data.Dataset.from_generator(
        get_data_generator(mode='valid', pattern=cfg.DATA.PATTERN.VALID),
        ((tf.float32, tf.float32), tf.int8),
        ((tf.TensorShape([*cfg.DATA.ARRAY_SIZE, 3]),
          tf.TensorShape([*cfg.DATA.ARRAY_SIZE, 3])),
         tf.TensorShape(None)))

    dataset = dataset.batch(200)
    dataset = dataset.prefetch(buffer_size=200)

    return dataset 
Example #30
Source File: data_feeder_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_input_int8(self):
    self._assert_dtype(
        np.int8, tf.int8, np.matrix([[1, 2], [3, 4]], dtype=np.int8))