Skip to content

Commit

Permalink
- add separate cmakelists
Browse files Browse the repository at this point in the history
- cleanup

Signed-off-by: jagadeesh <jagadeeshj@ideas2it.com>
  • Loading branch information
jagadeesh committed Sep 18, 2023
1 parent d4a431a commit 3a6df4e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 61 deletions.
23 changes: 2 additions & 21 deletions cpp/src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
set(MNIST_SRC_DIR "${torchserve_cpp_SOURCE_DIR}/src/examples/image_classifier/mnist")
find_package(OpenCV REQUIRED)

set(MNIST_SOURCE_FILES "")
list(APPEND MNIST_SOURCE_FILES ${MNIST_SRC_DIR}/mnist_handler.cc)
add_library(mnist_handler SHARED ${MNIST_SOURCE_FILES})
target_include_directories(mnist_handler PUBLIC ${MNIST_SRC_DIR})
target_link_libraries(mnist_handler PRIVATE ts_backends_torch_scripted ts_utils ${TORCH_LIBRARIES})


set(RESNET_SRC_DIR "${torchserve_cpp_SOURCE_DIR}/src/examples/image_classifier/resnet-18")

set(RESNET_SOURCE_FILES "")
set(OPENCV_DIR "/usr/local/include/opencv4")
list(APPEND RESNET_SOURCE_FILES ${RESNET_SRC_DIR}/resnet-18_handler.cc)
add_library(resnet-18_handler SHARED ${RESNET_SOURCE_FILES})
target_include_directories(resnet-18_handler PUBLIC ${OPENCV_DIR})
target_include_directories(resnet-18_handler PUBLIC ${RESNET_SRC_DIR})
target_link_libraries(resnet-18_handler PRIVATE ts_backends_torch_scripted ts_utils ${TORCH_LIBRARIES})
target_link_libraries(resnet-18_handler PRIVATE "/usr/local/lib/libopencv_imgcodecs.so")
target_link_libraries(resnet-18_handler PRIVATE "/usr/local/lib/libopencv_cudawarping.so")
target_link_libraries(resnet-18_handler PRIVATE "/usr/local/lib/libopencv_cudaimgproc.so")
target_link_libraries(resnet-18_handler PRIVATE "/usr/local/lib/libopencv_core.so")
add_subdirectory(image_classifier)
19 changes: 19 additions & 0 deletions cpp/src/examples/image_classifier/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set(MNIST_SRC_DIR "${torchserve_cpp_SOURCE_DIR}/src/examples/image_classifier/mnist")

set(MNIST_SOURCE_FILES "")
list(APPEND MNIST_SOURCE_FILES ${MNIST_SRC_DIR}/mnist_handler.cc)
add_library(mnist_handler SHARED ${MNIST_SOURCE_FILES})
target_include_directories(mnist_handler PUBLIC ${MNIST_SRC_DIR})
target_link_libraries(mnist_handler PRIVATE ts_backends_torch_scripted ts_utils ${TORCH_LIBRARIES})

set(RESNET_SRC_DIR "${torchserve_cpp_SOURCE_DIR}/src/examples/image_classifier/resnet-18")

set(RESNET_SOURCE_FILES "")

list(APPEND RESNET_SOURCE_FILES ${RESNET_SRC_DIR}/resnet-18_handler.cc)
add_library(resnet-18_handler SHARED ${RESNET_SOURCE_FILES})
target_include_directories(resnet-18_handler PUBLIC ${OPENCV_DIR})
target_include_directories(resnet-18_handler PUBLIC ${RESNET_SRC_DIR})
target_link_libraries(resnet-18_handler PRIVATE ts_backends_torch_scripted ts_utils ${TORCH_LIBRARIES})
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( resnet-18_handler PRIVATE ${OpenCV_LIBS} )
40 changes: 0 additions & 40 deletions cpp/src/examples/image_classifier/resnet-18/resnet-18_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,9 @@ std::vector<torch::jit::IValue> ResnetHandler::Preprocess(
"Empty payload");
continue;
}
/*
case2: the image is sent as string of bytesarray
if (dtype_it->second == "String") {
try {
auto b64decoded_str = folly::base64Decode(data_it->second);
torchserve::Converter::StrToBytes(b64decoded_str, image);
} catch (folly::base64_decode_error e) {
TS_LOGF(ERROR, "Failed to base64Decode for request id: {}, error: {}",
request.request_id,
e.what());
}
}
*/

try {
if (dtype_it->second == torchserve::PayloadType::kDATA_TYPE_BYTES) {
// case2: the image is sent as bytesarray
// torch::serialize::InputArchive archive;
// archive.load_from(std::istringstream
// iss(std::string(data_it->second)));
/*
std::istringstream iss(std::string(data_it->second.begin(),
data_it->second.end())); torch::serialize::InputArchive archive;
images.emplace_back(archive.load_from(iss, torch::Device device);
std::vector<char> bytes(
static_cast<char>(*data_it->second.begin()),
static_cast<char>(*data_it->second.end()));
images.emplace_back(torch::pickle_load(bytes).toTensor().to(*device));
*/

cv::Mat image = cv::imdecode(data_it->second, cv::IMREAD_COLOR);

// Check if the image was successfully decoded
Expand Down Expand Up @@ -169,17 +140,6 @@ void ResnetHandler::Postprocess(
const torch::Tensor& data,
std::pair<std::string&, std::map<uint8_t, std::string>&>& idx_to_req_id,
std::shared_ptr<torchserve::InferenceResponseBatch>& response_batch) {
std::ifstream jsonFile("index_to_name.json");
if (!jsonFile.is_open()) {
std::cerr << "Failed to open JSON file.\n";
}
std::string jsonString((std::istreambuf_iterator<char>(jsonFile)),
std::istreambuf_iterator<char>());
jsonFile.close();
folly::dynamic parsedJson = folly::parseJson(jsonString);
if (!parsedJson.isObject()) {
std::cerr << "Invalid JSON format.\n";
}
for (const auto& kv : idx_to_req_id.second) {
try {
auto response = (*response_batch)[kv.second];
Expand Down

0 comments on commit 3a6df4e

Please sign in to comment.