【问题标题】:Why I got AttributeError: 'NoneType' object has no attribute 'label_map'为什么我得到 AttributeError:'NoneType' 对象没有属性 'label_map'
【发布时间】:2021-08-20 02:01:07
【问题描述】:

我是 ML 和 CV 领域的新手。 刚刚在 colab 上使用 TensorFlow Lite Model Maker 训练沙拉检测器来训练我的 tensorflow lite 自定义模型。 但是遇到了这个问题。

AttributeError: 'NoneType' 对象没有属性 'label_map'

这里是代码。

!pip install -q tflite-model-maker
!pip install -q pycocotools 

import numpy as np
import os

from tflite_model_maker.config import ExportFormat
from tflite_model_maker import model_spec
from tflite_model_maker import object_detector

import tensorflow as tf
assert tf.__version__.startswith('2')

tf.get_logger().setLevel('ERROR')
from absl import logging
logging.set_verbosity(logging.ERROR)

spec = model_spec.get('efficientdet_lite2')

train_data, validation_data, test_data = object_detector.DataLoader.from_csv('gs://zmlcoral/strips/export_data-untitled_1629418661144-2021-08-20T01:44:38.803321Z/image_object_detection_1.csv')

我的谷歌云数据集

model = object_detector.create(train_data, model_spec=spec, batch_size=8, train_whole_model=True, validation_data=validation_data)

错误发生在这里

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-187f39c1697e> in <module>()
----> 1 model = object_detector.create(train_data, model_spec=spec, batch_size=8, train_whole_model=True, validation_data=validation_data)

/usr/local/lib/python3.7/dist-packages/tensorflow_examples/lite/model_maker/core/task/object_detector.py in create(cls, train_data, model_spec, validation_data, epochs, batch_size, train_whole_model, do_train)
    281           model_spec.compat_tf_versions, compat.get_tf_behavior()))
    282 
--> 283     object_detector = cls(model_spec, train_data.label_map, train_data)
    284 
    285     if do_train:

AttributeError: 'NoneType' object has no attribute 'label_map'

谁能帮我解决这个问题。谢谢。

【问题讨论】:

  • 显然train_dataNone。所以要么检查它,要么尝试找出它为什么是None 并修复它。

标签: python tensorflow-lite nonetype


【解决方案1】:

我能够让它与 python csv writer 的这些设置一起工作。我还建议使用完整的全局路径,因为使用文件的相对路径似乎不起作用:(

import csv
import glob

# the headings for the google models
train_marker = 'TRAIN,'
test_marker  = 'TEST,'
val_marker   = 'VALIDATION,'

# default bounding box encapsulating image
b_box = '0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0'

train = glob.glob("yourpath/train/*.jpg")
test  = glob.glob("yourpath/test/*.jpg")
val   = glob.glob("yourpath/val/*.jpg")


with open('droneData.csv', 'w', encoding="utf-8") as csvfile:
    csv_writer = csv.writer(csvfile, quotechar = '\'',delimiter=',',quoting = csv.QUOTE_NONE,escapechar = ' ')
                        
    for file_path in train:
        csv_writer.writerow([train_marker +  file_path + ',' + label + b_box])

    for file_path in test:
        csv_writer.writerow([test_marker + '/' + file_path + ',' + label + b_box])

    for file_path in val:
        csv_writer.writerow([val_marker + '/' + file_path + ',' + label + b_box])

【讨论】:

    猜你喜欢
    • 2021-03-29
    相关资源
    最近更新 更多