tags: RKNN pytorch Target Detection


def export_pytorch_model():
net = models.resnet18(pretrained=True)#E NET
net.eval()
trace_model = torch.jit.trace(net, torch.Tensor(1,3,224,224))#Tensor also needs to be modified according to your own model
trace_model.save('./resnet18.pt')
model_path = 'Weight file path (.pth)'
label_path = 'Data set label path'
class_names = [name.strip() for name in open(label_path).readlines()]
export_pytorch_model(model_path,class_names)
def export_pytorch_model(model_path,class_names):
net = create_mobilenetv1_ssd_lite_025extra(len(class_names), width_mult=0.25 ,is_test=True)
device = torch.device("cpu")
net=net.to(device)
net.load(model_path)
net.eval()
example = torch.rand(1, 3, 300, 300)#Input size
example = example.to(device)
trace_model = torch.jit.trace(net, example)#This function does not support the TORCH <1.6, my TORCH version is 1.6
trace_model.save('./models/ssd.pt')#save route
if __name__ == '__main__':
model_path = 'Weight path'
label_path = 'Tag path'
class_names = [name.strip() for name in open(label_path).readlines()]
export_pytorch_model(model_path,class_names)
model = 'models/ssd.pt'
input_size_list = [[3,300,300]]#Input model only accepts fixed size input
# Create RKNN object
rknn = RKNN()
# pre-process config
print('--> config model')
rknn.config(mean_values=[[0,0,0]], std_values=[[255,255,255]], reorder_channel='0 1 2')
#For the input data is the three -channel (CIN0, CIN1, CIN2) data, after the pre -processing, the output data is (COUT0, COUT1, COUT2). The calculation process is as follows:
#COUT0 = (CIN0 -M0)/S0, COUT1 = (CIN1 -M1)/S0, COUT2 = (CIN2 -M2)/S0, where Mean_values = [[m0, m1, m2]]]
#Std_values = [[s0, s1, s2]]. Generally, use mean_values = [[0,0,0]], std_values = [[255, 255,255]] to be normalized to [0, 1]
#Between mean_values = [[0,0,0]], std_values = [[128, 128,128]] to normalize the input data to [-1, 1]; in other cases you need to calculate it yourself! Intersection Intersection Intersection
#Reorner_Channel: It means whether to adjust the order of the image channel. ‘0 1 2’ indicates that according to the input pass
#In order to reason, such as RGB when entering the picture input, then the input layer is passed to the input layer according to the order of the RGB;
#1 0 'means that the input will be transformed into a channel. For example, the passage sequence of the channel is RGB during the input.
#Profinned to the input layer. Similarly, the order of the channel when the input is BGR will be converted to RGB and then passed to the input layer.
print('done')
# Load pytorch model
print('--> Loading model')
ret = rknn.load_pytorch(model=model, input_size_list=input_size_list)
if ret != 0:
print('Load pytorch model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=False)
if ret != 0:
print('Build pytorch failed!')
exit(ret)
print('done')
# Export rknn model
print('--> Export RKNN model')
ret = rknn.export_rknn('models/ssd.rknn')
if ret != 0:
print('Export ssd.rknn failed!')
exit(ret)
print('Export ssd.rknn success!')
rknn.release()
python -m rknn.bin.visualization


import numpy as np
from rknn.api import RKNN
import torch
from vision.ssd.mobilenetv1_ssd_lite_025extra import create_mobilenetv1_ssd_lite_025extra#Network path
def export_pytorch_model(model_path,class_names):
net = create_mobilenetv1_ssd_lite_025extra(len(class_names), width_mult=0.25 ,is_test=True)
device = torch.device("cpu")
net=net.to(device)
net.load(model_path)
net.eval()
example = torch.rand(1, 3, 300, 300)
example = example.to(device)
trace_model = torch.jit.trace(net, example)
trace_model.save('./models/ssd.pt')
def show_outputs(output):
output_sorted = sorted(output, reverse=True)
top5_str = '\n-----TOP 5-----\n'
for i in range(5):
value = output_sorted[i]
index = np.where(output == value)
for j in range(len(index)):
if (i + j) >= 5:
break
if value > 0:
topi = '{}: {}\n'.format(index[j], value)
else:
topi = '-1: 0.0\n'
top5_str += topi
print(top5_str)
def show_perfs(perfs):
perfs = 'perfs: {}\n'.format(perfs)
print(perfs)
def softmax(x):
return np.exp(x)/sum(np.exp(x))
if __name__ == '__main__':
model_path = 'models/ws0.25tmax400extra0.25/mb1-ssd-lite-025extra-Epoch-399-Loss-3.2293308803013394.pth'
label_path = 'models/voc-model-labels.txt'
class_names = [name.strip() for name in open(label_path).readlines()]
export_pytorch_model(model_path,class_names)
model = 'models/ssd.pt'
input_size_list = [[3,300,300]]#Input model only accepts fixed size input
# Create RKNN object
rknn = RKNN()
# pre-process config
print('--> config model')
rknn.config(mean_values=[[0,0,0]], std_values=[[255,255,255]], reorder_channel='0 1 2')
print('done')
# Load pytorch model
print('--> Loading model')
ret = rknn.load_pytorch(model=model, input_size_list=input_size_list)
if ret != 0:
print('Load pytorch model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=False)
if ret != 0:
print('Build pytorch failed!')
exit(ret)
print('done')
# Export rknn model
print('--> Export RKNN model')
ret = rknn.export_rknn('models/ssd.rknn')
if ret != 0:
print('Export ssd.rknn failed!')
exit(ret)
print('Export ssd.rknn success!')
rknn.release()
xport rknn model
print(‘–> Export RKNN model’)
ret = rknn.export_rknn(‘models/ssd.rknn’)
if ret != 0:
print(‘Export ssd.rknn failed!’)
exit(ret)
print(‘Export ssd.rknn success!’)
rknn.release()
Introduction After the SSD model came out in 2015, the development of end-to-end target detection models was popular. Subsequent Yolo v2 has taken its application in the visual field of the industry t...
Target detection consists of two independent tasks, ie classification and positioning. The R-CNN series target detector consists of two phases, namely the regional proposal network and classification ...
SE-SSD 3D Target Detection Model Adaptation 1, environmental adaptation 2, data preparation Data set download God link Data set preparation: DET3D environment installation Kitti data set ready PS (ver...
Detailed introduction to SSD target detection network model Detailed introduction and summary of R-CNN, Fast RCNN and Faster RCNN algorithms Detailed introduction and summary of YOLO series (YOLOv1-YO...
SSD motivation The one stage detection method after YOLOv1 wants to solve the problem of insufficient recall of small targets in the single stage of Yolo, and then there is SSD. Method summary SSD use...
Transformation through rknn's own graphical interface Quantification When reasoning, the RKNN model will standardize the input data, that is, this is not to change the data. Since the input is not a g...
Turn RKNN model memo ...
1. First download the YOLOV4 model file,click to download 2, prepare the data set Create a Projects / YOLO folder in the same-level directory that downloads well in the YOLOV4 folder, then put your ow...
In this tutorial, we will fine-tune the pre-trained Mask R-CNN models that have been trained in Penn-Fudan database. It contains 170 images and 345 examples. We will use it to illustrate how to use ne...
Case 1: Test only This case is suitable for you just want to test the SSD, use a few of your own graphs to test, get the results, and do not train the model yourself. Code 1 Instructions: My environme...