Apollo 11.0
自动驾驶开放平台
batch_stream.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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
18
19#ifdef NV_TENSORRT_MAJOR
20 #if NV_TENSORRT_MAJOR == 8
22 #endif
23#endif
24
25#include <algorithm>
26
27#include "absl/strings/str_cat.h"
28
29#include "cyber/common/log.h"
30
31namespace apollo {
32namespace perception {
33namespace inference {
34
35BatchStream::BatchStream(int batchSize, int maxBatches, std::string dataPath)
36 : mBatchSize(batchSize), mMaxBatches(maxBatches), mPath(dataPath) {
37 FILE *file = fopen((mPath + "Batch0").c_str(), "rb");
38 if (file != nullptr) {
39 int d[4];
40 int fs = static_cast<int>(fread(d, sizeof(int), 4, file));
41 CHECK_EQ(fs, 4);
42 mDims = nvinfer1::DimsNCHW{d[0], d[1], d[2], d[3]};
43 fclose(file);
44 mImageSize = mDims.c() * mDims.h() * mDims.w();
45 mBatch.resize(mBatchSize * mImageSize, 0);
46 mFileBatch.resize(mDims.n() * mImageSize, 0);
47 reset(0);
48 }
49}
50
52
53void BatchStream::reset(int firstBatch) {
54 if (mPath != "") {
55 mBatchCount = 0;
56 mFileCount = 0;
57 mFileBatchPos = mDims.n();
58 skip(firstBatch);
59 }
60}
61
63 if (mBatchCount == mMaxBatches) {
64 return false;
65 }
66
67 for (int csize = 1, batchPos = 0; batchPos < mBatchSize;
68 batchPos += csize, mFileBatchPos += csize) {
69 CHECK_GT(mFileBatchPos, 0);
70 CHECK_LE(mFileBatchPos, mDims.n());
71 // mMaxBatches > number of batches in the files
72 if (mFileBatchPos == mDims.n() && !update()) {
73 return false;
74 }
75
76 // copy the smaller of: elements left to fulfill the request,
77 // or elements left in the file buffer.
78 csize = std::min(mBatchSize - batchPos, mDims.n() - mFileBatchPos);
79 std::copy_n(getFileBatch() + mFileBatchPos * mImageSize, csize * mImageSize,
80 getBatch() + batchPos * mImageSize);
81 }
82 mBatchCount++;
83 return true;
84}
85
86void BatchStream::skip(int skipCount) {
87 if (mBatchSize >= mDims.n() && mBatchSize % mDims.n() == 0 &&
88 mFileBatchPos == mDims.n()) {
89 mFileCount += skipCount * mBatchSize / mDims.n();
90 return;
91 }
92
93 int x = mBatchCount;
94 for (int i = 0; i < skipCount; ++i) {
95 next();
96 }
97 mBatchCount = x;
98}
99
100bool BatchStream::update() {
101 std::string inputFileName = absl::StrCat(mPath, "Batch", mFileCount++);
102 FILE *file = fopen(inputFileName.c_str(), "rb");
103 if (file == nullptr) {
104 return false;
105 }
106
107 int d[4];
108 int fs = static_cast<int>(fread(d, sizeof(int), 4, file));
109 CHECK_EQ(fs, 4);
110 ACHECK(mDims.n() == d[0] && mDims.c() == d[1] && mDims.h() == d[2] &&
111 mDims.w() == d[3]);
112
113 size_t readInputCount =
114 fread(getFileBatch(), sizeof(float), mDims.n() * mImageSize, file);
115 CHECK_EQ(readInputCount, size_t(mDims.n() * mImageSize));
116 fclose(file);
117 mFileBatchPos = 0;
118 return true;
119}
120
121} // namespace inference
122} // namespace perception
123} // namespace apollo
int32_t & h()
Definition rt_legacy.h:46
int32_t & n()
Definition rt_legacy.h:30
int32_t & c()
Definition rt_legacy.h:38
int32_t & w()
Definition rt_legacy.h:54
#define ACHECK(cond)
Definition log.h:80
class register implement
Definition arena_queue.h:37