Exemplo n.º 1
0
    def create_image_operation_list(
            params: DatasetParams) -> ImageOperationList:
        params.image_input = ImageInput.REGION_IMAGE
        params.page_scale_reference = PageScaleReference.NORMALIZED_X2

        operations = [
            ImageLoadFromPageOperation(invert=True,
                                       files=[('gray_norm_x2', False)]),
            ImageDrawRegions(block_types=[BlockType.DROP_CAPITAL]
                             if params.cut_region else [],
                             color=0),
            ImageExtractDewarpedStaffLineImages(params.dewarp,
                                                params.cut_region, params.pad,
                                                params.center,
                                                params.staff_lines_only),
        ]
        if params.apply_fcn_model is not None:
            operations += [
                ImageRescaleToHeightOperation(
                    height=params.apply_fcn_height if params.
                    apply_fcn_height else params.height),
                ImagePadToPowerOf2(params.apply_fcn_pad_power_of_2),
                ImageApplyFCN(params.apply_fcn_model),
            ]

        operations += [
            ImageRescaleToHeightOperation(height=params.height),
        ]

        if params.pad_power_of_2:
            operations.append(ImagePadToPowerOf2(params.pad_power_of_2))

        return ImageOperationList(operations)
Exemplo n.º 2
0
    def create_image_operation_list(
            params: DatasetParams) -> ImageOperationList:
        params.image_input = ImageInput.REGION_IMAGE
        params.page_scale_reference = PageScaleReference.NORMALIZED_X2

        operations = [
            ImageLoadFromPageOperation(
                invert=True,
                files=[(params.page_scale_reference.file('binary'), True)]),
            # ImageExtractTextLineImages({BlockType.LYRICS}, params.cut_region, params.pad),
            ImageExtractDeskewedLyrics(),
            ImageRescaleToHeightOperation(height=params.height),
        ]
        return ImageOperationList(operations)
Exemplo n.º 3
0
 def force_dataset_params(params: DatasetParams):
     params.dewarp = True
     params.center = True
     params.staff_lines_only = True
     params.pad_power_of_2 = False
     params.calamari_codec = CalamariCodec()
     with open(os.path.join(this_dir, 'default_codec.json'), 'r') as f:
         params.calamari_codec = CalamariCodec.from_dict(json.load(f))
Exemplo n.º 4
0
 def create_image_operation_list(
         params: DatasetParams) -> ImageOperationList:
     params.page_scale_reference = PageScaleReference.NORMALIZED
     return ImageOperationList([
         ImageLoadFromPageOperation(
             invert=True,
             files=[(params.page_scale_reference.file(
                 'gray' if params.gray else 'binary'), False)]),
         ImageExtractStaffLineImages(
             full_page=params.full_page,
             pad=params.pad,
             extract_region_only=params.extract_region_only,
             gt_line_thickness=params.gt_line_thickness),
         # ImageScaleOperation(0.5),  # Do not scale here, the line detector needs full resolution images
         ImagePadToPowerOf2(),  # Padding also done in line detector
     ])
Exemplo n.º 5
0
 def force_dataset_params(params: DatasetParams):
     params.height = 48
Exemplo n.º 6
0
    from omr.dataset.datafiles import dataset_by_locked_pages, LockState
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ommr4all.settings')
    import django
    django.setup()
    b = DatabaseBook('Pa_14819')

    train_pcgts, val_pcgts = dataset_by_locked_pages(0.9999, [LockState(Locks.LAYOUT, True)], True, [b])
    trainer_params = CalamariTrainer.default_params()
    trainer_params.l_rate = 1e-3
    trainer_params.load = '/home/ls6/wick/Documents/Projects/calamari_models/fraktur_historical_ligs/0.ckpt.json'

    params = DatasetParams(
        gt_required=True,
        height=48,
        cut_region=True,
        pad=[0, 10, 0, 20],
        #lyrics_normalization=LyricsNormalization.ONE_STRING,
    )
    train_params = AlgorithmTrainerSettings(
        params,
        train_pcgts,
        val_pcgts,
        params=trainer_params,
        calamari_params=CalamariParams(
            network="cnn=40:3x3,pool=2x2,cnn=60:3x3,pool=2x2,lstm=200,dropout=0.5",
            n_folds=1,
        )
    )
    trainer = CalamariTrainer(train_params)
    trainer.train(b)
Exemplo n.º 7
0
    def __init__(self, pcgts: List[PcGts], params: DatasetParams):
        super().__init__(pcgts, params)


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from imageio import imsave
    from shared.pcgtscanvas import PcGtsCanvas
    from omr.steps.algorithmtypes import AlgorithmTypes
    pages = [p for p in DatabaseBook('Cai_72_GT').pages()]
    # pages = [DatabaseBook('Graduel_Part_1').page('Graduel_de_leglise_de_Nevers_025')]
    # pages = [DatabaseBook('New_York').page('21v')]
    params = DatasetParams(
        pad=[0, 10, 0, 40],
        dewarp=True,
        center=True,
        staff_lines_only=True,
        cut_region=False,
        height=120,
    )

    print(params.to_json())

    at = AlgorithmTypes.SYMBOLS_PC

    if at == AlgorithmTypes.SYMBOLS_SEQUENCE_TO_SEQUENCE:
        f, ax = plt.subplots(9, max(2, len(pages)), sharex='all', sharey='all')
        for i, p in enumerate(pages):
            pcgts = PcGts.from_file(p.file('pcgts'))
            dataset = SymbolDetectionDataset([pcgts], params)
            calamari_dataset = dataset.to_calamari_dataset(train=True)
            for a, (sample, out) in enumerate(
Exemplo n.º 8
0
    def __init__(
        self,
        pcgts: List[PcGts],
        params: DatasetParams,
    ):
        super().__init__(pcgts, params)


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from database import DatabaseBook
    page = DatabaseBook('Graduel').pages()[0]
    pcgts = PcGts.from_file(page.file('pcgts'))
    params = DatasetParams(
        full_page=True,
        gray=True,
        pad=(5, 5),
    )
    dataset = PCDataset([pcgts], params)
    images = dataset.load()

    f, ax = plt.subplots(len(images), 3, sharex='all')
    for i, out in enumerate(images):
        img, region, mask = out.line_image, out.region, out.mask
        if np.min(img.shape) > 0:
            print(img.shape)
            if params.full_page:
                ax[0].imshow(img)
                ax[1].imshow(region)
                ax[2].imshow(mask)
            else:
Exemplo n.º 9
0
 def default_dataset_params() -> DatasetParams:
     return DatasetParams(
         pad=[0, 10, 0, 40],
         cut_region=False,
     )
Exemplo n.º 10
0
if __name__ == '__main__':
    import random
    import numpy as np
    random.seed(1)
    np.random.seed(1)
    b = DatabaseBook('Graduel_Fully_Annotated')
    from omr.dataset.datafiles import dataset_by_locked_pages, LockState
    train_pcgts, val_pcgts = dataset_by_locked_pages(
        0.8, [LockState(Locks.SYMBOLS, True),
              LockState(Locks.LAYOUT, True)], True, [b])
    dataset_params = DatasetParams(
        gt_required=True,
        height=40,
        dewarp=True,
        cut_region=False,
        pad=[0, 10, 0, 20],
        center=True,
        staff_lines_only=True,
        masks_as_input=False,
    )
    train_settings = AlgorithmTrainerSettings(
        dataset_params=dataset_params,
        train_data=train_pcgts,
        validation_data=val_pcgts,
        params=AlgorithmTrainerParams(l_rate=1e-3, ),
        calamari_params=CalamariParams(
            network='cnn=40:3x3,pool=1x2,cnn=80:3x3,lstm=100,dropout=0.5',
            n_folds=0,
            channels=1,
        ))
    trainer = OMRTrainer(train_settings)
Exemplo n.º 11
0
 def __init__(self, pcgts: List[PcGts], params: DatasetParams):
     params.pad = (0, 10, 0, 20)
     params.dewarp = False
     params.staff_lines_only = True
     params.cut_region = True
     super().__init__(pcgts, params)
Exemplo n.º 12
0
        params.staff_lines_only = True
        params.cut_region = True
        super().__init__(pcgts, params)


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from database.database_book import DatabaseBook
    from omr.dataset.dataset import LyricsNormalizationParams

    pages = [p for p in DatabaseBook('Gothic_Test').pages()]
    params = DatasetParams(
        height=60,
        gt_required=True,
        cut_region=True,
        pad=[0, 10, 0, 20],
        page_scale_reference=PageScaleReference.HIGHRES,
        lyrics_normalization=LyricsNormalizationParams(
            LyricsNormalization.ONE_STRING),
    )

    if True:
        page = pages[0]
        pcgts = PcGts.from_file(page.file('pcgts'))
        dataset = TextDataset([pcgts], params)
        calamari_dataset = dataset.to_text_line_calamari_dataset()
        f, ax = plt.subplots(len(calamari_dataset.samples()), 1, sharey='all')
        for i, (sample, out) in enumerate(
                zip(calamari_dataset.samples(), dataset.load())):
            img, region, mask = out.line_image, out.region, out.mask
            print(img.shape, region.shape, mask)
Exemplo n.º 13
0
    parser.add_argument("--pad", type=int, default=[0], nargs="+")
    parser.add_argument("--pad_to_power_of_2", type=int, default=None)
    parser.add_argument("--center", action='store_true')
    parser.add_argument("--cut_region", action='store_true')
    parser.add_argument("--dewarp", action='store_true')
    parser.add_argument("--use_regions", action="store_true", default=False)
    parser.add_argument("--neume_types", action="store_true", default=False)
    parser.add_argument("--gray", action="store_true")
    parser.add_argument("--full_page", action="store_true")
    parser.add_argument("--extract_region_only", action="store_true")
    parser.add_argument("--gt_line_thickness", default=3, type=int)
    parser.add_argument("--min_number_of_staff_lines", default=4, type=int)
    parser.add_argument("--max_number_of_staff_lines", default=4, type=int)

    parser.add_argument("--page_segmentation_architecture", type=lambda t: Architecture[t], choices=list(Architecture), default=PageSegmentationTrainerParams().architecture)
    parser.add_argument("--lyrics_normalization", type=lambda t: LyricsNormalization[t], choices=list(LyricsNormalization), default=DatasetParams().lyrics_normalization.lyrics_normalization)
    parser.add_argument("--lyrics_mixed_case", action='store_true')

    parser.add_argument("--calamari_n_folds", type=int, default=0)
    parser.add_argument("--calamari_single_folds", type=int, nargs='+')
    parser.add_argument("--calamari_network", type=str, default='cnn=40:3x3,pool=2x2,cnn=60:3x3,pool=2x2,lstm=200,dropout=0.5')
    parser.add_argument("--calamari_channels", type=int, default=1)
    parser.add_argument("--calamari_ctc_decoder", type=str, choices=[CTCDecoderParams.CTCDecoderType.Name(x) for x in CTCDecoderParams.CTCDecoderType.values()], default=CTCDecoderParams.CTCDecoderType.Name(AlgorithmPredictorParams().ctcDecoder.params.type))
    parser.add_argument("--calamari_ctc_word_separator", type=str, default=AlgorithmPredictorParams().ctcDecoder.params.word_separator)
    parser.add_argument("--calamari_ctc_decoder_beam_width", type=int, default=AlgorithmPredictorParams().ctcDecoder.params.beam_width)
    parser.add_argument("--calamari_ctc_dictionary_from_gt", action='store_true')
    parser.add_argument("--calamari_ctc_dictionary", type=str)
    parser.add_argument("--calamari_ctc_decoder_non_word_chars", type=str, default="".join(AlgorithmPredictorParams().ctcDecoder.params.non_word_chars))

    # evaluation parameters
    parser.add_argument("--seed", type=int, default=1)