tensorflow 使用tf.striing访问www.example.com中的dict中的值tf.dataset.Dataset.map

l7wslrjt  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(180)

我创建了一个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)
j5fpnvbx

j5fpnvbx1#

当将要素名称从字符串转换为整型或向其中添加粗糙Tensor时,可以使用相位方法Map标注和数据。Train example
示例:与使用www.example.com函数的Map相同dataset.map,但它专门用于转换为int,或者您可以对〈data,label〉使用粗糙Tensor

import os
from os.path import exists

import tensorflow as tf
import tensorflow_io as tfio
from google.protobuf import json_format

import matplotlib.pyplot as plt

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""           
image = tf.io.read_file( "F:\\Pictures\\Kids\\images.jpg" )
image = tf.io.decode_jpeg(image)                            
image = tf.cast( image, dtype=tf.int64 )
                    
example = tf.train.Example(
    features=tf.train.Features(
        feature={
            "1": tf.train.Feature(
            
                int64_list=tf.train.Int64List(
                    value=tf.constant( image, shape=( 183 * 275 * 3 ) ).numpy() )
                    
                    ),
            "2": tf.train.Feature(
            
                int64_list=tf.train.Int64List(
                    value=tf.constant( image, shape=( 183 * 275 * 3 ) ).numpy() )
                    
                    )       
            }))

data_string = json_format.MessageToJson(example)
example_binary = tf.io.decode_json_example(data_string)

example_phase = tf.io.parse_example(
    serialized=[example_binary.numpy()],
    features = {    "1": tf.io.FixedLenFeature(shape=[ 183 * 275 * 3 ], dtype=tf.int64),
                    "2": tf.io.FixedLenFeature(shape=[ 183 * 275 * 3 ], dtype=tf.int64)
                })

data = list(example_phase.items())
label = [ int(x[0]) for x in data ]

data = [ x[1] for x in data ]
data = [ x[0] for x in data ]

dataset = tf.data.Dataset.from_tensors(( data, label ))

# for d in dataset:
    # print( d )

plt.imshow( tf.constant( example_phase['1'], shape=( 183, 275, 3) ) )
plt.show()

输出:由样本和指标组成的数据集,指标告诉数据集数据的类别。它应该考虑与其他指标的不同,但如何确定?而不仅仅是模型或训练次数来确定。

(<tf.Tensor: shape=(2, 150975), dtype=int64, numpy=
array([[227, 162, 132, ..., 227, 169, 129],
       [227, 162, 132, ..., 227, 169, 129]], dtype=int64)>, <tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2])>)

相关问题