首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用imageai -module 'keras.backend‘检测对象没有属性’get_session‘

使用imageai -module 'keras.backend‘检测对象没有属性’get_session‘
EN

Stack Overflow用户
提问于 2020-11-26 16:33:14
回答 1查看 1K关注 0票数 0

我有以下代码

代码语言:javascript
复制
from imageai.Detection import ObjectDetection
detector = ObjectDetection()

然后我得到了这个错误

代码语言:javascript
复制
AttributeError                            Traceback (most recent call last)
<ipython-input-30-0381e3fc0028> in <module>
----> 1 detector = ObjectDetection()
      2 
      3 # model_path = "./models/yolo-tiny.h5"
      4 # execution_path = os.getcwd()
      5 

~\anaconda3\lib\site-packages\imageai\Detection\__init__.py in __init__(self)
     86         self.__yolo_model_image_size = (416, 416)
     87         self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = "", "", ""
---> 88         self.sess = K.get_session()
     89 
     90         # Unique instance variables for TinyYOLOv3.

AttributeError: module 'keras.backend' has no attribute 'get_session'

在运行之后,我导入了tensorflow和keras,这些分别是

代码语言:javascript
复制
print(tensorflow.__version__)
print(keras.__version__)

2.3.1
2.4.3

我试着安装tensorflow=1.13.1,因为我读到它应该会有所帮助,但那是2018年开始的,但没有起作用。

我能做些什么来修复这个错误?

或者有没有其他方法来使用预先训练过的目标检测模型?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-02 00:12:04

您正在使用https://github.com/OlafenwaMoses/ImageAI

尽管不反对,但该存储库的最后一次提交是从2019年1月开始的。

此外,它们还将过时的网络整合到自己的框架中。

(例如,角蛋白-视黄醇不受欢迎)

既然如此,我将回答你最后一个问题:

‘还有其他方法可以使用预先训练过的对象检测模型吗?’:

是的,有。

tensorflowpytorch

这些图书馆是目前深度学习的主要图书馆,提供给他们。

例如,用torchvision.models.detectionhttps://github.com/pytorch/vision/tree/master/torchvision/models/detection编写的检测模型很少。

注1:要安装pytorch,必须在conda环境中运行:

conda install torchvision -c pytorch

注2:以下代码已功能化,将文档字符串组合在:https://github.com/pytorch/vision/blob/master/torchvision/models/detection/retinanet.py

本教程是:

https://debuggercafe.com/faster-rcnn-object-detection-with-pytorch/

我建议你也看看他们。

代码语言:javascript
复制
import cv2
import requests
import torchvision
import numpy as np

from torchvision import transforms
from PIL import Image
from io import BytesIO

coco_names = [
    '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
    'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
    'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
    'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
    'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
    'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
    'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
    'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
COLORS = np.random.uniform(0, 255, size=(len(coco_names), 3))

# read an image from the internet
url = "https://raw.githubusercontent.com/fizyr/keras-retinanet/master/examples/000000008021.jpg"
response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")

# create a retinanet inference model
model = torchvision.models.detection.retinanet_resnet50_fpn(pretrained=True, score_thresh=0.3)
model.eval()

# predict detections in the input image
image_as_tensor = transforms.Compose([transforms.ToTensor(), ])(image)
outputs = model(image_as_tensor.unsqueeze(0))

# post-process the detections ( filter them out by score )
detection_threshold = 0.5
pred_classes = [coco_names[i] for i in outputs[0]['labels'].cpu().numpy()]
pred_scores = outputs[0]['scores'].detach().cpu().numpy()
pred_bboxes = outputs[0]['boxes'].detach().cpu().numpy()
boxes = pred_bboxes[pred_scores >= detection_threshold].astype(np.int32)
classes = pred_classes
labels = outputs[0]['labels']

# draw predictions
image = cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2RGB)
for i, box in enumerate(boxes):
    color = COLORS[labels[i]]
    cv2.rectangle(image, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), color, 2)
    cv2.putText(image, classes[i], (int(box[0]), int(box[1] - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2,
                lineType=cv2.LINE_AA)
cv2.imshow('Image', image)
cv2.waitKey(0)

输出:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65025949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档