Attempt to open the model at the given path and load it onto the EdgeTPU device.
163 {
164
165 TfLiteStatus tfReturnStatus = TfLiteStatus::kTfLiteCancelled;
166 std::vector<edgetpu::EdgeTpuManager::DeviceEnumerationRecord> vValidDevices;
167
168
169 switch (eDeviceType)
170 {
171 case eAuto: m_tpuDevice.type = edgetpu::DeviceType(-1); break;
172 case ePCIe: m_tpuDevice.type = edgetpu::DeviceType::kApexPci; break;
173 case eUSB: m_tpuDevice.type = edgetpu::DeviceType::kApexUsb; break;
174 default: m_tpuDevice.type = edgetpu::DeviceType(-1); break;
175 }
176
177
178 m_pTFLiteModel = tflite::FlatBufferModel::VerifyAndBuildFromFile(m_szModelPath.c_str());
179
180 if (m_pTFLiteModel != nullptr)
181 {
182
183 std::vector<edgetpu::EdgeTpuManager::DeviceEnumerationRecord> vDevices = this->
GetHardwareDevices();
185
186
187
188 for (unsigned int unIter = 0; unIter < vDevices.size(); ++unIter)
189 {
190
191 bool bValidDevice = true;
192
193
194 for (unsigned int nJter = 0; nJter < vAlreadyOpenedDevices.size(); ++nJter)
195 {
196
197 if (vAlreadyOpenedDevices[nJter]->GetDeviceEnumRecord().path == vDevices[unIter].path)
198 {
199
200 bValidDevice = false;
201 }
202
203 else if (eDeviceType != eAuto)
204 {
205
206 if (vDevices[unIter].type != m_tpuDevice.type)
207 {
208
209 bValidDevice = false;
210 }
211 }
212 }
213
214
215 if (bValidDevice)
216 {
217
218 vValidDevices.emplace_back(vDevices[unIter]);
219 }
220 }
221
222
223 if (vValidDevices.size() > 0)
224 {
225
226 for (unsigned int unIter = 0; unIter < vValidDevices.size() && !m_bDeviceOpened; ++unIter)
227 {
228
229 LOG_INFO(logging::g_qSharedLogger,
230 "Attempting to load {} onto {} device at {} ({})...",
231 m_szModelPath,
233 vValidDevices[unIter].path,
235
236
237 m_pEdgeTPUContext = this->
GetEdgeManager()->OpenDevice(vValidDevices[unIter].type, vValidDevices[unIter].path, m_tpuDeviceOptions);
238
239
240 if (m_pEdgeTPUContext != nullptr && m_pEdgeTPUContext->IsReady())
241 {
242
243 tflite::ops::builtin::BuiltinOpResolverWithXNNPACK tfResolver;
244 tfResolver.AddCustom(edgetpu::kCustomOp, edgetpu::RegisterCustomOp());
245
246 if (tflite::InterpreterBuilder(*m_pTFLiteModel, tfResolver)(&m_pInterpreter) != kTfLiteOk)
247 {
248
249 LOG_ERROR(logging::g_qSharedLogger,
250 "Unable to build interpreter for model {} with device {} ({})",
251 m_szModelPath,
252 vValidDevices[unIter].path,
254
255
256 m_pInterpreter.reset();
257 m_pEdgeTPUContext.reset();
258
259
260 tfReturnStatus = TfLiteStatus::kTfLiteUnresolvedOps;
261 }
262 else
263 {
264
265 m_pInterpreter->SetExternalContext(kTfLiteEdgeTpuContext, m_pEdgeTPUContext.get());
266
267 if (m_pInterpreter->AllocateTensors() != kTfLiteOk)
268 {
269
270 LOG_WARNING(logging::g_qSharedLogger,
271 "Even though device was opened and interpreter was built, allocation of tensors failed for model {} with device {} ({})",
272 m_szModelPath,
273 vValidDevices[unIter].path,
275
276
277 m_pInterpreter.reset();
278 m_pEdgeTPUContext.reset();
279
280
281 tfReturnStatus = TfLiteStatus::kTfLiteDelegateDataWriteError;
282 }
283 else
284 {
285
286 LOG_INFO(logging::g_qSharedLogger,
287 "Successfully opened and loaded model {} with device {} ({})",
288 m_szModelPath,
289 vValidDevices[unIter].path,
291
292
293 m_bDeviceOpened = true;
294
295
296 tfReturnStatus = TfLiteStatus::kTfLiteOk;
297 }
298 }
299 }
300 else
301 {
302
303 LOG_ERROR(logging::g_qSharedLogger,
304 "Unable to open device {} ({}) for model {}.",
305 vValidDevices[unIter].path,
307 m_szModelPath);
308 }
309 }
310 }
311 else
312 {
313
314 LOG_ERROR(logging::g_qSharedLogger,
315 "No valid devices were found for model {}. Device type is {}",
316 m_szModelPath,
318 }
319 }
320 else
321 {
322
323 LOG_ERROR(logging::g_qSharedLogger, "Unable to load model {}. Does it exist at this path? Is this actually compiled for the EdgeTPU?", m_szModelPath);
324 }
325
326
327 return tfReturnStatus;
328 }
edgetpu::EdgeTpuManager * GetEdgeManager()
Retrieves a pointer to an EdgeTPUManager instance from the libedgetpu library.
Definition TensorflowTPU.hpp:417
static std::vector< edgetpu::EdgeTpuManager::DeviceEnumerationRecord > GetHardwareDevices()
Retrieve a list of EdgeTPU devices from the edge API.
Definition TensorflowTPU.hpp:358
static std::vector< std::shared_ptr< edgetpu::EdgeTpuContext > > GetOpenedHardwareDevices()
Retrieve a list of already opened EdgeTPU devices from the edge API.
Definition TensorflowTPU.hpp:385
std::string DeviceTypeToString(edgetpu::DeviceType eDeviceType)
to_string method for converting a device type to a readable string.
Definition TensorflowTPU.hpp:442