我發現了一個類似的問題,但它不太適合這種情況。
我在同一個目錄中有多個類的一些影像,我想根據它們的檔案名進行標記。我還有其他目錄,其中包含我想包含在同一資料集中的更多影像。
這是檔案夾結構的示例:
train/directory_1/cat_1.png
train/directory_1/cat_2.png
train/directory_1/dog_1.png
train/directory_1/dog_2.png
...
train/directory_2/cat_1.png
train/directory_2/cat_2.png
train/directory_2/dog_1.png
train/directory_2/dog_2.png
有沒有什么有效的方法可以從這個結構創建資料集?因為我通常使用image_dataset_from_directory()
但在這種情況下如果沒有移動影像它就不起作用。
uj5u.com熱心網友回復:
您可以將所有檔案路徑存盤在一個串列中,并用它創建一個 tensorflow 資料集。
獲取所有檔案路徑的簡單方法是
import glob
paths = glob.glob("train/*/*")
#If you want only the .png files
paths = glob.glob("train/*/*.png")
完整示例我在 Colab 上創建了一些示例影像
paths = glob.glob("/content/*/*.jpeg")
paths
>>>
['/content/dir_1/cat_1.jpeg',
'/content/dir_1/dog_1.jpeg',
'/content/dir_2/dog_2.jpeg',
'/content/dir_2/cat_2.jpeg']
paths = glob.glob("/content/*/*.jpeg")
label_dict = {'cat':0, 'dog':1}
def get_label(x):
label = x.split('.')[0].rsplit('_')[-2].split('/')[-1]
return label_dict[label]
labels = [get_label(i) for i in paths]
labels
>>>
[0, 1, 1, 0]
創建資料集
def preprocess(x,y):
img = tf.io.read_file(x)
img = tf.io.decode_png(img, channels=3)
img = tf.cast(img,dtype=tf.float32)
img = img / 255.
return img,y
dataset = tf.data.Dataset.from_tensor_slices((paths,labels))
dataset = dataset.map(preprocess)
dataset = dataset.batch(2)
for x,y in dataset.take(1):
print(x.shape, y)
>>>
(2, 192, 262, 3) tf.Tensor([0 1], shape=(2,), dtype=int32)
uj5u.com熱心網友回復:
沒有任何方法可以讓您自動執行此操作image_dataset_from_directory
(至少我知道)。但是,您自己創建資料集非常容易。
您首先必須遍歷您的目錄以搜索影像。您將遇到的每個影像的路徑保存在一個串列中paths
。對于每個影像,您檢查類,在這種情況下是狗或貓,然后創建一個字典,在其中將每個類名映射到不同的 int,即該類的標簽。對于每個影像,您將其標簽保存在串列中labels
。
import glob
paths = []
labels = []
labels_dict = {}
index = 0
for filepath in glob.iglob('train/*/*.jpg'):
paths.append(filepath)
class_name = filepath.split('_')[1].split("/")[-1]
if class_name not in labels_dict:
labels_dict[class_name] = index
index = 1
labels.append(labels_dict[class_name])
print(labels)
print(paths)
print(labels_dict)
輸出:
# labels:
[0, 1, 1, 1, 1, 0]
# paths
['train/dir_1/dog_1.jpg', 'train/dir_1/cat_2.jpg', 'train/dir_1/cat_1.jpg', 'train/dir_2/cat_1.jpg', 'train/dir_2/cat_2.jpg', 'train/dir_2/dog_1.jpg']
# labels_dict
{'dog': 0, 'cat': 1}
現在您已經準備好路徑和標簽,您可以從這個答案開始:
import tensorflow as tf
paths = tf.constant(paths)
labels = tf.constant(labels)
# create a dataset returning slices of `paths`
dataset = tf.data.Dataset.from_tensor_slices((paths, labels))
# parse every image in the dataset using `map`
def _parse_function(filename, label):
image_string = tf.io.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string, channels=3)
image = tf.cast(image_decoded, tf.float32)
return image, label
dataset = dataset.map(_parse_function)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/508472.html
上一篇:網格搜索問題的最大遞回深度誤差