Exemplo n.º 1
0
def learn(model):
    print("Loading feature data...")
    positive = np.load(corpus_path("patterns.npy"))
    negative = np.load(corpus_path("nopatterns.npy"))
    validate = np.load(corpus_path("validation.npy"))
    print("Fitting model...")
    model.train(positive, negative, validate)
Exemplo n.º 2
0
def score(model):
    print("Computing scores on examples...")
    with open(corpus_path("manifest.yaml")) as f:
        manifest = yaml.load(f)
    scores = []
    for cls in manifest.keys():
        for img in sorted(manifest[cls].keys()):
            path = corpus_path(manifest[cls][img]["path"])
            im = io.imread(path, as_grey=True)
            x = feature_stats(im)
            score = float(model.score(np.array([x])))
            scores.append((score, path, cls))
    return scores
Exemplo n.º 3
0
def main():
    print("Select model:")
    for i in range(len(models)):
        print("[{0}] {1}".format(i, models[i][0]))
    m = int(input("> "))
    if not 0 <= m < len(models):
        print("Invalid selection.")
        return
    if m != 0:
        model = models[m][1]()
        learn(model)
        scores = score(model)
        plot(scores, models[m][0])
    else:
        trained_models = []
        for m in models[1:]:
            print("Training", m[0])
            model = m[1]()
            learn(model)
            trained_models.append(model)
        path = corpus_path("trained_models.p")
        print("Saving trained models to", path)
        with open(path, "wb") as f:
            pickle.dump(trained_models, f)
Exemplo n.º 4
0
def preload_models():
    global trained_models
    with open(corpus_path("trained_models.p"), "rb") as f:
        trained_models = pickle.load(f)