這是我之前創建的用于模型的批處理資料集:
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
train_path,
label_mode = 'categorical', #it is used for multiclass classification. It is one hot encoded labels for each class
validation_split = 0.2, #percentage of dataset to be considered for validation
subset = "training", #this subset is used for training
seed = 1337, # seed is set so that same results are reproduced
image_size = img_size, # shape of input images
batch_size = batch_size, # This should match with model batch size
)
valid_ds = tf.keras.preprocessing.image_dataset_from_directory(
train_path,
label_mode ='categorical',
validation_split = 0.2,
subset = "validation", #this subset is used for validation
seed = 1337,
image_size = img_size,
batch_size = batch_size,
)
如果我運行 for 回圈,我就可以訪問 img 陣列和標簽:
for images, labels in train_ds:
print(labels)
但是,如果我嘗試像這樣訪問它們:
嘗試 1)
images, labels = train_ds
我收到以下值錯誤: ValueError: too many values to unpack (expected 2)
嘗試 2:
如果我嘗試像這樣打開它:
images = train_ds[:,0] # get the 0th column of all rows
labels = train_ds[:,1] # get the 1st column of all rows
我收到以下錯誤: TypeError: 'BatchDataset' object is not subscriptable
有沒有辦法讓我在不通過 for 回圈的情況下提取標簽和影像?
uj5u.com熱心網友回復:
只需取消批處理您的資料集并將資料轉換為串列:
import tensorflow as tf
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir, validation_split=0.2, subset="training",
seed=123, batch_size=batch_size)
train_ds = train_ds.unbatch()
images = list(train_ds.map(lambda x, y: x))
labels = list(train_ds.map(lambda x, y: y))
print(len(labels))
print(len(images))
Found 3670 files belonging to 5 classes.
Using 2936 files for training.
2936
2936
uj5u.com熱心網友回復:
針對您的特殊情況下,train_ds將是一個張量物件,其中的每個元素是一個元組:(image,label)。
可能嘗試類似:
# train_ds = [(image,label) …]
images = train_ds[:,0] # get the 0th column of all rows
labels = train_ds[:,1] # get the 1st column of all rows
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/396766.html
