繁体   English   中英

加载数据时 map 文本和图像的最佳方式

[英]Best way to map Text and Image while loading the data

我有一个 csv 文件,看起来有点像照片。

CSV文件

我正在构建一个 model,它将图像及其相应的文本( df['Content']作为输入。

我想知道以下列方式加载此数据的最佳方式:

  • df['Image_location']中的图像加载到张量中。
  • 并保留图像到相应文本的顺序。
  • 保留相应的 label ( df['Sentiment'] )

关于如何做到这一点的任何想法?

您可以尝试使用tf.data.Dataset API。

创建虚拟数据:

import numpy
from PIL import Image

for i in range(1, 3):
  imarray = numpy.random.rand(64,64,3) * 255
  im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
  im.save('result_image{}.png'.format(i))

过程:

import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(data= {'Location': ['some.txt', 'some-other.txt'], 
                         'Content': ['This road was ok', 'This was wonderful'],
                         'Score': [0.0353, -0.341],
                         'Sentiment': ['Neutral', 'Positive'],
                         'Image_location': ['/content/result_image1.png', '/content/result_image2.png']})

features = df[['Content', 'Image_location']]
labels = df['Sentiment']

dataset = tf.data.Dataset.from_tensor_slices((features, labels))
def process_path(x):
  content, image_path = x[0], x[1]
  img = tf.io.read_file(image_path)
  img = tf.io.decode_png(img, channels=3)
  return content, img

dataset = dataset.map(lambda x, y: (process_path(x), y))

for x, y in dataset.take(1):
  content = x[0]
  image = x[1]
  print('Content -->', content)
  print('Sentiment -->', y)
  plt.imshow(image.numpy())
Content --> tf.Tensor(b'This road was ok', shape=(), dtype=string)
Sentiment --> tf.Tensor(b'Neutral', shape=(), dtype=string)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM