def test_dataframes_equal(self): lrochead = igen.ReadLROCHeadCombinedCraterCSV( filelroc="../catalogues/LROCCraters.csv", filehead="../catalogues/HeadCraters.csv", sortlat=True) lrochead_nosort = igen.ReadLROCHeadCombinedCraterCSV( filelroc="../catalogues/LROCCraters.csv", filehead="../catalogues/HeadCraters.csv", sortlat=False) assert np.all(lrochead == self.lrochead_t) assert not np.all(lrochead == lrochead_nosort)
def get_craters(lroc_csv_path, head_csv_path, sub_cdim, R_km): sys.path.append('../../DeepMoon/') import input_data_gen as igen craters = igen.ReadLROCHeadCombinedCraterCSV(filelroc=lroc_csv_path, filehead=head_csv_path) craters = igen.ResampleCraters(craters, sub_cdim, None, arad=R_km) return craters
def setup(self): # Seed. self.seed = 1337 # Image length. self.imlen = 256 # Image. self.img = Image.open( "LunarLROLrocKaguya_1180mperpix_downsamp.png").convert("L") self.imgsize = self.img.size # Crater catalogue. self.craters = igen.ReadLROCHeadCombinedCraterCSV( filelroc="../catalogues/LROCCraters.csv", filehead="../catalogues/HeadCraters.csv", sortlat=True) # Long/lat limits self.cdim = [-180., 180., -60., 60.] # Coordinate systems. self.iglobe = ccrs.Globe(semimajor_axis=1737400, semiminor_axis=1737400, ellipse=None)
start_time = time.time() # Utilize mpi4py for multithreaded processing. if use_mpi4py: from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() print("Thread {0} of {1}".format(rank, size)) istart = rank * amt else: istart = 0 # Read source image and crater catalogs. img = Image.open(source_image_path).convert("L") craters = igen.ReadLROCHeadCombinedCraterCSV(filehead=head_csv_path) # Sample subset of image. Co-opt igen.ResampleCraters to remove all # craters beyond cdim (either sub or source). if sub_cdim != source_cdim: img = igen.InitialImageCut(img, source_cdim, sub_cdim) # This always works, since sub_cdim < source_cdim. craters = igen.ResampleCraters(craters, sub_cdim, None, arad=R_km) # Generate input images. igen.GenDataset(img, craters, outhead, rawlen_range=rawlen_range, rawlen_dist=rawlen_dist, ilen=ilen,
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")