我创建了一个tf.data.Dataset
,从list_files
开始,获取所有图片的路径。
{
"img1.png": {
data ...
},
"img2.png": ...
}
因此,key-value就是图像名称。
我可以很容易地从list_files
提供的路径中提取图像名称。但是,这是tf.string
,不能直接使用它(?)来访问注解中的值。
有没有简单的方法把tf.string
转换成python字符串,这样我就可以从json文件中读取groundtruth数据了?
或者将注解转换为正确的tf type
。
from typing import Mapping
from numpy import ndarray
import tensorflow as tf
import cv2 as cv
from pathlib import Path
from typing import Any, Mapping, NamedTuple
import json
class Point:
x: float
y: float
def __init__(self, x: float, y: float):
self.x = x
self.y = y
class BoundingBox(NamedTuple):
top: float
left: float
bottom: float
right: float
class Annotation:
image: tf.Tensor
bounding_box: tf.Tensor
is_visible: bool
def __init__(self, image, bounding_box, is_visible):
self.image = image
self.bounding_box = bounding_box
self.is_visible = is_visible
LABELS = {
"NO_CLUB": 0,
"CLUB": 1,
"bbox": BoundingBox,
}
def is_in_split(image_path: tf.string, is_training: bool) -> bool:
hash = tf.strings.to_hash_bucket_fast(image_path, 10)
if is_training:
return hash < 8
else:
return hash >= 8
def create_image_and_annotation(image_path: tf.string, annotation: Mapping[str, Any]):
bits = tf.io.read_file(image_path)
file_split = tf.strings.split(image_path, "/")
image_name = file_split[-1]
suffix = tf.strings.split(image_name, ".")[-1]
jpeg = [
tf.convert_to_tensor("jpg", dtype=tf.string),
tf.convert_to_tensor("JPG", dtype=tf.string),
tf.convert_to_tensor("jpeg", dtype=tf.string),
tf.convert_to_tensor("JPEG", dtype=tf.string),
]
is_jpeg = [tf.math.equal(suffix, s) for s in jpeg]
png = [
tf.convert_to_tensor("png", dtype=tf.string),
tf.convert_to_tensor("PNG", dtype=tf.string),
]
is_png = [tf.math.equal(suffix, s) for s in png]
if tf.math.reduce_any(is_jpeg):
image = tf.io.decode_jpeg(bits, channels=3)
else:
image = tf.io.decode_png(bits, channels=3)
# Here I want to use image_name to access the annotation for the specific image! <---
bounding_box = BoundingBox(0,0,10,10)
return image, (bounding_box, True)
def createDataset(dir: Path, annotation: Mapping[str, Any], is_training: bool) -> tf.data.Dataset:
image_path_png = str(dir / "images" / "*.png")
image_path_PNG = str(dir / "images" / "*.PNG")
image_path_jpg = str(dir / "images" / "*.jpg")
image_path_JPG = str(dir / "images" / "*.JPG")
image_path_jpeg = str(dir / "images" / "*.jpeg")
image_path_JPEG = str(dir / "images" / "*.JPEG")
image_dirs = [image_path_png, image_path_PNG, image_path_jpg, image_path_JPG, image_path_jpeg, image_path_JPEG]
dataset = (tf.data.Dataset.list_files(image_dirs)
.shuffle(1000)
.map(lambda x: create_image_and_annotation(x, annotation))
)
for d in dataset:
pass
return dataset
def getDataset(data_root_path: Path, is_training: bool) -> tf.data.Dataset:
dirs = [x for x in data_root_path.iterdir() if x.is_dir()]
datasets = []
for dir in dirs:
json_path = dir / "annotations.json"
with open(json_path) as json_file:
annotation = json.load(json_file)
createDataset(dir, annotation, is_training=is_training)
training_data = getDataset(Path("/home/erik/Datasets/ClubHeadDetection"), True)
1条答案
按热度按时间j5fpnvbx1#
当将要素名称从字符串转换为整型或向其中添加粗糙Tensor时,可以使用相位方法Map标注和数据。Train example
示例:与使用www.example.com函数的Map相同dataset.map,但它专门用于转换为int,或者您可以对〈data,label〉使用粗糙Tensor
输出:由样本和指标组成的数据集,指标告诉数据集数据的类别。它应该考虑与其他指标的不同,但如何确定?而不仅仅是模型或训练次数来确定。