Example #1
0
def generate_data_NN_tagged(times,
                            root_dir,
                            NN,
                            NetName,
                            size=(128, 128),
                            append=True):
    if append:
        start_index = sum(1 for s in os.listdir(root_dir)
                          if s.startswith(SAMPLE_FOLDER_PREFIX))
    else:
        start_index = 0

    for i in tqdm(range(times)):
        sample_folder = os.path.join(
            root_dir, SAMPLE_FOLDER_PREFIX + "{}".format(i + start_index))
        try:
            os.mkdir(sample_folder)
        except FileExistsError:
            pass

        # if the cv is good enough then it will save if not it goes again
        evaluation = 0
        while evaluation < 0.5:

            r = np.random.choice([0, 1])
            shapes = ShapeGenerator(128, 128)
            if r == 0:
                #shapes.add_smallcorner_ellipse()
                centre = shapes.add_side_ellipse()
                cleanimage = shapes.image.copy()

                #add some extra ellispses but not ontop of the tagged one
                for i in range(np.random.choice([1, 2])):
                    theta = np.random.choice([90, 180])
                    shapes.rotation(angle=theta)
                    shapes.add_smallcorner_ellipse()
                    shapes.rotation(angle=-theta)
            else:
                centre = shapes.add_smallcorner_ellipse()
                #shapes.add_side_ellipse()
                cleanimage = shapes.image.copy()
                #add some extra ellispses but not ontop of the tagged one
                for i in range(np.random.choice([1, 2, 3])):
                    theta = np.random.choice([90, 180, 270])
                    shapes.rotation(angle=theta)
                    shapes.add_smallcorner_ellipse()
                    shapes.rotation(angle=-theta)

            #need to trun the grey btis white for the clean segmentation
            datas = cleanimage.getdata()
            new_image_data = []
            for item in datas:
                # change all grey pixels to white
                if item in list(range(50, 255)):
                    new_image_data.append(255)
                else:
                    new_image_data.append(item)
            # update image data
            cleanimage.putdata(new_image_data)
            # now all white :)

            #need to randomise the corner the ellipse was placed in
            theta = np.random.choice([0, 90, 180, 270])
            shapes.rotation(angle=theta)
            cleanimage = cleanimage.rotate(theta)
            centre = rotate_around_point_highperf(centre,
                                                  theta,
                                                  origin=(shapes.height / 2,
                                                          shapes.width / 2))

            #add some grey/white holes
            shapes.add_holes2(
                numholes=np.random.randint(40, 50),
                width=np.random.randint(3, 4),
            )
            #add blur
            shapes.add_blur(sig=1.5)

            cvshapes = ChanVeseSelect(shapes.image, centre)
            cvshapes.run(500, gamma=1, lmb=2, theta=0.1)

            # they are meant to be reshaped inside Jaccard but python is
            # ignoring that for some reason so Im doing it here :))))
            u1 = np.reshape(cvshapes.u, np.size(cvshapes.u))
            u2 = np.reshape(clean_seg, np.size(clean_seg))
            evaluation = EM.Jaccard(u1, u2)

        dsim = dst.DeepSegmentation(shapes.image, NN, cvshapes.geo, cvshapes.u)
        dsim.run(1000,
                 lmb_reg=10,
                 epsilon=0.001,
                 gamma=10,
                 show_iterations=True)

        # save all the images
        cleanimage.save(fp=os.path.join(sample_folder,
                                        IMAGE_TYPE_NAMES["clean"]),
                        format="PNG")
        np.save(
            file=os.path.join(sample_folder, SEGMENTATION_TYPE_NAMES["clean"]),
            arr=np.array(cleanimage) / 255,
        )

        shapes.image.save(fp=os.path.join(sample_folder,
                                          IMAGE_TYPE_NAMES["dirty"]),
                          format="PNG")

        np.save(
            file=os.path.join(sample_folder,
                              SEGMENTATION_TYPE_NAMES["chan-vese"]),
            arr=cvshapes.u,
        )

        cvim = Image.fromarray(255 * cvshapes.u).convert("L")
        cvim.save(fp=os.path.join(sample_folder,
                                  IMAGE_TYPE_NAMES["chan-vese"]),
                  format="PNG")
        np.save(
            file=os.path.join(sample_folder, "tag.npy"),
            arr=np.array(np.rint(centre).astype(int)),
        )
        np.save(
            file=os.path.join(sample_folder, NetName + "_seg" + ".npy"),
            arr=np.array(dsim.u),
        )
        im = Image.fromarray(255 * np.array(dsim.u)).convert("L")
        im.save(fp=os.path.join(sample_folder, NetName + ".png"), format="PNG")

        #save the Metrics
        cv = np.reshape(cvshapes.u, np.size(cvshapes.u))
        dseg = np.reshape(np.array(dsim.u), np.size(clean_seg))
        cln = np.reshape(clean_seg, np.size(clean_seg))
        np.save(
            file=os.path.join(sample_folder, "Jaccard_" + "cv" + ".npy"),
            arr=EM.Jaccard(cv, cln),
        )
        np.save(
            file=os.path.join(sample_folder, "Jaccard_" + NetName + ".npy"),
            arr=EM.Jaccard(dseg, cln),
        )
        np.save(
            file=os.path.join(sample_folder, "Sorensen_" + "cv" + ".npy"),
            arr=EM.Sorensen(cv, cln),
        )
        np.save(
            file=os.path.join(sample_folder, "Sorensen_" + NetName + ".npy"),
            arr=EM.Sorensen(dseg, cln),
        )
Example #2
0
def generate_data(times, root_dir, size=(128, 128), append=True):
    if append:
        start_index = sum(1 for s in os.listdir(root_dir)
                          if s.startswith(SAMPLE_FOLDER_PREFIX))
    else:
        start_index = 0

    for i in tqdm(range(times)):
        sample_folder = os.path.join(
            root_dir, SAMPLE_FOLDER_PREFIX + "{}".format(i + start_index))
        try:
            os.mkdir(sample_folder)
        except FileExistsError:
            pass

        shapes = ShapeGenerator(128, 128)
        shapes.add_polygon(times=np.random.randint(10, 35))
        shapes.add_ellipse(times=np.random.randint(10, 35))
        shapes.add_holes(numholes=np.random.randint(5, 20),
                         width=np.random.randint(5, 20))

        shapes.image.save(fp=os.path.join(sample_folder,
                                          IMAGE_TYPE_NAMES["clean"]),
                          format="PNG")
        np.save(
            file=os.path.join(sample_folder, SEGMENTATION_TYPE_NAMES["clean"]),
            arr=np.array(shapes.image) / 255,
        )
        shapes.add_noise()
        shapes.image.save(fp=os.path.join(sample_folder,
                                          IMAGE_TYPE_NAMES["dirty"]),
                          format="PNG")

        shapes = ChanVese(shapes.image)
        shapes.run(steps=500, show_iterations=False)
        # save in chan-vese
        np.save(
            file=os.path.join(sample_folder,
                              SEGMENTATION_TYPE_NAMES["chan-vese"]),
            arr=shapes.u,
        )
Example #3
0
def generate_data_NN(times,
                     root_dir,
                     NN,
                     NetName,
                     size=(128, 128),
                     append=True):
    if append:
        start_index = sum(1 for s in os.listdir(root_dir)
                          if s.startswith(SAMPLE_FOLDER_PREFIX))
    else:
        start_index = 0

    for i in tqdm(range(times)):
        sample_folder = os.path.join(
            root_dir, SAMPLE_FOLDER_PREFIX + "{}".format(i + start_index))
        try:
            os.mkdir(sample_folder)
        except FileExistsError:
            pass

        # if the cv is good enough then it will save if not it goes again
        evaluation = 0
        while evaluation < 0.5:

            shapes = ShapeGenerator(128, 128)
            shapes.add_ellipse(times=np.random.randint(1, 3), size=0.2 * 128)

            cleanimage = shapes.image.copy()

            #need to trun the grey btis white for the clean segmentation
            datas = cleanimage.getdata()
            new_image_data = []
            for item in datas:
                # change all grey pixels to white
                if item in list(range(50, 255)):
                    new_image_data.append(255)
                else:
                    new_image_data.append(item)

            # update image data
            cleanimage.putdata(new_image_data)
            clean_seg = np.array(cleanimage)
            # now all white :)

            #add some grey/white holes
            shapes.add_holes(
                numholes=np.random.randint(40, 50),
                width=np.random.randint(3, 4),
            )
            #add blur
            shapes.add_blur(sig=1.5)

            cvshapes = ChanVese(shapes.image)
            cvshapes.run(steps=500, show_iterations=False)

            # they are meant to be reshaped inside Jaccard but python is
            # ignoring that for some reason so Im doing it here :))))
            u1 = np.reshape(cvshapes.u, np.size(cvshapes.u))
            u2 = np.reshape(clean_seg, np.size(clean_seg))
            evaluation = EM.Jaccard(u1, u2)

        dsim = ds.DeepSegmentation(shapes.image, NN, cvshapes.u)
        dsim.run(1000, lmb_reg=10, epsilon=0.001, show_iterations=True)

        # save all the images
        cleanimage.save(fp=os.path.join(sample_folder,
                                        IMAGE_TYPE_NAMES["clean"]),
                        format="PNG")
        np.save(
            file=os.path.join(sample_folder, SEGMENTATION_TYPE_NAMES["clean"]),
            arr=np.array(cleanimage) / 255,
        )

        shapes.image.save(fp=os.path.join(sample_folder,
                                          IMAGE_TYPE_NAMES["dirty"]),
                          format="PNG")

        np.save(
            file=os.path.join(sample_folder,
                              SEGMENTATION_TYPE_NAMES["chan-vese"]),
            arr=cvshapes.u,
        )

        cvim = Image.fromarray(255 * cvshapes.u).convert("L")
        cvim.save(fp=os.path.join(sample_folder,
                                  IMAGE_TYPE_NAMES["chan-vese"]),
                  format="PNG")

        np.save(
            file=os.path.join(sample_folder, NetName + "_seg" + ".npy"),
            arr=np.array(dsim.u),
        )
        im = Image.fromarray(255 * np.array(dsim.u)).convert("L")
        im.save(fp=os.path.join(sample_folder, NetName + ".png"), format="PNG")

        #save the Metrics
        cv = np.reshape(cvshapes.u, np.size(cvshapes.u))
        dseg = np.reshape(np.array(dsim.u), np.size(clean_seg))
        cln = np.reshape(clean_seg, np.size(clean_seg))
        np.save(
            file=os.path.join(sample_folder, "Jaccard_" + "cv" + ".npy"),
            arr=EM.Jaccard(cv, cln),
        )
        np.save(
            file=os.path.join(sample_folder, "Jaccard_" + NetName + ".npy"),
            arr=EM.Jaccard(dseg, cln),
        )
        np.save(
            file=os.path.join(sample_folder, "Sorensen_" + "cv" + ".npy"),
            arr=EM.Sorensen(cv, cln),
        )
        np.save(
            file=os.path.join(sample_folder, "Sorensen_" + NetName + ".npy"),
            arr=EM.Sorensen(dseg, cln),
        )
Example #4
0
def create(
    times=1,
    size=(128, 128),
    cleansave="images/clean/clean_",
    dirtysave="images/dirty/dirty_",
    chansave="images/chan-vese/chanvese_",
    datacleansaave="data/clean/clean_",
    datadirtysave="data/dirty/dirty_",
    datachansave="data/chan-vese/chanvese_",
):

    for i in range(times):
        e = datetime.datetime.now().strftime("%m_%d_%H_%M_%S_%f")
        # create a clean image
        shapes = ShapeGenerator(128, 128)
        shapes.add_polygon(times=20)
        shapes.add_ellipse(times=20)
        shapes.add_holes(numholes=20)
        # save in clean
        shapes.image.save(fp=cleansave + e + ".png", format="PNG")
        np.save(file=datacleansaave + e, arr=np.array(shapes.image) / 255)
        # add noise
        shapes.add_noise()
        # save in dirty
        shapes.image.save(fp=dirtysave + e + ".png", format="PNG")
        np.save(file=datadirtysave + e, arr=np.array(shapes.image) / 255)
        # apply chan-vese
        shapes = ChanVese(shapes.image)
        shapes.run(steps=400, show_iterations=False)
        # save in chan-vese
        np.save(file=datachansave + e, arr=shapes.u)
        im = Image.fromarray(255 * shapes.u).convert("L")
        im.save(fp=chansave + e + ".png", format="PNG")