Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 向TF图像文件发布地面真实性映射问题_Tensorflow_Image Processing - Fatal编程技术网

Tensorflow 向TF图像文件发布地面真实性映射问题

Tensorflow 向TF图像文件发布地面真实性映射问题,tensorflow,image-processing,Tensorflow,Image Processing,我遇到了一个问题,将我的基本事实作为标签映射到我的图像训练集。我的colab%cd/内容中的文件名中有基本事实。我试图在空白处拆分,并标记我的张量 /content/imageset1/Anger 1000.jpg /content/imageset1/Anger 1011.jpg /content/imageset1/Anger 1029.jpg .... /content/imageset1/dest 14608.jpg 我正在努力使用解码\u jpeg\u和\u标签功能 GCS_PATT

我遇到了一个问题,将我的基本事实作为标签映射到我的图像训练集。我的colab%cd/内容中的文件名中有基本事实。我试图在空白处拆分,并标记我的张量

/content/imageset1/Anger 1000.jpg
/content/imageset1/Anger 1011.jpg
/content/imageset1/Anger 1029.jpg
....
/content/imageset1/dest 14608.jpg

我正在努力使用解码\u jpeg\u和\u标签功能

GCS_PATTERN = '/content/imageset1/*.jpg'
nb_images = len(tf.io.gfile.glob(GCS_PATTERN)) 
GCS_OUTPUT = BUCKET + '/emotions'  # prefix for output file names
BATCHES = 16 # images are split into batches
shard_size = math.ceil(1.0 * nb_images / BATCHES)
TARGET_SIZE = [100, 100] # this is given in the readme, we will use this later
CLASSES = ['Surprise',
          'Fear',
          'Disgust',
          'Happiness',
          'Sadness',
          'Anger',
          'Neutral']
    # groundtruth labels

# functions for writing TFRecord entries
# Feature values are always stored as lists, a single data element will be a list of size 1
def _bytestring_feature(list_of_bytestrings):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=list_of_bytestrings))

def _int_feature(list_of_ints): # int64
  return tf.train.Feature(int64_list=tf.train.Int64List(value=list_of_ints))  

def to_tfrecord(tfrec_filewriter, img_bytes, label): #, height, width):  
  class_num = np.argmax(np.array(CLASSES)==label) # this comes from our emotion labels 1=surprise
  one_hot_class = np.eye(len(CLASSES))[class_num]     
  feature = {
      "image": _bytestring_feature([img_bytes]), # one image in the list
      "class": _int_feature([class_num]) #,        # one class in the list
  }
  return tf.train.Example(features=tf.train.Features(feature=feature))

def decode_jpeg_and_label(filepath):
    bits = tf.io.read_file(filepath)
    image = tf.image.decode_jpeg(bits)
    # parse emotion name from containing directory
    label = tf.strings.split(tf.expand_dims(filepath+'*/*.jpeg', axis=-1), sep=' ')
    label2 = label.values[-2]
    return image, label2

def resize_and_crop_image(image, label):
#Resizing to preserve aspect ratio
  w = tf.shape(image)[0]
  h = tf.shape(image)[1]
  tw = TARGET_SIZE[1]
  th = TARGET_SIZE[0]
  resize_crit = (w * th) / (h * tw)
  image = tf.cond(resize_crit < 1,
                  lambda: tf.image.resize(image, [w*tw/w, h*tw/w]), # if true
                  lambda: tf.image.resize(image, [w*th/h, h*th/h])  # if false
                 )
  nw = tf.shape(image)[0]
  nh = tf.shape(image)[1]
  image = tf.image.crop_to_bounding_box(image, (nw - tw) // 2, (nh - th) // 2, tw, th)
  return image, label

def recompress_image(image, label):
  height = tf.shape(image)[0]
  width = tf.shape(image)[1]
  image = tf.cast(image, tf.uint8)
  image = tf.image.encode_jpeg(image, optimize_size=True, chroma_downsampling=False)
  return image, label, height, width

print("Writing TFRecords")
tt0 = time.time()
filenames = tf.data.Dataset.list_files(GCS_PATTERN) 
dataset1 = filenames.map(decode_jpeg_and_label)
dataset2 = dataset1.map(resize_and_crop_image)  
dataset3 = dataset2.map(recompress_image)
dataset3 = dataset3.batch(shard_size) # sharding: there will be one "batch" of images per file 
for shard, (image, label, height, width) in enumerate(dataset3):
  # batch size used as shard size here
  shard_size = image.numpy().shape[0]
  # number of records in the filename
  filename = GCS_OUTPUT + "{:02d}-{}.tfrec".format(shard, shard_size)
   
  with tf.io.TFRecordWriter(filename) as out_file:
    for i in range(shard_size):
      example = to_tfrecord(out_file,
                            image.numpy()[i], # re-compressed image: already a byte string
                            label.numpy()[i] #, height.numpy()[i], width.numpy()[i]
                            )
      out_file.write(example.SerializeToString())
    print("Wrote file {} containing {} records".format(filename, shard_size))
print("Total time: "+str(time.time()-tt0))
GCS_PATTERN='/content/imageset1/*.jpg'
nb_images=len(tf.io.gfile.glob(GCS_模式))
GCS#u OUTPUT=BUCKET+'/emotions'#输出文件名的前缀
批次=16#图像被分为多个批次
shard_size=math.ceil(1.0*nb_图像/批)
TARGET_SIZE=[100100]#这在自述文件中给出,我们稍后将使用它
CLASSES=[“惊喜”,
“恐惧”,
“恶心”,
"幸福",,
“悲伤”,
“愤怒”,
“中立”]
#地面真相标签
#用于写入记录项的函数
#要素值始终存储为列表,单个数据元素将是大小为1的列表
def_bytestring_功能(_bytestring列表):
返回tf.train.Feature(bytes\u list=tf.train.BytesList(value=list\u of\u bytestrings))
定义int功能(int列表):#int64
返回tf.train.Feature(int64\u list=tf.train.Int64List(value=list\u of\u ints))
def to_tfrecord(tfrec_filewriter,img_字节,标签):#,高度,宽度):
class_num=np.argmax(np.array(CLASSES)=label)#这来自我们的情感标签1=sapping
一个热类=np.eye(len(CLASSES))[class\u num]
特征={
“图像”:_bytestring_功能([img_字节]),列表中有一个图像
“类”:_int_feature([class_num])#,列表中的一个类
}
返回tf.train.Example(features=tf.train.features(feature=feature))
def decode_jpeg_和_标签(文件路径):
bits=tf.io.read\u文件(文件路径)
image=tf.image.decode\u jpeg(位)
#从包含目录解析情感名称
label=tf.strings.split(tf.expand_dims(filepath+'*/*.jpeg',axis=-1),sep='')
label2=标签值[-2]
返回图像,label2
def调整大小和裁剪图像(图像、标签):
#调整大小以保持纵横比
w=tf.形状(图像)[0]
h=tf.形状(图像)[1]
tw=目标尺寸[1]
th=目标_大小[0]
调整大小(临界值=(w*th)/(h*tw)
image=tf.cond(调整大小/临界值<1,
lambda:tf.image.resize(图像,[w*tw/w,h*tw/w]),如果为真
lambda:tf.image.resize(图像,[w*th/h,h*th/h])#如果为false
)
nw=tf.形状(图像)[0]
nh=tf.形状(图像)[1]
image=tf.image.crop_到_边界_框(image,(nw-tw)//2,(nh-th)//2,tw,th)
返回图像、标签
def重新压缩图像(图像、标签):
高度=tf.形状(图像)[0]
宽度=tf.形状(图像)[1]
image=tf.cast(image,tf.uint8)
image=tf.image.encode\u jpeg(图像,优化大小=True,色度下采样=False)
返回图像、标签、高度、宽度
打印(“写入记录”)
tt0=时间。时间()
filenames=tf.data.Dataset.list_文件(GCS_模式)
dataset1=filenames.map(解码\u jpeg\u和\u标签)
dataset2=dataset1.map(调整大小和裁剪图像)
dataset3=dataset2.map(重新压缩图像)
dataset3=dataset3.batch(shard_size)#切分:每个文件将有一批图像
对于枚举(数据集3)中的碎片(图像、标签、高度、宽度):
#批大小用作此处的碎片大小
shard_size=image.numpy().shape[0]
#文件名中的记录数
filename=GCS_OUTPUT+“{:02d}-{}.tfrec”。格式(碎片,碎片大小)
将tf.io.TFRecordWriter(文件名)作为输出文件:
对于范围内的i(碎片大小):
示例=到\u tfrecord(输出\u文件,
image.numpy()[i],#重新压缩的图像:已经是一个字节字符串
label.numpy()[i]#,height.numpy()[i],width.numpy()[i]
)
out\u file.write(例如.SerializeToString())
打印(“写入的文件{}包含{}条记录”。格式(文件名,碎片大小))
打印(“总时间:+str(time.time()-tt0))
谢谢