def test(): img_arr = read_image('demo.jpg') img = t.from_numpy(img_arr)[None] faster_rcnn = FasterRCNN() trainer = FasterRCNNTrainer(faster_rcnn).cuda() trainer.load('weights/chainer_best_model_converted_to_pytorch_0.7053.pth') opt.caffe_pretrain = True _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img, visualize=True) vis_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]).reshape(-1))
def predict(self, imgs=[]): imglist = [] for i in imgs: img_path = os.path.join(self.imgs_path, i) img = read_image(img_path) if self.isneed_enhance: img = Image_Enhance().api(img) img = t.from_numpy(img)[None] imglist.append(img) for index, img in enumerate(imglist): starttime = datetime.datetime.now() _bboxes, _labels, _scores = self.trainer.faster_rcnn.predict( img, visualize=True) endtime = datetime.datetime.now() print('predict time consum=%s' % round( (endtime-starttime).microseconds/1000000+(endtime-starttime).seconds, 6)) if self.imgs_vis_path: img_path = os.path.join(self.imgs_vis_path, imgs[index]) img = read_image(img_path) img = t.from_numpy(img)[None] ax = vis_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]).reshape(-1)) fig = ax.get_figure() fig.savefig("output.png")
def single_predict(self, img_path=''): starttime = datetime.datetime.now() img = read_image(img_path) img = t.from_numpy(img)[None] _bboxes, _labels, _scores = self.trainer.faster_rcnn.predict( img, visualize=True) endtime = datetime.datetime.now() print('predict time consum=%s' % round( (endtime-starttime).microseconds/1000000+(endtime-starttime).seconds, 6)) ax = vis_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]).reshape(-1)) fig = ax.get_figure() fig.savefig("output.png")
import os import torch as t from utils.config import opt from model import FasterRCNNVGG16 from trainer import FasterRCNNTrainer from data.util import read_image from utils.vis_tool import vis_bbox from utils import array_tool as at img = read_image('misc/demo.jpg') img = t.from_numpy(img)[None] faster_rcnn = FasterRCNNVGG16() trainer = FasterRCNNTrainer(faster_rcnn).cuda() trainer.load('./fasterrcnn_12222105_0.712649824453_caffe_pretrain.pth') opt.caffe_pretrain = False # this model was trained from torchvision-pretrained model _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img, visualize=True) vis_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]).reshape(-1))
# This can be removed once PyTorch 0.4.x is out. # See https://discuss.pytorch.org/t/question-about-rebuild-tensor-v2/14560 import torch._utils try: torch._utils._rebuild_tensor_v2 except AttributeError: def _rebuild_tensor_v2(storage, storage_offset, size, stride, requires_grad, backward_hooks): tensor = torch._utils._rebuild_tensor(storage, storage_offset, size, stride) tensor.requires_grad = requires_grad tensor._backward_hooks = backward_hooks return tensor torch._utils._rebuild_tensor_v2 = _rebuild_tensor_v2 #%% faster_rcnn = FasterRCNNVGG16() trainer = FasterRCNNTrainer(faster_rcnn).cuda() trainer.load('./checkpoints/fasterrcnn_09031352_0') opt.caffe_pretrain=True # this model was trained from caffe-pretrained model # Plot examples on training set dataset = RSNADataset(opt.root_dir) for i in range(0, len(dataset)): sample = dataset[i] img = sample['image'] ori_img_ = inverse_normalize(at.tonumpy(img)) # plot predicti bboxes _bboxes, _labels, _scores = trainer.faster_rcnn.predict([ori_img_], visualize=True) pred_img = vis_bbox(ori_img_, at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]))
from utils.vis_tool import vis_bbox from utils import array_tool as at from utils.average import AverageVal import numpy as np import matplotlib.pyplot as plt os.environ["CUDA_VISIBLE_DEVICES"] = "4" logger = AverageVal() faster_rcnn = FasterRCNNVGG16() trainer = FasterRCNNTrainer(faster_rcnn, logger).cuda() trainer.load("checkpoints/fasterrcnn_03090654_0.6984418117245029") for i in range(1, 192): img_path = "movie/input/{}.jpg".format(i) img = read_image(img_path) img = t.from_numpy(img)[None] opt.caffe_pretrain = False # this model was trained from caffe-pretrained model _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img, visualize=True) output_path = "movie/output/{}.jpg".format(i) vis_bbox( at.tonumpy(img[0]), at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]).reshape(-1), figsize=(15, 8.5), ) plt.axis("off") plt.savefig(output_path)
import os import torch as t from utils.config import Config from model import FasterRCNNVGG16 from trainer import FasterRCNNTrainer from data.util import read_image from utils.vis_tool import vis_bbox from utils import array_tool as at %matplotlib inline img_name = 'demo.jpg' raw_img = read_image(f'/content/drive/My Drive/lq_det_hyper/lq_det/misc/{img_name}') raw_img = t.from_numpy(raw_img).unsqueeze(dim=0) faster_rcnn = FasterRCNNVGG16() trainer = FasterRCNNTrainer(faster_rcnn, using_visdom=False).cuda() trainer.load('/content/drive/My Drive/lq_det_hyper/lq_det/ckpt/fasterrcnn_12222105_0.712649824453_caffe_pretrain.pth') Config.caffe_vgg=True # this model was trained from caffe-pretrained model _bboxes, _labels, _scores = trainer.faster_rcnn.predict(raw_img, visualize=True) img, bbox, label, score = (at.tonumpy(raw_img[0]), at.tonumpy(_bboxes[0]), at.tonumpy(_labels[0]).reshape(-1), at.tonumpy(_scores[0]).reshape(-1)) vis_bbox(img, bbox, label, score) import matplotlib.pyplot as plt plt.show()