我們可以使用帶有 flow_from_directory 方法的 ImageDataGenerator 生成圖像數(shù)據(jù)集。train_datagen = ImageDataGenerator( rescale=1./255, #scale images from integers 0-255 to floats 0-1. shear_range=0.2, zoom_range=0.2, # zoom in or out in images horizontal_flip=True) #horizontal flip of imagestrain_set = train_datagen.flow_from_directory(..)并顯示:Found 200 images belonging to 2 classes我想寫一個(gè)循環(huán)來計(jì)算train_set上的圖像數(shù)量For image in train_set: count = count+1print(count)但這并沒有顯示任何東西!
3 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
要訪問數(shù)據(jù)集的樣本數(shù)量,您首先必須知道它是哪種類型:
如果您使用的是 ImageDataGenerator,那么:
type(train_ds)
將返回 tensorflow.python.keras.preprocessing.image.DirectoryIterator。在這種情況下,您可以通過以下方式訪問樣本數(shù)量:
train_ds.samples
但是,如果您正在使用以下方法創(chuàng)建數(shù)據(jù)集:
train_ds = tf.keras.preprocessing.image_dataset_from_directory( rescale = 1/255. , etc...)
然后
type(train_ds)
將返回 tensorflow.python.data.ops.dataset_ops.BatchDataset 這意味著您可以使用間接訪問樣本數(shù)
len(train_ds.file_paths)
添加回答
舉報(bào)
0/150
提交
取消