'Attributeerror module 'torchvision.models.detection' has no attribute 'ssdlite320_mobilenet_v3_large'

I had executed object detection using Deep Neural Network which is SSD with backbone of MobilenetV3 on raspberry pi 4B, but it turns out this error "Attributeerror module 'torchvision.models.detection' has no attribute 'ssdlite320_mobilenet_v3_large'".

The following is the specifications of my experiment:

model = SSD with mobilenetV3
library = pyTorch
torch = 1.7.1
torchvision = 0.8.2
device = raspberry pi 4B (ArmV8)

Here is my script:

# import the necessary packages
from torchvision.models import detection, mobilenet
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import pickle
import torch
import time
import cv2

import paramiko
import psutil
import GPUtil
import datetime 
import mysql.connector

import ftplib
import pysftp

mydb = mysql.connector.connect(
  host="aaa",
  user="bbb",
  password="ccc!",
  database="ddd"
)
mycursor = mydb.cursor()


labels = 'pytorch/labels/coco_classes.pickle'
# class_name = 'pytorch/labels/coco.names'
# model = 'frcnn-mobilenet'
# model = 'frcnn-resnet'
# model = 'mrcnn-resnet'
# model = 'retinanet'
model = 'ssd-mobilenet'
# model = 'ssd-vgg16'
confidence_param = 0.5



DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
CLASSES = pickle.loads(open(labels, "rb").read())
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

MODELS = {
    "frcnn-resnet": detection.fasterrcnn_resnet50_fpn,
    "frcnn-mobilenet": detection.fasterrcnn_mobilenet_v3_large_320_fpn,
    "retinanet": detection.retinanet_resnet50_fpn,
    "ssd-mobilenet": detection.ssdlite320_mobilenet_v3_large,
    "ssd-vgg16": detection.ssd300_vgg16,
    "mrcnn-resnet": detection.maskrcnn_resnet50_fpn    
}

# load the model and set it to evaluation mode
print("[INFO] loading model...")
model = MODELS[model](pretrained=True, progress=True, num_classes=len(CLASSES), pretrained_backbone=True).to(DEVICE)
modelsum = model.eval()

print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
fps = FPS().start() 

# auto stop when timeout
time.sleep(2.0)
timeout = time.time() + 10
pTime = 0
cTime = 0
current_time = datetime.datetime.now() 

# loop over the frames from the video stream
while True:
    cTime = time.time()
    frame = vs.read()
    frame = imutils.resize(frame, width=600)
    orig = frame.copy()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = frame.transpose((2, 0, 1))
    frame = np.expand_dims(frame, axis=0)
    frame = frame / 255.0
    frame = torch.FloatTensor(frame)

    frame = frame.to(DEVICE)
    detections = model(frame)[0]

    # gpu configuration
    GPUs = GPUtil.getGPUs()
    gpu = GPUs[0]
    gpu1 = gpu.load * 100
    cpu2 = psutil.cpu_percent() + 10
    ram1 = psutil.virtual_memory()[2]

    cpu3 = '{:.2f}'.format(cpu2)
    gpu2 = '{:.2f}'.format(gpu1)
    print ('')
    print ('---------------------------------')
    print ('cpu usage: ', cpu3, '%')
    print ('gpu usage: ', gpu2, '%')
    ram1 = psutil.virtual_memory()[2]
    print('RAM: ', ram1, '%')

    # loop over the detections
    for i in range(0, len(detections["boxes"])):
        
        confidence = detections["scores"][i]
        idx = int(detections["labels"][i])

        if idx == 1 and confidence > confidence_param:
            print ('---object has been detected!---')

            box = detections["boxes"][i].detach().cpu().numpy()
            (startX, startY, endX, endY) = box.astype("int")

            label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
            cv2.rectangle(orig, (startX, startY), (endX, endY), COLORS[idx], 2)
            accuracy = label
            
            

            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(orig, label, (startX, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
            
            s1 = cTime-pTime
            s2 = '{:.2f}'.format(s1)
            fps1 = 1/(cTime-pTime)
            fps2 = '{:.2f}'.format(fps1)
            
            pTime = cTime
            
            print ("bounding box: ", startX, startY, endX, endY)

            startXa = int(startX)
            startYa = int(startY)
            endXa = int(endX)
            endYa = int(endY)

            try:
                xid = mycursor.lastrowid
                print('Record ID: ', xid)
                xid += 1
            except:
                pass

            print ('Throughput rate: ', fps2,"fps")
            print ('---------------------------------')
            print ('')


            break

        else:
            
            print ('---NO object has been detected---')
          
            s1 = cTime-pTime
            s2 = '{:.2f}'.format(s1)

            fps1 = 1/(cTime-pTime)
            fps2 = '{:.2f}'.format(fps1)
            
            pTime = cTime
            
            try:
                xid = mycursor.lastrowid
                print('Record ID: ', xid)
                xid += 1
            except:
                pass

            print ('Throughput rate: ', fps2,"fps")
            print ('---------------------------------')
            print ('')


          

            break

    cv2.imshow("Frame", orig)
    key = cv2.waitKey(1) & 0xFF

    if time.time() > timeout:
        break

    if key == ord("q"):
        break


    fps.update()

  

# stop the timer and display FPS information
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

cv2.destroyAllWindows()
vs.stop()


Solution 1:[1]

SSDLite was only added to torchvision in version 0.10.0 (release message).

That means you need to update torch and torchvision on your device by running

pip3 install --upgrade torch
pip3 install --upgrade torchvision

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pascalco