'can not print the value of each tensor inside a transformation function for tf.dataset
I am learning the tensorflow which version is 2.8.0 on my MacBook M1. I am learning COCO dataset, and extracting the data from TFRecordDataset for watching data detail.So, I want to debug or print the data value in the open_image function.
class DatasetTransformer:
def __init__(self, config):
self.INCLUDE_MASK = config.INCLUDE_MASK
self.LABEL_HEIGHT = config.LABEL_HEIGHT
self.LABEL_WIDTH = config.LABEL_WIDTH
self.IMAGE_SIZE = config.IMAGE_SIZE
self.PAF_GAUSSIAN_SIGMA_SQ = config.PAF_GAUSSIAN_SIGMA_SQ
self.KPT_HEATMAP_GAUSSIAN_SIGMA_SQ = config.KPT_HEATMAP_GAUSSIAN_SIGMA_SQ
self.PAF_NUM_FILTERS = config.PAF_NUM_FILTERS
self.HEATMAP_NUM_FILTERS = config.HEATMAP_NUM_FILTERS
self.JOINTS_DEF = config.JOINTS_DEF
self.JOINTS_SIDES = config.JOINTS_SIDES
self.KEYPOINTS_SIDES = config.KEYPOINTS_SIDES
self.CONTRAST_RANGE = config.CONTRAST_RANGE
self.BRIGHTNESS_RANGE = config.BRIGHTNESS_RANGE
self.HUE_RANGE = config.HUE_RANGE
self.SATURATION_RANGE = config.SATURATION_RANGE
# for parsing TFrecords files
self.feature_description = {
'id' : tf.io.FixedLenFeature([1], tf.int64),
'image_raw': tf.io.FixedLenFeature([], tf.string),
'size' : tf.io.FixedLenFeature([2], tf.int64),
'kpts' : tf.io.FixedLenFeature([], tf.string),
'joints' : tf.io.FixedLenFeature([], tf.string),
'mask' : tf.io.FixedLenFeature([], tf.string)
}
def read_tfrecord(self, serialized_example):
parsed = tf.io.parse_single_example(serialized_example, self.feature_description)
idd = parsed['id']
image_raw = parsed['image_raw']
size = parsed['size']
kpts = tf.io.parse_tensor(parsed['kpts'], tf.float32)
joints = tf.io.parse_tensor(parsed['joints'], tf.float32)
mask = tf.io.parse_tensor(parsed['mask'], tf.float32)
mask = tf.ensure_shape(mask, ([self.LABEL_HEIGHT, self.LABEL_WIDTH]))
mask = tf.expand_dims(mask, axis=-1) # required to concat
kpts = tf.RaggedTensor.from_tensor(kpts)
joints = tf.RaggedTensor.from_tensor(joints)
return {"id": idd, "image_raw": image_raw, "size": size, "kpts": kpts, "joints": joints, "mask": mask}
def open_image(self, elem):
image_raw = elem["image_raw"]
image = tf.image.decode_jpeg(image_raw, channels=3)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.image.resize(image, self.IMAGE_SIZE)
new_elem = {}
new_elem.update(elem)
new_elem.pop("image_raw")
new_elem["image"] = image
print("debug:", new_elem["image"])
return new_elem
def build_training_ds(tfrecord_filenames: list, config) -> tf.data.Dataset:
dataset_transformer = DatasetTransformer(config)
ds = tf.data.TFRecordDataset(tfrecord_filenames)
ds = ds.map(dataset_transformer.read_tfrecord, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds = ds.map(dataset_transformer.open_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
return ds
dst = build_training_ds(tfrecord_files_train, cfg)
According to the print sentence in the open_image function above, it just give the output as below:
debug: Tensor("resize/Squeeze:0", shape=(368, 368, 3), dtype=float32)
Is that an output about an autograph? I want to print every tensor value in each ds.map iteration for debugging. Is there any way to solve it?
update: I have checked other questions, and someone suggested using tf.py_function, but I have no idea which data type to specify.
Solution 1:[1]
I figured a way to do it was like this
function dot_click(e:InteractionEvent){
for( i in 0...current_dots.length){
if(current_dots[i] == e.currentTarget) {
trace("Clicked")
}
}
}
had to pass the interactive parameter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jeffrey pernia |
