【问题标题】:Using ImageAI I get => AttributeError: module 'tensorflow' has no attribute 'to_float'使用 ImageAI 我得到 => AttributeError: module 'tensorflow' has no attribute 'to_float'
【发布时间】:2020-04-14 10:47:32
【问题描述】:

我想按照本教程 (https://medium.com/deepquestai/train-object-detection-ai-with-6-lines-of-code-6d087063f6ff) 检测图像中的对象。但是我收到一条错误消息,我无法解决。鉴于我无法从 imageai 更改源代码,因此无法以这种方式修复错误 (https://github.com/google/tangent/issues/95),我该怎么办?

这些是我的导入:

!pip3 install tensorflow-gpu==1.13.1
!pip install imageai --upgrade
from imageai.Detection.Custom import DetectionModelTrainer

我运行这段代码:

data_path = 'leaf-images-with-pascal-voc-annotations/'

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory=data_path)
trainer.setTrainConfig(object_names_array=['leaf'], batch_size=16, num_experiments=100, 
                       train_from_pretrained_model="pretrained-yolov3.h5")
trainer.trainModel()

我尝试使用不同版本的 tensorflow 但收到此错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-d42b2127d681> in <module>
      6 trainer.setTrainConfig(object_names_array=['leaf'], batch_size=16, num_experiments=100, 
      7                        train_from_pretrained_model="pretrained-yolov3.h5")
----> 8 trainer.trainModel()

/opt/conda/lib/python3.6/site-packages/imageai/Detection/Custom/__init__.py in trainModel(self)
    272             noobj_scale=self.__train_noobj_scale,
    273             xywh_scale=self.__train_xywh_scale,
--> 274             class_scale=self.__train_class_scale,
    275         )
    276 

/opt/conda/lib/python3.6/site-packages/imageai/Detection/Custom/__init__.py in _create_model(self, nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale)
    551                     noobj_scale=noobj_scale,
    552                     xywh_scale=xywh_scale,
--> 553                     class_scale=class_scale
    554                 )
    555         else:

/opt/conda/lib/python3.6/site-packages/imageai/Detection/Custom/yolo.py in create_yolov3_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale)
    292                             noobj_scale,
    293                             xywh_scale,
--> 294                             class_scale)([input_image, pred_yolo_1, true_yolo_1, true_boxes])
    295 
    296     # Layer 83 => 86

/opt/conda/lib/python3.6/site-packages/imageai/Detection/Custom/yolo.py in __init__(self, anchors, max_grid, batch_size, warmup_batches, ignore_thresh, grid_scale, obj_scale, noobj_scale, xywh_scale, class_scale, **kwargs)
     22         max_grid_h, max_grid_w = max_grid
     23 
---> 24         cell_x = tf.to_float(tf.reshape(tf.tile(tf.range(max_grid_w), [max_grid_h]), (1, max_grid_h, max_grid_w, 1, 1)))
     25         cell_y = tf.transpose(cell_x, (0,2,1,3,4))
     26         self.cell_grid = tf.tile(tf.concat([cell_x,cell_y],-1), [batch_size, 1, 1, 3, 1])

AttributeError: module 'tensorflow' has no attribute 'to_float'

【问题讨论】:

    标签: python tensorflow imageai


    【解决方案1】:

    我在尝试使用 2.3.0 版中的 to_float 方法时也遇到了同样的错误

    看来,此方法已在较新版本的库中删除。

    为使其正常工作,我已将代码更改为使用 cast 方法而不是 to_float

    下面是对我有用的示例代码

    num=5
    #as_float = tf.to_float(num)
    #Change the above code line and use cast method instead
    as_float=tf.cast(num, tf.float32)
    as_float
    

    【讨论】:

      【解决方案2】:

      ImageAI 库的当前状态似乎未修复,与最新版本的 tensorflow 等不兼容。

      使用这些版本对我有用:

      #Currently I found these to work together:
      pip install opencv-python==4.1.2.30
      pip install keras==2.3.1
      pip install tensorflow==1.14.0
      pip install tensorflow-gpu==1.14.0
      pip install imageai --upgrade
      NOTE: using imageai == 2.1.5
      

      【讨论】:

        猜你喜欢
        • 2022-12-01
        • 2021-01-11
        • 2020-09-24
        • 1970-01-01
        • 2022-08-04
        • 2014-05-08
        • 1970-01-01
        • 2020-09-04
        • 2021-05-28
        相关资源
        最近更新 更多