You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
with torch.no_grad():
weights, imgsz = opt['weights'], opt['img-size']
set_logging()
device = select_device(opt['device'])
half = device.type != 'cpu'
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check img_size
if half:
model.half()
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters())))
img0 = cv2.imread(source_image_path)
img = letterbox(img0, imgsz, stride=stride)[0]
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
Inference
t1 = time_synchronized()
pred = model(img, augment= False)[0]
Apply NMS
classes = None
if opt['classes']:
classes = []
for class_name in opt['classes']:
classes.append(names.index(class_name))
if classes:
classes = [i for i in range(len(names)) if i not in classes]
pred = non_max_suppression(pred, opt['conf-thres'], opt['iou-thres'], classes= [17], agnostic= False)
t2 = time_synchronized()
for i, det in enumerate(pred):
s = ''
s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(img0.shape)[[1, 0, 1, 0]]
if len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
for *xyxy, conf, cls in reversed(det):
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=3)
The code is given below
Give path of source image
source_image_path = '/content/gdrive/MyDrive/yolov7/inference/images/horses.jpg'
with torch.no_grad():
weights, imgsz = opt['weights'], opt['img-size']
set_logging()
device = select_device(opt['device'])
half = device.type != 'cpu'
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check img_size
if half:
model.half()
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters())))
img0 = cv2.imread(source_image_path)
img = letterbox(img0, imgsz, stride=stride)[0]
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
Inference
t1 = time_synchronized()
pred = model(img, augment= False)[0]
Apply NMS
classes = None
if opt['classes']:
classes = []
for class_name in opt['classes']:
if classes:
pred = non_max_suppression(pred, opt['conf-thres'], opt['iou-thres'], classes= [17], agnostic= False)
t2 = time_synchronized()
for i, det in enumerate(pred):
s = ''
s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(img0.shape)[[1, 0, 1, 0]]
if len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
The errors
AssertionError Traceback (most recent call last)
in <cell line: 5>()
6 weights, imgsz = opt['weights'], opt['img-size']
7 set_logging()
----> 8 device = select_device(opt['device'])
9 half = device.type != 'cpu'
10 model = attempt_load(weights, map_location=device) # load FP32 model
/content/gdrive/MyDrive/yolov7/utils/torch_utils.py in select_device(device, batch_size)
69 elif device: # non-cpu device requested
70 os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
---> 71 assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availability
72
73 cuda = not cpu and torch.cuda.is_available()
AssertionError: CUDA unavailable, invalid device 0 requested
The text was updated successfully, but these errors were encountered: