| 1715 | } |
| 1716 | |
| 1717 | int quant_main(int argc, const char* argv[]) { |
| 1718 | if (argc < 4) { |
| 1719 | DLOG(INFO) << "Usage: ./quantized.out src.mnn dst.mnn preTreatConfig.json\n"; |
| 1720 | return 0; |
| 1721 | } |
| 1722 | const char* modelFile = argv[1]; |
| 1723 | const char* preTreatConfig = argv[3]; |
| 1724 | const char* dstFile = argv[2]; |
| 1725 | DLOG(INFO) << ">>> modelFile: " << modelFile; |
| 1726 | DLOG(INFO) << ">>> preTreatConfig: " << preTreatConfig; |
| 1727 | DLOG(INFO) << ">>> dstFile: " << dstFile; |
| 1728 | std::unique_ptr<MNN::NetT> netT; |
| 1729 | { |
| 1730 | std::shared_ptr<MNN::Interpreter> interp(MNN::Interpreter::createFromFile(modelFile), MNN::Interpreter::destroy); |
| 1731 | if (nullptr == interp.get()) { |
| 1732 | return 0; |
| 1733 | } |
| 1734 | netT = MNN::UnPackNet(interp->getModelBuffer().first); |
| 1735 | } |
| 1736 | |
| 1737 | // temp build net for inference |
| 1738 | flatbuffers::FlatBufferBuilder builder(1024); |
| 1739 | auto offset = MNN::Net::Pack(builder, netT.get()); |
| 1740 | builder.Finish(offset); |
| 1741 | int size = builder.GetSize(); |
| 1742 | auto ocontent = builder.GetBufferPointer(); |
| 1743 | |
| 1744 | // model buffer for creating mnn Interpreter |
| 1745 | std::unique_ptr<uint8_t> modelForInference(new uint8_t[size]); |
| 1746 | memcpy(modelForInference.get(), ocontent, size); |
| 1747 | |
| 1748 | std::unique_ptr<uint8_t> modelOriginal(new uint8_t[size]); |
| 1749 | memcpy(modelOriginal.get(), ocontent, size); |
| 1750 | |
| 1751 | netT.reset(); |
| 1752 | netT = MNN::UnPackNet(modelOriginal.get()); |
| 1753 | |
| 1754 | // quantize model's weight |
| 1755 | DLOG(INFO) << "Calibrate the feature and quantize model..."; |
| 1756 | std::shared_ptr<Calibration> calibration( |
| 1757 | new Calibration(netT.get(), modelForInference.get(), size, preTreatConfig, std::string(modelFile), std::string(dstFile))); |
| 1758 | if (!calibration->valid()) { |
| 1759 | return 0; |
| 1760 | } |
| 1761 | calibration->runQuantizeModel(); |
| 1762 | calibration->dumpTensorScales(dstFile); |
| 1763 | DLOG(INFO) << "Quantize model done!"; |
| 1764 | |
| 1765 | return 0; |
| 1766 | } |
no test coverage detected