簡體   English   中英

object 檢測與 imageai -module 'keras.backend' 沒有屬性 'get_session'-

[英]object detection with imageai -module 'keras.backend' has no attribute 'get_session'-

我有以下代碼

from imageai.Detection import ObjectDetection
detector = ObjectDetection()

然后我得到了這個錯誤

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 ,這些分別是版本

print(tensorflow.__version__)
print(keras.__version__)

2.3.1
2.4.3

我嘗試安裝 tensorflow=1.13.1 因為我讀到它應該在某個地方有所幫助,但那是從 2018 年開始的,它沒有用。

我能做些什么來修復這個錯誤?

或者有沒有其他方法可以使用預訓練的 object 檢測模型?

您正在使用https://github.com/OlafenwaMoses/ImageAI
盡管它沒有被棄用,但該存儲庫的最后一次提交是從 2019 年 1 月開始的。
此外,他們在其框架中集成了過時的網絡
(例如,不推薦使用 keras-retinanet)

鑒於此,我將回答您的最后一個問題:
“還有其他方法可以使用預訓練的 object 檢測模型嗎?”:

是的,有。
tensorflowpytorch
目前是深度學習的主要庫,請提供它們。

例如,pytorch 在torchvision.models.detection中編碼的檢測模型很少: https://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/
我建議你也看看他們。

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)

Output: 視網膜網絡示例

暫無
暫無

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

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