Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DGtest application #10

Merged
merged 40 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ac19a9e
DGtest / ParseDigit added
hansely123 Jan 7, 2019
b8261fd
DGtest
hansely123 Jan 7, 2019
9e33258
DGtest
hansely123 Jan 7, 2019
16687b7
ParseDigit
hansely123 Jan 7, 2019
fe31679
Combined
hansely123 Jan 8, 2019
0886999
DGtest
hansely123 Jan 8, 2019
4c33e41
Delete
hansely123 Jan 8, 2019
af462f4
Delete
hansely123 Jan 8, 2019
e20dcf5
Delete
hansely123 Jan 8, 2019
86de7e3
Update README.md
hansely123 Jan 8, 2019
9ba330a
CMakeLists
hansely123 Jan 8, 2019
1b9f370
Merge branch 'master' of https://github.com/hansely/MIVisionX
hansely123 Jan 8, 2019
62da059
CMake
hansely123 Jan 8, 2019
ca7df71
CMake
hansely123 Jan 8, 2019
b4baaff
CMake version
hansely123 Jan 8, 2019
98bfbbc
Delete
hansely123 Jan 8, 2019
c0bac6e
Update README.md
hansely123 Jan 8, 2019
ab0a653
Update CMakeLists.txt
hansely123 Jan 8, 2019
2c5fd48
Update README.md
hansely123 Jan 8, 2019
87c4b3f
Update README.md
hansely123 Jan 8, 2019
60d7b2e
Update README.md
hansely123 Jan 8, 2019
d6321e2
Add files via upload
hansely123 Jan 8, 2019
17b3129
Update README.md
hansely123 Jan 8, 2019
780598e
Update README.md
hansely123 Jan 8, 2019
03001af
Add files via upload
hansely123 Jan 9, 2019
5d4f9a0
Delete image2-Cropped.jpg
hansely123 Jan 9, 2019
d9230b0
Delete image2-Cropped2.png
hansely123 Jan 9, 2019
5e2f7b6
Add files via upload
hansely123 Jan 9, 2019
8a38aa0
Update README.md
hansely123 Jan 9, 2019
7fa1f69
Update README.md
hansely123 Jan 9, 2019
32315da
Update README.md
hansely123 Jan 9, 2019
13b9c9a
Update README.md
hansely123 Jan 10, 2019
70757f0
Update README.md
hansely123 Jan 10, 2019
03da12d
Update README.md
hansely123 Jan 10, 2019
938299a
Update README.md
hansely123 Jan 10, 2019
16ac66f
Update README.md
hansely123 Jan 10, 2019
3e3ed21
Update README.md
hansely123 Jan 10, 2019
b577cad
Update README.md
hansely123 Jan 11, 2019
f802f6b
Update README.md
hansely123 Jan 11, 2019
1eb11e4
Update README.md
hansely123 Jan 11, 2019
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
101 changes: 101 additions & 0 deletions apps/DGtest/Argmax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "Argmax.h"
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>


int Argmax::mCount = 0;

Argmax::Argmax(const char* fileName, const std::string labelName, const std::string tagName) : mFileName(fileName) {

std::ifstream label(labelName, std::ios_base::in);
if (!label) {
std::cout << "Failed on loading filename: " << labelName << std::endl;
exit(-1);
}
// else {
// std::cout << "Label name: " << labelName << std::endl;
// }

mLabel = txtToVector(label);
label.close();

std::ifstream tag(tagName, std::ios_base::in);
if (!tag) {
std::cout << "Failed on loading filename: " << tagName << std::endl;
exit(-1);
}
// else {
// std::cout << "Tag name: " << tagName << std::endl;
// }

mTag = txtToVector(tag);
tag.close();
}

Argmax::~Argmax() {
}

void Argmax::setIndex (const std::vector<float> &vec) {
auto itr = max_element(vec.begin(), vec.end());
mIndex = distance(vec.begin(), itr);
}

int Argmax::getIndex () {
return mIndex;
}

void Argmax::printResult(const std::vector<std::string> &label, const std::vector<std::string> &tag) {
std::cout << "Tag name: " << tag.at(mCount) << std::endl;
mCount++;
std::cout << "Classified as: " << label.at(getIndex()) << std::endl << std::endl;
}

int Argmax::getLabelSize() {
return mLabel.size();
}

int Argmax::getTagSize() {
return mTag.size();
}

std::vector<std::string> Argmax::txtToVector(std::ifstream &textFile) {
std::string str;
std::vector<std::string> vec;
while (getline(textFile,str)) {
vec.push_back(str);
}
return vec;
}

void Argmax::run() {
std::vector<float> vec;
int classSize = getLabelSize();
int count = 0;
float c;

FILE *file = fopen(mFileName, "rb");
if (!file) {
std::cout << "Failed on loading filename: " << mFileName << std::endl;
exit(-1);
}
// else {
// std::cout << "File name: " << mFileName << std::endl;
// }

std::cout << std::endl << "Classification Result" << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl << std::endl;

while (fread(&c, sizeof(float),1, file)) {
vec.push_back(c);
count++;
if (count%classSize == 0) {
setIndex(vec);
printResult(mLabel, mTag);
vec.clear();
}
}

fclose(file);
}
76 changes: 76 additions & 0 deletions apps/DGtest/Argmax.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <vector>
#include <fstream>

/**
* Utility class for reading the tensor file and argmax it against the label file
*/
class Argmax
{
public:
Argmax(const char* fileName, const std::string labelName, const std::string tagName);
~Argmax();

/**
* Caculate the index number with the maximum probability
*/
void setIndex (const std::vector<float> &vec);

/**
* Get the current index
*/
int getIndex ();

/**
* Get the size of the label file
*/
int getLabelSize();

/**
* Get the size of the label file
*/
int getTagSize();

/**
* Prints out the result
*/
void printResult(const std::vector<std::string> &list, const std::vector<std::string> &tag);

/**
* Converts the text file to the vector
*/
std::vector<std::string> txtToVector(std::ifstream &textFile);

/**
* Run the argmax
*/
void run();

private:

/**
* The index of the maximum probability will be stored
*/
int mIndex;

/**
* The count of a current image
*/
static int mCount;

/**
* File name to open and read the tensor object
*/
const char* mFileName;

/**
* File stream to open and read the label text file
*/
std::vector<std::string> mLabel;

/**
* File stream to open and read the tag text file
*/
std::vector<std::string> mTag;
};
43 changes: 43 additions & 0 deletions apps/DGtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2015 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

cmake_minimum_required (VERSION 2.8)
project (annmodule)
set (CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(OpenCL REQUIRED)
find_package(OpenCV QUIET)
include_directories (${OpenCL_INCLUDE_DIRS} ${OpenCL_INCLUDE_DIRS}/Headers )
include_directories (/opt/rocm/include)
link_directories (/opt/rocm/lib)
list(APPEND SOURCES annmodule.cpp)
add_library(${PROJECT_NAME} SHARED ${SOURCES})
target_link_libraries(${PROJECT_NAME} openvx vx_nn)

set(TARGET_CPP Argmax.cpp VXtensor.cpp DGtest.cpp)
add_executable(DGTest main.cpp ${TARGET_CPP})
if (OpenCV_FOUND)
target_compile_definitions(DGTest PUBLIC ENABLE_OPENCV=1)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(DGTest ${OpenCV_LIBRARIES})
else(OpenCV_FOUND)
target_compile_definitions(DGTest PUBLIC ENABLE_OPENCV=0)
endif(OpenCV_FOUND)
target_link_libraries(DGTest openvx vx_nn ${PROJECT_NAME})
114 changes: 114 additions & 0 deletions apps/DGtest/DGtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "DGtest.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <chrono>
#include <unistd.h>
#if ENABLE_OPENCV
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#endif

#define ERROR_CHECK_OBJECT(obj) { vx_status status = vxGetStatus((vx_reference)(obj)); if(status != VX_SUCCESS) { vxAddLogEntry((vx_reference)context, status , "ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); return status; } }
#define ERROR_CHECK_STATUS(call) { vx_status status = (call); if(status != VX_SUCCESS) { printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); exit(-1); } }

static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[])
{
size_t len = strlen(string);
if (len > 0) {
printf("%s", string);
if (string[len - 1] != '\n')
printf("\n");
fflush(stdout);
}
}

inline int64_t clockCounter()
{
return std::chrono::high_resolution_clock::now().time_since_epoch().count();
}

inline int64_t clockFrequency()
{
return std::chrono::high_resolution_clock::period::den / std::chrono::high_resolution_clock::period::num;
}

DGtest::DGtest(const char* weights, std::string inputFile, std::string outputFile, const int batchSize) : mWeights(weights), mBatchSize(batchSize){
mContext = vxCreateContext();
if(vxGetStatus((vx_reference)mContext)) {
std::cerr << "ERROR: vxCreateContext(...) failed" << std::endl;
exit(-1);
}
mInputTensor = std::make_unique<VXtensor>(mContext, mBatchSize, inputFile, VX_READ_ONLY);

mGraph = vxCreateGraph(mContext);
if(vxGetStatus((vx_reference)mGraph)) {
std::cerr << "ERROR: vxCreateGraph(...) failed" << std::endl;
exit(-1);
}
mOutputTensor = std::make_unique<VXtensor>(mContext, mBatchSize, outputFile, VX_WRITE_ONLY);
};

DGtest::~DGtest(){

ERROR_CHECK_STATUS(vxReleaseContext(&mContext));
printf("DGtest successful\n");
};


void DGtest::runInference() {
//read in from the specified input tensor
if(mInputTensor->readTensor() < 0) {
std::cout << "Failed to initialize tensor data from " << mInputTensor->getFileName() << std::endl;
exit(-1);
}
std::cout << "OK: initialized tensor 'data' from " << mInputTensor->getFileName() << std::endl;
int64_t freq = clockFrequency(), t0, t1;
t0 = clockCounter();
vxRegisterLogCallback(mContext, log_callback, vx_false_e);

//add input tensor, output tensor, and weights to the graph
vx_status status = annAddToGraph(mGraph, mInputTensor->getTensor(), mOutputTensor->getTensor(), mWeights);
if(status) {
printf("ERROR: annAddToGraph() failed (%d)\n", status);
exit(-1);
}

//verify the graph
status = vxVerifyGraph(mGraph);
if(status) {
printf("ERROR: vxVerifyGraph(...) failed (%d)\n", status);
exit(-1);
}
t1 = clockCounter();
printf("OK: graph initialization with annAddToGraph() took %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);

//process the graph
t0 = clockCounter();
status = vxProcessGraph(mGraph);
t1 = clockCounter();
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
exit(-1);
}
printf("OK: vxProcessGraph() took %.3f msec (1st iteration)\n", (float)(t1-t0)*1000.0f/(float)freq);

//write out to the specified output tensor
if(mOutputTensor->writeTensor() < 0) {
std::cout << "Failed to write tensor data to " << mOutputTensor->getFileName() << std::endl;
exit(-1);
}
std::cout << "OK: wrote tensor 'loss' into " << mOutputTensor->getFileName() << std::endl;

t0 = clockCounter();
int N = 100;
for(int i = 0; i < N; i++) {
status = vxProcessGraph(mGraph);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
printf("OK: vxProcessGraph() took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
ERROR_CHECK_STATUS(vxReleaseGraph(&mGraph));
}
53 changes: 53 additions & 0 deletions apps/DGtest/DGtest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include "annmodule.h"
#include "VXtensor.h"
#include <memory>

/**
* Class to run the inference
*/

class DGtest
{
public:
DGtest(const char* weights, std::string inputFile, std::string outputFile, const int batchSize);
~DGtest();

/**
* Run the inference
*/
void runInference();

private:

/**
* Weights file name
*/
const char* mWeights;

/**
* The pointer to the input tensor object
*/
std::unique_ptr<VXtensor> mInputTensor;

/**
* The pointer to the output tensor object
*/
std::unique_ptr<VXtensor> mOutputTensor;

/**
* The batch size to run the inference
*/
int mBatchSize;

/**
* Context that will be used for the inference
*/
vx_context mContext;

/**
* Graph that will be used for the inference
*/
vx_graph mGraph;
};
Binary file added apps/DGtest/Examples/digit006-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/DGtest/Examples/digit007-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/DGtest/Examples/digit008-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/DGtest/Examples/digit009-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading