![](/img/trans.png)
[英]Best way to process texts and decode videos while loading the data in Tensorflow
[英]Best way to map Text and Image while loading the data
您可以嘗試使用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.