当前位置:网站首页>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)
边栏推荐
- Go zero microservice Practice Series (III. API definition and table structure design)
- 状态压缩DP例题(旅行商问题和填矩形问题)
- Meta universe land: what makes digital real estate valuable
- 报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
- ST表学习
- Redis related
- SSM integration preliminary details
- Codeforces Round #798 (Div. 2)ABCD
- Necessary for Architects: system capacity status checklist
- 为发泄对上司不满,百度95后程序员删库被判9个月
猜你喜欢
服务器的使用
为发泄对上司不满,百度95后程序员删库被判9个月
Codeforces Round #798 (Div. 2)ABCD
很妙的贪心(F2. Nearest Beautiful Number (hard version))
数据库学习笔记(第十五章)
领导说要明天上线,这货压根不知道开发流程
Digital DP example
State compression DP example (traveling salesman problem and rectangle filling problem)
Analysis and summary of 2021ccpc online games
【TcaplusDB知识库】TcaplusDB机型管理介绍
随机推荐
【TcaplusDB知识库】Tmonitor系统升级介绍
Flutter simple and excellent open source dialog uses free_ dialog
vivo大规模 Kubernetes 集群自动化运维实践
2021CCPC网络赛榜单
硬件工程师薪资虚高,你认可吗?
求组合数四种方法
Euler function and finding Euler function by linear sieve
Count the number of special subsequences (0, 1, 2) DP
第七章 文件管理作业
Web3 系统构建:去中心化的原则、模型和方法(上)
Prim求最小生成树(朴素版稠密图)
Necessary for Architects: system capacity status checklist
Nim游戏阶梯 Nim游戏和SG函数应用(集合游戏)
2021ccpc online competition list
Folder data synchronization tool sync folders Pro
日志1111
WinForm resolves frequent refresh of black screen
C file package and download
As a tester, these basic knowledge are essential
ARM64 上的性能怪兽:API 网关 Apache APISIX 在 AWS Graviton3 上的安装和性能测试