簡體   English   中英

“DirectoryIterator”類型的對象沒有 len()

[英]object of type 'DirectoryIterator' has no len()

我正在嘗試創建一個 Inception v3 CNN 模型。 這樣做時,我遇到此代碼的以下錯誤:-

def extract_features(sample_count, datagen):
    start = dt.datetime.now()
    features =  np.zeros(shape=(sample_count, 5, 5, 2048))
    labels = np.zeros(shape=(sample_count,10))
    generator = datagen
    i = 0
    for inputs_batch,labels_batch in generator:
        stop = dt.datetime.now()
        time = (stop - start).seconds
        print('\r',
              'Extracting features from batch', str(i+1), '/', len(datagen),
              '-- run time:', time,'seconds',
              end='')
        
        features_batch = base_model.predict(inputs_batch)
        
        features[i * batch_size : (i + 1) * batch_size] = features_batch
        labels[i * batch_size : (i + 1) * batch_size] = labels_batch
        i += 1
        
        if i * batch_size >= sample_count:
            break
            
    print("\n")
    return features, labels
train_features, train_labels = extract_features(1097, train_generator)
test_features, test_labels = extract_features(272, test_generator)
Error: - 
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-24-d27864586d21> in <module>
    ----> 1 train_features, train_labels = extract_features(1097, train_generator)
          2 test_features, test_labels = extract_features(272, test_generator)

    <ipython-input-23-94a0a25bf25a> in extract_features(sample_count, datagen)
          9         time = (stop - start).seconds
         10         print('\r',
    ---> 11               'Extracting features from batch', str(i+1), '/', len(datagen),
         12               '-- run time:', time,'seconds',
         13               end='')

    TypeError: object of type 'DirectoryIterator' has no len()

TF 版本 = 1.14.0 Python 版本 = 3.6.9

嘗試更改此行:

        print('\r',
          'Extracting features from batch', str(i+1), '/', len(datagen),
          '-- run time:', time,'seconds',
          end='')

對此:

         print('\r',
          'Extracting features from batch', str(i+1), '/', len(list(datagen)),
          '-- run time:', time,'seconds',
          end='')

經過大量挖掘,我發現問題在於 Keras 與 TF 1.14.0 的兼容性。 Keras 2.2.4 兼容 TF 1.14.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM