-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patha3.cpp
More file actions
170 lines (145 loc) · 4.96 KB
/
a3.cpp
File metadata and controls
170 lines (145 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// B657 assignment 3 skeleton code, D. Crandall
//
// Compile with: "make"
//
// This skeleton code implements nearest-neighbor classification
// using just matching on raw pixel values, but on subsampled "tiny images" of
// e.g. 20x20 pixels.
//
// It defines an abstract Classifier class, so that all you have to do
// :) to write a new algorithm is to derive a new class from
// Classifier containing your algorithm-specific code
// (i.e. load_model(), train(), and classify() methods) -- see
// NearestNeighbor.h for a prototype. So in theory, you really
// shouldn't have to modify the code below or the code in Classifier.h
// at all, besides adding an #include and updating the "if" statement
// that checks "algo" below.
//
// See assignment handout for command line and project specifications.
//
#include "CImg.h"
#include <ctime>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <vector>
#include <Sift.h>
#include <sys/types.h>
#include <dirent.h>
#include <map>
#include <numeric>
#include <opencv2/core.hpp>
typedef CImg<double> Image;
//Use the cimg namespace to access the functions easily
using namespace cimg_library;
using namespace std;
using namespace cv;
// Dataset data structure, set up so that e.g. dataset["bagel"][3] is
// filename of 4th bagel image in the dataset
typedef map<string, vector<string> > Dataset;
#include <Classifier.h>
#include <NearestNeighbor.h>
#include <EigenClassifier.h>
#include <Haar.h>
#include <SVM.h>
#include <neural.h>
#include <svm2.h>
#include <BOW.h>
// Figure out a list of files in a given directory.
//
vector<string> files_in_directory(const string &directory, bool prepend_directory = false)
{
vector<string> file_list;
DIR *dir = opendir(directory.c_str());
if(!dir)
throw std::string("Can't find directory " + directory);
struct dirent *dirent;
while ((dirent = readdir(dir)))
if(dirent->d_name[0] != '.')
file_list.push_back((prepend_directory?(directory+"/"):"")+dirent->d_name);
closedir(dir);
return file_list;
}
int main(int argc, char **argv)
{
try {
string buildData = "true";
if(argc < 3)
throw string("Insufficent number of arguments");
else if(argc>3)
buildData = argv[3];
string mode = argv[1];
string algo = argv[2];
// Scan through the "train" or "test" directory (depending on the
// mode) and builds a data structure of the image filenames for each class.
Dataset filenames;
vector<string> class_list = files_in_directory(mode);
for(vector<string>::const_iterator c = class_list.begin(); c != class_list.end(); ++c)
filenames[*c] = files_in_directory(mode + "/" + *c, true);
// set up the classifier based on the requested algo
Classifier *classifier=0;
if(algo == "nn")
classifier = new NearestNeighbor(class_list);
else if(algo == "haar")
classifier = new Haar(class_list);
else if(algo == "baseline")
classifier = new SVM(class_list);
else if(algo == "svm2")
classifier = new SVM2(class_list);
else if(algo == "eigen")
classifier = new EigenClassifier(class_list);
else if(algo == "deep")
classifier = new Neural(class_list);
else if(algo == "bow")
classifier = new BOW(class_list);
else
throw std::string("unknown classifier " + algo);
// now train or test!
if(mode == "train")
{
if(algo!="bow")
{
classifier->train(filenames);
}
else
{
if(buildData=="true")
{
classifier->train(filenames);
system("./svm_multiclass_learn -c 0.1 bow-train-features bow-model ");
}
else
{
system("./svm_multiclass_learn -c 0.1 bow-train-features bow-model ");
}
}
}
else if(mode == "test")
{
if(algo!="bow")
{
classifier->test(filenames);
}
else
{
if(buildData=="true")
{
classifier->test(filenames);
system("./svm_multiclass_classify bow-test-features bow-model bow-predictions");
system("python score.py bow-predictions bow-test-features");
}
else
{
system("./svm_multiclass_classify bow-test-features bow-model bow-predictions");
system("python score.py bow-predictions bow-test-features");
}
}
}
else
throw std::string("unknown mode!");
}
catch(const string &err) {
cerr << "Error: " << err << endl;
}
}