def test_gendataset(self, tmpdir, ringwidth): amt = 10 zeropad = 2 outhead = str(tmpdir.join('gentest')) igen.GenDataset(self.img, self.craters, outhead, rawlen_range=[300, 1000], rawlen_dist='log', ilen=self.imlen, tglen=self.imlen, cdim=self.cdim, minpix=1, ringwidth=ringwidth, amt=amt, istart=0, seed=self.seed) imgs_h5 = h5py.File(outhead + '_images.hdf5', 'r') craters_h5 = pd.HDFStore(outhead + '_craters.hdf5', 'r') for i in range(amt): # Find image number. img_number = "img_{i:0{zp}d}".format(i=i, zp=zeropad) # Load box. box = np.array(imgs_h5['pix_bounds'][img_number][...]) im = self.img.crop(box) im.load() # Obtain long/lat bounds for coordinate transform. ix = box[::2] iy = box[1::2] llong, llat = trf.pix2coord(ix, iy, self.cdim, list(self.img.size), origin='upper') llbd = np.r_[llong, llat[::-1]] # Downsample image. im = im.resize([self.imlen, self.imlen], resample=Image.NEAREST) # Remove all craters that are too small to be seen in image. ctr_sub = igen.ResampleCraters(self.craters, llbd, im.size[1], minpix=1) # Convert Plate Carree to Orthographic. [imgo, ctr_xy, distortion_coefficient, clonglat_xy] = ( igen.PlateCarree_to_Orthographic( im, llbd, ctr_sub, iglobe=self.iglobe, ctr_sub=True, slivercut=0.5)) imgo_arr = np.asanyarray(imgo) # Make target mask. tgt = np.asanyarray(imgo.resize((self.imlen, self.imlen), resample=Image.BILINEAR)) mask = igen.make_mask(ctr_xy, tgt, binary=True, rings=True, ringwidth=ringwidth, truncate=True) assert np.all(imgo_arr == imgs_h5['input_images'][i, ...]) assert np.all(mask == imgs_h5['target_masks'][i, ...]) assert np.all(llbd == imgs_h5['longlat_bounds'][img_number][...]) assert (distortion_coefficient == imgs_h5['pix_distortion_coefficient'][img_number][0]) assert np.all(clonglat_xy[["x", "y"]] == imgs_h5['cll_xy'][img_number][...]) assert np.all(ctr_xy == craters_h5[img_number]) imgs_h5.close() craters_h5.close()
# 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, cdim=sub_cdim, arad=R_km, minpix=minpix, tglen=tglen, binary=True, rings=True, ringwidth=ringwidth, truncate=truncate, amt=amt, istart=istart, verbose=verbose) elapsed_time = time.time() - start_time if verbose: print("Time elapsed: {0:.1f} min".format(elapsed_time / 60.))
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")