当前位置:网站首页>Model building process 1==miidock
Model building process 1==miidock
2022-06-13 11:08:00 【Firefly】
Introduce modules
import torch
import torch.nn as nn
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
Define a network model
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 3, 3, padding=(0, 0), bias=False)
def forward(self, x):
x = self.conv1(x)
return x
net = Net()
Define convolution weights
#sobel Full edge detection operator
conv_rgb_core_sobel = [
[[-1,-1,-1],[-1,8,-1], [-1, -1, -1],
[0,0,0],[0,0,0], [0,0,0],
[0,0,0],[0,0,0], [0,0,0]
],
[[0,0,0],[0,0,0], [0,0,0],
[-1,-1,-1],[-1,8,-1], [-1, -1, -1],
[0,0,0],[0,0,0], [0,0,0]
],
[[0,0,0],[0,0,0], [0,0,0],
[0,0,0],[0,0,0], [0,0,0],
[-1,-1,-1],[-1,8,-1], [-1, -1, -1],
]]
#sobel Vertical edge detection operator
conv_rgb_core_sobel_vertical = [
[[-1,0,1],[-2,0,2], [-1, 0, 1],
[0,0,0],[0,0,0], [0,0,0],
[0,0,0],[0,0,0], [0,0,0]
],
[[0,0,0],[0,0,0], [0,0,0],
[-1,0,1],[-2,0,2], [-1, 0, 1],
[0,0,0],[0,0,0], [0,0,0]
],
[[0,0,0],[0,0,0], [0,0,0],
[0,0,0],[0,0,0], [0,0,0],
[-1,0,1],[-2,0,2], [-1, 0, 1],
]]
#sobel Horizontal edge detection operator
conv_rgb_core_sobel_horizontal = [
[[1,2,1],[0,0,0], [-1, -2, -1],
[0,0,0],[0,0,0], [0,0,0],
[0,0,0],[0,0,0], [0,0,0]
],
[[0,0,0],[0,0,0], [0,0,0],
[1,2,1],[0,0,0], [-1, -2, -1],
[0,0,0],[0,0,0], [0,0,0]
],
[[0,0,0],[0,0,0], [0,0,0],
[0,0,0],[0,0,0], [0,0,0],
[1,2,1],[0,0,0], [-1, -2, -1],
]]
Network load weight function
def sobel(net, kernel):
sobel_kernel = np.array(kernel, dtype='float32')
sobel_kernel = sobel_kernel.reshape((3, 3, 3, 3))
net.conv1.weight.data = torch.from_numpy(sobel_kernel)
params = list(net.parameters())
Open a picture
pil_img = Image.open("../yarn_2.jpg")
#display(pil_img)
input_img = np.array(pil_img)
print(input_img.shape)
Image normalization
input_tensor = (input_img.astype(np.float32) - 127.5) / 128 # to [-1, 1]
print(input_tensor.shape)
input_tensor = torch.Tensor(input_tensor).permute((2, 0, 1))
input_tensor = input_tensor.unsqueeze(0)
print("input shape:", input_tensor.shape)
The input picture is converted to PyTorch The tensor has a format
input_tensor = (input_img.astype(np.float32) - 127.5) / 128 # to [-1, 1]
input_tensor = torch.Tensor(input_tensor).permute((2, 0, 1))
print(input_tensor.shape)
input_tensor = input_tensor.unsqueeze(0)
print("input shape:", input_tensor.shape)
Model reasoning
global sobel_img_t
global sobel_vertical_img_t
global sobel_horizontal_img_t
sobel_img_t = None
sobel_vertical_img_t = None
sobel_horizontal_img_t = None
# Load network weights
sobel(net, conv_rgb_core_sobel_vertical)
# Run the network in reasoning mode
with torch.no_grad():
out = net(input_tensor)
sobel_vertical_img_t = out.numpy()[0].transpose([1, 2, 0])
# Load network weights
sobel(net, conv_rgb_core_sobel_horizontal)
Run the network in reasoning mode
with torch.no_grad():
out = net(input_tensor)
sobel_horizontal_img_t = out.numpy()[0].transpose([1, 2, 0])
# Load network weights
sobel(net, conv_rgb_core_sobel)
# Run the network in reasoning mode
with torch.no_grad():
out = net(input_tensor)
sobel_img_t = out.numpy()[0].transpose([1, 2, 0])
# printout
plt.figure()
plt.subplot(1, 5, 1)
plt.imshow(input_img)
plt.subplot(1, 5, 2)
plt.imshow(sobel_img_t)
plt.subplot(1, 5, 3)
plt.imshow(sobel_vertical_img_t)
plt.subplot(1, 5, 4)
plt.imshow(sobel_horizontal_img_t)
plt.subplot(1, 5, 5)
out = np.sqrt(np.square(sobel_vertical_img_t) + np.square(sobel_horizontal_img_t))
plt.imshow(out)
plt.show()
export onnx The Internet
with torch.no_grad():
torch.onnx.export(net,input_tensor,"./model.onnx",
export_params=True,input_names=["input0"],
output_names=["output0"])
print(" Export network complete ")
Use ncnn Tools will onnx Network switch ncnn The Internet
# The following code invokes the... In the user environment ncnn Tools , Please make sure that you have installed and added environment variables
#ncnn After compilation , stay build/tools/onnx An executable file will be generated in onnx2ncnn
# Execute commands at the terminal ./onnx2ncnn mobilenetv2.onnx mobilenetv2.param mobilenetv2.bin
def onnx_to_ncnn(input_shape,
onnx="out/model.onnx",
ncnn_param="out/conv0.param",
ncnn_bin="out/conv0.bin"):
import os
cmd=f"onnx2ncnn {
onnx} {
ncnn_param} {
ncnn_bin}"# The tool catalog can be changed
os.system(cmd)
with open(ncnn_param) as f:
content=f.read().split("\n")
if len(input_shape)==1:
content[2]+="0={}".format(input_shape[0])
else:
content[2]+="0={} 1={} 2={}".format(input_shape[2],input_shape[1],input_shape[0])
content="\n".join(content)
with open(ncnn_param,"w") as f:
f.write(content)
onnx_to_ncnn(input_shape=(3,224,224),
onnx="./model.onnx",
ncnn_param="./conv0.param",
ncnn_bin="./conv0.bin")
print("net success!")
Go to maxihub Quantitative models (int8)
Edge detection model deployment
#v831 Run edge detection code
from maix import nn, camera, display, image
import numpy as np
import time
model = {
"param": "./sobel_int8.param",
"bin": "./sobel_int8.bin"
}
input_size = (224, 224, 3)
output_size = (222, 222, 3)
options = {
"model_type": "awnn",
"inputs": {
"input0": input_size
},
"outputs": {
"output0": output_size
},
"mean": [127.5, 127.5, 127.5],
"norm": [0.0078125, 0.0078125, 0.0078125],
}
print("-- load model:", model)
m = nn.load(model, opt=options)
print("-- load ok")
while True:
img = camera.capture().resize(224,224)
out = m.forward(img, quantize=True, layout="hwc")
out, = out.astype(np.float32).reshape(output_size)
out = (np.ndarray.__abs__(out) * 255 / out.max()).astype(np.uint8)
data = out.tobytes()
img2 = img.load(data,(222, 222), mode="RGB")
display.show(img2)
边栏推荐
- F2. nearest beautiful number (hard version)
- socket编程(上)
- 【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
- 2020 ICPC Asia Taiwan Online Programming Contest C Circles
- Go zero microservice Practice Series (III. API definition and table structure design)
- Digital DP example
- 塔米狗知识|全面剖析国有企业并购含义及其作用
- Vivo large scale kubernetes cluster automation operation and maintenance practice
- Ubuntu installs MySQL compressed package for future reference
- About instruction set bits and instruction architecture bits
猜你喜欢
Flutter simple and excellent open source dialog uses free_ dialog
vivo大规模 Kubernetes 集群自动化运维实践
状态压缩DP例题(旅行商问题和填矩形问题)
socket编程(中)
What is 400g Ethernet?
Database learning notes (Chapter 15)
Pagoda access changed from IP to domain name
Vivo large scale kubernetes cluster automation operation and maintenance practice
Ue5 random point in bounding boxf from stream
领导说要明天上线,这货压根不知道开发流程
随机推荐
Implementation of singleton mode
Alibaba's employees decreased by 4000 in the first quarter; Programmers wrote scripts to hang up vaccine numbers and were arrested for making a profit of 400000 yuan; Sohu encounters epic email fraud,
[tool chain series] Notepad++
Brief description of redo logs and undo logs in MySQL
Similarities and differences between commonAPI and AUTOSAR AP communication management
Gauss elimination for solving N-element equations
【TcaplusDB知识库】Tmonitor系统升级介绍
C file package and download
ACP | 东北地理所在气象-空气质量双向耦合模式研究中取得进展
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
Talk about MySQL indexing mechanism
Private computing fat core concepts and stand-alone deployment
Nim game ladder Nim game and SG function application (set game)
Analysis and summary of 2021ccpc online games
St table learning
Vivo large scale kubernetes cluster automation operation and maintenance practice
作为一个测试人员,这些基础知识必不可少
COM的模式变化引起的IPdu Handling【接收截止日期监控】
【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
22、ADS使用记录之E类功放设计(下)