question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I convert pynb to python but I got no output:



from trt_pose.parse_objects import ParseObjects
from trt_pose.draw_objects import DrawObjects
import PIL.Image
import torchvision.transforms as transforms
import cv2
import time
from torch2trt import TRTModule
import torch2trt
import torch
import trt_pose.models
import json
import trt_pose.coco
import os
import sys


with open('human_pose.json', 'r') as f:
    human_pose = json.load(f)

topology = trt_pose.coco.coco_category_to_topology(human_pose)


num_parts = len(human_pose['keypoints'])
num_links = len(human_pose['skeleton'])

model = trt_pose.models.resnet18_baseline_att(
    num_parts, 2 * num_links).cuda().eval()


MODEL_WEIGHTS = 'resnet18_baseline_att_224x224_A_epoch_249.pth'

model.load_state_dict(torch.load(MODEL_WEIGHTS))


WIDTH = 224
HEIGHT = 224

data = torch.zeros((1, 3, HEIGHT, WIDTH)).cuda()


model_trt = torch2trt.torch2trt(
    model, [data], fp16_mode=True, max_workspace_size=1 << 25)


OPTIMIZED_MODEL = 'resnet18_baseline_att_224x224_A_epoch_249_trt.pth'

torch.save(model_trt.state_dict(), OPTIMIZED_MODEL)


model_trt = TRTModule()
model_trt.load_state_dict(torch.load(OPTIMIZED_MODEL))


t0 = time.time()
torch.cuda.current_stream().synchronize()
for i in range(50):
    y = model_trt(data)
torch.cuda.current_stream().synchronize()
t1 = time.time()

print(50.0 / (t1 - t0))


mean = torch.Tensor([0.485, 0.456, 0.406]).cuda()
std = torch.Tensor([0.229, 0.224, 0.225]).cuda()
device = torch.device('cuda')


def preprocess(image):
    global device
    device = torch.device('cuda')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = PIL.Image.fromarray(image)
    image = transforms.functional.to_tensor(image).to(device)
    image.sub_(mean[:, None, None]).div_(std[:, None, None])
    return image[None, ...]


parse_objects = ParseObjects(topology)
draw_objects = DrawObjects(topology)


if __name__ == '__main__':
    v_f = sys.argv[1]
    cap = cv2.VideoCapture(v_f)

    while(True):
        ret, image = cap.read()
        if ret:
            data = preprocess(image)
            cmap, paf = model_trt(data)
            cmap, paf = cmap.detach().cpu(), paf.detach().cpu()
            # , cmap_threshold=0.15, link_threshold=0.15)
            counts, objects, peaks = parse_objects(cmap, paf)
            print(counts)
            print(objects)
            draw_objects(image, counts, objects, peaks)
            cv2.imshow('res', image)
            cv2.waitKey(1)

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:15

github_iconTop GitHub Comments

2reactions
gongysh2004commented, Mar 17, 2021

haha, the secret is to resize the frame into 224,224 before calling model_trt

1reaction
tucachmo2202commented, Mar 29, 2021

@lweingart ` import os import time import json import cv2 import torchvision.transforms as transforms import PIL.Image from trt_pose.draw_objects import DrawObjects from trt_pose.parse_objects import ParseObjects import trt_pose.coco import trt_pose.models import torch from torch2trt import TRTModule from PIL import Image

WIDTH = 256 HEIGHT = 256 mean = torch.Tensor([0.485, 0.456, 0.406]).cuda() std = torch.Tensor([0.229, 0.224, 0.225]).cuda() device = torch.device(‘cuda’)

with open(‘human_pose.json’, ‘r’) as f: human_pose = json.load(f)

topology = trt_pose.coco.coco_category_to_topology(human_pose) num_parts = len(human_pose[‘keypoints’]) num_links = len(human_pose[‘skeleton’])

OPTIMIZED_MODEL = ‘resnet50_baseline_att_256x256_A_epoch_249_trt.pth’ print(OPTIMIZED_MODEL)

model_trt = TRTModule() model_trt.load_state_dict(torch.load(OPTIMIZED_MODEL))

parse_objects = ParseObjects(topology) draw_objects = DrawObjects(topology)

def preprocess(image): global device device = torch.device(‘cuda’)

image = PIL.Image.fromarray(image)
image = transforms.functional.to_tensor(image).to(device)
image.sub_(mean[:, None, None]).div_(std[:, None, None])
return image[None, ...]

def process_img(image): ori_image = image.copy() image = cv2.resize(image, (WIDTH, HEIGHT)) data = preprocess(image) start = time.time() start_model = time.time() cmap, paf = model_trt(data) print("FPS model: ", 1.0/(time.time() - start_model)) cmap, paf = cmap.detach().cpu(), paf.detach().cpu() counts, objects, peaks = parse_objects(cmap, paf)#, cmap_threshold=0.15, link_threshold=0.15) print("FPS: ", 1.0/(time.time() - start)) draw_objects(ori_image, counts, objects, peaks) return ori_image

def predict_image(path = ‘1.jpg’): image = cv2.imread(path) img = process_img(image) cv2.imshow(“as”, img) cv2.waitKey() cv2.destroyAllWindows()

def predict_video(path_video): print(path_video) if os.path.exists(path_video): print(“exist path video”) vid = cv2.VideoCapture(path_video)

while(True):
    ret, frame = vid.read() 
    if not ret:
        # print("no frame")
        break
    
    frame = process_img(frame)
    frame = cv2.resize(frame, (frame.shape[1]//2, frame.shape[0]//2))
    cv2.imshow("as", frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
    
vid.release()
cv2.destroyAllWindows() 

predict_video(‘Hog_2.mp4’) ` Hope it help you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

No Result - SoundCloud
Play No Result on SoundCloud and discover followers on SoundCloud | Stream tracks, albums, playlists on desktop and mobile.
Read more >
No-result Definition & Meaning | YourDictionary
(cricket) The result of a one-day match in which a side fails to complete their innings, or to bat the minimum number of...
Read more >
No Result designs, themes, templates and ... - Dribbble
No Result. Inspirational designs, illustrations, and graphic elements from the world's best designers. Want more inspiration? Browse our search results.
Read more >
What is another word for "with no result"? - WordHippo
Find 408 synonyms for "with no result" and other similar words that you can use instead based on 2 separate contexts from our...
Read more >
12 Awesome No Results Page Examples (+6 UI Design Best ...
In case of a “no results found” page, information about what happened is usually included in the spot that people often skip over,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found