Esempio n. 1
0
def continuous_search(input_file, 
                      input_mask, 
                      startfile=None,
                      checkpoint='checkpoint.txt',
                      best_mask_file="temp_mask.png",
                      pop_size=10):
    """Run genetic search continuously.
    
    input_file: the original image
    input_mask: the ground truth mask for the image
    pop_size: the size of the population
    Runs indefinitely unless a perfect value (0.0) is reached.
    """
    mydata = base_classes.pipedata()
    mydata.img = imageio.imread(input_file)
    mydata.gmask = imageio.imread(input_mask)

    pname = Path(input_file)
    outfile=pname.parent.joinpath(f"_{pname.stem}.txt")
    mask_file = pname.parent.joinpath(f"{pname.stem}_bestsofar.png")
    
    #TODO: Read this file in and set population first
    workflow.addalgos([colorspace, segmentor, segment_fitness])
    wf = workflow()
    
    my_evolver = GeneticSearch.Evolver(workflow, mydata, pop_size=pop_size)
    population = my_evolver.newpopulation()
    best_fitness=2.0
    if outfile.exists():
        inlist, fitness=read_pop(outfile)
        for fit in fitness:
            if fit < best_fitness:
                best_fitness = fit
        previous_pop = my_evolver.copy_pop_list(inlist)
        if len(previous_pop) > len(population):
            population = previous_pop[:-len(population)]
        else:
            for index, ind in enumerate(previous_pop):
                population[index] = ind
        print(f"######### Done importing previous list {best_fitness}")

    iteration = 0

    while best_fitness > 0.0:
        print(f"running {iteration} iteration")
        population = my_evolver.run(ngen=1,population=population)
            
        #Get the best algorithm so far
        best_so_far = my_evolver.hof[0]
        fitness = best_so_far.fitness.values[0]
        if (fitness < best_fitness):
            best_fitness = fitness
            print(f"\n\n\n\nIteration {iteration} Finess Improved to {fitness}")

            #Generate mask of best so far.
            seg = workflow(paramlist=best_so_far)
            mydata = seg.pipe(mydata)
            imageio.imwrite(mask_file,mydata.mask);
            write_vector(f"{outfile}", f"[{iteration}, {fitness}, {best_so_far}]") 
        iteration += 1
Esempio n. 2
0
def test_run_algo():
    """Unit test for runAlgo function.
     Checks to see if the output is what it's supposed to be in this case."""
    individual = Segmentors.segmentor()
    data = pipedata()
    data.img = TEST_IM_COLOR
    data.gmask = TEST_IM_COLOR[:, :, 0]
    individual.runAlgo(data)
Esempio n. 3
0
def test_colorspace_pipe():
    from see import ColorSpace
    from see import base_classes
    img, gmask = test_loading_image_examples()
    data = base_classes.pipedata()
    data.img = img
    data.gmask = gmask
    cs = ColorSpace.colorspace()
    data = cs.pipe(data)
Esempio n. 4
0
def continuous_search(input_file, 
                      input_mask, 
                      startfile=None,
                      checkpoint='checkpoint.txt',
                      best_mask_file="temp_mask.png", 
                      pop_size=10):
    """Run genetic search continuously.
    
    input_file: the original image
    input_mask: the ground truth mask for the image
    pop_size: the size of the population
    Runs indefinitely unless a perfect value (0.0) is reached.
    """
    mydata = base_classes.pipedata()
    mydata.img = imageio.imread(input_file)
    mydata.gmask = imageio.imread(input_mask)

    outfile=f"{input_file}.txt"
    
    #TODO: Read this file in and set population first
    workflow.addalgos([colorspace, segmentor, segment_fitness])
    wf = workflow()
    
    #Run the search
    my_evolver = GeneticSearch.Evolver(workflow, mydata, pop_size=pop_size)

    best_fitness=2.0
    iteration = 0
 
    while(best_fitness > 0.0):
        print(f"running {iteration} iteration")
        if(startfile):
            population = my_evolver.run(ngen=1, startfile=startfile)
        else:
            population = my_evolver.run(ngen=1)
            
        #Get the best algorithm so far
        params = my_evolver.hof[0]

        #Generate mask of best so far.
        seg = workflow(paramlist=params)
        mydata = seg.pipe(mydata)
        
        fitness = mydata.fitness
        if (fitness < best_fitness):
            best_fitness = fitness
            print(f"\n\n\n\nIteration {iteration} Finess Improved to {fitness}")
            my_evolver.writepop(population, filename="checkpoint.pop")
            imageio.imwrite(best_mask_file,mydata.mask);
            GeneticSearch.write_algo_vector(fpop_file, f"[{iteration}, {fitness}, {params}]\n") 
            ###TODO Output [fitness, seg]
        iteration += 1
def test_newpopulation():
    """Unit test for newpopulation function. Checks the type and length of
     the new population."""
    img = np.zeros((20, 20, 3))
    img[4:10, 4:10, :] = 1
    mask = img[:, :, 0]

    data = base_classes.pipedata()
    data.img = img
    data.gmask = mask
    data.fitness = 2
    evolv = GeneticSearch.Evolver(Segmentors.segmentor, data, pop_size=10)
    assert isinstance(evolv.tool.population(), list)
    assert len(evolv.tool.population()) == 10
def test_mutate():
    """Unit test for mutate function. Checks type and length of the
     new population after mutation."""
    img = np.zeros((20, 20, 3))
    img[4:10, 4:10, :] = 1
    mask = img[:, :, 0]

    data = base_classes.pipedata()
    data.img = img
    data.gmask = mask
    evolv = GeneticSearch.Evolver(Segmentors.segmentor, data, pop_size=10)
    tpop = evolv.mutate(evolv.tool.population())
    assert isinstance(tpop, list)
    assert len(tpop) == 10
def test_nextgen():
    """Unit test for nextgen function. Checks the type and length of the new population,
     and checks that the population is evolving."""
    img = np.zeros((20, 20, 3))
    img[4:10, 4:10, :] = 1
    mask = img[:, :, 0]

    data = base_classes.pipedata()
    data.img = img
    data.gmask = mask
    evolv = GeneticSearch.Evolver(Segmentors.segmentor, data, pop_size=10)
    pop = evolv.tool.population()
    tpop = evolv.mutate(pop)
    assert isinstance(tpop, list)
    assert len(tpop) == 10
    assert tpop != pop
def test_run():
    """Unit test for run function. Checks the type and length of the final population,
     and checks that the population evolved."""
    img = np.zeros((20, 20, 3))
    img[4:10, 4:10, :] = 1
    mask = img[:, :, 0]

    data = base_classes.pipedata()
    data.img = img
    data.gmask = mask
    data.fitness = 2
    evolv = GeneticSearch.Evolver(Segmentors.segmentor, data, pop_size=10)
    start_pop = evolv.tool.population()
    final_pop = evolv.run()
    assert isinstance(final_pop, list)
    assert len(final_pop) == 10
    assert final_pop != start_pop
Esempio n. 9
0
    type=int,
    help="population size of each generation to run genetic search (default: 20)",
)

args = parser.parse_args()

# Initialize Algorithm Space and Workflow
algorithm_space = Classifier.algorithmspace

workflow.addalgos([Classifier, ClassifierFitness])
wf = workflow()


# Create Data: Sklearn tutorial toy datasets
# Moons
moons_ds = pipedata()
moons_ds.name = "Moons"
moons_ds.X, moons_ds.y = make_moons(noise=0.3, random_state=0)

# Circles
circles_ds = pipedata()
circles_ds.name = "Circles"
circles_ds.X, circles_ds.y = make_circles(
    noise=0.2, factor=0.5, random_state=1)

# Linearly Seperable dataset
lin_ds = pipedata()
lin_ds.X, lin_ds.y = make_classification(
    n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=1
)
rng = np.random.RandomState(2)