|
1 | 1 | #include <algorithm> |
2 | 2 | #include <filesystem> |
3 | 3 | #include <iomanip> |
| 4 | +#include <iostream> |
4 | 5 | #include <numeric> |
| 6 | +#include <opencv2/opencv.hpp> |
5 | 7 | #include <sstream> |
6 | 8 | #include <unordered_map> |
7 | 9 |
|
8 | 10 | #include "build.hpp" |
9 | 11 | #include "graph_transformations/graph_transformations.hpp" |
| 12 | +#include "layers_fused/ConvRelu.hpp" |
10 | 13 | #include "perf/benchmarking.hpp" |
11 | 14 |
|
12 | 15 | using namespace it_lab_ai; |
13 | 16 |
|
| 17 | +void alexnet_inf_careless(Graph& graph, const RuntimeOptions& options, |
| 18 | + Tensor& input, Tensor& output) { |
| 19 | + auto* o = new Tensor(output); |
| 20 | + auto* i = new Tensor(input); |
| 21 | + graph.inference(options); |
| 22 | + if (graph.getLayersCount() == 0) { |
| 23 | + throw std::runtime_error("No layers"); |
| 24 | + } |
| 25 | + graph.setOutput(graph.getLayerFromID(graph.getLayersCount() - 1), *o); |
| 26 | + graph.setInput(graph.getLayerFromID(0), *i); |
| 27 | +} |
| 28 | + |
| 29 | +void alexnet_comparison() { |
| 30 | + std::vector<size_t> counts = {979, 1134, 1031, 1009, 981, |
| 31 | + 891, 957, 1027, 973, 1008}; |
| 32 | + size_t sum = std::accumulate(counts.begin(), counts.end(), size_t{0}); |
| 33 | + int count_pic = static_cast<int>(sum) + 10; |
| 34 | + std::vector<float> res(count_pic * 28 * 28, 1.0F); |
| 35 | + Tensor input; |
| 36 | + Shape sh1({1, 5, 5, 3}); |
| 37 | + std::vector<float> vec; |
| 38 | + vec.reserve(75); |
| 39 | + for (int i = 0; i < 75; ++i) { |
| 40 | + vec.push_back(3); |
| 41 | + } |
| 42 | + Tensor output = make_tensor(vec, sh1); |
| 43 | + |
| 44 | + Shape sh({static_cast<size_t>(count_pic), 1, 28, 28}); |
| 45 | + Tensor t = make_tensor<float>(res, sh); |
| 46 | + input = t; |
| 47 | + |
| 48 | + RuntimeOptions options; |
| 49 | + Graph graph; |
| 50 | + Graph graph2; |
| 51 | + build_graph_linear(graph, input, output, options, true, false); |
| 52 | + Graph subgraph; |
| 53 | + std::shared_ptr<Layer> layer_0 = std::make_shared<ConvolutionalLayer>(); |
| 54 | + std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu"); |
| 55 | + subgraph.setInput(layer_0, input); |
| 56 | + subgraph.makeConnection(layer_0, layer_1); |
| 57 | + std::shared_ptr<Layer> layer_to = std::make_shared<ConvReluLayer>( |
| 58 | + std::dynamic_pointer_cast<ConvolutionalLayer>(layer_0)); |
| 59 | + changed_subgraphs(graph, subgraph, layer_to, graph2, input, options); |
| 60 | + Tensor input_c = input; |
| 61 | + Tensor output_c = output; |
| 62 | + auto time1 = elapsed_time_avg<double, std::milli>( |
| 63 | + 4, alexnet_inf_careless, graph, options, input_c, output_c); |
| 64 | + print_time_stats(graph); |
| 65 | + auto time2 = elapsed_time_avg<double, std::milli>( |
| 66 | + 4, alexnet_inf_careless, graph2, options, input_c, output_c); |
| 67 | + print_time_stats(graph2); |
| 68 | + std::cout << time1 << " for unchanged graph\n"; |
| 69 | + std::cout << time2 << " for convrelu graph\n"; |
| 70 | +} |
| 71 | + |
14 | 72 | int main() { |
15 | 73 | int type = 2; |
16 | 74 | Tensor input = make_tensor(std::vector<int>({0})); |
17 | 75 | RuntimeOptions options; |
| 76 | + alexnet_comparison(); |
18 | 77 | if (type == 0) { |
19 | 78 | Graph graph1; |
20 | 79 | build_graph(graph1, input, input, MODEL_PATH_DENSENET_ONNX, options, false); |
21 | 80 |
|
22 | 81 | Graph subgraph; |
23 | | - Tensor scale = make_tensor(std::vector<float>({1.0})); |
| 82 | + Tensor scale = make_tensor(std::vector<float>({1.0F})); |
24 | 83 | std::shared_ptr<Layer> layer_0 = |
25 | 84 | std::make_shared<BatchNormalizationLayer>(scale, scale, scale, scale); |
26 | 85 | std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu"); |
|
0 commit comments