Skip to content
This repository was archived by the owner on Mar 1, 2022. It is now read-only.

Commit 1d50d02

Browse files
committed
version 2.0.2
1 parent e5be508 commit 1d50d02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5131
-9427
lines changed

caffe2onnx/proto/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
import os
2-
import importlib
3-
4-
5-
def import_caffe_pb2(caffe_proto_name):
6-
caffe_pb2 = importlib.import_module("proto.%s_pb2"%caffe_proto_name)
7-
return caffe_pb2

caffe2onnx/proto/caffe.proto

Lines changed: 0 additions & 1449 deletions
This file was deleted.

caffe2onnx/proto/caffe_pb2.py

Lines changed: 0 additions & 5964 deletions
This file was deleted.

caffe2onnx/proto/caffe_upsample.proto

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,20 @@ message LayerParameter {
422422
optional ThresholdParameter threshold_param = 128;
423423
optional TileParameter tile_param = 138;
424424
optional WindowDataParameter window_data_param = 129;
425+
optional InterpParameter interp_param = 166;
426+
optional ShuffleChannelParameter shuffle_channel_param = 164;
427+
optional PermuteParameter permute_param = 202;
428+
optional PriorBoxParameter prior_box_param = 203;
429+
optional DetectionOutputParameter detection_output_param = 204;
430+
optional DetectionEvaluateParameter detection_evaluate_param = 205;
431+
optional NormalizeParameter norm_param = 206;
432+
optional AxpyParameter axpy_param = 151;
433+
optional ReLU6Parameter relu6_param = 100000;
434+
}
435+
436+
437+
message ShuffleChannelParameter {
438+
optional uint32 group = 1[default = 1]; // The number of group
425439
}
426440

427441
// Message that stores parameters used to apply transformation
@@ -1456,3 +1470,207 @@ message PReLUParameter {
14561470
// Whether or not slope parameters are shared across channels.
14571471
optional bool channel_shared = 2 [default = false];
14581472
}
1473+
1474+
message ReLU6Parameter {
1475+
optional float negative_slope = 1 [default = 0];
1476+
}
1477+
1478+
message InterpParameter {
1479+
optional int32 height = 1 [default = 0]; // Height of output
1480+
optional int32 width = 2 [default = 0]; // Width of output
1481+
optional int32 zoom_factor = 3 [default = 1]; // zoom factor
1482+
optional int32 shrink_factor = 4 [default = 1]; // shrink factor
1483+
optional int32 pad_beg = 5 [default = 0]; // padding at begin of input
1484+
optional int32 pad_end = 6 [default = 0]; // padding at end of input
1485+
}
1486+
1487+
message PermuteParameter {
1488+
// The new orders of the axes of data. Notice it should be with
1489+
// in the same range as the input data, and it starts from 0.
1490+
// Do not provide repeated order.
1491+
repeated uint32 order = 1;
1492+
}
1493+
1494+
message PriorBoxParameter {
1495+
// Encode/decode type.
1496+
enum CodeType {
1497+
CORNER = 1;
1498+
CENTER_SIZE = 2;
1499+
CORNER_SIZE = 3;
1500+
}
1501+
// Minimum box size (in pixels). Required!
1502+
repeated float min_size = 1;//对应论文2.2节中公式(4)中的sk×网络输入层输入图像[data层的输入]大小
1503+
// Maximum box size (in pixels). Required!
1504+
repeated float max_size = 2;
1505+
// Various of aspect ratios. Duplicate ratios will be ignored.
1506+
// If none is provided, we use default ratio 1.
1507+
repeated float aspect_ratio = 3; // 等宽比
1508+
// If true, will flip each aspect ratio.
1509+
// For example, if there is aspect ratio "r",
1510+
// we will generate aspect ratio "1.0/r" as well.
1511+
optional bool flip = 4 [default = true]; // 是否反转等宽比
1512+
// If true, will clip the prior so that it is within [0, 1]
1513+
optional bool clip = 5 [default = false]; // 是否进行裁剪,是否保证默认框整个在网络输入层输入图像内)
1514+
// Variance for adjusting the prior bboxes.
1515+
repeated float variance = 6;
1516+
// By default, we calculate img_height, img_width, step_x, step_y based on
1517+
// bottom[0] (feat) and bottom[1] (img). Unless these values are explicitely
1518+
// provided.
1519+
// Explicitly provide the img_size.
1520+
optional uint32 img_size = 7;
1521+
// Either img_size or img_h/img_w should be specified; not both.
1522+
optional uint32 img_h = 8;//网络输入层输入图像的高(或自行设置的高度)
1523+
optional uint32 img_w = 9;//网络输入层输入图像的宽(或自行设置的高度)
1524+
1525+
// Explicitly provide the step size.
1526+
optional float step = 10;
1527+
// Either step or step_h/step_w should be specified; not both.
1528+
optional float step_h = 11;
1529+
optional float step_w = 12;
1530+
1531+
// Offset to the top left corner of each cell.
1532+
optional float offset = 13 [default = 0.5];
1533+
}
1534+
1535+
// Message that store parameters used by DetectionOutputLayer
1536+
message DetectionOutputParameter {
1537+
// Number of classes to be predicted. Required!
1538+
optional uint32 num_classes = 1;
1539+
// If true, bounding box are shared among different classes.
1540+
optional bool share_location = 2 [default = true];
1541+
// Background label id. If there is no background class,
1542+
// set it as -1.
1543+
optional int32 background_label_id = 3 [default = 0];
1544+
// Parameters used for non maximum suppression.
1545+
optional NonMaximumSuppressionParameter nms_param = 4;
1546+
// Parameters used for saving detection results.
1547+
optional SaveOutputParameter save_output_param = 5;
1548+
// Type of coding method for bbox.
1549+
optional PriorBoxParameter.CodeType code_type = 6 [default = CORNER];
1550+
// If true, variance is encoded in target; otherwise we need to adjust the
1551+
// predicted offset accordingly.
1552+
optional bool variance_encoded_in_target = 8 [default = false];
1553+
// Number of total bboxes to be kept per image after nms step.
1554+
// -1 means keeping all bboxes after nms step.
1555+
optional int32 keep_top_k = 7 [default = -1];
1556+
// Only consider detections whose confidences are larger than a threshold.
1557+
// If not provided, consider all boxes.
1558+
optional float confidence_threshold = 9;
1559+
// If true, visualize the detection results.
1560+
optional bool visualize = 10 [default = false];
1561+
// The threshold used to visualize the detection results.
1562+
optional float visualize_threshold = 11;
1563+
// If provided, save outputs to video file.
1564+
optional string save_file = 12;
1565+
}
1566+
1567+
// Message that store parameters used by DetectionEvaluateLayer
1568+
message DetectionEvaluateParameter {
1569+
// Number of classes that are actually predicted. Required!
1570+
optional uint32 num_classes = 1;
1571+
// Label id for background class. Needed for sanity check so that
1572+
// background class is neither in the ground truth nor the detections.
1573+
optional uint32 background_label_id = 2 [default = 0];
1574+
// Threshold for deciding true/false positive.
1575+
optional float overlap_threshold = 3 [default = 0.5];
1576+
// If true, also consider difficult ground truth for evaluation.
1577+
optional bool evaluate_difficult_gt = 4 [default = true];
1578+
// A file which contains a list of names and sizes with same order
1579+
// of the input DB. The file is in the following format:
1580+
// name height width
1581+
// ...
1582+
// If provided, we will scale the prediction and ground truth NormalizedBBox
1583+
// for evaluation.
1584+
optional string name_size_file = 5;
1585+
// The resize parameter used in converting NormalizedBBox to original image.
1586+
optional ResizeParameter resize_param = 6;
1587+
}
1588+
1589+
message ResizeParameter {
1590+
//Probability of using this resize policy
1591+
optional float prob = 1 [default = 1];
1592+
1593+
enum Resize_mode {
1594+
WARP = 1;
1595+
FIT_SMALL_SIZE = 2;
1596+
FIT_LARGE_SIZE_AND_PAD = 3;
1597+
}
1598+
optional Resize_mode resize_mode = 2 [default = WARP];
1599+
optional uint32 height = 3 [default = 0];
1600+
optional uint32 width = 4 [default = 0];
1601+
// A parameter used to update bbox in FIT_SMALL_SIZE mode.
1602+
optional uint32 height_scale = 8 [default = 0];
1603+
optional uint32 width_scale = 9 [default = 0];
1604+
1605+
enum Pad_mode {
1606+
CONSTANT = 1;
1607+
MIRRORED = 2;
1608+
REPEAT_NEAREST = 3;
1609+
}
1610+
// Padding mode for BE_SMALL_SIZE_AND_PAD mode and object centering
1611+
optional Pad_mode pad_mode = 5 [default = CONSTANT];
1612+
// if specified can be repeated once (would fill all the channels)
1613+
// or can be repeated the same number of times as channels
1614+
// (would use it them to the corresponding channel)
1615+
repeated float pad_value = 6;
1616+
1617+
enum Interp_mode { //Same as in OpenCV
1618+
LINEAR = 1;
1619+
AREA = 2;
1620+
NEAREST = 3;
1621+
CUBIC = 4;
1622+
LANCZOS4 = 5;
1623+
}
1624+
//interpolation for for resizing
1625+
repeated Interp_mode interp_mode = 7;
1626+
}
1627+
1628+
message NonMaximumSuppressionParameter {
1629+
// Threshold to be used in nms.
1630+
optional float nms_threshold = 1 [default = 0.3];
1631+
// Maximum number of results to be kept.
1632+
optional int32 top_k = 2;
1633+
// Parameter for adaptive nms.
1634+
optional float eta = 3 [default = 1.0];
1635+
}
1636+
1637+
message SaveOutputParameter {
1638+
// Output directory. If not empty, we will save the results.
1639+
optional string output_directory = 1;
1640+
// Output name prefix.
1641+
optional string output_name_prefix = 2;
1642+
// Output format.
1643+
// VOC - PASCAL VOC output format.
1644+
// COCO - MS COCO output format.
1645+
optional string output_format = 3;
1646+
// If you want to output results, must also provide the following two files.
1647+
// Otherwise, we will ignore saving results.
1648+
// label map file.
1649+
optional string label_map_file = 4;
1650+
// A file which contains a list of names and sizes with same order
1651+
// of the input DB. The file is in the following format:
1652+
// name height width
1653+
// ...
1654+
optional string name_size_file = 5;
1655+
// Number of test images. It can be less than the lines specified in
1656+
// name_size_file. For example, when we only want to evaluate on part
1657+
// of the test images.
1658+
optional uint32 num_test_image = 6;
1659+
// The resize parameter used in saving the data.
1660+
optional ResizeParameter resize_param = 7;
1661+
}
1662+
1663+
message AxpyParameter {
1664+
1665+
}
1666+
1667+
// Message that stores parameters used by NormalizeLayer
1668+
message NormalizeParameter {
1669+
optional bool across_spatial = 1 [default = true];
1670+
// Initial value of scale. Default is 1.0 for all
1671+
optional FillerParameter scale_filler = 2;
1672+
// Whether or not scale parameters are shared across channels.
1673+
optional bool channel_shared = 3 [default = true];
1674+
// Epsilon for not dividing by zero while normalizing variance
1675+
optional float eps = 4 [default = 1e-10];
1676+
}

caffe2onnx/proto/caffe_upsample_pb2.py

Lines changed: 2679 additions & 1361 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

caffe2onnx/src/OPs/Add.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import caffe2onnx.src.c2oObject as Node
2+
3+
4+
def get_add_output_shape(input_shape):
5+
output_shape = input_shape[0]
6+
return [output_shape]
7+
8+
9+
def create_add_node(layer, node_name, input_name, output_name, input_shape):
10+
output_shape = get_add_output_shape(input_shape)
11+
12+
node = Node.c2oNode(layer, node_name, 'Add', input_name, output_name, input_shape, output_shape)
13+
14+
return node

caffe2onnx/src/OPs/Axpy.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import caffe2onnx.src.c2oObject as Node
2+
from typing import *
3+
import copy
4+
5+
6+
def need_add_reshape(input_shape: List[List]) -> bool:
7+
return len(input_shape[0]) != len(input_shape[1])
8+
9+
10+
def get_param_shape(input_shape: List[List]) -> List:
11+
input = input_shape[0]
12+
scale = copy.deepcopy(input_shape[1])
13+
if len(input) > len(scale):
14+
for i in range(len(input) - len(scale)):
15+
scale.append(1)
16+
return scale
17+
18+
def broadcast_scale(input_shape: List[List]) -> List[List]:
19+
input = input_shape[0]
20+
scale = input_shape[1]
21+
if len(input) > len(scale):
22+
for i in range(len(input) - len(scale)):
23+
scale.append(1)
24+
broadcast_shape = [input, scale]
25+
elif len(input) < len(scale):
26+
print("the scale should be less than input")
27+
exit(-1)
28+
else:
29+
broadcast_shape = [input, scale]
30+
return broadcast_shape
31+
32+
33+
def get_mul_output_shape(input_shape: List[List]) -> List[List]:
34+
output_shape = input_shape[1]
35+
return [output_shape]
36+
37+
38+
def create_axpy_mul_node(layer, node_name, input_name, output_name, input_shape):
39+
40+
new_node_name = node_name + "_middle"
41+
output_shape = get_mul_output_shape(input_shape)
42+
new_input_name = [input_name[0], input_name[1]]
43+
new_output_name = [output_name[0] + "_mul"]
44+
new_input_shape = [input_shape[0], input_shape[1]]
45+
46+
node = Node.c2oNode(layer, new_node_name, 'Mul', new_input_name, new_output_name, new_input_shape, output_shape)
47+
48+
return node
49+
50+
def get_add_output_shape(input_shape):
51+
52+
output_shape = input_shape[1]
53+
54+
return [output_shape]
55+
56+
def create_axpy_add_node(layer, node_name, input_name, output_name, input_shape):
57+
58+
output_shape = get_add_output_shape(input_shape)
59+
new_input_name = [node_name + "_mul", input_name[2]]
60+
new_input_shape = [input_shape[1], input_shape[2]]
61+
node = Node.c2oNode(layer, node_name, "Add", new_input_name, output_name, input_shape, output_shape)
62+
63+
return node

caffe2onnx/src/OPs/BatchNorm.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import caffe2onnx.src.c2oObject as Node
22

3-
##-----------------------------BatchNormalization layer = BatchNorm + Scale-------------------------------------##
4-
# Get hyperparameters
3+
54
def getBNAttri(layer):
6-
# Hyperparameter dictionary
7-
8-
dict = {"epsilon": layer.batch_norm_param.eps, # Slip coefficient
9-
"momentum": layer.batch_norm_param.moving_average_fraction
5+
eps = layer.batch_norm_param.eps
6+
momentum = layer.batch_norm_param.moving_average_fraction
7+
dict = {"epsilon": eps,
8+
"momentum": momentum
109
}
1110
return dict
1211

13-
# Calculate the output dimension
12+
1413
def getBNOutShape(input_shape):
1514
output_shape = input_shape
1615
return output_shape
1716

18-
# Build node
17+
1918
def createBN(layer, nodename, inname, outname, input_shape):
2019
dict = getBNAttri(layer)
21-
# Calculate output_shape, the output dimension is equal to the input dimension
2220
output_shape = getBNOutShape(input_shape)
23-
24-
# Build node
2521
node = Node.c2oNode(layer, nodename, "BatchNormalization", inname, outname, input_shape, output_shape,dict)
26-
return node
22+
return node

caffe2onnx/src/OPs/Clip.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import caffe2onnx.src.c2oObject as Node
2+
3+
4+
def get_attribute(layer):
5+
attributes = {}
6+
max_attribute = 0
7+
min_attribute = 0
8+
if layer.type == 'ReLU6':
9+
max_attribute = 6.0
10+
min_attribute = 0
11+
12+
attribute = {
13+
'max': max_attribute,
14+
'min': min_attribute
15+
}
16+
return attributes
17+
18+
19+
def get_clip_output_shape(input_shape):
20+
output_shape = input_shape
21+
return output_shape
22+
23+
24+
def create_clip_node(layer, node_name, input_name, output_name, input_shape):
25+
output_shape = get_clip_output_shape(input_shape)
26+
node = Node.c2oNode(layer, node_name, 'Clip', input_name, output_name, input_shape, output_shape)
27+
return node

0 commit comments

Comments
 (0)