簡體   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