Exemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue',
                        help='select tissue to train.',
                        default=None)
    parser.add_argument('--inputFolder',
                        help='Select input folder.',
                        default=None)
    parser.add_argument('--outputFolder',
                        help='select output folder',
                        default=None)
    parser.add_argument('--scale', help='select output folder', default=None)
    args = parser.parse_args()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    if args.scale:
        config.scale = int(args.scale)

    if args.tissue == 'Ganglioneuroma':
        n_freq = 20
    else:
        n_freq = 30
    print("Scale: " + args.scale)

    print(config.diagnosis)
    print(config.outputFolder)
    tools = Tools()

    annotated_nuclei = AnnotatedObjectSet()
    ids_images = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'images', '*.tif'))
    ids_masks = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    # Create dataset for training the pix2pix-network based on image pairs
    #for index,elem in enumerate(ids_paths):
    for index, elem in enumerate(ids_images):
        test = AnnotatedImage()
        #test.readFromPath(tools.getLocalDataPath(elem[1],1),tools.getLocalDataPath(groundtruth_path[0],3))
        test.readFromPath(ids_images[index], ids_masks[index], type='uint16')
        enhanced_images = tools.enhanceImage(test,
                                             flip_left_right=True,
                                             flip_up_down=True,
                                             deform=False)
        for index, img in enumerate(enhanced_images):
            annotated_nuclei.addObjectImage(
                img, useBorderObjects=config.useBorderObjects)

    # Create the image pairs
    tools.createPix2pixDataset(annotated_nuclei,
                               config,
                               n_freq=n_freq,
                               tissue=args.tissue)

    e = 1
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue',
                        help='select tissue to train.',
                        default=None)
    parser.add_argument('--inputFolder',
                        help='Select input folder.',
                        default=None)
    parser.add_argument('--outputFolder',
                        help='select output folder',
                        default=None)
    parser.add_argument('--scale', help='select output folder', default=None)
    parser.add_argument('--mode', help='select output folder', default='train')
    parser.add_argument('--resultsfile',
                        help='select output folder',
                        default=None)
    parser.add_argument('--overlap', help='select output folder', default=None)
    args = parser.parse_args()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder
    if args.scale == '1':
        config.scale = True
    if args.mode:
        config.mode = args.mode
    if args.resultsfile:
        config.resultsfile = args.resultsfile
    if args.overlap:
        config.overlap = int(args.overlap)

    print(config.diagnosis)
    print(config.outputFolder)
    print(config.scale)
    tools = Tools()

    annotated_nuclei = AnnotatedObjectSet()
    ids_images = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'images', '*.tif'))
    ids_masks = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    # Create dataset for training the pix2pix-network based on image pairs
    #for index,elem in enumerate(ids_paths):
    for index, elem in enumerate(ids_images):
        test = AnnotatedImage()
        test.readFromPath(ids_images[index], ids_masks[index], type='uint16')
        #enhanced_images = tools.enhanceImage(test,flip_left_right=True,flip_up_down=True,deform=True)
        #for index,img in enumerate(enhanced_images):
        #    annotated_nuclei.addObjectImage(img,useBorderObjects=config.useBorderObjects)
        annotated_nuclei.addObjectImage(
            test,
            useBorderObjects=config.useBorderObjects,
            path_to_img=ids_images[index])
    # Create and save the image tiles
    tools.createAndSaveTiles(annotated_nuclei, config)
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue', help='select tissue to train.', default="Ganglioneuroma")
    parser.add_argument('--inputFolder', help='Select input folder.', default=None)
    parser.add_argument('--outputFolder', help='select output folder', default=None)
    parser.add_argument('--scale', help='select output folder', default=None)
    parser.add_argument('--mode', help='select output folder', default='train')
    parser.add_argument('--resultsfile', help='select output folder', default=None)
    parser.add_argument('--overlap', help='select output folder', default=None)
    parser.add_argument('--ending', help='select output folder', default=None)
    parser.add_argument('--scalesize', help='select output folder', default=None)
    args = parser.parse_args()
    
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder
    if args.scale == '1':
        config.scale=True
    if args.mode:
        config.mode=args.mode
    if args.resultsfile:
        config.resultsfile=args.resultsfile
    if args.overlap:
        config.overlap=int(args.overlap)

    print(config.diagnosis)
    print(config.outputFolder)
    print(config.scale)
    tools = Tools()

    def takeSecond(elem):
        return os.path.basename(elem)

    annotated_nuclei = AnnotatedObjectSet()
    print("Input folder: " + args.inputFolder)
    ids_images = glob.glob(os.path.join(args.inputFolder,'*.' + args.ending))
    ids_images.sort(key=takeSecond)

    # Create dataset for training the pix2pix-network based on image pairs
    #for index,elem in enumerate(ids_paths):
    for index, elem in enumerate(ids_images):
        print(ids_images[index])
        test = AnnotatedImage()
        test.readFromPathOnlyImage(ids_images[index])
        #enhanced_images = tools.enhanceImage(test,flip_left_right=True,flip_up_down=True,deform=True)
        #for index,img in enumerate(enhanced_images):
        #    annotated_nuclei.addObjectImage(img,useBorderObjects=config.useBorderObjects)
        annotated_nuclei.addObjectImage(test, useBorderObjects=config.useBorderObjects,path_to_img=ids_images[index])
        cv2.imwrite(r"/root/flo/tmp/test_before_tiling.jpg",test.getRaw())
    # Create and save the image tiles
    cv2.imwrite(r"/root/flo/tmp/test_before_tiling.jpg",test.getRaw())
    tools.createAndSaveTilesForSampleSegmentation(annotated_nuclei,config,float(args.scalesize))
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue',
                        help='select tissue to train.',
                        default=None)
    parser.add_argument('--inputFolder',
                        help='Select input folder.',
                        default=None)
    parser.add_argument('--outputFolder',
                        help='select output folder',
                        default=None)
    parser.add_argument('--nr_images',
                        help='select number of images to create',
                        default=None)
    parser.add_argument('--overlapProbability',
                        help='select overlapProbability',
                        default=None)
    parser.add_argument('--scale', help='select output folder', default=None)

    args = parser.parse_args()
    tisquant = TisQuantExtract()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    if args.overlapProbability:
        args.overlapProbability = float(args.overlapProbability)
    else:
        args.overlapProbability = 0.5
    if args.scale == '1':
        config.scale = True

    print(config.diagnosis)
    tools = Tools()

    annotated_nuclei = AnnotatedObjectSet()
    annotated_images = []
    #ids_paths = tisquant.dbconnector.execute(query=tisquant.getLevel3AnnotatedImagesByDiagnosis_Query(diagnosis = config.diagnosis,magnification = config.magnification, staining_type = config.staining_type, staining = config.staining, segmentation_function = config.segmentation_function, annotator = config.annotator, device = config.device))
    ids_images = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'images', '*.tif'))
    ids_masks = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    #for index,elem in enumerate(ids_paths):
    for index, elem in enumerate(ids_images):
        #groundtruth_paths = tisquant.dbconnector.execute(tisquant.getLevel3AnnotationByImageId_Query(elem[0],config.annotator))
        #groundtruth_paths = tisquant.dbconnector.execute(tisquant.getLevel3AnnotationByImageIdUsingMaxExperience_Query(elem[0], config.annotator))
        #for groundtruth_path in groundtruth_paths:
        test = AnnotatedImage()
        #    test.readFromPath(tools.getLocalDataPath(elem[1],1),tools.getLocalDataPath(groundtruth_path[0],3))
        test.readFromPath(ids_images[index], ids_masks[index])
        annotated_images.append(test)

    # Create artificial new dataset
    scales = tools.getNormalizedScales(annotated_images)

    for index, img in enumerate(annotated_images):
        test = AnnotatedImage()
        if config.scale:
            test.createWithArguments(
                tools.rescale_image(img.getRaw(),
                                    (scales[index], scales[index])),
                tools.rescale_mask(img.getMask(),
                                   (scales[index], scales[index]),
                                   make_labels=True))
        else:
            test.createWithArguments(img.getRaw(), img.getMask())
        annotated_nuclei.addObjectImage(
            test, useBorderObjects=config.useBorderObjects)
    if config.scale == 0:
        if args.tissue == 'Ganglioneuroma':
            possible_numbers = [9, 16, 25, 36, 49]
        else:
            possible_numbers = [4, 4, 9]
    else:
        possible_numbers = [9, 16, 25, 36, 49]

    # How many images?
    if not args.nr_images:
        args.nr_images = 10
    else:
        args.nr_images = int(args.nr_images)

    for t in tqdm(range(0, args.nr_images)):
        # Create artificial image
        number_nuclei = random.randint(0, possible_numbers.__len__() - 1)
        img = ArtificialAnnotatedImage(
            width=256,
            height=256,
            number_nuclei=possible_numbers[number_nuclei],
            probabilityOverlap=args.overlapProbability)
        total_added = 0
        for i in range(0, possible_numbers[number_nuclei]):
            test = annotated_nuclei.returnArbitraryObject()
            if (randint(0, 1)):
                test = tools.arbitraryEnhance(test)
                total_added += img.addImageAtGridPosition(test)
        if (total_added > 0):
            shape_y = img.getRaw().shape[0]
            shape_x = img.getRaw().shape[1]
            img_new = np.zeros((shape_y, shape_x * 2, 3), dtype=np.float32)
            img_new[:, 0:shape_x,
                    0] = img_new[:, 0:shape_x,
                                 1] = img_new[:, 0:shape_x,
                                              2] = img_new[:,
                                                           shape_x:2 * shape_x,
                                                           0] = img_new[:,
                                                                        shape_x:
                                                                        2 *
                                                                        shape_x,
                                                                        1] = img_new[:,
                                                                                     shape_x:
                                                                                     2
                                                                                     *
                                                                                     shape_x,
                                                                                     2] = img.getRaw(
                                                                                     )
            scipy.misc.toimage(
                img_new, cmin=0.0,
                cmax=1.0).save(config.outputFolder + config.diagnosis[0] +
                               '\\images\\Img_' + str(t) + '.jpg')
            tifffile.imsave(config.outputFolder + config.diagnosis[0] +
                            '\\masks\\Mask_' + str(t) + '.tif',
                            img.getMask(),
                            dtype=np.uint8)
    e = 1
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue',
                        help='select tissue to train.',
                        default=None)
    parser.add_argument('--outputFolder',
                        help='select output folder',
                        default=None)
    args = parser.parse_args()
    tisquant = TisQuantExtract()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    print(config.diagnosis)
    print(config.outputFolder)
    tools = Tools()

    annotated_nuclei = AnnotatedObjectSet()
    for fold in ['train', 'val', 'test', 'train_and_val']:
        path_train = os.path.join(config.outputFolder, fold)
        if not os.path.exists(path_train):
            os.makedirs(path_train)
        path_output = os.path.join(config.outputFolder, fold,
                                   config.diagnosis[0])
        if not os.path.exists(path_output):
            os.makedirs(path_output)
        path_output_images = os.path.join(config.outputFolder, fold,
                                          config.diagnosis[0], 'images')
        if not os.path.exists(path_output_images):
            os.makedirs(path_output_images)
        path_output_masks = os.path.join(config.outputFolder, fold,
                                         config.diagnosis[0], 'masks')
        if not os.path.exists(path_output_masks):
            os.makedirs(path_output_masks)

    # Create dataset for training the pix2pix-network based on image pairs
    fold = 'train'
    running_ind = 0

    ids_paths = tisquant.dbconnector.execute(
        query=tisquant.getLevel3AnnotatedImagesByDiagnosis_Query(
            diagnosis=config.diagnosis,
            magnification=config.magnification,
            staining_type=config.staining_type,
            staining=config.staining,
            segmentation_function=config.segmentation_function,
            annotator=config.annotator,
            device=config.device))
    random.shuffle(ids_paths)

    nr_trainval = round(ids_paths.__len__() * 0.8)
    nr_val = round(nr_trainval * 0.8)

    for index, elem in enumerate(ids_paths):

        groundtruth_paths = tisquant.dbconnector.execute(
            tisquant.getLevel3AnnotationByImageIdUsingMaxExperience_Query(
                elem[0], config.annotator))
        if (index < nr_val):
            folds = ['train', 'train_and_val']
        elif (index < nr_trainval):
            folds = ['val', 'train_and_val']
        else:
            folds = ['test']
        for fold in folds:
            path_output_images = os.path.join(config.outputFolder, fold,
                                              config.diagnosis[0], 'images')
            path_output_masks = os.path.join(config.outputFolder, fold,
                                             config.diagnosis[0], 'masks')
            for groundtruth_path in groundtruth_paths:
                copyfile(
                    tools.getLocalDataPath(elem[1], 1),
                    os.path.join(
                        path_output_images,
                        config.diagnosis[0] + '_' + str(running_ind) + '.tif'))
                print('Copying rawimage ' +
                      tools.getLocalDataPath(elem[1], 1) + '...')
                copyfile(
                    tools.getLocalDataPath(groundtruth_path[0], 3),
                    os.path.join(
                        path_output_masks,
                        config.diagnosis[0] + '_' + str(running_ind) + '.tif'))
                print('Copying mask ' +
                      tools.getLocalDataPath(groundtruth_path[0], 3) + '...')
        running_ind = running_ind + 1
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue',
                        help='select tissue to train.',
                        default=None)
    parser.add_argument('--inputFolder',
                        help='Select input folder.',
                        default=None)
    parser.add_argument('--outputFolder',
                        help='select output folder',
                        default=None)
    parser.add_argument('--scale', help='select output folder', default=None)
    args = parser.parse_args()
    tisquant = TisQuantExtract()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    if args.scale:
        config.scale = int(args.scale)

    print("Scale: " + args.scale)

    print(config.diagnosis)
    print(config.outputFolder)
    tools = Tools()

    annotated_nuclei = AnnotatedObjectSet()
    #ids_paths = tisquant.dbconnector.execute(query=tisquant.getLevel3AnnotatedImagesByDiagnosis_Query(diagnosis = config.diagnosis,magnification = config.magnification, staining_type = config.staining_type, staining = config.staining, segmentation_function = config.segmentation_function, annotator = config.annotator, device = config.device))
    ids_images = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'images', '*.tif'))
    ids_masks = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    # Create dataset for training the pix2pix-network based on image pairs
    #for index,elem in enumerate(ids_paths):
    for index, elem in enumerate(ids_images):
        #groundtruth_paths = tisquant.dbconnector.execute(tisquant.getLevel3AnnotationByImageId_Query(elem[0],config.annotator)) # Pathes from groundtruth from all annotators
        #groundtruth_paths = tisquant.dbconnector.execute(tisquant.getLevel3AnnotationByImageIdUsingMaxExperience_Query(elem[0], config.annotator)) # Pathes from groundtruth from most experienced annotator
        #for groundtruth_path in groundtruth_paths:
        test = AnnotatedImage()
        #test.readFromPath(tools.getLocalDataPath(elem[1],1),tools.getLocalDataPath(groundtruth_path[0],3))
        test.readFromPath(ids_images[index], ids_masks[index])
        enhanced_images = tools.enhanceImage(test,
                                             flip_left_right=True,
                                             flip_up_down=True,
                                             deform=True)
        for index, img in enumerate(enhanced_images):
            annotated_nuclei.addObjectImage(
                img, useBorderObjects=config.useBorderObjects)

    # Create the image pairs
    tools.createPix2pixDataset(annotated_nuclei, config)
    """
    #img = ArtificialAnnotatedImage.transformToArtificialImage(annotated_nuclei.images[0])
    img = ArtificialAnnotatedImage(width=256,height=256)
    for i in range(0,10):
        test = annotated_nuclei.returnArbitraryObject()
        img.addImageAtRandomPosition(test)
    plt.figure(1)
    plt.imshow(img.getRaw(),cmap='gray')
    plt.figure(2)
    plt.imshow(img.getMask())
    plt.show()
    """
    e = 1
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue', help='select tissue to train.', default=None)
    parser.add_argument('--inputFolder', help='Select input folder.', default=None)
    parser.add_argument('--outputFolder', help='select output folder', default=None)
    parser.add_argument('--nr_images', help='select number of images to create', default=None)
    parser.add_argument('--overlapProbability', help='select overlapProbability', default=None)
    parser.add_argument('--scale', help='select output folder', default=None)
    parser.add_argument('--img_prefix', help='select output folder', default='Img_')
    parser.add_argument('--mask_prefix', help='select output folder', default='Mask_')
    #random.seed(13431)
    args = parser.parse_args()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    if args.overlapProbability:
        args.overlapProbability = float(args.overlapProbability)
    else:
        args.overlapProbability = 0.5

    if args.tissue == 'Ganglioneuroma':
        n_freq = 20#15
    else:
        n_freq = 30

    if args.scale == '1':
        config.scale=True

    print(config.diagnosis)
    tools = Tools()

    annotated_nuclei =[]
    annotated_images = []
    ids_images = glob.glob(os.path.join(args.inputFolder,config.diagnosis[0],'images','*.tif'))
    ids_masks = glob.glob(os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    for index, elem in enumerate(ids_images):
        test = AnnotatedImage()
        test.readFromPath(ids_images[index], ids_masks[index],type='uint16')
        annotated_images.append(test)

    # Create artificial new dataset
    scales = tools.getNormalizedScales(annotated_images)
    running = 0
    for index,img in enumerate(annotated_images):
        test = AnnotatedImage()
        annotated_nuclei.append(AnnotatedObjectSet())
        if config.scale:
            test.createWithArguments(tools.rescale_image(img.getRaw(),(scales[index],scales[index])),tools.rescale_mask(img.getMask(),(scales[index],scales[index]), make_labels=True))
        else:
            test.createWithArguments(img.getRaw(),img.getMask())
        annotated_nuclei[running].addObjectImage(test, useBorderObjects=config.useBorderObjects, tissue=args.tissue, scale=Config.scale)
        running += 1
        del test
    if config.scale == 0:
            if args.tissue == 'Ganglioneuroma':
                possible_numbers = [9, 16, 25, 36, 49]
            else:
                possible_numbers = [4, 4, 9]
    else:
        possible_numbers = [9,16,25,36,49]

    # How many images?
    if not args.nr_images:
        args.nr_images=10
    else:
        args.nr_images=int(args.nr_images)

    for t in tqdm(range(0,args.nr_images)):
        nr_img = random.randint(0,annotated_nuclei.__len__()-1)
        # Create artificial image
        number_nuclei = random.randint(0, possible_numbers.__len__()-1)

        # calculate Background
        tmp_image = annotated_nuclei[nr_img].images[0].getRaw()
        tmp_mask = annotated_nuclei[nr_img].images[0].getMask()
        kernel = np.ones((15, 15), np.uint8)
        bg = cv2.erode((tmp_mask == 0).astype(np.uint8), kernel, iterations=1)
        bg = np.sort(tmp_image[np.where(bg>0)])
        img = ArtificialAnnotatedImage(width=256,height=256,number_nuclei=possible_numbers[number_nuclei],probabilityOverlap=args.overlapProbability,background=bg)
        total_added = 0
        for i in range(0,possible_numbers[number_nuclei]):
            test = annotated_nuclei[nr_img].returnArbitraryObject()
            if (randint(0,1)):
                test = tools.arbitraryEnhance(test)
                total_added += img.addImageAtGridPosition(test)
        if (total_added > 0):
            shape_y = img.getRaw().shape[0]
            shape_x = img.getRaw().shape[1]
            img_new = np.zeros((shape_y,shape_x*2,3),dtype=np.float32)
            img_new[:,0:shape_x,0] = img_new[:,0:shape_x,1] = img_new[:,0:shape_x,2] = img_new[:,shape_x:2*shape_x,0] = img_new[:,shape_x:2*shape_x,1] = img_new[:,shape_x:2*shape_x,2] = img.getRaw()
            scipy.misc.toimage(img_new, cmin=0.0, cmax=1.0).save(config.outputFolder + config.diagnosis[0] + '\\images\\' + args.img_prefix + str(t) + '.jpg')
            tifffile.imsave(config.outputFolder + config.diagnosis[0] + '\\masks\\' + args.mask_prefix + str(t) + '.tif',img.getMask(),dtype=np.uint8)
    e=1