当前位置:网站首页>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)
边栏推荐
- SSM integration preliminary details
- Nature communications - modeling armed conflict risk under climate change using machine learning and time series data
- 求组合数四种方法
- Ue5 small knowledge points geometry script modeling
- 恶意代码实战分析Lab05-01
- vivo大规模 Kubernetes 集群自动化运维实践
- Use of servers
- D evaluate twice map
- 宝塔中查看mysql默认密码
- 抖音如此重视直播销售外卖套餐,会不会是创业者巨大机会?
猜你喜欢

2021CCPC网络赛题解加总结

Experience of electric competition - program-controlled wind pendulum

ue5 小知识点 geometry script modeling

Brief request process

Vivo large scale kubernetes cluster automation operation and maintenance practice

Brief introduction to memory structure of virtual machine

Meta universe land: what makes digital real estate valuable

容斥原理(能被整除的数)

Nim game ladder Nim game and SG function application (set game)

St table learning
随机推荐
Determine the maximum match between bipartite graph and bipartite graph
Redis related
Wechat applet customer service automatic reply - PHP implementation
What is 400g Ethernet?
高斯消元求n元方程组
塔米狗知识|全面剖析国有企业并购含义及其作用
To vent their dissatisfaction with their superiors, Baidu post-95 programmers were sentenced to 9 months for deleting the database
St table learning
Multithreading starts from the lockless queue of UE4 (thread safe)
ST表学习
About instruction set bits and instruction architecture bits
【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
Acwing game 55
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
【TcaplusDB知识库】TcaplusDB常规单据介绍
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
Implementation of singleton mode
Ubuntu installs MySQL compressed package for future reference
Ue5 random point in bounding boxf from stream
Anonymity in Web3 and NFT