示例#1
0
def detect_craters_with_model(model, sd_input_images, ctrs):
    pred = model.predict(sd_input_images)    
    images_count = len(sd_input_images)
    for i in range(images_count):
        print(i)
        labeled = get_craters_from_database(ctrs, i)
        predicted = tmt.template_match_t(pred[i].copy(), minrad=2.)
示例#2
0
def main():
    # pdb.set_trace()
    zenodo_path = './mars_data/'
    model = load_model('models/model_30k.h5')

    #### TEST CRATERS
    test_imgs = h5py.File(zenodo_path + '/mars_images_5k.hdf5', 'r')

    test_data = {
        'imgs': [
            test_imgs['input_images'][...].astype('float32'),
            test_imgs['target_masks'][...].astype('float32')
        ]
    }
    proc.preprocess(test_data)
    sd_input_images = test_data['imgs'][0]
    sd_target_masks = test_data['imgs'][1]

    images = [25, 36]
    plot_dir = "plots"

    for iwant in images:
        pred = model.predict(sd_input_images[iwant:iwant + 1])
        extracted_rings = tmt.template_match_t(
            pred[0].copy(), minrad=2.)  # x coord, y coord, radius

        fig = plt.figure(figsize=[16, 16])
        plt.rcParams["font.size"] = 20
        [[ax1, ax4], [ax2, ax3]] = fig.subplots(2, 2)
        ax1.imshow(sd_input_images[iwant].squeeze(),
                   origin='upper',
                   cmap='Greys_r',
                   vmin=0,
                   vmax=1.1)
        ax2.imshow(sd_target_masks[iwant].squeeze(),
                   origin='upper',
                   cmap='Greys_r')
        ax3.imshow(pred[0], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
        ax4.imshow(sd_input_images[iwant].squeeze(),
                   origin='upper',
                   cmap="Greys_r")
        for x, y, r in extracted_rings:
            circle = plt.Circle((x, y),
                                r,
                                color='blue',
                                fill=False,
                                linewidth=2,
                                alpha=0.5)
            ax4.add_artist(circle)
        ax1.set_title('Mars DEM Image')
        ax2.set_title('Ground-Truth Target Mask')
        ax3.set_title('CNN Predictions')
        ax4.set_title('Post-CNN Craters')

        current_dir = os.path.dirname(os.path.abspath(__file__))
        plot_location = os.path.join(
            current_dir,
            plot_dir + "/trained_model_results" + str(iwant) + ".png")
        plt.savefig(plot_location)
示例#3
0
def extract_unique_craters(CP, craters_unique):
    """Top level function that extracts craters from model predictions,
    converts craters from pixel to real (degree, km) coordinates, and filters
    out duplicate detections across images.

    Parameters
    ----------
    CP : dict
        Crater Parameters needed to run the code.
    craters_unique : array
        Empty master array of unique crater tuples in the form 
        (long, lat, radius).

    Returns
    -------
    craters_unique : array
        Filled master array of unique crater tuples.
    """

    # Load/generate model preds
    try:
        preds = h5py.File(CP['dir_preds'], 'r')[CP['datatype']]
        print("Loaded model predictions successfully")
    except:
        print("Couldnt load model predictions, generating")
        preds = get_model_preds(CP)

    # need for long/lat bounds
    P = h5py.File(CP['dir_data'], 'r')
    llbd, pbd, distcoeff = ('longlat_bounds', 'pix_bounds',
                            'pix_distortion_coefficient')
    #r_moon = 1737.4
    dim = (float(CP['dim']), float(CP['dim']))

    N_matches_tot = 0
    for i in range(CP['start_of_images'],
                   CP['start_of_images'] + CP['n_imgs']):
        id = proc.get_id(i)

        coords = tmt.template_match_t(preds[i])

        # convert, add to master dist
        if len(coords) > 0:

            new_craters_unique = estimate_longlatdiamkm(
                dim, P[llbd][id], P[distcoeff][id][0], coords)
            N_matches_tot += len(coords)

            # Only add unique (non-duplicate) craters
            if len(craters_unique) > 0:
                craters_unique = add_unique_craters(new_craters_unique,
                                                    craters_unique, CP['llt2'],
                                                    CP['rt2'])
            else:
                craters_unique = np.concatenate(
                    (craters_unique, new_craters_unique))

    np.save(CP['dir_result'], craters_unique)
    return craters_unique
def get_matched_craters_indices_in_single_image(my_craters, model_prediction,
                                                all_craters):
    [real_coordinates, labeled_craters] = get_image_craters_data(my_craters)
    predicted = tmt.template_match_t(model_prediction, minrad=2.)
    [match, fn, fp] = sc.match_circle_lists(labeled_craters, predicted, 0.1)
    matched_craters = [x[0] for x in match]
    matched_crater_indices = []
    for detected_crater in real_coordinates[matched_craters]:
        crater_index_in_list = get_index_of_detected_crater_in_the_list(
            all_craters, detected_crater)
        matched_crater_indices.append(crater_index_in_list)
    return np.array(matched_crater_indices)
示例#5
0
def main():
    # pdb.set_trace()
    path = './input_data/'
    model = load_model('models/model_keras1.2.2.h5')

    #### TEST CRATERS
    test_imgs = h5py.File(path + '/Mercury_images.hdf5', 'r')

    images = test_imgs['input_images'][:100, :, :].astype('float32')
    test_data = {'imgs': [images[np.sum(images, axis=(1, 2)) > 0]]}
    #for img in test_data['imgs'][0]:
    #    print(np.sum(img), img[img > 0].shape)
    proc.preprocess(test_data)
    sd_input_images = test_data['imgs'][0]

    print(len(sd_input_images))
    images = [2, 10, 20, 44]
    plot_dir = "plots"

    for iwant in images:
        print("Predicting on image", iwant)
        pred = model.predict(sd_input_images[iwant:iwant + 1])
        extracted_rings = tmt.template_match_t(
            pred[0].copy(), minrad=2.)  # x coord, y coord, radius
        fig = plt.figure(figsize=[9, 9])
        [ax1, ax2, ax3] = fig.subplots(1, 3)
        ax1.imshow(sd_input_images[iwant].squeeze(),
                   origin='upper',
                   cmap='Greys_r',
                   vmin=0,
                   vmax=1.1)
        ax2.imshow(pred[0], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
        ax3.imshow(sd_input_images[iwant].squeeze(),
                   origin='upper',
                   cmap="Greys_r")
        for x, y, r in extracted_rings:
            circle = plt.Circle((x, y),
                                r,
                                color='blue',
                                fill=False,
                                linewidth=2,
                                alpha=0.5)
            ax3.add_artist(circle)
        ax1.set_title('Mercury DEM Image')
        ax2.set_title('CNN Predictions')
        ax3.set_title('Post-CNN Craters')

        current_dir = os.path.dirname(os.path.abspath(__file__))
        plot_location = os.path.join(
            current_dir,
            plot_dir + "/trained_model_results_Mercury" + str(iwant) + ".png")
        plt.savefig(plot_location)
示例#6
0
def extract_unique_craters(CP, craters_unique):
    """Top level function that extracts craters from model predictions,
    converts craters from pixel to real (degree, km) coordinates, and filters
    out duplicate detections across images.

    Parameters
    ----------
    CP : dict
        Crater Parameters needed to run the code.
    craters_unique : array
        Empty master array of unique crater tuples in the form 
        (long, lat, radius).

    Returns
    -------
    craters_unique : array
        Filled master array of unique crater tuples.
    """

    # Load/generate model preds
    try:
        preds = h5py.File(CP['dir_preds'], 'r')[CP['datatype']]
        print("Loaded model predictions successfully")
    except:
        print("Couldnt load model predictions, generating")
        preds = get_model_preds(CP)

    # need for long/lat bounds
    P = h5py.File(CP['dir_data'], 'r')
    llbd, pbd, distcoeff = ('longlat_bounds', 'pix_bounds',
                            'pix_distortion_coefficient')
    #r_moon = 1737.4
    dim = (float(CP['dim']), float(CP['dim']))

    N_matches_tot = 0
    for i in range(CP['start_of_images'],CP['start_of_images']+CP['n_imgs']):
        id = proc.get_id(i)
        
        coords = tmt.template_match_t(preds[i])
示例#7
0
              %f""" % np.max(maxrad))
        print("")

def keys(f):
    return [key for key in f.keys()]
'''             
--------------------------------------------------------------------------------------
'''


preds , data = get_model_preds(CP)

for i in range (1,20):
    img_no=i
 #Use scikit-image template matching to extract crater locations.  Only search for craters with r >= 3 pixels.
    extracted_rings = tmt.template_match_t(preds[img_no].copy(), minrad=1.)
    train_imgs = h5py.File('/home/karan/deepmars/input_data/train_images.hdf5', 'r')

    crater=pd.HDFStore('/home/karan/deepmars/input_data/train_craters.hdf5', 'r')


    fig = plt.figure(figsize=[16, 16])
    [[ax1, ax2], [ax3, ax4]] = fig.subplots(2,2)
    ax1.imshow(train_imgs['input_images'][img_no][...], origin='upper', cmap='Greys_r', vmin=50, vmax=200)
    ax2.imshow(train_imgs['target_masks'][img_no][...], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
    ax3.imshow(preds[img_no], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
    ax4.imshow(train_imgs['input_images'][img_no][...], origin='upper', cmap="Greys_r")
    for x, y, r in extracted_rings:
        circle = plt.Circle((x, y), r, color='blue', fill=False, linewidth=2, alpha=0.5)
    ax4.add_artist(circle)
    ax1.set_title('Venus DEM Image')
def extract_unique_craters(CP, craters_unique):
    """Top level function that extracts craters from model predictions,
    converts craters from pixel to real (degree, km) coordinates, and filters
    out duplicate detections across images.

    Parameters
    ----------
    CP : dict
        Crater Parameters needed to run the code.
    craters_unique : array
        Empty master array of unique crater tuples in the form
        (long, lat, radius).

    Returns
    -------
    craters_unique : array
        Filled master array of unique crater tuples.
    """

    # Load/generate model preds
    try:
        preds = h5py.File(CP['dir_preds'], 'r')[CP['datatype']]

        print("Loaded model predictions successfully")
    except:
        print("Couldnt load model predictions, generating")
        preds = get_model_preds(CP)
    Data, Carters = get_data(CP)
    # need for long/lat bounds
    P = h5py.File(CP['dir_data'], 'r')
    llbd, pbd, distcoeff = ('longlat_bounds', 'pix_bounds',
                            'pix_distortion_coefficient')
    #r_moon = 1737.4
    dim = (float(CP['dim']), float(CP['dim']))

    N_matches_tot = 0
    if not os.path.exists(CP['result_img']):
        os.mkdir(CP['result_img'])
    lenstr = ""
    lenstr1 = "true_carter"
    lenstr2 = "detect_carter"
    lenstr3 = "undetected_carter"
    num = 0
    num1 = 0
    num2 = 0
    num3 = 0
    for i in range(CP['n_imgs']):
        id = proc.get_id(i, 2)
        print("Drawing picture:%d" % i)
        input_images = Data[CP['datatype']][0][i]
        imgs = Image.fromarray(input_images.astype('uint8')).convert('RGB')
        img = cv2.cvtColor(np.asarray(imgs), cv2.COLOR_RGB2BGR)

        coords = tmt.template_match_t(preds[i])
        num = num + len(coords)
        lenstr = lenstr + " " + str(len(coords))
        matplotlib.image.imsave(CP['result_img'] + "/" + str(i) + '_mask.jpg',
                                preds[i])
        true_carter, detect_carter, Undetected_carter = get_coords_classification(
            coords, Carters[i])
        lenstr1 = lenstr1 + " " + str(len(true_carter))
        num1 = num1 + len(true_carter)
        lenstr2 = lenstr2 + " " + str(len(detect_carter))
        num2 = num2 + len(detect_carter)
        lenstr3 = lenstr3 + " " + str(len(Undetected_carter))
        num3 = num3 + len(Undetected_carter)
        draw_pic(img, coords, Carters[i],
                 CP['result_img'] + "/" + str(i) + '.jpg')
        if len(coords) > 0:
            # for i in range(len(coords)):
            new_craters_unique = estimate_longlatdiamkm(
                dim, P[llbd][id], P[distcoeff][id][0], coords)
            N_matches_tot += len(coords)
            #print(id,new_craters_unique)
            # Only add unique (non-duplicate) craters
            if len(craters_unique) > 0:
                craters_unique = add_unique_craters(new_craters_unique,
                                                    craters_unique, CP['llt2'],
                                                    CP['rt2'])
            else:
                craters_unique = np.concatenate(
                    (craters_unique, new_craters_unique))
    print(lenstr)
    print("total num:%d" % num)
    print(lenstr1)
    print(num1)
    print(lenstr2)
    print(num2)
    print(lenstr3)
    print(num3)
    np.save(CP['dir_result'], craters_unique)
    return craters_unique
示例#9
0
def main():
    pdb.set_trace()
    zenodo_path = './data/'
    deepmoon_path = os.path.dirname(os.getcwd())
    train_imgs = h5py.File(zenodo_path + 'train_images.hdf5', 'r')
    fig = plt.figure(figsize=[12, 6])
    [ax1, ax2] = fig.subplots(1, 2)
    ax1.imshow(train_imgs['input_images'][3][...],
               origin='upper',
               cmap='Greys_r',
               vmin=120,
               vmax=200)
    ax2.imshow(train_imgs['target_masks'][3][...],
               origin='upper',
               cmap='Greys_r')
    plt.show()

    ctrs = pd.HDFStore(zenodo_path + 'train_craters.hdf5', 'r')

    # pdb.set_trace()

    Image.MAX_IMAGE_PIXELS = None
    img = Image.open(zenodo_path +
                     "/LunarLROLrocKaguya_118mperpix.png").convert("L")
    # Read and combine the LROC and Head datasets (stored under ../catalogues)
    craters = igen.ReadLROCHeadCombinedCraterCSV(
        filelroc="catalogues/LROCCraters.csv",
        filehead="catalogues/HeadCraters.csv")

    # Generate 100 image/target sets, and corresponding crater dataframes.  np.random.seed is set for consistency.
    igen.GenDataset(img,
                    craters,
                    zenodo_path + '/test_zenodo',
                    amt=25,
                    seed=1337)

    gen_imgs = h5py.File(zenodo_path + '/test_zenodo_images.hdf5', 'r')
    sample_data = {
        'imgs': [
            gen_imgs['input_images'][...].astype('float32'),
            gen_imgs['target_masks'][...].astype('float32')
        ]
    }
    proc.preprocess(
        sample_data
    )  #now, input images is shape 25 x 256 x 256 x 1, target_masks is shape 25 x 256 x 256

    sd_input_images = sample_data['imgs'][0]  #25 x 256 x 256 x 1
    sd_target_masks = sample_data['imgs'][1]  #25 x 256 x 256p

    # Plot the data for fun.
    fig = plt.figure(figsize=[12, 6])
    [ax1, ax2] = fig.subplots(1, 2)
    ax1.imshow(sd_input_images[5].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[5].squeeze(), origin='upper', cmap='Greys_r')
    plt.savefig("plots/processed_image_and_mask1.png")

    fig = plt.figure(figsize=[12, 6])
    [ax1, ax2] = fig.subplots(1, 2)
    ax1.imshow(sd_input_images[8].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[8].squeeze(), origin='upper', cmap='Greys_r')
    plt.savefig("plots/processed_image_and_mask2.png")

    fig = plt.figure(figsize=[12, 6])
    [ax1, ax2] = fig.subplots(1, 2)
    ax1.imshow(sd_input_images[12].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[12].squeeze(), origin='upper', cmap='Greys_r')
    plt.savefig("plots/processed_image_and_mask3.png")

    fig = plt.figure(figsize=[12, 6])
    [ax1, ax2] = fig.subplots(1, 2)
    ax1.imshow(sd_input_images[16].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[16].squeeze(), origin='upper', cmap='Greys_r')
    plt.savefig("plots/processed_image_and_mask4.png")

    sample_images = train_imgs['input_images'][0:20]
    sample_masks = train_imgs['target_masks'][0:20]

    pdb.set_trace()
    model = load_model('models/model_30k.h5')

    #### TEST CRATERS
    test_imgs = h5py.File(zenodo_path + '/test_images.hdf5', 'r')

    test_data = {
        'imgs': [
            test_imgs['input_images'][...].astype('float32'),
            test_imgs['target_masks'][...].astype('float32')
        ]
    }
    proc.preprocess(test_data)
    sd_input_images = test_data['imgs'][0]
    sd_target_masks = test_data['imgs'][1]

    iwant = 3
    pred = model.predict(sd_input_images[iwant:iwant + 1])
    extracted_rings = tmt.template_match_t(
        pred[0].copy(), minrad=2.)  # x coord, y coord, radius

    fig = plt.figure(figsize=[16, 16])
    [[ax1, ax2], [ax3, ax4]] = fig.subplots(2, 2)
    ax1.imshow(sd_input_images[iwant].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[iwant].squeeze(),
               origin='upper',
               cmap='Greys_r')
    ax3.imshow(pred[0], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
    ax4.imshow(sd_input_images[iwant].squeeze(),
               origin='upper',
               cmap="Greys_r")
    for x, y, r in extracted_rings:
        circle = plt.Circle((x, y),
                            r,
                            color='blue',
                            fill=False,
                            linewidth=2,
                            alpha=0.5)
        ax4.add_artist(circle)
    ax1.set_title('Moon DEM Image')
    ax2.set_title('Ground-Truth Target Mask')
    ax3.set_title('CNN Predictions')
    ax4.set_title('Post-CNN Craters')
    plt.savefig("plots/trained_model_results1.png")

    iwant = 6
    pred = model.predict(sd_input_images[iwant:iwant + 1])
    extracted_rings = tmt.template_match_t(
        pred[0].copy(), minrad=2.)  # x coord, y coord, radius

    fig = plt.figure(figsize=[16, 16])
    [[ax1, ax2], [ax3, ax4]] = fig.subplots(2, 2)
    ax1.imshow(sd_input_images[iwant].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[iwant].squeeze(),
               origin='upper',
               cmap='Greys_r')
    ax3.imshow(pred[0], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
    ax4.imshow(sd_input_images[iwant].squeeze(),
               origin='upper',
               cmap="Greys_r")
    for x, y, r in extracted_rings:
        circle = plt.Circle((x, y),
                            r,
                            color='blue',
                            fill=False,
                            linewidth=2,
                            alpha=0.5)
        ax4.add_artist(circle)
    ax1.set_title('Moon DEM Image')
    ax2.set_title('Ground-Truth Target Mask')
    ax3.set_title('CNN Predictions')
    ax4.set_title('Post-CNN Craters')
    plt.savefig("plots/trained_model_results2.png")

    iwant = 13
    pred = model.predict(sd_input_images[iwant:iwant + 1])
    extracted_rings = tmt.template_match_t(
        pred[0].copy(), minrad=2.)  # x coord, y coord, radius

    fig = plt.figure(figsize=[16, 16])
    [[ax1, ax2], [ax3, ax4]] = fig.subplots(2, 2)
    ax1.imshow(sd_input_images[iwant].squeeze(),
               origin='upper',
               cmap='Greys_r',
               vmin=0,
               vmax=1.1)
    ax2.imshow(sd_target_masks[iwant].squeeze(),
               origin='upper',
               cmap='Greys_r')
    ax3.imshow(pred[0], origin='upper', cmap='Greys_r', vmin=0, vmax=1)
    ax4.imshow(sd_input_images[iwant].squeeze(),
               origin='upper',
               cmap="Greys_r")
    for x, y, r in extracted_rings:
        circle = plt.Circle((x, y),
                            r,
                            color='blue',
                            fill=False,
                            linewidth=2,
                            alpha=0.5)
        ax4.add_artist(circle)
    ax1.set_title('Moon DEM Image')
    ax2.set_title('Ground-Truth Target Mask')
    ax3.set_title('CNN Predictions')
    ax4.set_title('Post-CNN Craters')
    plt.savefig("plots/trained_model_results3.png")