-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.c
174 lines (139 loc) · 5.53 KB
/
classifier.c
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
163
164
165
166
167
168
169
170
171
172
173
174
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include "knn.h"
/*****************************************************************************/
/* Do not add anything outside the main function here. Any core logic other */
/* than what is described below should go in `knn.c`. You've been warned! */
/*****************************************************************************/
/**
* main() takes in the following command line arguments.
* -K <num>: K value for kNN (default is 1)
* -d <distance metric>: a string for the distance function to use
* euclidean or cosine (or initial substring such as "eucl", or "cos")
* -p <num_procs>: The number of processes to use to test images
* -v : If this argument is provided, then print additional debugging information
* (You are welcome to add print statements that only print with the verbose
* option. We will not be running tests with -v )
* training_data: A binary file containing training image / label data
* testing_data: A binary file containing testing image / label data
* (Note that the first three "option" arguments (-K <num>, -d <distance metric>,
* and -p <num_procs>) may appear in any order, but the two dataset files must
* be the last two arguments.
*
* You need to do the following:
* - Parse the command line arguments, call `load_dataset()` appropriately.
* - Create the pipes to communicate to and from children
* - Fork and create children, close ends of pipes as needed
* - All child processes should call `child_handler()`, and exit after.
* - Parent distributes the test dataset among children by writing:
* (1) start_idx: The index of the image the child should start at
* (2) N: Number of images to process (starting at start_idx)
* Each child should get at most N = ceil(test_set_size / num_procs) images
* (The last child might get fewer if the numbers don't divide perfectly)
* - Parent waits for children to exit, reads results through pipes and keeps
* the total sum.
* - Print out (only) one integer to stdout representing the number of test
* images that were correctly classified by all children.
* - Free all the data allocated and exit.
* - Handle all relevant errors, exiting as appropriate and printing error message to stderr
*/
void usage(char *name) {
fprintf(stderr, "Usage: %s -v -K <num> -d <distance metric> -p <num_procs> training_list testing_list\n", name);
}
int main(int argc, char *argv[]) {
int opt;
int K = 1; // default value for K
char *dist_metric = "euclidean"; // default distant metric
int num_procs = 1; // default number of children to create
int verbose = 0; // if verbose is 1, print extra debugging statements
int total_correct = 0; // Number of correct predictions
while((opt = getopt(argc, argv, "vK:d:p:")) != -1) {
switch(opt) {
case 'v':
verbose = 1;
case 'K':
K = atoi(optarg);
break;
case 'd':
dist_metric = optarg;
break;
case 'p':
num_procs = atoi(optarg);
break;
default:
usage(argv[0]);
exit(1);
}
}
if(optind >= argc) {
fprintf(stderr, "Expecting training images file and test images file\n");
exit(1);
}
char *training_file = argv[optind];
optind++;
char *testing_file = argv[optind];
// TODO The following lines are included to prevent compiler warnings
// and should be removed when you use the variables.
(void)K;
(void)dist_metric;
(void)num_procs;
// Set which distance function to use
/* You can use the following string comparison which will allow
* prefix substrings to match:
*
* If the condition below is true then the string matches
* if (strncmp(dist_metric, "euclidean", strlen(dist_metric)) == 0){
* //found a match
* }
*/
// TODO
// Load data sets
if(verbose) {
fprintf(stderr,"- Loading datasets...\n");
}
Dataset *training = load_dataset(training_file);
if ( training == NULL ) {
fprintf(stderr, "The data set in %s could not be loaded\n", training_file);
exit(1);
}
Dataset *testing = load_dataset(testing_file);
if ( testing == NULL ) {
fprintf(stderr, "The data set in %s could not be loaded\n", testing_file);
exit(1);
}
// Create the pipes and child processes who will then call child_handler
if(verbose) {
printf("- Creating children ...\n");
}
// TODO
// Distribute the work to the children by writing their starting index and
// the number of test images to process to their write pipe
// TODO
// Wait for children to finish
if(verbose) {
printf("- Waiting for children...\n");
}
// TODO
// When the children have finised, read their results from their pipe
// TODO
if(verbose) {
printf("Number of correct predictions: %d\n", total_correct);
}
// This is the only print statement that can occur outside the verbose check
printf("%d\n", total_correct);
// Clean up any memory, open files, or open pipes
// TODO
return 0;
}