我正在創建一些批處理 TensorFlow 資料集 tf.keras.preprocessing.image_dataset_from_directory:
image_size = (90, 120)
batch_size = 32
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
os.path.join(model_split_dir,'train'),
validation_split=0.25,
subset="training",
seed=1,
image_size=image_size,
batch_size=batch_size
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
os.path.join(model_split_dir,'train'),
validation_split=0.25,
subset="validation",
seed=1,
image_size=image_size,
batch_size=batch_size
)
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
os.path.join(model_split_dir,'test'),
seed=1,
image_size=image_size,
batch_size=batch_size
)
如果我隨后使用以下 for 回圈從其中一個資料集中獲取影像和標簽資訊,則每次運行它都會得到不同的輸出:
for images, labels in test_ds:
print(labels)
例如,第一批在一次運行中將如下所示:
tf.Tensor([0 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1], shape=(32,), dtype=int32)
但是當回圈再次運行時就完全不同了;
tf.Tensor([1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 0 0], shape=(32,), dtype=int32)
每次我遍歷它時,順序怎么會有所不同?TensorFlow 資料集是無序的嗎?根據我的發現,它們應該是有序的,所以我不知道為什么 for 回圈每次都以不同的順序回傳標簽。
對此的任何見解將不勝感激。
更新:資料集順序的改組按預期作業。對于我的測驗資料,我只需要將 shuffle 設定為 False。非常感謝@AloneTogether!
uj5u.com熱心網友回復:
shuffle的引數默認tf.keras.preprocessing.image_dataset_from_directory設定為True,如果您想要確定的結果,可以嘗試將其設定為False:
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)
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
image_size=(28, 28),
batch_size=5,
shuffle=False)
for x, y in train_ds:
print(y)
break
另一方面,這總是會產生隨機結果:
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
seed=None,
image_size=(28, 28),
batch_size=5,
shuffle=True)
for x, y in train_ds:
print(y)
break
如果您設定隨機種子 和shuffle=True,資料集將被洗牌一次,但您將獲得確定性結果:
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
seed=123,
image_size=(28, 28),
batch_size=5,
shuffle=True)
for x, y in train_ds:
print(y)
break
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/455297.html
