本文整理汇总了Python中tensorflow.compat.v2.uint8方法的典型用法代码示例。如果您正苦于以下问题:Python v2.uint8方法的具体用法?Python v2.uint8怎么用?Python v2.uint8使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v2
的用法示例。
在下文中一共展示了v2.uint8方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _info
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _info(self):
return tfds.core.DatasetInfo(
builder=self,
description=_DESCRIPTION,
features=tfds.features.FeaturesDict(
{
"images": {
"clean": tfds.features.Image(
shape=[224, 224, 3], dtype=tf.uint8, encoding_format="png"
),
"adversarial_univperturbation": tfds.features.Image(
shape=[224, 224, 3], dtype=tf.uint8, encoding_format="png"
),
"adversarial_univpatch": tfds.features.Image(
shape=[224, 224, 3], dtype=tf.uint8, encoding_format="png"
),
},
"label": tfds.features.ClassLabel(names=_LABELS),
"imagename": tfds.features.Text(),
}
),
supervised_keys=("images", "label"),
homepage=_URL,
citation=_CITATION,
)
开发者ID:twosixlabs,项目名称:armory,代码行数:27,代码来源:resisc45_densenet121_univpatch_and_univperturbation_adversarial_224x224.py
示例2: _info
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _info(self):
return tfds.core.DatasetInfo(
builder=self,
description=_DESCRIPTION,
features=tfds.features.FeaturesDict(
{
"images": {
"clean": tfds.features.Tensor(
shape=[224, 224, 3], dtype=tf.uint8
),
"adversarial": tfds.features.Tensor(
shape=[224, 224, 3], dtype=tf.uint8
),
},
"label": tfds.features.Tensor(shape=(), dtype=tf.int64),
}
),
supervised_keys=("images", "label"),
)
示例3: _info
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _info(self):
features = {
'image':
tfds.features.Image(encoding_format='jpeg'),
'image/filename':
tfds.features.Text(),
'faces':
tfds.features.Sequence({
'bbox': tfds.features.BBoxFeature(),
'blur': tf.uint8,
'expression': tf.bool,
'illumination': tf.bool,
'occlusion': tf.uint8,
'pose': tf.bool,
'invalid': tf.bool,
}),
}
return tfds.core.DatasetInfo(
builder=self,
description=_DESCRIPTION,
features=tfds.features.FeaturesDict(features),
homepage=_PROJECT_URL,
citation=_CITATION,
)
示例4: _run_inference_on_images
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _run_inference_on_images(self, image):
"""Cast image to float and run inference.
Args:
image: uint8 Tensor of shape [1, None, None, 3]
Returns:
Tensor dictionary holding detections.
"""
label_id_offset = 1
image = tf.cast(image, tf.float32)
image, shapes = self._model.preprocess(image)
prediction_dict = self._model.predict(image, shapes)
detections = self._model.postprocess(prediction_dict, shapes)
classes_field = fields.DetectionResultFields.detection_classes
detections[classes_field] = (
tf.cast(detections[classes_field], tf.float32) + label_id_offset)
for key, val in detections.items():
detections[key] = tf.cast(val, tf.float32)
return detections
示例5: _generate_examples
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _generate_examples(self, path):
"""Yields examples."""
clean_key = "clean"
adversarial_key = "adversarial"
def _parse(serialized_example):
ds_features = {
"height": tf.io.FixedLenFeature([], tf.int64),
"width": tf.io.FixedLenFeature([], tf.int64),
"label": tf.io.FixedLenFeature([], tf.int64),
"adv-image": tf.io.FixedLenFeature([], tf.string),
"clean-image": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(serialized_example, ds_features)
img_clean = tf.io.decode_raw(example["clean-image"], tf.float32)
img_adv = tf.io.decode_raw(example["adv-image"], tf.float32)
# float values are integers in [0.0, 255.0] for clean and adversarial
img_clean = tf.cast(img_clean, tf.uint8)
img_clean = tf.reshape(img_clean, (example["height"], example["width"], 3))
img_adv = tf.cast(img_adv, tf.uint8)
img_adv = tf.reshape(img_adv, (example["height"], example["width"], 3))
return {clean_key: img_clean, adversarial_key: img_adv}, example["label"]
ds = tf.data.TFRecordDataset(filenames=[path])
ds = ds.map(lambda x: _parse(x))
default_graph = tf.compat.v1.keras.backend.get_session().graph
ds = tfds.as_numpy(ds, graph=default_graph)
for i, (img, label) in enumerate(ds):
yield str(i), {
"images": img,
"label": label,
}
示例6: _canonicalize_jit_arg
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _canonicalize_jit_arg(x):
if isinstance(x, tf_np.ndarray):
return x.data
else:
try:
# We need to convert `int` to the most precise dtype, otherwise the dtype
# of the result may be different from numpy's. For example, when a binary
# op takes in a Python integer 5 and an array of uint32, numpy will pick
# uint32 as 5's dtype, while tf.convert_to_tensor will choose int32 which
# will cause the two arguments to be promoted to int64. We pick uint8
# here, which will be promoted to uint32 by the binary op.
# Note that we prefer unsigned int to signed int when both are equally
# precise. For example, for 5, we pick uint8 instead of int8. There is no
# reason to prefer one to the other, because for each there is a case
# where the behavior diverges from numpy. If we prefer signed int,
# consider the case where the first operand is 5 and the second is
# 2**64-1. Numpy picks uint64 as the result dtype, but because we choose a
# signed type for 5 such as int8, the result type will be float64. On the
# other hand, if we prefer unsigned int, consider the case where the first
# operand is 2**31-1 and the second is -1. Numpy will pick int32, but
# because we choose uint32 for 2*32-1, the result will be int64. The root
# of the problem is that `jit` converts `int` to tensors (hence committing
# to a dtype) too early, when we don't have enough information about the
# jitted function (e.g. which subset of the arguments should be promoted
# together using np.result_type). tf.function doesn't have this problem
# because it doesn't convert `int` to tensors. jax.jit doesn't have this
# problem because it converts `int` to "int tracer" which doesn't commit
# to a dtype.
# TODO(wangpeng): Revisit this design and see whether we can improve `jit`
# and tf.function.
dtype = most_precise_int_dtype(x)
if dtype is None and isinstance(x, float):
dtype = tf_np.default_float_type()
return tf.convert_to_tensor(value=x, dtype=dtype)
except (TypeError, ValueError):
return x
示例7: _convert_uint8_to_bfloat16
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def _convert_uint8_to_bfloat16(ts: Any):
"""Casts uint8 to bfloat16 if input is uint8.
Args:
ts: any tensor or nested tensor structure, such as EnvOutput.
Returns:
Converted structure.
"""
return tf.nest.map_structure(
lambda t: tf.cast(t, tf.bfloat16) if t.dtype == tf.uint8 else t, ts)
示例8: fake_image_dataset
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def fake_image_dataset(*args, **kwargs):
num_examples = 30
return tf.data.Dataset.from_generator(
lambda: ({
"image": np.ones(shape=(32, 32, 3), dtype=np.uint8),
"label": i % 10,
} for i in range(num_examples)),
output_types={"image": tf.uint8, "label": tf.int64},
output_shapes={"image": (32, 32, 3), "label": ()},
)
示例9: get_dummy_input
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def get_dummy_input(self, input_type):
"""Get dummy input for the given input type."""
if input_type == 'image_tensor':
return np.zeros(shape=(1, 20, 20, 3), dtype=np.uint8)
if input_type == 'float_image_tensor':
return np.zeros(shape=(1, 20, 20, 3), dtype=np.float32)
elif input_type == 'encoded_image_string_tensor':
image = Image.new('RGB', (20, 20))
byte_io = io.BytesIO()
image.save(byte_io, 'PNG')
return [byte_io.getvalue()]
elif input_type == 'tf_example':
image_tensor = tf.zeros((20, 20, 3), dtype=tf.uint8)
encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).numpy()
example = tf.train.Example(
features=tf.train.Features(
feature={
'image/encoded':
dataset_util.bytes_feature(encoded_jpeg),
'image/format':
dataset_util.bytes_feature(six.b('jpeg')),
'image/source_id':
dataset_util.bytes_feature(six.b('image_id')),
})).SerializeToString()
return [example]
示例10: __call__
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import uint8 [as 别名]
def __call__(self, input_tensor):
with tf.device('cpu:0'):
image = tf.map_fn(
_decode_image,
elems=input_tensor,
dtype=tf.uint8,
parallel_iterations=32,
back_prop=False)
return self._run_inference_on_images(image)