-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·34 lines (28 loc) · 1.43 KB
/
Copy pathtrain.py
File metadata and controls
executable file
·34 lines (28 loc) · 1.43 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
#!/usr/bin/env python
from utils import ximages
from learning import SVMClassifier, Dataset, Observation
from argparse import ArgumentParser
class ImageObservation(Observation):
'''An observation the _data_ attribute of which is a cv2 image'''
def as_vector(self):
return self.data.ravel()
def get_image_observations(folders):
for f in folders:
for im in ximages(f):
yield ImageObservation(im, f)
if __name__ == "__main__":
parser = ArgumentParser(description="Train an image classifier")
parser.add_argument('folders', type=str, nargs='+', help="List of folder paths, each containing a specific category of image. The format/size of the images must be consistent.")
parser.add_argument('-v', '--validate', type=int, default=None, metavar='N_FOLDS',
help="Instead of creating a model, get the classifier's precision using K-fold validation (supply the number of folds).")
parser.add_argument('-o', '--output', type=str, default='model.pkl', metavar='OUTPATH', help="Save model to a file.")
args = parser.parse_args()
images = get_image_observations(args.folders)
dataset = Dataset(images)
classifier = SVMClassifier(dataset)
if args.validate is not None:
print "K-fold validation..."
precision = classifier.validate(n_folds=args.validate, verbose=True)
raise SystemExit("Precision:", precision)
classifier.train()
classifier.dump(args.output)