Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tasm-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ target_link_libraries(
)
add_test(tasm_test tasm_test)

file(COPY test-inputs DESTINATION ${CMAKE_CURRENT_BINARY_DIR})



121 changes: 121 additions & 0 deletions tasm-test/src/DatabaseTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "LayoutDatabase.h"
#include "Files.h"
#include <gtest/gtest.h>

namespace tasm {

class DatabaseTestFixture : public ::testing::Test {
public:
DatabaseTestFixture() {}

protected:
void SetUp() {
// set test configuration
std::unordered_map<std::string, std::string> options {
{EnvironmentConfiguration::DefaultLayoutsDB, "test-layout.db"},
};
auto config = EnvironmentConfiguration::instance(EnvironmentConfiguration(options));
std::experimental::filesystem::remove(config.defaultLayoutDatabasePath());
LayoutDatabase::instance()->open();
}
void TearDown() {
std::experimental::filesystem::remove(EnvironmentConfiguration::instance().defaultLayoutDatabasePath());
}
};

TEST_F(DatabaseTestFixture, testOpenDb) {
std::shared_ptr<LayoutDatabase> layoutDatabase = LayoutDatabase::instance();

std::experimental::filesystem::path layouts = EnvironmentConfiguration::instance().defaultLayoutDatabasePath();
assert(std::experimental::filesystem::exists(layouts));
std::experimental::filesystem::remove(layouts);

layoutDatabase->open();
assert(std::experimental::filesystem::exists(layouts));
}

TEST_F(DatabaseTestFixture, testAddSelectLayout) {
std::shared_ptr<LayoutDatabase> layoutDatabase = LayoutDatabase::instance();

std::string video = "video";
unsigned int id = 0;

std::vector<unsigned int> heights{1, 2, 3};
std::vector<unsigned int> widths{1, 2, 3, 4};
unsigned int numColumns = widths.size();
unsigned int numRows = heights.size();
std::shared_ptr<TileLayout> tileLayout = std::make_shared<TileLayout>(numColumns, numRows, widths, heights);

layoutDatabase->addTileLayout(video, id, 40, 50, tileLayout);

std::shared_ptr<TileLayout> selectedLayout = layoutDatabase->tileLayoutForId(video, id);
assert(selectedLayout->numberOfColumns() == numColumns);
assert(selectedLayout->numberOfRows() == numRows);
assert(selectedLayout->widthsOfColumns() == widths);
assert(selectedLayout->heightsOfRows() == heights);
assert(*selectedLayout == *tileLayout);
}

TEST_F(DatabaseTestFixture, testSelectLayoutIds) {
std::shared_ptr<LayoutDatabase> layoutDatabase = LayoutDatabase::instance();

std::string video = "video";

std::vector<unsigned int> heights{1, 2, 3};
std::vector<unsigned int> widths{1, 2, 3, 4};
unsigned int numColumns = widths.size();
unsigned int numRows = heights.size();
std::shared_ptr<TileLayout> tileLayout = std::make_shared<TileLayout>(numColumns, numRows, widths, heights);

std::vector<int> layoutIds{0, 2, 4, 7};

for (auto &id : layoutIds) {
layoutDatabase->addTileLayout(video, id, id, id + 10, tileLayout);
}

// add an id that won't be selected, since it doesn't include frame 10
layoutDatabase->addTileLayout(video, 10, 11, 12, tileLayout);

std::vector<int> foundLayoutIds = layoutDatabase->tileLayoutIdsForFrame(video, 10);
std::sort(layoutIds.begin(), layoutIds.end());
std::sort(foundLayoutIds.begin(), foundLayoutIds.end());
assert(layoutIds == foundLayoutIds);
}

TEST_F(DatabaseTestFixture, testTotalWidthHeight) {
std::shared_ptr<LayoutDatabase> layoutDatabase = LayoutDatabase::instance();

std::string name = "name";
std::vector<unsigned int> heights;
std::vector<unsigned int> widths;
unsigned int maxHeight = 10;
unsigned int maxWidth = 7;
unsigned int totalHeight = 0;
unsigned int totalWidth = 0;

for (unsigned int i = 0; i <= maxHeight; ++i) {
heights.push_back(i);
totalHeight += i;
}

for (unsigned int i = 0; i <= maxWidth; ++i) {
widths.push_back(i);
totalWidth += i;
}

unsigned int id = 2;
unsigned int firstFrame = 40;
unsigned int lastFrame = 59;
unsigned int numColumns = widths.size();
unsigned int numRows = heights.size();
std::shared_ptr<TileLayout> tileLayout = std::make_shared<TileLayout>(numColumns, numRows, widths, heights);

layoutDatabase->addTileLayout(name, id, firstFrame, lastFrame, tileLayout);

assert(layoutDatabase->totalWidth(name) == totalWidth);
assert(layoutDatabase->totalHeight(name) == totalHeight);
assert(layoutDatabase->largestWidth(name) == maxWidth);
assert(layoutDatabase->largestHeight(name) == maxHeight);
assert(layoutDatabase->maximumFrame(name) == lastFrame);
}
} // namespace tasm
193 changes: 193 additions & 0 deletions tasm-test/src/LayoutBenchmarkTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#include "TiledVideoManager.h"
#include "SemanticIndex.h"
#include "VideoManager.h"
#include "Tasm.h"
#include <gtest/gtest.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <functional>

using namespace tasm;

const int NUM_VIDEOS = 2;
const std::string VIDEO_PATHS[NUM_VIDEOS] {"/home/maureen/red_videos/red_6sec_2k.mp4", "/home/maureen/red_videos/red_900sec_2k.mp4"};
const std::string LABEL_PATHS[NUM_VIDEOS] {"/home/maureen/NFLX_dataset/detections/labels/birdsincage.db", "/home/maureen/visualroad_tiling/labels_yolo/traffic-2k-001_yolo.db"};
const std::string VIDEO_LABELS[NUM_VIDEOS] {"bird", "car"};
const std::string VIDEO_NAMES[NUM_VIDEOS] {"birdsincage", "traffic-2k-001_yolo"};
const int NUM_FRAMES[NUM_VIDEOS] {180, 27000};
const int IDS[NUM_VIDEOS] {197, 8476}; // from tileLayoutIdsForFrame

const int NUM_ITERATIONS = 3;
const int FUNCTION_REPETITIONS = 50; // for functions that are too fast to measure without repetition
const std::string SEP = ",";
const std::string IMPLEMENTATION = "layout database";
const std::string OUTFILE_NAME = "layout_database_benchmark.csv";

void printStat(std::ofstream &file, std::string experiment, std::string step, std::string video, int frame,
int iteration, long time, int repetitions) {
file << IMPLEMENTATION << SEP << experiment << SEP << step << SEP << video << SEP << frame << SEP << iteration <<
SEP << time << SEP << repetitions << "\n";
file.flush();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to also flush the ofstream so that even if something fails and the tests don't finish, we should hopefully have partial results in the output file.


void runBenchmark(std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile,
int frameNumber, std::string experiment, std::function<void(TiledVideoManager &)> testFunction) {
std::chrono::system_clock::time_point totalStart = std::chrono::system_clock::now();
std::chrono::system_clock::time_point constructorStart = std::chrono::system_clock::now();

TiledVideoManager tiledVideoManager(entry);

std::chrono::system_clock::time_point constructorEnd = std::chrono::system_clock::now();
std::chrono::system_clock::time_point functionStart = std::chrono::system_clock::now();

for (int i = 0; i < FUNCTION_REPETITIONS; i++) {
testFunction(tiledVideoManager);
}

std::chrono::system_clock::time_point functionEnd = std::chrono::system_clock::now();
std::chrono::system_clock::time_point totalEnd = std::chrono::system_clock::now();

auto totalDuration = std::chrono::duration_cast<std::chrono::microseconds>(totalEnd - totalStart).count();
auto constructorDuration = std::chrono::duration_cast<std::chrono::microseconds>(constructorEnd - constructorStart).count();
auto functionDuration = std::chrono::duration_cast<std::chrono::microseconds>(functionEnd - functionStart).count();

// output data to csv file
printStat(outputFile, experiment, "total", videoName, frameNumber, iteration, totalDuration, FUNCTION_REPETITIONS);
printStat(outputFile, experiment, "constructor", videoName, frameNumber, iteration, constructorDuration, FUNCTION_REPETITIONS);
printStat(outputFile, experiment, "processing", videoName, frameNumber, iteration, functionDuration, FUNCTION_REPETITIONS);
}

void storeVideos(std::string videoName, std::string videoPath, std::string label, int iteration, std::ofstream &outputFile) {
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();

// store red video around objects
TASM tasm(SemanticIndex::IndexType::LegacyWH);
tasm.storeWithNonUniformLayout(videoPath, videoName, videoName, label);

auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();

// output data to csv file
printStat(outputFile, "storeVideos", "processing", videoName, -1, iteration, duration, 1);
}

void testConstructor(std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile) {
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();

TiledVideoManager tiledVideoManager(entry);

auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();

// output data to csv file
printStat(outputFile, "testConstructor", "constructor", videoName, -1, iteration, duration, 1);
}

void testTileLayoutIdsForFrame (std::string videoName, std::shared_ptr<TiledEntry> entry, int frames, int iteration, std::ofstream &outputFile) {
for (int frameNumber = 0; frameNumber < frames; frameNumber += frames / 4) {
runBenchmark(videoName, entry, iteration, outputFile, frameNumber, "tileLayoutIdsForFrame", [frameNumber](TiledVideoManager &manager) {
manager.tileLayoutIdsForFrame(frameNumber);
});
}
}

void testTileLayoutForId (std::string videoName, std::shared_ptr<TiledEntry> entry, int id, int iteration, std::ofstream &outputFile) {
runBenchmark(videoName, entry, iteration, outputFile, -1, "tileLayoutForId", [id] (TiledVideoManager &manager){
manager.tileLayoutForId(id);
});
}

void testLocationOfTileForId (std::string videoName, std::shared_ptr<TiledEntry> entry, int frames, int id, int iteration, std::ofstream &outputFile) {
for (int frameNumber = 0; frameNumber < frames; frameNumber += frames / 4) {
runBenchmark(videoName, entry, iteration, outputFile, frameNumber, "locationOfTileForId", [frameNumber, id] (TiledVideoManager &manager){
manager.locationOfTileForId(frameNumber, id);
});
}
}

void testTotalWidth (std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile) {
runBenchmark(videoName, entry, iteration, outputFile, -1, "testTotalWidth", [] (TiledVideoManager &manager){
manager.totalWidth();
});
}

void testTotalHeight (std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile) {
runBenchmark(videoName, entry, iteration, outputFile, -1, "totalHeight", [] (TiledVideoManager &manager){
manager.totalHeight();
});
}

void testLargestWidth (std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile) {
runBenchmark(videoName, entry, iteration, outputFile, -1, "largestWidth", [] (TiledVideoManager &manager){
manager.largestWidth();
});
}

void testLargestHeight (std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile) {
runBenchmark(videoName, entry, iteration, outputFile, -1, "largestHeight", [] (TiledVideoManager &manager){
manager.largestHeight();
});
}

void testMaximumFrame (std::string videoName, std::shared_ptr<TiledEntry> entry, int iteration, std::ofstream &outputFile) {
runBenchmark(videoName, entry, iteration, outputFile, -1, "maximumFrame", [] (TiledVideoManager &manager){
manager.maximumFrame();
});
}

class LayoutBenchmarkTestFixture : public ::testing::Test {
public:
LayoutBenchmarkTestFixture() {
}
};

TEST_F(LayoutBenchmarkTestFixture, runBenchmarks) {
// set test configuration
std::unordered_map<std::string, std::string> options {
{EnvironmentConfiguration::DefaultLayoutsDB, "benchmark-layout.db"},
{EnvironmentConfiguration::CatalogPath, "benchmark-resources"},
{EnvironmentConfiguration::DefaultLabelsDB, "benchmark-labels.db"}
};
auto config = EnvironmentConfiguration::instance(EnvironmentConfiguration(options));

// open output file and add header
std::ofstream outputFile;
outputFile.open(OUTFILE_NAME, std::ios::out);
outputFile << "implementation" << SEP << "experiment" << SEP << "step" << SEP << "video" << SEP << "frame"\
<< SEP << "iteration" << SEP << "time" << SEP << "repetitions\n";

// run experiments for each video
for (int i = 0; i < NUM_VIDEOS; i++) {
std::string labelPath = LABEL_PATHS[i];
std::string videoPath = VIDEO_PATHS[i];
std::string label = VIDEO_LABELS[i];
std::string name = VIDEO_NAMES[i];
int frames = NUM_FRAMES[i];
int id = IDS[i];

std::experimental::filesystem::remove(config.defaultLayoutDatabasePath());
LayoutDatabase::instance()->open();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the only two lines that are specific to the layout database instance?


// load object database from real video
std::experimental::filesystem::remove(config.defaultLabelsDatabasePath());
std::experimental::filesystem::copy(labelPath, config.defaultLabelsDatabasePath());

// initialize tiledentry
std::shared_ptr<TiledEntry> entry = std::make_shared<TiledEntry>(name);

for (int j = 0; j < NUM_ITERATIONS; j++) {
storeVideos(name, videoPath, label, j, outputFile);
testConstructor(name, entry, j, outputFile);
testTileLayoutIdsForFrame(name, entry, frames, j, outputFile);
testTileLayoutForId(name, entry, id, j, outputFile);
testLocationOfTileForId(name, entry, frames, id, j, outputFile);
testTotalWidth(name, entry, j, outputFile);
testTotalHeight(name, entry, j, outputFile);
testLargestWidth(name, entry, j, outputFile);
testLargestHeight(name, entry, j, outputFile);
testMaximumFrame(name, entry, j, outputFile);
}
}
outputFile.close();
}
14 changes: 14 additions & 0 deletions tasm-test/src/SemanticIndexTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ using namespace tasm;
class SemanticIndexTestFixture : public testing::Test {
public:
SemanticIndexTestFixture() {}

protected:
void SetUp() {
// set test configuration
std::unordered_map<std::string, std::string> options {
{EnvironmentConfiguration::DefaultLabelsDB, "test-labels.db"}
};
auto config = EnvironmentConfiguration::instance(EnvironmentConfiguration(options));
std::experimental::filesystem::remove_all(config.defaultLabelsDatabasePath());
}
void TearDown() {
// remove test resources
std::experimental::filesystem::remove_all(EnvironmentConfiguration::instance().defaultLabelsDatabasePath());
}
};

TEST_F(SemanticIndexTestFixture, testCreateDB) {
Expand Down
Loading