I'm want to use OpenCV's KNN algorithm to classify 4 features into one of two classes. In a text file, I have my training data in the following format:
feature_1,feature_2,feature_3,feature_4,class
where feature_1, feature_3, feature_4 and class are integers and feature_2 is of type float. The first line of the text file contains the headings for each feature.
However, the OpenCV documentation (http://docs.opencv.org/modules/ml/doc/k_nearest_neighbors.html) states that the train function requires the training data in the Mat data structure.
I'm confused as to how I can convert my text file of training data, to a Mat. If anyone can help me out with this I would really appreciate it.
Basicly, OpenCV implements CvMLData which can read csv files (and your file is a comma separated file).
according to documentation: http://docs.opencv.org/modules/ml/doc/mldata.html
Once you create an CvMLData object, you can use read_csv method:
read_csv(const char* filename)
to load it, and then use get_values() to get pointer to the input data as Mat and get_responses() to get the pointer to the labels as Mat
To set which column is considered as "response" (label) use the set_response_idx method
Related
I recently used cppflow on VS 2019 on Windows 10.
My original data is data in 4 columns per row. I want to use neural network to classify precipitation particles. I have trained and saved my model (.pb) on python. Because it is text data in .txt, and the example described in the cppflow document uses pictures as input, I would like to ask what is the function of cppflow input in .txt text?
Read the .txt file into a std::vector then convert into a cppflow::tensor.
I tried to read the .mat file with -v7.3 in C++. As the .mat file with version -7.3 is the same as the hdf5 file, I try to read the mat file with the hdf5 API. I able to open group, reference, and the dataset. I also able to read the dataset with struct, int, double, or character array format.
But I see one dataset show its name as a class type. But I don't know how I read it. I attached an image for better understanding.
"error" field shows the value of a class type name. When I open it in matlab it shows like the below picture -
I also try a compound data type to read it. But I can not able to read. Can you suggest me any way to read the data from -v7.3 mat file in C++?
we are trying to detect special characters like + and - inside an image using tensorflow by extending the MNIST sample code -> https://github.com/opensourcesblog/tensorflow-mnist
We have also been able to create a binary encoded file using our sample images needed for training the neural network by using the sample code -> https://github.com/jkarnows/idx-formatter
But we are not finding a way how to create a label file for our images and then to create a binary encoded label file using the label file
Both of these files are very important for proceeding further .
Anybody having any idea is most welcome to share them with us
The data is in gzip format, decoded with extract_labels, each label on 8 bytes. You can use numpy.getbuffer to convert a uint8 array back.
Alternatively, you can just create your own extract_labels which reads labels in whatever format you see fit.
How to read image data from .cr2 (raw image format by Canon) in C++?
The only one operation I need to perform is to read pixel data of .cr2 file directly if it is possible, otherwise I would like to convert it to any loss-less image and read its pixels' data.
Any suggestions?
I would go with ImageMagick too. You don't have to convert all your files up front, you can do them one at a time as you need them.
In your program, rather than opening the CR2 file, just open a pipe (popen() call) that is executing an ImageMagick command like
convert file.cr2 ppm:-
then you can read the extremely simple PPM format which is described here - basically just a line of ASCII text that tells you the file type, then another line of ASCII text that tells you the image dimensions, followed by a max value and then the data in binary.
Later on you can actually use the ImageMagick library and API if you need to.
I know matlab matrix can be loaded into C++ program in some ways, while none of these ways seem to be efficient or convenient.
I have seen others modified the header of the '.mat' file, then it can be directly loaded into C++ program with armadillo.
Anyone has ideas how to modify the header file?
It's not just save the matlab '.mat' file into ascii format. The loading time and storage space is larger than binary format.
To store a 2GB binary mat file, I need at least 20GB to store it in ASCII format.
Loading 100MB binary mat file takes less than 1 second, load same size ASCII text data takes much longer.
I don't think save the matlab mat file into ASCII format and load it into armadillo is a good solution.
According to the Armadillo documentation:
file_type can be one of the following:
...
raw_ascii:
Numerical data stored in raw ASCII format, without a header. The numbers are separated by whitespace. The number of columns must be the same in each row. Cubes are loaded as one slice. Data which was saved in Matlab/Octave using the -ascii option can be read in Armadillo, except for complex numbers. Complex numbers are stored in standard C++ notation, which is a tuple surrounded by brackets: eg. (1.23,4.56) indicates 1.24 + 4.56i.
You should therefore be able to load a Matlab matrix written in text format, contained in a file called "MatlabMatrix.mat", by using the following code:
arma::mat fromMatlab;
fromMatlab.load("MatlabMatrix.mat", arma::raw_ascii);
Also, a related question can be found here.
You can export your data in matlab in low level binary format and then load it in armadillo with the arma::raw_binary option.
e.g. in MATLAB:
m=10;
A = randn(m,m);
name = 'test.bin'
[F,err] = fopen(name,'w');
if F<0,error(err);end
fwrite(F,A,'double');
fclose(F);
load with armadillo:
arma::mat A;
std::string name = "test.bin";
A.load(name,arma::raw_binary);
A.print("A");
The only thing is that you lose the matrix dimensions of the original matrix, as armadillo loads it in a vectorized form, so you have to reshape it per hand after loading.
To include matrix dimensions you can mimic the armadillo header when saving in matlab and then use the arma::arma_binary option when loading. If you are interested in that option I can also tell you how to do it.