-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (62 loc) · 2.32 KB
/
main.py
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
from modules.negative_set import generate_negative_set, get_box_parameters
from modules import data, models, descriptor_vector, validation, selection
import numpy as np
from skimage.transform import resize
import pickle
# Params
TRAIN = True
SAVE_MODEL = False
SAVE_NEGATIVES = False
LIMIT = 100
NEG_SIZE = 150
TRAIN_RATE = 0.70
CLASSIFIER = 'random_forest'
MODEL_PARAMS = {
'n_estimators': 100,
}
VECTORIZATION_PARAMS = {
'vectorize': descriptor_vector.hog,
# 'vectorize_kwargs': {},
}
def main():
if TRAIN:
clf = models.create_model(CLASSIFIER, MODEL_PARAMS)
print("Loading data...")
images = data.load_images(limit=LIMIT)
labels = data.load_labels(limit=LIMIT)
print("Params:")
box_size = get_box_parameters(labels)[1:3]
box_size = box_size[0] - 10, box_size[1] - 10
print(" ", clf.__class__.__name__)
print(" ", MODEL_PARAMS)
print(" ", LIMIT, "images,", NEG_SIZE, "negatives")
print(" box_size:", box_size)
print(" ", VECTORIZATION_PARAMS)
print("Generating negative set...")
negatives = generate_negative_set(images, labels, set_size=NEG_SIZE, save=SAVE_NEGATIVES)
all_labels = np.concatenate([labels, negatives])
print("Creating train & validation sets with negatives...")
train_labels, valid_labels = data.train_valid_sets(len(images), all_labels, TRAIN_RATE)
print("Training...")
models.train(clf, images, box_size, train_labels, **VECTORIZATION_PARAMS)
if SAVE_MODEL:
import pdb; pdb.set_trace()
print('Saving alllll')
to_save = [clf, images, box_size, train_labels, valid_labels]
model_file = open('./temp_model.pickle', 'wb')
pickle.dump(to_save, model_file)
else:
print("Loading all..")
model_file = open('./temp.pickle', 'rb')
clf, images, box_size, train_labels, valid_labels, predictions = pickle.load(model_file)
# print("Predicting and validate on test examples...")
# scores, results = models.predict_and_validate(clf, images, box_size, valid_labels, **VECTORIZATION_PARAMS)
print("\nPredicting with windows...")
valid_indexes = np.unique(valid_labels[:,0]) - 1
predictions = models.predict(clf, images, box_size, **VECTORIZATION_PARAMS, only=valid_indexes)
print("\nPredicting with windows and validate...")
results = validation.rate_predictions(predictions, valid_labels)
print("Test now !")
import pdb; pdb.set_trace()
if __name__ == '__main__':
main()