Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//===- ResNetBenchmark.cpp ---------------------------------------------===//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This file implements the benchmark for e2e resnet.
//
//===----------------------------------------------------------------------===//
#include "Utils/Container.h"
#include <benchmark/benchmark.h>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <string>
namespace {
// Declare the resnet C interface.
extern "C" {
void _mlir_ciface_resnet(MemRef<float, 2> *output, MemRef<float, 4> *input);
}
// TODO: Add input image preprocessing, the current preprocessing only has
// resize step.
const cv::Mat imagePreprocessing() {
cv::Mat inputImage = cv::imread(
"../../benchmarks/DeepLearning/Models/ResNet-V2-50/Images/ice-cream.png");
assert(!inputImage.empty() && "Could not read the image.");
cv::Mat resizedImage;
int imageWidth = 224;
int imageHeight = 224;
cv::resize(inputImage, resizedImage, cv::Size(imageWidth, imageHeight),
cv::INTER_LINEAR);
return resizedImage;
}
cv::Mat image = imagePreprocessing();
// TODO: figure out the correct strides layout.
intptr_t sizesInput[4] = {1, image.rows, image.cols, 3};
intptr_t sizesOutput[2] = {1, 1001};
MemRef<float, 4> input(image, sizesInput);
MemRef<float, 2> output(sizesOutput);
// Define benchmark function.
void BM_ResNet(benchmark::State &state) {
for (auto _ : state) {
for (int i = 0; i < state.range(0); ++i) {
_mlir_ciface_resnet(&output, &input);
}
}
}
// Softmax function.
void softmax(float *input, size_t size) {
assert(0 <= size <= sizeof(input) / sizeof(float));
int i;
float m, sum, constant;
m = -INFINITY;
for (i = 0; i < size; ++i) {
if (m < input[i]) {
m = input[i];
}
}
sum = 0.0;
for (i = 0; i < size; ++i) {
sum += exp(input[i] - m);
}
constant = m + log(sum);
for (i = 0; i < size; ++i) {
input[i] = exp(input[i] - constant);
}
}
std::string getLabel(int idx) {
std::ifstream in(
"../../benchmarks/DeepLearning/Models/ResNet-V2-50/Labels.txt");
assert(in.is_open() && "Could not read the label file.");
std::string label;
for (int i = 0; i < idx; ++i)
std::getline(in, label);
std::getline(in, label);
in.close();
return label;
}
} // namespace
// Register benchmarking function with different arguments.
BENCHMARK(BM_ResNet)->Arg(1);
// Print result function.
void printResult() {
// Run the model and activation function.
_mlir_ciface_resnet(&output, &input);
auto out = output.getData();
softmax(out, 1001);
// Find the classification and print the result.
float maxVal = 0;
float maxIdx = 0;
for (int i = 0; i < 1001; ++i) {
if (out[i] > maxVal) {
maxVal = out[i];
maxIdx = i;
}
}
std::cout << "Classification Index: " << maxIdx << std::endl;
std::cout << "Classification: " << getLabel(maxIdx) << std::endl;
std::cout << "Probability: " << maxVal << std::endl;
}