Skip to content

Commit

Permalink
saliency testing (#201)
Browse files Browse the repository at this point in the history
added benchmarking for saliency and matching

---------

Co-authored-by: Samir Rashid <Samir-Rashid@godsped.com>
Co-authored-by: Triton UAS <ucsdtuas@gmail.com>
Co-authored-by: Igor Aprelev <hashreds@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 12, 2024
1 parent 4816b19 commit 822e62a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
1 change: 0 additions & 1 deletion docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ stop-jetson-pixhawk-compose:
run-jetson-cuda-check:
docker run -it --rm --runtime nvidia -i ghcr.io/tritonuas/obcpp:jetson /obcpp/build/bin/cuda_check


# NOTE: Use this target for development where you want to quicly edit source code and recompile.
# This will spawn up the jetson container, launch you into a bash shell and mount the
# host's obcpp directory at "/obcpp" in the container. This means you can edit the source
Expand Down
2 changes: 1 addition & 1 deletion docker/jetson-pixhawk-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
obcpp:
image: tritonuas/obcpp:jetson
image: ghcr.io/tritonuas/obcpp:jetson
runtime: nvidia
network_mode: "host"
devices:
Expand Down
1 change: 1 addition & 0 deletions src/cv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(FILES
set(LIB_DEPS
obcpp_protos
obcpp_utilities
obcpp_camera
)

add_library(${LIB_NAME} STATIC
Expand Down
8 changes: 7 additions & 1 deletion src/cv/saliency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <opencv2/opencv.hpp>
#include "utilities/logging.hpp"


#define REGULAR_TARGET 1
#define MANNIKIN 2

Expand Down Expand Up @@ -34,7 +35,12 @@ std::vector<CroppedTarget> Saliency::salience(cv::Mat image) {
tensor = tensor.toType(c10::kFloat).div(255);
// swap axis
tensor = Saliency::transpose(tensor, { (2), (0), (1) });
auto input_to_net = ToInput(tensor);

// eventually add device as member of Saliency
c10::Device device = torch::cuda::is_available() ? torch::kCUDA : torch::kCPU;
auto tensor_cuda = tensor.to(device);

auto input_to_net = ToInput(tensor_cuda);

/*
* forward() runs an inference on the input image using the provided model
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/cv_matching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "cv/matching.hpp"
#include "utilities/constants.hpp"
#include "utilities/common.hpp"

// Download these test images by running "make pull_matching_test_images" or from the test.zip here https://drive.google.com/drive/u/1/folders/1opXBWx6bBF7J3-JSFtrhfFIkYis9qhGR
// Or, any cropped not-stolen images will work
Expand Down Expand Up @@ -79,7 +80,10 @@ int main(int argc, char* argv[]) {
cv::Mat ref4 = cv::imread(refImagePath4);
referenceImages.push_back(std::make_pair(ref4, BottleDropIndex(1)));

auto startLoad = getUnixTime_ms().count();
Matching matcher(bottlesToDrop, referenceImages, modelPath);
auto endLoad = getUnixTime_ms().count();
LOG_F(INFO, "time to load: %lu milliseconds", endLoad - startLoad);
cv::Mat image = cv::imread(imageMatchPath);
Bbox dummyBox(0, 0, 0, 0);
CroppedTarget cropped = {
Expand All @@ -95,15 +99,21 @@ int main(int argc, char* argv[]) {
false
};

auto startMatch = getUnixTime_ms().count();
MatchResult result = matcher.match(cropped);
auto endMatch = getUnixTime_ms().count();
LOG_F(INFO, "\nTRUE MATCH TEST:\nClosest is bottle at index %d\nThe similarity is %.3f\n",
int(result.bottleDropIndex),
result.distance);

auto startMatchFalse = getUnixTime_ms().count();
MatchResult resultFalse = matcher.match(croppedFalse);
auto endMatchFalse = getUnixTime_ms().count();
LOG_F(INFO, "\nFALSE MATCH TEST:\nClosest is bottle at index %d\nThe similarity is %.3f\n",
int(resultFalse.bottleDropIndex),
resultFalse.distance);
LOG_F(INFO, "time to match: %lu milliseconds",
(endMatch - startMatch + endMatchFalse - startMatchFalse)/2);

return 0;
}
11 changes: 11 additions & 0 deletions tests/integration/cv_saliency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@
#include <string>

#include "cv/saliency.hpp"
#include "utilities/common.hpp"
#include "loguru.hpp"

// expected arguments: <path-to-model> <path-to-image>
int main(int argc, const char* argv[]) {

if (argc != 3) {
std::cerr << "usage: example-app <path-to-model> <path-to-image>\n";
return -1;
}

// convert image to tensor

const char* modelPath = argv[1];
auto startLoad = getUnixTime_ms().count();
Saliency sal(modelPath);
auto endLoad = getUnixTime_ms().count();
LOG_F(INFO, "time to load: %u milliseconds", endLoad - startLoad);

const char* imgPath = argv[2];
cv::Mat img = cv::imread(imgPath, cv::IMREAD_COLOR);

auto startSalience = getUnixTime_ms().count();
std::vector<CroppedTarget> predictions = sal.salience(img);
auto endSalience = getUnixTime_ms().count();
LOG_F(INFO, "time to saliency: %u milliseconds", endSalience - startSalience);

img = cv::imread(imgPath, cv::IMREAD_COLOR);

Expand All @@ -36,6 +46,7 @@ int main(int argc, const char* argv[]) {
// cv::namedWindow("cropped targets", cv::WINDOW_FULLSCREEN);
// cv::imshow("cropped targets", img);
// cv::waitKey(0);

cv::imwrite("croppedTargets.jpg", img);
LOG_F(INFO, "saved croppedTargets.jpg to build/");
// testing: save input image to file path (cv::imsave?) with bounding boxes overlayed
Expand Down

0 comments on commit 822e62a

Please sign in to comment.