Commit 152383e1 authored by Christopher Jan-Steffen Brix's avatar Christopher Jan-Steffen Brix
Browse files

ArgMax fix, improved custom c program

parent 3ebcf810
#include "CNNTranslator.h"
#include "cNNCalculator_connector.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <armadillo>
#include <string> //Check if it can be removed
#include <iostream>
#include <map>
int main(int argc, char* argv[]) {
if(argc < 2){ //Note: argc=1 if no arguments are provided
std::cout << "Missing argument: Path to test image must be provided " << std::endl;
exit(1);
}
std::string filePath = argv[1];
if (!std::ifstream(filePath).good()) {
std::cerr << "Image loading failure, test image '" << filePath << "' does not exist." << std::endl;
exit(1);
}
cv::Mat img = cv::imread(filePath);
std::cout << "== original image size: " << img.size() << " ==" << std::endl;
// scale image to fit
cv::Size scale(28,28);
cv::resize(img, img, scale);
std::cout << "== simply resize: " << img.size() << " ==" << std::endl;
size_t channels = 1;
size_t height = img.rows;
size_t width = img.cols;
vector<float> data(channels*height*width);
for(size_t j=0; j<height; j++){
for(size_t k=0; k<width; k++){
cv::Vec3b intensity = img.at<cv::Vec3b>(j, k);
for(size_t i=0; i<channels; i++){
data[i*height*width + j*height + k] = (float) intensity[i];
}
}
}
cNNCalculator_connector connector;
connector.init();
connector.image1 = conv_to< icube >::from( CNNTranslator::translateToCube(data, vector<size_t> {channels,height,width}) );
connector.image2 = conv_to< icube >::from( CNNTranslator::translateToCube(data, vector<size_t> {channels,height,width}) );
connector.image3 = conv_to< icube >::from( CNNTranslator::translateToCube(data, vector<size_t> {channels,height,width}) );
connector.image4 = conv_to< icube >::from( CNNTranslator::translateToCube(data, vector<size_t> {channels,height,width}) );
connector.image5 = conv_to< icube >::from( CNNTranslator::translateToCube(data, vector<size_t> {channels,height,width}) );
connector.image6 = conv_to< icube >::from( CNNTranslator::translateToCube(data, vector<size_t> {channels,height,width}) );
connector.execute();
int classIndex = (int)connector.res;
std::cout << "== SUM: " << classIndex << std::endl;
return 0;
}
#include "CNNTranslator.h"
#include "cNNCalculator_connector.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <armadillo>
#include <string> //Check if it can be removed
#include <iostream>
#include <map>
int main(int argc, char** argv) {
printf("hallo\n");
}
package cNNCalculator;
component ArgMax<Z(1:oo) n = 2>{
ports in Q^{n} inputVector,
out Z(0:oo) maxIndex,
out Q maxValue;
implementation Math{
maxIndex = 0;
maxValue = inputVector(1);
for i = 2:n
if inputVector(i) > maxValue
maxIndex = i - 1;
maxValue = inputVector(i);
end
end
}
}
package cNNCalculator;
component Argmaxs{
ports
in Q(0:1)^10 input1,
in Q(0:1)^10 input2,
in Q(0:1)^10 input3,
out Z(0:999) max;
implementation Math{
max = amax(input1) + 10*amax(input2) + 100*amax(input3);
}
}
......@@ -11,20 +11,33 @@ component Calculator {
out Z(0:1998) out1;
instance Argmaxs args1;
instance Argmaxs args2;
instance ArgMax<10> number1_ones;
instance ArgMax<10> number1_tens;
instance ArgMax<10> number1_hundreds;
instance ArgMax<10> number2_ones;
instance ArgMax<10> number2_tens;
instance ArgMax<10> number2_hundreds;
instance DigitCombiner number1;
instance DigitCombiner number2;
instance Add add;
connect in1_1 -> args1.input1;
connect in1_2 -> args1.input2;
connect in1_3 -> args1.input3;
connect in2_1 -> args2.input1;
connect in2_2 -> args2.input2;
connect in2_3 -> args2.input3;
connect in1_1 -> number1_ones.inputVector;
connect in1_2 -> number1_tens.inputVector;
connect in1_3 -> number1_hundreds.inputVector;
connect in2_1 -> number2_ones.inputVector;
connect in2_2 -> number2_tens.inputVector;
connect in2_3 -> number2_hundreds.inputVector;
connect args1.max -> add.num1;
connect args2.max -> add.num2;
connect number1_ones.maxIndex -> number1.ones;
connect number1_tens.maxIndex -> number1.tens;
connect number1_hundreds.maxIndex -> number1.hundreds;
connect number2_ones.maxIndex -> number2.ones;
connect number2_tens.maxIndex -> number2.tens;
connect number2_hundreds.maxIndex -> number2.hundreds;
connect number1.number -> add.num1;
connect number2.number -> add.num2;
connect add.sum -> out1;
......
package cNNCalculator;
component Connector {
ports in Q(0:1)^{1, 28, 28} image1,
in Q(0:1)^{1, 28, 28} image2,
in Q(0:1)^{1, 28, 28} image3,
in Q(0:1)^{1, 28, 28} image4,
in Q(0:1)^{1, 28, 28} image5,
in Q(0:1)^{1, 28, 28} image6,
ports in Z(0:255)^{1, 28, 28} image1,
in Z(0:255)^{1, 28, 28} image2,
in Z(0:255)^{1, 28, 28} image3,
in Z(0:255)^{1, 28, 28} image4,
in Z(0:255)^{1, 28, 28} image5,
in Z(0:255)^{1, 28, 28} image6,
out Z(0:1998) res;
instance VGG16 predictor1;
......
package cNNCalculator;
component DigitCombiner{
ports
in Z(0:9) hundreds,
in Z(0:9) tens,
in Z(0:9) ones,
out Z(0:999) number;
implementation Math{
number = ones + 10 * tens + 100 * hundreds;
}
}
package cNNCalculator;
component VGG16{
ports in Q(0:1)^{1, 28, 28} image,
ports in Z(0:255)^{1, 28, 28} image,
out Q(0:1)^{10} predictions;
implementation CNN {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment