| 617 | } |
| 618 | |
| 619 | IBinaryProtoBlob* CaffeParser::parseBinaryProto(const char* fileName) noexcept |
| 620 | { |
| 621 | CHECK_NULL_RET_NULL(fileName) |
| 622 | using namespace google::protobuf::io; |
| 623 | |
| 624 | std::ifstream stream(fileName, std::ios::in | std::ios::binary); |
| 625 | if (!stream) |
| 626 | { |
| 627 | RETURN_AND_LOG_ERROR(nullptr, "Could not open file " + std::string{fileName}); |
| 628 | } |
| 629 | |
| 630 | IstreamInputStream rawInput(&stream); |
| 631 | CodedInputStream codedInput(&rawInput); |
| 632 | #if GOOGLE_PROTOBUF_VERSION >= 3011000 |
| 633 | codedInput.SetTotalBytesLimit(INT_MAX); |
| 634 | #else |
| 635 | // Note: This WARs the very low default size limit (64MB) |
| 636 | codedInput.SetTotalBytesLimit(INT_MAX, -1); |
| 637 | #endif |
| 638 | |
| 639 | trtcaffe::BlobProto blob; |
| 640 | bool ok = blob.ParseFromCodedStream(&codedInput); |
| 641 | stream.close(); |
| 642 | |
| 643 | if (!ok) |
| 644 | { |
| 645 | RETURN_AND_LOG_ERROR(nullptr, "parseBinaryProto: Could not parse mean file"); |
| 646 | } |
| 647 | |
| 648 | Dims4 dims{1, 1, 1, 1}; |
| 649 | if (blob.has_shape()) |
| 650 | { |
| 651 | int size = blob.shape().dim_size(), s[4] = {1, 1, 1, 1}; |
| 652 | for (int i = 4 - size; i < 4; i++) |
| 653 | { |
| 654 | assert(blob.shape().dim(i) < INT32_MAX); |
| 655 | s[i] = static_cast<int>(blob.shape().dim(i)); |
| 656 | } |
| 657 | dims = Dims4{s[0], s[1], s[2], s[3]}; |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | dims = Dims4{blob.num(), blob.channels(), blob.height(), blob.width()}; |
| 662 | } |
| 663 | |
| 664 | const int dataSize = parserutils::volume(dims); |
| 665 | assert(dataSize > 0); |
| 666 | |
| 667 | const trtcaffe::Type blobProtoDataType = CaffeWeightFactory::getBlobProtoDataType(blob); |
| 668 | const auto blobProtoData = CaffeWeightFactory::getBlobProtoData(blob, blobProtoDataType, mTmpAllocs); |
| 669 | |
| 670 | if (dataSize != (int) blobProtoData.second) |
| 671 | { |
| 672 | std::cout << "CaffeParser::parseBinaryProto: blob dimensions don't match data size!!" << std::endl; |
| 673 | return nullptr; |
| 674 | } |
| 675 | |
| 676 | const int dataSizeBytes = dataSize * CaffeWeightFactory::sizeOfCaffeType(blobProtoDataType); |
no test coverage detected