forked from IQ.Lvbs/IQ.Pilot
IQ.Pilot Release Commit @ b6534c0
This commit is contained in:
17
iqpilot/selfdrive/iqmodeld/tests/tf_test/build.sh
Executable file
17
iqpilot/selfdrive/iqmodeld/tests/tf_test/build.sh
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
# Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
TF_ROOT="${TF_ROOT:-/home/batman/one/external/tensorflow}"
|
||||
TF_INCLUDE_DIR="${TF_INCLUDE_DIR:-$TF_ROOT/include}"
|
||||
TF_LIB_DIR="${TF_LIB_DIR:-$TF_ROOT/lib}"
|
||||
CXX="${CXX:-clang++}"
|
||||
|
||||
exec "$CXX" \
|
||||
-std=c++17 \
|
||||
-I "$TF_INCLUDE_DIR" \
|
||||
-L "$TF_LIB_DIR" \
|
||||
-Wl,-rpath="$TF_LIB_DIR" \
|
||||
main.cc \
|
||||
-ltensorflow
|
||||
92
iqpilot/selfdrive/iqmodeld/tests/tf_test/main.cc
Normal file
92
iqpilot/selfdrive/iqmodeld/tests/tf_test/main.cc
Normal file
@@ -0,0 +1,92 @@
|
||||
// Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/c/c_api.h"
|
||||
|
||||
namespace {
|
||||
|
||||
struct FileBlob {
|
||||
std::vector<uint8_t> bytes;
|
||||
};
|
||||
|
||||
FileBlob read_blob(const std::filesystem::path &path) {
|
||||
FILE *handle = fopen(path.c_str(), "rb");
|
||||
if (handle == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
fseek(handle, 0, SEEK_END);
|
||||
const long byte_count = ftell(handle);
|
||||
rewind(handle);
|
||||
|
||||
FileBlob blob;
|
||||
blob.bytes.resize(byte_count);
|
||||
const size_t read_count = fread(blob.bytes.data(), static_cast<size_t>(byte_count), 1, handle);
|
||||
fclose(handle);
|
||||
|
||||
if (read_count != 1) {
|
||||
blob.bytes.clear();
|
||||
}
|
||||
return blob;
|
||||
}
|
||||
|
||||
void free_tf_buffer(void *data, size_t) {
|
||||
free(data);
|
||||
}
|
||||
|
||||
TF_Buffer *make_tf_buffer(FileBlob &&blob) {
|
||||
auto *buffer = TF_NewBuffer();
|
||||
auto *payload = static_cast<uint8_t *>(malloc(blob.bytes.size()));
|
||||
assert(payload != nullptr);
|
||||
memcpy(payload, blob.bytes.data(), blob.bytes.size());
|
||||
buffer->data = payload;
|
||||
buffer->length = blob.bytes.size();
|
||||
buffer->data_deallocator = free_tf_buffer;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string pb_path_from_prefix(const char *prefix) {
|
||||
return std::string(prefix) + ".pb";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
printf("usage: %s <graph-prefix>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const std::string pb_path = pb_path_from_prefix(argv[1]);
|
||||
printf("loading model %s\n", pb_path.c_str());
|
||||
|
||||
FileBlob blob = read_blob(pb_path);
|
||||
if (blob.bytes.empty()) {
|
||||
printf("FAIL: unable to read graph bytes\n");
|
||||
return 1;
|
||||
}
|
||||
printf("loaded model of size %zu\n", blob.bytes.size());
|
||||
|
||||
std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)> status(TF_NewStatus(), TF_DeleteStatus);
|
||||
std::unique_ptr<TF_Graph, decltype(&TF_DeleteGraph)> graph(TF_NewGraph(), TF_DeleteGraph);
|
||||
std::unique_ptr<TF_ImportGraphDefOptions, decltype(&TF_DeleteImportGraphDefOptions)> options(
|
||||
TF_NewImportGraphDefOptions(), TF_DeleteImportGraphDefOptions);
|
||||
std::unique_ptr<TF_Buffer, decltype(&TF_DeleteBuffer)> buffer(make_tf_buffer(std::move(blob)), TF_DeleteBuffer);
|
||||
|
||||
TF_GraphImportGraphDef(graph.get(), buffer.get(), options.get(), status.get());
|
||||
if (TF_GetCode(status.get()) != TF_OK) {
|
||||
printf("FAIL: %s\n", TF_Message(status.get()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("SUCCESS\n");
|
||||
return 0;
|
||||
}
|
||||
32
iqpilot/selfdrive/iqmodeld/tests/tf_test/pb_loader.py
Executable file
32
iqpilot/selfdrive/iqmodeld/tests/tf_test/pb_loader.py
Executable file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
def _load_graph_bytes(graph_path: Path) -> bytes:
|
||||
return graph_path.read_bytes()
|
||||
|
||||
|
||||
def _parse_graph(graph_path: Path) -> tf.compat.v1.GraphDef:
|
||||
graph = tf.compat.v1.GraphDef()
|
||||
graph.ParseFromString(_load_graph_bytes(graph_path))
|
||||
return graph
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
if len(argv) < 2:
|
||||
print("Usage: pb_loader.py <graph.pb>")
|
||||
return 1
|
||||
_parse_graph(Path(argv[1]))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main(sys.argv))
|
||||
Reference in New Issue
Block a user