【发布时间】:2019-08-09 04:51:55
【问题描述】:
我有一个包含两个类的黑白图像的大数据集,其中目录的名称是类的名称:
- 目录
SELECTION包含所有带有label = selection 的图像; - 目录
NEUTRAL包含标签=中性的所有图像。
我需要将所有这些图像加载到 TensorFlow 数据集中,以更改 this 教程中的 MNIST 数据集。
我尝试遵循this 指南,它看起来不错,但有一些我不知道如何解决的问题。按照指南,我到了这里:
from __future__ import absolute_import, division, print_function
import os
import pathlib
import IPython.display as display
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(threshold=np.nan)
tf.enable_eager_execution()
tf.__version__
os.system('clear')
#### some tries for the SELECTION dataset ####
data_root = pathlib.Path('/Users/matteo/Desktop/DATASET_X/SELECTION/TRAIN_IMG')
all_image_paths = []
all_image_labels = []
for item in data_root.iterdir():
item_tmp = str(item)
if 'selection.png' in item_tmp:
all_image_paths.append(str(item))
all_image_labels.append(0)
image_count = len(all_image_paths)
label_names = ['selection', 'neutral']
label_to_index = dict((name, index) for index, name in enumerate(label_names))
img_path = all_image_paths[0]
img_raw = tf.read_file(img_path)
img_tensor = tf.image.decode_png(
contents=img_raw,
channels=1
)
print(img_tensor.numpy().min())
print(img_tensor.numpy().max())
#### it works fine till here ####
#### trying to make a function ####
#### problems from here ####
def load_and_decode_image(path):
print('[LOG:load_and_decode_image]: ' + str(path))
image = tf.read_file(path)
image = tf.image.decode_png(
contents=image,
channels=3
)
return image
image_path = all_image_paths[0]
label = all_image_labels[0]
image = load_and_decode_image(image_path)
print('[LOG:image.shape]: ' + str(image.shape))
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
print('shape: ', repr(path_ds.output_shapes))
print('type: ', path_ds.output_types)
print()
print('[LOG:path_ds]:' + str(path_ds))
如果我只加载一项,它可以工作,但是当我尝试这样做时:
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
如果我打印 path_ds.shape 它返回 shape: TensorShape([]) 所以它似乎不起作用。如果我尝试使用此块继续按照教程进行操作
image_ds = path_ds.map(load_and_decode_image, num_parallel_calls=AUTOTUNE)
plt.figure(figsize=(8, 8))
for n, image in enumerate(image_ds.take(4)):
print('[LOG:n, image]: ' + str(n) + ', ' + str(image))
plt.subplot(2, 2, n+1)
plt.imshow(image)
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.xlabel(' selection'.encode('utf-8'))
plt.title(label_names[label].title())
plt.show()
它给了我以下错误:
It's not possible open ' < string >': The file was not found (file: // /Users/matteo/Documents/GitHub/Cnn_Genetic/cnn_genetic/<string > ).
但问题是我不知道这个文件是什么以及它为什么要寻找它。我不需要绘制我的图像,但我想了解它为什么不起作用。如果我复制/粘贴教程代码,我会遇到同样的问题,所以我认为新的 tf 版本有问题。
所以....如果有人能告诉我哪里出错了,我将不胜感激。 感谢您的宝贵时间。
【问题讨论】:
-
如您的错误消息所述,其中一个文件丢失。在会话中运行 for 循环以找出
-
我认为程序会去寻找一个不存在的文件,因为它读取了错误的值。一个名为
<string>的文件实在太奇怪了。对我来说,问题是如果我打印path_ds.shape它会返回shape: TensorShape([])。
标签: python tensorflow dataset load local