566
社区成员
发帖
与我相关
我的任务
分享
1 import torchvision.models as models
2 resnet18 = models.resnet18(pretrained=True)
3 alexnet = models.alexnet(pretrained=True)
4 squeezenet = models.squeezenet1_0(pretrained=True)
5 vgg16 = models.vgg16(pretrained=True)
6 densenet = models.densenet161(pretrained=True)
7 inception = models.inception_v3(pretrained=True)
8 googlenet = models.googlenet(pretrained=True)
9 shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
10 mobilenet = models.mobilenet_v2(pretrained=True)
11 resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
12 wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)
13 mnasnet = models.mnasnet1_0(pretrained=True)
model = torchvision.models.resnet18(pretrained=True).eval()
dummy_input = torch.randn((1, 3, 224, 224))
torch.onnx.export(model, dummy_input, "resnet18.onnx")
python mo_onnx.py --input_model D:\python\pytorch_tutorial\resnet18.onnx
from __future__ import print_function
import cv2
import numpy as np
import logging as log
from openvino.inference_engine import IECore
with open('imagenet_classes.txt') as f:
labels = [line.strip() for line in f.readlines()]
def image_classification():
model_xml = "resnet18.xml"
model_bin = "resnet18.bin"
# Plugin initialization for specified device and load extensions library if specified
log.info("Creating Inference Engine")
ie = IECore()
# Read IR
log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
net = ie.read_network(model=model_xml, weights=model_bin)
log.info("Preparing input blobs")
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
# Read and pre-process input images
n, c, h, w = net.inputs[input_blob].shape
images = np.ndarray(shape=(n, c, h, w))
src = cv2.imread("D:/images/messi.jpg")
image = cv2.resize(src, (w, h))
image = np.float32(image) / 255.0
image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
image = image.transpose((2, 0, 1))
# Loading model to the plugin
log.info("Loading model to the plugin")
exec_net = ie.load_network(network=net, device_name="CPU")
# Start sync inference
log.info("Starting inference in synchronous mode")
res = exec_net.infer(inputs={input_blob: [image]})
# Processing output blob
log.info("Processing output blob")
res = res[out_blob]
label_index = np.argmax(res, 1)
label_txt = labels[label_index[0]]
cv2.putText(src, label_txt, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 255), 2, 8)
cv2.imshow("ResNet18-from Pytorch image classification", src)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
image_classification()