コード例 #1
0
def create_submission(contours, data_path, output_path, contour_type='i'):
    if contour_type == 'i':
        weights = 'model_logs/sunnybrook_i_unet_inv.h5'
    else:
        sys.exit('\ncontour type "%s" not recognized\n' % contour_type)

    crop_size = 128
    input_shape = (crop_size, crop_size, 1)
    num_classes = 2
    images, masks = export_all_contours(contours,
                                        data_path,
                                        output_path,
                                        crop_size,
                                        num_classes=num_classes)
    model = unet_model_inv(input_shape,
                           num_classes,
                           num_filters=8,
                           transfer=True,
                           contour_type=contour_type,
                           weights=weights)

    pred_masks = model.predict(images, batch_size=32, verbose=1)
    print('\nEvaluating dev set ...')
    result = model.evaluate(images, masks, batch_size=32)
    result = np.round(result, decimals=10)
    print('\nDev set result {:s}:\n{:s}'.format(str(model.metrics_names),
                                                str(result)))
    num = 0
    for idx, ctr in enumerate(contours):
        img, mask = read_contour(ctr, data_path, num_classes)
        h, w, d = img.shape
        tmp = pred_masks[idx, :]
        tmp = reshape(tmp, to_shape=(h, w, d))
        tmp = np.where(tmp > 0.5, 255, 0).astype('uint8')
        tmp2, coords, hierarchy = cv2.findContours(tmp.copy(), cv2.RETR_LIST,
                                                   cv2.CHAIN_APPROX_NONE)
        if not coords:
            print('\nNo detection in case: {:s}; image: {:d}'.format(
                ctr.case, ctr.img_no))
            coords = np.ones((1, 1, 1, 2), dtype='int')

        if contour_type == 'i':
            man_filename = ctr.ctr_endo_path[ctr.ctr_endo_path.rfind('\\') +
                                             1:]
        elif contour_type == 'm':
            man_filename = ctr.ctr_epi_path[ctr.ctr_epi_path.rfind('\\') + 1:]

        auto_filename = man_filename.replace('manual', 'auto')
        img_filename = re.sub(r'-[io]contour-manual.txt', '.dcm', man_filename)
        man_full_path = os.path.join(save_dir, ctr.case, 'contours-manual',
                                     'IRCCI-expert')
        auto_full_path = os.path.join(save_dir, ctr.case, 'contours-auto',
                                      'FCN')
        img_full_path = os.path.join(save_dir, ctr.case, 'DICOM')
        dcm = 'IM-0001-%04d.dcm' % (ctr.img_no)
        dcm_path = os.path.join(data_path, ctr.case, 'DICOM', dcm)
        overlay_full_path = os.path.join(save_dir, ctr.case, 'Overlay')
        for dirpath in [
                man_full_path, auto_full_path, img_full_path, overlay_full_path
        ]:
            if not os.path.exists(dirpath):
                os.makedirs(dirpath)
            if 'DICOM' in dirpath:
                src = dcm_path
                dst = os.path.join(dirpath, img_filename)
                shutil.copyfile(src, dst)
            elif 'Overlay' in dirpath:
                draw_result(ctr, data_path, overlay_full_path, contour_type,
                            coords)
            else:
                dst = os.path.join(auto_full_path, auto_filename)
                if not os.path.exists(auto_full_path):
                    os.makedirs(auto_full_path)
                with open(dst, 'wb') as f:
                    for cd in coords:
                        cd = np.squeeze(cd)
                        if cd.ndim == 1:
                            np.savetxt(f, cd, fmt='%d', delimiter=' ')
                        else:
                            for coord in cd:
                                np.savetxt(f, coord, fmt='%d', delimiter=' ')

    print('\nNumber of multiple detections: {:d}'.format(num))
    dst_eval = os.path.join(save_dir,
                            'evaluation_{:s}.txt'.format(contour_type))
    with open(dst_eval, 'wb') as f:
        f.write(
            ('Dev set result {:s}:\n{:s}'.format(str(model.metrics_names),
                                                 str(result))).encode('utf-8'))
        f.close()

    # Detailed evaluation:
    masks = np.squeeze(masks)
    pred_masks = np.squeeze(pred_masks)
    detail_eval = os.path.join(
        save_dir, 'evaluation_detail_{:s}.csv'.format(contour_type))
    evalArr = dice_coef_each(masks, pred_masks)
    caseArr = [ctr.case for ctr in contours]
    imgArr = [ctr.img_no for ctr in contours]
    resArr = [caseArr, imgArr]
    resArr.append(list(evalArr))
    resArr = np.transpose(resArr)
    np.savetxt(detail_eval, resArr, fmt='%s', delimiter=',')
def create_submission(contours, data_path, output_path, contour_type='i'):

    weight_t = 'model_logs/sunnybrook_a_unetres_inv_time.h5'
    weight_s = 'model_logs/sunnybrook_i_unetres_inv_drop_acdc.h5'
    crop_size = 128
    num_phases = 5
    num_classes = 2
    phase_dilation = 4
    input_shape = (num_phases, crop_size, crop_size, 1)
    input_shape_s = (crop_size, crop_size, 1)
    model_s = unet_res_model_Inv(input_shape_s,
                                 num_classes,
                                 nb_filters=8,
                                 transfer=True,
                                 contour_type=contour_type,
                                 weights=weight_s)
    model_t = unet_res_model_time(input_shape,
                                  num_classes,
                                  nb_filters=32,
                                  n_phases=num_phases,
                                  dilation=1,
                                  transfer=True,
                                  contour_type=contour_type,
                                  weights=weight_t)
    images, masks = export_all_contours(contours,
                                        data_path,
                                        output_path,
                                        crop_size=crop_size,
                                        num_classes=num_classes,
                                        num_phases=num_phases,
                                        phase_dilation=phase_dilation)
    s, p, h, w, d = images.shape
    print('\nFirst step predict set ...')
    temp_image_t = np.reshape(images, (s * p, h, w, d))
    temp_mask_t = model_s.predict(temp_image_t, batch_size=4, verbose=1)
    temp_mask_t = np.reshape(temp_mask_t, (s, p, h, w, d))
    # for idx_s in range(s):
    # img_t = images[idx_s,...]
    # temp_mask_t[idx_s] = model_s.predict(img_t)

    # for idx_p in range(p):
    #     mask = temp_mask_t[idx_s, idx_p, ...]
    #     img = images[idx_s, idx_p, ...]
    #     img = np.squeeze(img*mask)
    #     img_name = '{:d}-{:d}'.format(idx_s, idx_p)
    #     imsave(os.path.join(TEMP_CONTOUR_PATH, img_name + ".png"), img)

    print('\nTotal sample is {:d} for 2nd evaluation.'.format(s))
    print('\nSecond step predict set ...')
    pred_masks = model_t.predict(temp_mask_t, batch_size=4, verbose=1)
    print('\nEvaluating dev set ...')
    result = model_t.evaluate(temp_mask_t, masks, batch_size=4)
    result = np.round(result, decimals=10)
    print('\nDev set result {:s}:\n{:s}'.format(str(model_t.metrics_names),
                                                str(result)))

    for idx, ctr in enumerate(contours):
        print('\nPredict image sequence {:d}'.format(idx))
        img, mask = read_contour(ctr,
                                 data_path,
                                 num_classes,
                                 num_phases,
                                 num_phases_in_cycle=20,
                                 phase_dilation=phase_dilation)
        p, h, w, d = img.shape
        tmp = np.squeeze(pred_masks[idx, :])
        if tmp.ndim == 2:
            tmp = tmp[:, :, np.newaxis]
        tmp = np.where(tmp > 0.5, 255, 0).astype('uint8')
        tmp2, coords, hierarchy = cv2.findContours(tmp.copy(), cv2.RETR_LIST,
                                                   cv2.CHAIN_APPROX_NONE)

        if not coords:
            print('\nNo detection in case: {:s}; image: {:d}'.format(
                ctr.case, ctr.img_no))
            coords = np.ones((1, 1, 1, 2), dtype='int')

        overlay_full_path = os.path.join(save_dir, ctr.case, 'Overlay')
        draw_result(ctr, data_path, overlay_full_path, contour_type, coords)

    dst_eval = os.path.join(save_dir,
                            'evaluation_{:s}.txt'.format(contour_type))
    with open(dst_eval, 'wb') as f:
        f.write(
            ('Dev set result {:s}:\n{:s}'.format(str(model_t.metrics_names),
                                                 str(result))).encode('utf-8'))
        f.close()

    # Detailed evaluation:
    masks = np.squeeze(masks)
    pred_masks = np.squeeze(pred_masks)
    detail_eval = os.path.join(
        save_dir, 'evaluation_detail_{:s}.csv'.format(contour_type))
    evalArr = dice_coef_each(masks, pred_masks)
    caseArr = [ctr.case for ctr in contours]
    imgArr = [ctr.img_no for ctr in contours]
    resArr = [caseArr, imgArr]
    resArr.append(list(evalArr))
    resArr = np.transpose(resArr)
    np.savetxt(detail_eval, resArr, fmt='%s', delimiter=',')
コード例 #3
0
def create_submission(contours, data_path, output_path):
    if contour_type == 'i':
        weights = 'model_logs/sunnybrook_i_unetresnet_epoch100_aug4.h5'
    elif contour_type == 'm':
        weights = 'model_logs/sunnybrook_m.h5'
    else:
        sys.exit('\ncontour type "%s" not recognized\n' % contour_type)

    crop_size = 128
    input_shape = (crop_size, crop_size, 1)
    num_classes = 4
    images, masks = export_all_contours(contours,
                                        data_path,
                                        output_path,
                                        crop_size,
                                        num_classes=num_classes)
    model = unet_res_model(input_shape,
                           num_classes,
                           weights=weights,
                           transfer=True)

    pred_masks = model.predict(images, batch_size=32, verbose=1)
    print('\nEvaluating dev set ...')
    result = model.evaluate(images, masks, batch_size=32)
    result = np.round(result, decimals=10)
    print('\nDev set result {:s}:\n{:s}'.format(str(model.metrics_names),
                                                str(result)))
    num = 0
    for idx, ctr in enumerate(contours):
        img, mask = read_contour(ctr, data_path, num_classes)
        h, w, d = img.shape
        if contour_type == 'i':
            tmp = pred_masks[idx, ..., 3]
        elif contour_type == 'm':
            tmp = pred_masks[idx, ..., 2]
        elif contour_type == 'r':
            tmp = pred_masks[idx, ..., 1]
        #cv2.imwrite(data_path + '\\tmp\\' + 'i' + '{:04d}.png'.format(idx), pred_masks[idx,...,3])
        #cv2.imwrite(data_path + '\\tmp\\' + 'o' + '{:04d}.png'.format(idx), pred_masks[idx,...,2])
        #cv2.imwrite(data_path + '\\tmp\\' + 'r' + '{:04d}.png'.format(idx), pred_masks[idx, ..., 1])
        tmp = tmp[..., np.newaxis]
        tmp = reshape(tmp, to_shape=(h, w, d))
        tmp = np.where(tmp > 0.5, 255, 0).astype('uint8')
        tmp2, coords, hierarchy = cv2.findContours(tmp.copy(), cv2.RETR_LIST,
                                                   cv2.CHAIN_APPROX_NONE)
        if not coords:
            print('\nNo detection in case: {:s}; image: {:d}'.format(
                ctr.case, ctr.img_no))
            coords = np.ones((1, 1, 1, 2), dtype='int')
        if len(coords) > 1:
            print('\nMultiple detections in case: {:s}; image: {:d}'.format(
                ctr.case, ctr.img_no))
            lengths = []
            for coord in coords:
                lengths.append(len(coord))
            coords = [coords[np.argmax(lengths)]]
            num += 1
        coords = np.squeeze(coords)

        if contour_type == 'i':
            man_filename = ctr.ctr_endo_path[ctr.ctr_endo_path.rfind('\\') +
                                             1:]
        elif contour_type == 'o':
            man_filename = ctr.ctr_epi_path[ctr.ctr_epi_path.rfind('\\') + 1:]

        auto_filename = man_filename.replace('manual', 'auto')
        img_filename = re.sub(r'-[io]contour-manual.txt', '.dcm', man_filename)
        man_full_path = os.path.join(save_dir, ctr.case, 'contours-manual',
                                     'IRCCI-expert')
        auto_full_path = os.path.join(save_dir, ctr.case, 'contours-auto',
                                      'FCN')
        img_full_path = os.path.join(save_dir, ctr.case, 'DICOM')
        dcm = 'IM-0001-%04d.dcm' % (ctr.img_no)
        #dcm = 'IM-%s-%04d.dcm' % (SAX_SERIES[ctr.case], ctr.img_no)
        dcm_path = os.path.join(data_path, ctr.case, 'DICOM', dcm)
        overlay_full_path = os.path.join(save_dir, ctr.case, 'Overlay')
        for dirpath in [
                man_full_path, auto_full_path, img_full_path, overlay_full_path
        ]:
            if not os.path.exists(dirpath):
                os.makedirs(dirpath)
            if 'manual' in dirpath:
                if contour_type == 'i':
                    src = ctr.ctr_endo_path
                elif contour_type == 'o':
                    src = ctr.ctr_epi_path
                dst = os.path.join(dirpath, man_filename)
                shutil.copyfile(src, dst)
            elif 'DICOM' in dirpath:
                src = dcm_path
                dst = os.path.join(dirpath, img_filename)
                shutil.copyfile(src, dst)
            elif 'Overlay' in dirpath:
                draw_result(ctr, data_path, overlay_full_path, contour_type,
                            coords)
            else:
                dst = os.path.join(auto_full_path, auto_filename)
                with open(dst, 'wb') as f:
                    if coords.ndim == 1:
                        np.savetxt(f, coords, fmt='%d', delimiter=' ')
                    else:
                        for coord in coords:
                            #coord = np.squeeze(coord, axis=(1,))
                            #coord = np.append(coord, coord[:1], axis=0)
                            np.savetxt(f, coord, fmt='%d', delimiter=' ')

    print('\nNumber of multiple detections: {:d}'.format(num))
    dst_eval = os.path.join(save_dir,
                            'evaluation_{:s}.txt'.format(contour_type))
    with open(dst_eval, 'wb') as f:
        f.write(
            ('Dev set result {:s}:\n{:s}'.format(str(model.metrics_names),
                                                 str(result))).encode('utf-8'))
        f.close()

    # Detailed evaluation:
    detail_eval = os.path.join(
        save_dir, 'evaluation_detail_{:s}.csv'.format(contour_type))
    evalArr = dice_coef_endo_each(masks, pred_masks)
    caseArr = [ctr.case for ctr in contours]
    imgArr = [ctr.img_no for ctr in contours]
    resArr = np.transpose([caseArr, imgArr, evalArr])
    np.savetxt(detail_eval, resArr, fmt='%s', delimiter=',')
def create_submission(contours, data_path, output_path, contour_type='a'):
    if contour_type == 'a':
        weights = 'model_logs/temp_weights.hdf5'
    else:
        sys.exit('\ncontour type "%s" not recognized\n' % contour_type)
    num_phases = 9
    crop_size = 128
    input_shape = (num_phases, crop_size, crop_size, 1)
    num_classes = 3
    images, masks = export_all_contours(contours,
                                        data_path,
                                        output_path,
                                        crop_size,
                                        num_classes=num_classes,
                                        num_phases=num_phases)
    model = unet_model_time(input_shape,
                            downsize_filters_factor=2,
                            pool_size=(1, 2, 2),
                            n_labels=3,
                            initial_learning_rate=0.00001,
                            deconvolution=False,
                            weights=weights)

    pred_masks = model.predict(images, batch_size=8, verbose=1)
    print('\nEvaluating dev set ...')
    result = model.evaluate(images, masks, batch_size=8)
    result = np.round(result, decimals=10)
    print('\nDev set result {:s}:\n{:s}'.format(str(model.metrics_names),
                                                str(result)))
    num = 0

    for c_type in ['i', 'm']:
        for idx, ctr in enumerate(contours):
            img, mask = read_contour(ctr,
                                     data_path,
                                     num_classes,
                                     num_phases=1,
                                     num_phases_in_cycle=20,
                                     phase_dilation=1)
            p, h, w, d = img.shape
            if c_type == 'i':
                tmp = pred_masks[idx, 0, ..., 2]
            elif c_type == 'm':
                tmp = pred_masks[idx, 0, ..., 1]

            tmp = tmp[..., np.newaxis]
            tmp = reshape(tmp, to_shape=(h, w, d))
            tmp = np.where(tmp > 0.5, 255, 0).astype('uint8')
            tmp2, coords, hierarchy = cv2.findContours(tmp.copy(),
                                                       cv2.RETR_LIST,
                                                       cv2.CHAIN_APPROX_NONE)
            if not coords:
                print('\nNo detection in case: {:s}; image: {:d}'.format(
                    ctr.case, ctr.img_no))
                coords = np.ones((1, 1, 1, 2), dtype='int')

            if c_type == 'i':
                man_filename = ctr.ctr_endo_path[ctr.ctr_endo_path.
                                                 rfind('\\') + 1:]
            elif c_type == 'm':
                man_filename = ctr.ctr_epi_path[ctr.ctr_epi_path.rfind('\\') +
                                                1:]

            auto_filename = man_filename.replace('manual', 'auto')
            img_filename = re.sub(r'-[io]contour-manual.txt', '.dcm',
                                  man_filename)
            man_full_path = os.path.join(save_dir, ctr.case, 'contours-manual',
                                         'IRCCI-expert')
            auto_full_path = os.path.join(save_dir, ctr.case, 'contours-auto',
                                          'FCN')
            img_full_path = os.path.join(save_dir, ctr.case, 'DICOM')
            dcm = 'IM-0001-%04d.dcm' % (ctr.img_no)
            # dcm = 'IM-%s-%04d.dcm' % (SAX_SERIES[ctr.case], ctr.img_no)
            dcm_path = os.path.join(data_path, ctr.case, 'DICOM', dcm)
            overlay_full_path = os.path.join(save_dir, ctr.case, 'Overlay')
            for dirpath in [
                    man_full_path, auto_full_path, img_full_path,
                    overlay_full_path
            ]:
                if not os.path.exists(dirpath):
                    os.makedirs(dirpath)
                if 'DICOM' in dirpath:
                    src = dcm_path
                    dst = os.path.join(dirpath, img_filename)
                    shutil.copyfile(src, dst)
                elif 'Overlay' in dirpath:
                    draw_result(ctr, data_path, overlay_full_path, c_type,
                                coords)
                else:
                    dst = os.path.join(auto_full_path, auto_filename)
                    if not os.path.exists(auto_full_path):
                        os.makedirs(auto_full_path)
                    with open(dst, 'wb') as f:
                        for cd in coords:
                            cd = np.squeeze(cd)
                            if cd.ndim == 1:
                                np.savetxt(f, cd, fmt='%d', delimiter=' ')
                            else:
                                for coord in cd:
                                    np.savetxt(f,
                                               coord,
                                               fmt='%d',
                                               delimiter=' ')

        print('\nNumber of multiple detections: {:d}'.format(num))
        dst_eval = os.path.join(save_dir, 'evaluation_{:s}.txt'.format(c_type))
        with open(dst_eval, 'wb') as f:
            f.write(('Dev set result {:s}:\n{:s}'.format(
                str(model.metrics_names), str(result))).encode('utf-8'))
            f.close()

        # Detailed evaluation:
        detail_eval = os.path.join(save_dir,
                                   'evaluation_detail_{:s}.csv'.format(c_type))
        evalEndoArr = dice_coef_endo_each(masks, pred_masks)
        evalMyoArr = dice_coef_myo_each(masks, pred_masks)
        caseArr = [ctr.case for ctr in contours]
        imgArr = [ctr.img_no for ctr in contours]
        resArr = np.transpose([caseArr, imgArr, evalEndoArr, evalMyoArr])
        np.savetxt(detail_eval, resArr, fmt='%s', delimiter=',')