Skip to content

Commit 37aa544

Browse files
committed
fix parser, regex, int64_t in tensor, extra file
1 parent 27f89ea commit 37aa544

4 files changed

Lines changed: 135 additions & 8 deletions

File tree

app/Converters/parser_onnx.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ def onnx_to_json(model_path, output_json_path):
3939
if input.name in initializers_dict:
4040
continue
4141

42+
shape = []
43+
for dim in input.type.tensor_type.shape.dim:
44+
if dim.HasField('dim_value'):
45+
# 0 означает динамическую размерность в ONNX
46+
shape.append(dim.dim_value if dim.dim_value != 0 else -1)
47+
elif dim.HasField('dim_param'):
48+
# Обрабатываем именованные параметры размерностей
49+
shape.append(-1) # или можно сохранить как строку: dim.dim_param
50+
else:
51+
shape.append(-1) # неизвестная размерность
52+
4253
input_info = {
4354
"name": input.name,
44-
"shape": [dim.dim_value for dim in input.type.tensor_type.shape.dim],
55+
"shape": shape,
4556
"data_type": input.type.tensor_type.elem_type
4657
}
4758
break
@@ -151,12 +162,11 @@ def default(self, obj):
151162
json.dump(layer_info, f, indent=2, cls=CustomEncoder)
152163

153164
print(f"Модель успешно сохранена в {output_json_path}")
154-
print(f"Input shape: {input_info.get('shape', [])}")
155165

156166

157167
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
158168

159-
MODEL_PATH = os.path.join(BASE_DIR, 'docs\\models', 'yolo11x-cls.pt')
160-
MODEL_DATA_PATH = os.path.join(BASE_DIR, 'docs\\jsons', 'yolo11x-cls_onnx_model.json')
169+
MODEL_PATH = os.path.join(BASE_DIR, 'docs\\models', 'resnest101e_Opset16.onnx')
170+
MODEL_DATA_PATH = os.path.join(BASE_DIR, 'docs\\jsons', 'resnest101e_Opset16_onnx_model.json')
161171

162172
onnx_to_json(MODEL_PATH, MODEL_DATA_PATH)

app/Graph/build.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
196196
}
197197

198198
std::string get_base_layer_name(const std::string& tensor_name) {
199-
std::regex pattern("(_output|_out|:)[_\\d]*$");
199+
static const auto pattern = std::regex("(_output|_out|:)[_\\d]*$");
200200
return std::regex_replace(tensor_name, pattern, "");
201201
}
202202

@@ -1125,7 +1125,7 @@ std::unordered_map<int, std::string> load_class_names(
11251125
}
11261126
json json_data = json::parse(file);
11271127

1128-
for (auto& [key, value] : json_data.items()) {
1128+
for (const auto& [key, value] : json_data.items()) {
11291129
int class_id = std::stoi(key);
11301130
std::string class_name = value.get<std::string>();
11311131
class_names[class_id] = class_name;

include/layers/Tensor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace it_lab_ai {
1212

13-
enum class Type : uint8_t { kUnknown, kInt, kFloat };
13+
enum class Type : uint8_t { kUnknown, kInt, kInt64, kFloat };
1414

1515
template <typename T>
1616
std::vector<uint8_t>* to_byte(std::vector<T>& v) {
@@ -26,7 +26,7 @@ Type GetTypeEnum() {
2626
if constexpr (std::is_same_v<T, int>) {
2727
return Type::kInt;
2828
} else if constexpr (std::is_same_v<T, int64_t>) {
29-
return Type::kInt;
29+
return Type::kInt64;
3030
} else if constexpr (std::is_same_v<T, float>) {
3131
return Type::kFloat;
3232
} else {

src/layers/ConcatLayer.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include "layers/ConcatLayer.hpp"
2+
3+
namespace it_lab_ai {
4+
5+
void ConcatLayer::run(const std::vector<Tensor>& input,
6+
std::vector<Tensor>& output) {
7+
if (input.empty()) {
8+
throw std::runtime_error("ConcatLayer: No input tensors provided");
9+
}
10+
11+
if (input.size() == 1) {
12+
output = input;
13+
return;
14+
}
15+
16+
this->validate_inputs(input);
17+
18+
switch (input[0].get_type()) {
19+
case Type::kFloat:
20+
this->concatenate<float>(input, output[0]);
21+
break;
22+
case Type::kInt:
23+
this->concatenate<int>(input, output[0]);
24+
break;
25+
default:
26+
throw std::runtime_error("ConcatLayer: Unsupported input tensor type");
27+
}
28+
}
29+
30+
void ConcatLayer::validate_inputs(const std::vector<Tensor>& inputs) const {
31+
if (inputs.empty()) return;
32+
33+
const Shape& first_shape = inputs[0].get_shape();
34+
Type first_type = inputs[0].get_type();
35+
const int64_t normalized_axis = normalize_axis(first_shape.dims());
36+
37+
for (size_t i = 1; i < inputs.size(); ++i) {
38+
const Shape& shape = inputs[i].get_shape();
39+
if (shape.dims() != first_shape.dims()) {
40+
throw std::runtime_error(
41+
"ConcatLayer: All input tensors must have the same rank");
42+
}
43+
44+
if (inputs[i].get_type() != first_type) {
45+
throw std::runtime_error(
46+
"ConcatLayer: All input tensors must have the same type");
47+
}
48+
49+
for (size_t dim = 0; dim < shape.dims(); ++dim) {
50+
if (dim != static_cast<size_t>(normalized_axis) &&
51+
shape[dim] != first_shape[dim]) {
52+
throw std::runtime_error(
53+
"ConcatLayer: All input tensors must have the same shape except "
54+
"for the concatenation axis");
55+
}
56+
}
57+
}
58+
}
59+
60+
int64_t ConcatLayer::normalize_axis(size_t rank) const {
61+
if (rank == 0) {
62+
throw std::runtime_error("ConcatLayer: Cannot concatenate scalar tensors");
63+
}
64+
65+
int64_t axis = axis_;
66+
67+
if (axis < 0) {
68+
axis += static_cast<int64_t>(rank);
69+
}
70+
71+
if (axis < 0 || axis >= static_cast<int64_t>(rank)) {
72+
throw std::runtime_error("ConcatLayer: Axis " + std::to_string(axis_) +
73+
" out of range for tensor rank " +
74+
std::to_string(rank));
75+
}
76+
77+
return axis;
78+
}
79+
80+
std::vector<Tensor> ConcatLayer::reorderInputs(
81+
const std::vector<Tensor>& inputs) const {
82+
if (input_order_.empty() || input_order_.size() != inputs.size()) {
83+
return inputs;
84+
}
85+
86+
std::vector<Tensor> reordered(inputs.size());
87+
for (size_t i = 0; i < inputs.size(); ++i) {
88+
if (input_order_[i] >= 0 &&
89+
static_cast<size_t>(input_order_[i]) < inputs.size()) {
90+
reordered[i] = inputs[input_order_[i]];
91+
} else {
92+
throw std::runtime_error("ConcatLayer: Invalid input order index");
93+
}
94+
}
95+
return reordered;
96+
}
97+
98+
Shape ConcatLayer::calculate_output_shape(
99+
const std::vector<Tensor>& inputs) const {
100+
if (inputs.empty()) return Shape({});
101+
102+
const Shape& first_shape = inputs[0].get_shape();
103+
std::vector<size_t> output_dims(first_shape.dims());
104+
for (size_t i = 0; i < first_shape.dims(); ++i) {
105+
output_dims[i] = first_shape[i];
106+
}
107+
108+
const int64_t normalized_axis = normalize_axis(first_shape.dims());
109+
output_dims[normalized_axis] = 0;
110+
for (const auto& input : inputs) {
111+
output_dims[normalized_axis] += input.get_shape()[normalized_axis];
112+
}
113+
114+
return Shape(output_dims);
115+
}
116+
117+
} // namespace it_lab_ai

0 commit comments

Comments
 (0)