ฉันคิดว่าองเอกสารที่สามารถเป็นค่อนข้างสับสนและฉันคิดว่าพฤติกรรมนั้นมักแตกต่างขึ้นอยู่กับของคุณ Tensorflow และ Keras รุ่น สำหรับตัวอย่างใน ลังของผู้ใช้คือธิบายส่วนผสมที่แน่นอนพฤติกรรมคุณเป็นคาดหวัง โดยปกติแล้วคน flow_from_directory()
วิธีการอนุญาตให้คุณอ่านภาพโดยตรงจากหองไดเรกทอรีและ augment พวกเขาขณะที่ของรุ่นคือการฝึกและอย่างที่แล้วกล่าว นี่มัน iterates สำหรับทุกตัวอย่างในแต่ละโฟลเดอร์ทุก epoch. ใช้ตามตัวอย่างเช่นคุณสามารถตรวจสอบว่านี่เป็นคดี(ใน TF 2.7)โดยที่นอนขั้นตอนต่อ epoch อยู่ในความคืบหน้าแถบ:
import tensorflow as tf
BATCH_SIZE = 64
flowers = tf.keras.utils.get_file(
'flower_photos',
'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
untar=True)
img_gen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
horizontal_flip=True,
)
train_ds = img_gen.flow_from_directory(flowers, batch_size=BATCH_SIZE, shuffle=True, class_mode='sparse')
num_classes = 5
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu', input_shape=(256, 256, 3)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))
epochs=10
history = model.fit(
train_ds,
epochs=epochs
)
Found 3670 images belonging to 5 classes.
Epoch 1/10
6/58 [==>...........................] - ETA: 3:02 - loss: 2.0608
ถ้าคุณห่อ flow_from_directory
กับ tf.data.Dataset.from_generator
เหมือนนี้:
train_ds = tf.data.Dataset.from_generator(
lambda: img_gen.flow_from_directory(flowers, batch_size=BATCH_SIZE, shuffle=True, class_mode='sparse'),
output_types=(tf.float32, tf.float32))
คุณจะสังเกตเห็นว่าความคืบหน้าบาร์ดูเหมือนว่าเรื่องนี้เพราะ steps_per_epoch
ไม่ได้ถูกกำหนดไว้ในข้อบังคับ:
Epoch 1/10
Found 3670 images belonging to 5 classes.
29/Unknown - 104s 4s/step - loss: 2.0364
และถ้าคุณเพิ่มพารามิเตอร์,คุณจะเห็นขั้นตอนอยู่ในความคืบหน้าแถบ:
history = model.fit(
train_ds,
steps_per_epoch = len(from_directory),
epochs=epochs
)
Found 3670 images belonging to 5 classes.
Epoch 1/10
3/58 [>.............................] - ETA: 3:19 - loss: 4.1357
ในที่สุดก็เพื่อคำถามของคุณ:
วิธีใช้มันอย่างถูกต้องกำเนิดไฟฟ้าเรียบร้อมกับฟังก์ชันเหมาะสมที่จะมี
ข้อมูลในของฉันการฝึกตั้งค่ารวมถึงดั้งเดิม,ไม่มี augmented ภาพแล้ว
augmented ภาพต่างๆและต้องลงตรวจดูมันหลายครั้งแล้ว/ขั้น?
คุณสามารถเพียงเพิ่ม steps_per_epoch
ยิ่งกว่า number of samples // batch_size
โดย multiplying มีปัจจั:
history = model.fit(
train_ds,
steps_per_epoch = len(from_directory)*2,
epochs=epochs
)
Found 3670 images belonging to 5 classes.
Epoch 1/10
1/116 [..............................] - ETA: 12:11 - loss: 1.5885
ตอนนี้แทนที่จะเป็น 58 ขั้นตอนต่อ epoch คุณต้อง 116.