コード例 #1
0
    # WM MS lesion inference
    # --------------------------------------------------
    seg_time = time.time()

    "> CNN:", scan, "running WM lesion segmentation"
    sys.stdout.flush()
    options['test_scan'] = scan

    test_x_data = {
        scan: {
            m: os.path.join(options['tmp_folder'], n)
            for m, n in zip(options['modalities'], options['x_names'])
        }
    }

    out_seg = test_cascaded_model(model, test_x_data, options)

    print "> INFO:", scan, "CNN Segmentation time: ",\
        round(time.time() - seg_time), "sec"

    # If input images have been registered before segmentation -> T1w space,
    # then resample the segmentation  back to the original space
    if options['register_modalities']:
        print "> INFO:", scan, "Inverting lesion segmentation masks"
        invert_registration(current_folder, options)

    print "> INFO:", scan, "total pipeline time: ",\
        round(time.time() - total_time), "sec"

    # remove tmps if not set
    if options['save_tmp'] is False:
コード例 #2
0
def infer_segmentation(options):
    """
    Infer segmentation given the input options passed as parameters
    """

    # set GPU mode from the configuration file. So, this has to be updated before calling
    # the CNN libraries if the default config "~/.theanorc" has to be replaced.
    if options['mode'].find('cuda') == -1 and options['mode'].find(
            'gpu') == -1:
        os.environ[
            'THEANO_FLAGS'] = 'mode=FAST_RUN,device=cpu,floatX=float32,optimizer=fast_compile'
    else:
        os.environ['THEANO_FLAGS'] = 'mode=FAST_RUN,device=' + options[
            'mode'] + ',floatX=float32,optimizer=fast_compile'

    from CNN.base import test_cascaded_model
    from CNN.build_model import cascade_model

    # --------------------------------------------------
    # net configuration
    # take into account if the pretrained models have to be used
    # all images share the same network model
    # --------------------------------------------------
    options['full_train'] = True
    options['load_weights'] = True
    options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets')
    options['net_verbose'] = 0
    model = cascade_model(options)

    # --------------------------------------------------
    # process each of the scans
    # - image identification
    # - image registration
    # - skull-stripping
    # - WM segmentation
    # --------------------------------------------------
    options['task'] = 'testing'
    scan_list = os.listdir(options['test_folder'])
    scan_list.sort()

    for scan in scan_list:

        total_time = time.time()
        options['tmp_scan'] = scan
        # --------------------------------------------------
        # move things to a tmp folder before starting
        # --------------------------------------------------

        current_folder = os.path.join(options['test_folder'], scan)
        options['tmp_folder'] = os.path.normpath(
            os.path.join(current_folder, 'tmp'))

        # --------------------------------------------------
        # preprocess scans
        # --------------------------------------------------
        preprocess_scan(current_folder, options)

        # --------------------------------------------------
        # WM MS lesion inference
        # --------------------------------------------------
        seg_time = time.time()

        "> CNN:", scan, "running WM lesion segmentation"
        sys.stdout.flush()
        options['test_scan'] = scan

        test_x_data = {
            scan: {
                m: os.path.join(options['tmp_folder'], n)
                for m, n in zip(options['modalities'], options['x_names'])
            }
        }

        test_cascaded_model(model, test_x_data, options)
        print "> INFO:", scan, "CNN Segmentation time: ", round(
            time.time() - seg_time), "sec"
        print "> INFO:", scan, "total pipeline time: ", round(
            time.time() - total_time), "sec"

        # remove tmps if not set
        if options['save_tmp'] is False:
            try:
                os.rmdir(options['tmp_folder'])
                os.rmdir(
                    os.path.join(options['current_folder'],
                                 options['experiment']))
            except:
                pass

    print "> INFO: All processes have been finished. Have a good day!"
コード例 #3
0
def infer_segmentation(options):
    """
    Infer segmentation given the input options passed as parameters
    """

    # define the training backend
    define_backend(options)

    from CNN.base import test_cascaded_model
    from CNN.build_model import cascade_model

    # --------------------------------------------------
    # net configuration
    # take into account if the pretrained models have to be used
    # all images share the same network model
    # --------------------------------------------------
    options['full_train'] = True
    options['load_weights'] = True
    options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets')
    options['net_verbose'] = 0
    model = cascade_model(options)

    # --------------------------------------------------
    # process each of the scans
    # - image identification
    # - image registration
    # - skull-stripping
    # - WM segmentation
    # --------------------------------------------------
    options['task'] = 'testing'
    scan_list = os.listdir(options['test_folder'])
    scan_list.sort()

    for scan in scan_list:

        total_time = time.time()
        options['tmp_scan'] = scan
        # --------------------------------------------------
        # move things to a tmp folder before starting
        # --------------------------------------------------

        current_folder = os.path.join(options['test_folder'], scan)
        options['tmp_folder'] = os.path.normpath(
            os.path.join(current_folder, 'tmp'))

        # --------------------------------------------------
        # preprocess scans
        # --------------------------------------------------
        preprocess_scan(current_folder, options)

        # --------------------------------------------------
        # WM MS lesion inference
        # --------------------------------------------------
        seg_time = time.time()

        "> CNN:", scan, "running WM lesion segmentation"
        sys.stdout.flush()
        options['test_scan'] = scan

        test_x_data = {
            scan: {
                m: os.path.join(options['tmp_folder'], n)
                for m, n in zip(options['modalities'], options['x_names'])
            }
        }

        test_cascaded_model(model, test_x_data, options)
        print "> INFO:", scan, "CNN Segmentation time: ", round(
            time.time() - seg_time), "sec"
        print "> INFO:", scan, "total pipeline time: ", round(
            time.time() - total_time), "sec"

        # remove tmps if not set
        if options['save_tmp'] is False:
            try:
                os.rmdir(options['tmp_folder'])
                os.rmdir(
                    os.path.join(options['current_folder'],
                                 options['experiment']))
            except:
                pass

    print "> INFO: All processes have been finished. Have a good day!"
コード例 #4
0
def infer_segmentation(options):
    """
    Infer segmentation given the input options passed as parameters
    """

    # define the training backend
    define_backend(options)

    from CNN.base import test_cascaded_model
    from CNN.build_model import cascade_model

    # --------------------------------------------------
    # net configuration
    # take into account if the pretrained models have to be used
    # all images share the same network model
    # --------------------------------------------------
    options['full_train'] = True
    options['load_weights'] = True
    options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets')
    options['net_verbose'] = 0
    model = cascade_model(options)

    # --------------------------------------------------
    # process each of the scans
    # - image identification
    # - image registration
    # - skull-stripping
    # - WM segmentation
    # --------------------------------------------------

    # scan_list = os.listdir(options['test_folder'])
    # scan_list.sort()
    # # check and remove the folder which dose not contain the necessary modalities before prepossessing step
    # for check in scan_list:
    #    check_oututs(check, options)

    # update scan list after removing  the unnecessary folders before prepossessing step

    options['task'] = 'testing'
    scan_list = os.listdir(options['test_folder'])
    scan_list.sort()

    for scan in scan_list:

        total_time = time.time()
        options['tmp_scan'] = scan
        # --------------------------------------------------
        # move things to a tmp folder before starting
        # --------------------------------------------------

        current_folder = os.path.join(options['test_folder'], scan)
        options['tmp_folder'] = os.path.normpath(
            os.path.join(current_folder, 'tmp'))

        # --------------------------------------------------
        # preprocess scans
        # --------------------------------------------------
        # preprocess_scan(current_folder, options)

        # --------------------------------------------------
        # WM MS lesion inference
        # --------------------------------------------------
        seg_time = time.time()

        print("> CNN:", scan, "running WM lesion segmentation")
        sys.stdout.flush()
        options['test_scan'] = scan

        test_x_data = {
            scan: {
                m: os.path.join(options['tmp_folder'], n)
                for m, n in zip(options['modalities'], options['x_names'])
            }
        }

        test_cascaded_model(model, test_x_data, options)

        if options['register_modalities']:
            # print("> INFO:", scan, "Inverting lesion segmentation masks")
            print(CYELLOW + "Inverting lesion segmentation masks:",
                  CRED + scan + CEND, ".....started!" + CEND)
            invert_registration(current_folder, options)

        print("> INFO:", scan, "CNN Segmentation time: ",
              round(time.time() - seg_time), "sec")
        print("> INFO:", scan, "total pipeline time: ",
              round(time.time() - total_time), "sec")

        # remove tmps if not set
        if options['save_tmp'] is False:
            try:
                os.rmdir(options['tmp_folder'])
                os.rmdir(
                    os.path.join(options['current_folder'],
                                 options['experiment']))
            except:
                pass

    print("> INFO: All processes have been finished.")