1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #include "glow/Base/IO.h" |
18 | #include "glow/Base/Image.h" |
19 | #include "glow/Base/Tensor.h" |
20 | #include "glow/Quantization/Base/Base.h" |
21 | |
22 | #include "llvm/Support/CommandLine.h" |
23 | |
24 | #include <cmath> |
25 | |
26 | using namespace glow; |
27 | |
28 | llvm::cl::opt<std::string> InputFilename(llvm::cl::Positional, |
29 | llvm::cl::desc("<input file>" ), |
30 | llvm::cl::Required); |
31 | llvm::cl::opt<std::string> OutputFilename(llvm::cl::Positional, |
32 | llvm::cl::desc("<output file>" ), |
33 | llvm::cl::Required); |
34 | llvm::cl::OptionCategory QuantizationCat("Quantization Options" ); |
35 | llvm::cl::opt<float> QuantizationScale( |
36 | "scale" , |
37 | llvm::cl::desc("Quantization scale. If NaN, no quantization is performed." ), |
38 | llvm::cl::init(NAN), llvm::cl::cat(QuantizationCat)); |
39 | llvm::cl::opt<int> QuantizationOffset("offset" , |
40 | llvm::cl::desc("Quantization offset" ), |
41 | llvm::cl::init(0), |
42 | llvm::cl::cat(QuantizationCat)); |
43 | |
44 | int main(int argc, char **argv) { |
45 | llvm::cl::ParseCommandLineOptions(argc, argv); |
46 | const char *filename = argv[1]; |
47 | const char *outfile = argv[2]; |
48 | llvm::ArrayRef<float> mean = zeroMean; |
49 | llvm::ArrayRef<float> std = oneStd; |
50 | |
51 | if (useImagenetNormalization) { |
52 | mean = imagenetNormMean; |
53 | std = imagenetNormStd; |
54 | } |
55 | |
56 | Tensor png = readPngPpmImageAndPreprocess(filename, imageNormMode[0], |
57 | imageChannelOrderOpt[0], |
58 | imageLayoutOpt[0], mean, std); |
59 | if (!std::isnan(static_cast<float>(QuantizationScale))) { |
60 | TensorQuantizationParams TQP{QuantizationScale, QuantizationOffset}; |
61 | png = quantization::quantizeTensor(png, TQP); |
62 | } |
63 | writeToFile(png, outfile); |
64 | return 0; |
65 | } |
66 | |