def main(argv): filename = os.path.join(git_root, 'data', 'images', 'tile_8_14.jpeg') image = ctfi.load(filename, width=1024, height=1024, channels=3) image_subsampled = ctfi.subsample(image, 2) ## If not using eager execution #with tf.Session().as_default() as sess: # fig, ax = plt.subplots() # plt.imshow(image_subsampled.eval(session=sess)) # plt.show() # Using eager execution fig, ax = plt.subplots() plt.imshow(image_subsampled.numpy()) plt.show()
def main(argv): parser = argparse.ArgumentParser(description='Compute similarity heatmaps of windows around landmarks.') parser.add_argument('export_dir',type=str,help='Path to saved model.') parser.add_argument('mean', type=str, help='Path to npy file holding mean for normalization.') parser.add_argument('variance', type=str, help='Path to npy file holding variance for normalization.') parser.add_argument('source_filename', type=str,help='Image file from which to extract patch.') parser.add_argument('source_image_size', type=int, nargs=2, help='Size of the input image, HW.') parser.add_argument('source_landmarks', type=str,help='CSV file from which to extract the landmarks for source image.') parser.add_argument('target_filename', type=str,help='Image file for which to create the heatmap.') parser.add_argument('target_image_size', type=int, nargs=2, help='Size of the input image for which to create heatmap, HW.') parser.add_argument('target_landmarks', type=str,help='CSV file from which to extract the landmarks for target image.') parser.add_argument('patch_size', type=int, help='Size of image patch.') parser.add_argument('output', type=str) parser.add_argument('--method', dest='method', type=str, help='Method to use to measure similarity, one of KLD, SKLD, BD, HD, SQHD.') parser.add_argument('--stain_code_size', type=int, dest='stain_code_size', default=0, help='Optional: Size of the stain code to use, which is skipped for similarity estimation') parser.add_argument('--rotate', type=float, dest='angle', default=0, help='Optional: rotation angle to rotate target image') parser.add_argument('--subsampling_factor', type=int, dest='subsampling_factor', default=1, help='Factor to subsample source and target image.') parser.add_argument('--region_size', type=int, default=64) args = parser.parse_args() mean = np.load(args.mean) variance = np.load(args.variance) stddev = [np.math.sqrt(x) for x in variance] def denormalize(image): channels = [np.expand_dims(image[:,:,channel] * stddev[channel] + mean[channel],-1) for channel in range(3)] denormalized_image = ctfi.rescale(np.concatenate(channels, 2), 0.0, 1.0) return denormalized_image def normalize(image, name=None, num_channels=3): channels = [tf.expand_dims((image[:,:,:,channel] - mean[channel]) / stddev[channel],-1) for channel in range(num_channels)] return tf.concat(channels, num_channels) latest_checkpoint = tf.train.latest_checkpoint(args.export_dir) saver = tf.train.import_meta_graph(latest_checkpoint + '.meta', import_scope='imported') config = tf.ConfigProto() config.allow_soft_placement=True #config.log_device_placement=True # Load image and extract patch from it and create distribution. source_image = tf.expand_dims(ctfi.subsample(ctfi.load(args.source_filename,height=args.source_image_size[0], width=args.source_image_size[1]),args.subsampling_factor),0) args.source_image_size = list(map(lambda x: int(x / args.subsampling_factor), args.source_image_size)) #Load image for which to create the heatmap target_image = tf.expand_dims(ctfi.subsample(ctfi.load(args.target_filename,height=args.target_image_size[0], width=args.target_image_size[1]),args.subsampling_factor),0) args.target_image_size = list(map(lambda x: int(x / args.subsampling_factor), args.target_image_size)) source_landmarks = get_landmarks(args.source_landmarks, args.subsampling_factor) target_landmarks = get_landmarks(args.target_landmarks, args.subsampling_factor) region_size = args.region_size region_center = [int(region_size / 2),int(region_size / 2)] num_patches = region_size**2 possible_splits = cutil.get_divisors(num_patches) num_splits = possible_splits.pop(0) while num_patches / num_splits > 512 and len(possible_splits) > 0: num_splits = possible_splits.pop(0) split_size = int(num_patches / num_splits) offset = 64 center_idx = np.prod(region_center) X, Y = np.meshgrid(range(offset, region_size + offset), range(offset, region_size + offset)) coords = np.concatenate([np.expand_dims(Y.flatten(),axis=1),np.expand_dims(X.flatten(),axis=1)],axis=1) coords_placeholder = tf.placeholder(tf.float32, shape=[split_size, 2]) source_landmark_placeholder = tf.placeholder(tf.float32, shape=[1, 2]) target_landmark_placeholder = tf.placeholder(tf.float32, shape=[1, 2]) source_image_region = tf.image.extract_glimpse(source_image,[region_size + 2*offset, region_size+ 2*offset], source_landmark_placeholder, normalized=False, centered=False) target_image_region = tf.image.extract_glimpse(target_image,[region_size + 2*offset, region_size+ 2*offset], target_landmark_placeholder, normalized=False, centered=False) source_patches_placeholder = tf.map_fn(lambda x: get_patch_at(x, source_image, args.patch_size), source_landmark_placeholder, parallel_iterations=8, back_prop=False)[0] target_patches_placeholder = tf.squeeze(tf.map_fn(lambda x: get_patch_at(x, target_image_region, args.patch_size), coords_placeholder, parallel_iterations=8, back_prop=False)) with tf.Session(config=config).as_default() as sess: saver.restore(sess, latest_checkpoint) source_patches_cov, source_patches_mean = tf.contrib.graph_editor.graph_replace([sess.graph.get_tensor_by_name('imported/z_log_sigma_sq/BiasAdd:0'),sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0')] ,{ sess.graph.get_tensor_by_name('imported/patch:0'): normalize(source_patches_placeholder) }) source_patches_distribution = tf.contrib.distributions.MultivariateNormalDiag(source_patches_mean[:,args.stain_code_size:], tf.exp(source_patches_cov[:,args.stain_code_size:])) target_patches_cov, target_patches_mean = tf.contrib.graph_editor.graph_replace([sess.graph.get_tensor_by_name('imported/z_log_sigma_sq/BiasAdd:0'),sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0')] ,{ sess.graph.get_tensor_by_name('imported/patch:0'): normalize(target_patches_placeholder) }) target_patches_distribution = tf.contrib.distributions.MultivariateNormalDiag(target_patches_mean[:,args.stain_code_size:], tf.exp(target_patches_cov[:,args.stain_code_size:])) similarities_skld = source_patches_distribution.kl_divergence(target_patches_distribution) + target_patches_distribution.kl_divergence(source_patches_distribution) similarities_bd = ctf.bhattacharyya_distance(source_patches_distribution, target_patches_distribution) similarities_sad = tf.reduce_sum(tf.abs(source_patches_placeholder - target_patches_placeholder), axis=[1,2,3]) source_patches_grayscale = tf.image.rgb_to_grayscale(source_patches_placeholder) target_patches_grayscale = tf.image.rgb_to_grayscale(target_patches_placeholder) similarities_nmi = tf.map_fn(lambda x: nmi_tf(tf.squeeze(source_patches_grayscale), tf.squeeze(x), 20), target_patches_grayscale) with open(args.output + "_" + str(region_size) + ".csv",'wt') as outfile: fp = csv.DictWriter(outfile, ["method", "landmark", "min_idx", "min_idx_value", "rank", "landmark_value"]) methods = ["SKLD", "BD", "SAD", "MI"] fp.writeheader() results = [] for k in range(len(source_landmarks)): heatmap_fused = np.ndarray((region_size, region_size, len(methods))) feed_dict={source_landmark_placeholder: [source_landmarks[k,:]], target_landmark_placeholder: [target_landmarks[k,:]] } for i in range(num_splits): start = i * split_size end = start + split_size batch_coords = coords[start:end,:] feed_dict.update({coords_placeholder: batch_coords}) similarity_values = np.array(sess.run([similarities_skld,similarities_bd, similarities_sad, similarities_nmi],feed_dict=feed_dict)).transpose() #heatmap.extend(similarity_values) for idx, val in zip(batch_coords, similarity_values): heatmap_fused[idx[0] - offset, idx[1] - offset] = val for c in range(len(methods)): heatmap = heatmap_fused[:,:,c] if c == 3: min_idx = np.unravel_index(np.argmax(heatmap),heatmap.shape) min_indices = np.array(np.unravel_index(list(reversed(np.argsort(heatmap.flatten()))),heatmap.shape)).transpose().tolist() else: min_idx = np.unravel_index(np.argmin(heatmap),heatmap.shape) min_indices = np.array(np.unravel_index(np.argsort(heatmap.flatten()),heatmap.shape)).transpose().tolist() landmark_value = heatmap[region_center[0], region_center[1]] rank = min_indices.index(region_center) fp.writerow({"method": methods[c],"landmark": k, "min_idx": min_idx, "min_idx_value": heatmap[min_idx[0], min_idx[1]],"rank": rank , "landmark_value": landmark_value}) #matplotlib.image.imsave(args.output + "_" + str(region_size)+ "_"+ methods[c] + "_" + str(k) + ".jpeg", heatmap, cmap='plasma') outfile.flush() print(min_idx, rank) fp.writerows(results) sess.close() return 0
def main(argv): parser = argparse.ArgumentParser( description='Compute codes and reconstructions for image.') parser.add_argument('export_dir', type=str, help='Path to saved model.') parser.add_argument( 'mean', type=str, help='Path to npy file holding mean for normalization.') parser.add_argument( 'variance', type=str, help='Path to npy file holding variance for normalization.') parser.add_argument('source_filename', type=str, help='Image file from which to extract patch.') parser.add_argument('source_image_size', type=int, nargs=2, help='Size of the input image, HW.') parser.add_argument('offsets', type=int, nargs=2, help='Position where to extract the patch.') parser.add_argument('patch_size', type=int, help='Size of image patch.') parser.add_argument('target_filename', type=str, help='Image file for which to create the heatmap.') parser.add_argument( 'target_image_size', type=int, nargs=2, help='Size of the input image for which to create heatmap, HW.') parser.add_argument( 'method', type=str, help= 'Method to use to measure similarity, one of KLD, SKLD, BD, HD, SQHD.') parser.add_argument( '--stain_code_size', type=int, dest='stain_code_size', default=0, help= 'Optional: Size of the stain code to use, which is skipped for similarity estimation' ) parser.add_argument('--rotate', type=float, dest='angle', default=0, help='Optional: rotation angle to rotate target image') parser.add_argument('--subsampling_factor', type=int, dest='subsampling_factor', default=1, help='Factor to subsample source and target image.') args = parser.parse_args() mean = np.load(args.mean) variance = np.load(args.variance) stddev = [np.math.sqrt(x) for x in variance] def denormalize(image): channels = [ np.expand_dims( image[:, :, channel] * stddev[channel] + mean[channel], -1) for channel in range(3) ] denormalized_image = ctfi.rescale(np.concatenate(channels, 2), 0.0, 1.0) return denormalized_image def normalize(image, name=None): channels = [ tf.expand_dims( (image[:, :, :, channel] - mean[channel]) / stddev[channel], -1) for channel in range(3) ] return tf.concat(channels, 3, name=name) latest_checkpoint = tf.train.latest_checkpoint(args.export_dir) saver = tf.train.import_meta_graph(latest_checkpoint + '.meta', import_scope='imported') with tf.Session(graph=tf.get_default_graph()).as_default() as sess: # Load image and extract patch from it and create distribution. source_image = ctfi.subsample( ctfi.load(args.source_filename, height=args.source_image_size[0], width=args.source_image_size[1]), args.subsampling_factor) args.source_image_size = list( map(lambda x: int(x / args.subsampling_factor), args.source_image_size)) patch = normalize( tf.expand_dims( tf.image.crop_to_bounding_box(source_image, args.offsets[0], args.offsets[1], args.patch_size, args.patch_size), 0)) patch_cov, patch_mean = tf.contrib.graph_editor.graph_replace([ sess.graph.get_tensor_by_name('imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], {sess.graph.get_tensor_by_name('imported/patch:0'): patch}) #patch_distribution = tf.contrib.distributions.MultivariateNormalTriL(loc=patch_mean[:,args.stain_code_size:], scale_tril=patch_cov[:,args.stain_code_size:,args.stain_code_size:]) patch_descriptor = tf.concat([ patch_mean[:, args.stain_code_size:], tf.layers.flatten(patch_cov[:, args.stain_code_size:]) ], -1) sim_vals = [] structure_code_size = patch_mean.get_shape().as_list( )[1] - args.stain_code_size #Load image for which to create the heatmap target_image = ctfi.subsample( ctfi.load(args.target_filename, height=args.target_image_size[0], width=args.target_image_size[1]), args.subsampling_factor) args.target_image_size = list( map(lambda x: int(x / args.subsampling_factor), args.target_image_size)) target_image = tf.contrib.image.rotate(target_image, np.radians(args.angle)) heatmap_height = args.target_image_size[0] - (args.patch_size - 1) heatmap_width = args.target_image_size[1] - (args.patch_size - 1) # Compute byte size as: width*height*channels*sizeof(float32) patch_size_in_byte = args.patch_size**2 * 3 * 4 max_patches = int(max_patch_buffer_size / patch_size_in_byte) max_num_rows = int(max_patches / heatmap_width) max_chunk_size = int(max_buffer_size_in_byte / patch_size_in_byte) #Iteration over image regions that we can load num_iterations = int(args.target_image_size[0] / max_num_rows) + 1 all_chunks = list() all_similarities = list() chunk_tensors = list() chunk_sizes = np.zeros(num_iterations, dtype=np.int) chunk_sizes.fill(heatmap_width) for i in range(num_iterations): processed_rows = i * max_num_rows rows_to_load = min(max_num_rows + (args.patch_size - 1), args.target_image_size[0] - processed_rows) if rows_to_load < args.patch_size: break # Extract region for which we can compute patches target_image_region = tf.image.crop_to_bounding_box( target_image, processed_rows, 0, rows_to_load, args.target_image_size[1]) # Size = (image_width - patch_size - 1) * (image_height - patch_size - 1) for 'VALID' padding and # image_width * image_height for 'SAME' padding all_image_patches = tf.unstack( normalize( ctfi.extract_patches(target_image_region, args.patch_size, strides=[1, 1, 1, 1], padding='VALID'))) possible_chunk_sizes = get_divisors(len(all_image_patches)) for size in possible_chunk_sizes: if size < max_chunk_size: chunk_sizes[i] = size break # Partition patches into chunks chunked_patches = list( create_chunks(all_image_patches, chunk_sizes[i])) chunked_patches = list(map(tf.stack, chunked_patches)) all_chunks.append(chunked_patches) chunk_tensor = tf.placeholder( tf.float32, shape=[chunk_sizes[i], args.patch_size, args.patch_size, 3], name='chunk_tensor_placeholder') chunk_tensors.append(chunk_tensor) image_patches_cov, image_patches_mean = tf.contrib.graph_editor.graph_replace( [ sess.graph.get_tensor_by_name( 'imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], { sess.graph.get_tensor_by_name('imported/patch:0'): chunk_tensor }) image_patches_descriptors = tf.concat([ image_patches_mean[:, args.stain_code_size:], tf.layers.flatten(image_patches_cov[:, args.stain_code_size:]) ], -1) distances = dist_kl(patch_descriptor, image_patches_descriptors, structure_code_size) similarities = tf.squeeze(distances) all_similarities.append(similarities) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) saver.restore(sess, latest_checkpoint) for i in range(len(all_chunks)): for chunk in all_chunks[i]: #chunk_vals = sess.run(all_similarities[i], feed_dict={chunk_tensors[i]: sess.run(chunk)}) sim_vals.extend( sess.run(all_similarities[i], feed_dict={chunk_tensors[i]: sess.run(chunk)})) print(len(sim_vals)) sim_heatmap = np.reshape(sim_vals, [heatmap_height, heatmap_width]) heatmap_tensor = tf.expand_dims( tf.expand_dims(tf.convert_to_tensor(sim_heatmap), -1), 0) dy, dx = tf.image.image_gradients(heatmap_tensor) sim_vals_normalized = 1.0 - ctfi.rescale(sim_heatmap, 0.0, 1.0) k_min = 20 min_indices = np.unravel_index( np.argsort(sim_vals)[:k_min], sim_heatmap.shape) fig_min, ax_min = plt.subplots(4, 5) for i in range(k_min): target_patch = tf.image.crop_to_bounding_box( target_image, int(min_indices[0][i]), int(min_indices[1][i]), args.patch_size, args.patch_size) ax_min[int(i / 5), int(i % 5)].imshow(sess.run(target_patch)) ax_min[int(i / 5), int(i % 5)].set_title('y:' + str(min_indices[0][i]) + ', x:' + str(min_indices[1][i])) fig, ax = plt.subplots(2, 3) cmap = 'plasma' denormalized_patch = denormalize(sess.run(patch)[0]) max_sim_val = np.max(sim_vals) max_idx = np.unravel_index(np.argmin(sim_heatmap), sim_heatmap.shape) target_image_patch = tf.image.crop_to_bounding_box( target_image, max_idx[0], max_idx[1], args.patch_size, args.patch_size) print(max_idx) print(min_indices) ax[1, 0].imshow(sess.run(source_image)) ax[1, 1].imshow(sess.run(target_image)) ax[0, 0].imshow(denormalized_patch) heatmap_image = ax[0, 2].imshow(sim_heatmap, cmap=cmap) ax[0, 1].imshow(sess.run(target_image_patch)) #dx_image = ax[0,2].imshow(np.squeeze(sess.run(dx)), cmap='bwr') #dy_image = ax[1,2].imshow(np.squeeze(sess.run(dy)), cmap='bwr') gradient_image = ax[1, 2].imshow(np.squeeze(sess.run(dx + dy)), cmap='bwr') fig.colorbar(heatmap_image, ax=ax[0, 2]) #fig.colorbar(dx_image, ax=ax[0,2]) #fig.colorbar(dy_image, ax=ax[1,2]) fig.colorbar(gradient_image, ax=ax[1, 2]) plt.show() sess.close() print("Done!") return 0
def _subsampling_op(features): features['patch'] = ctfi.subsample(features['patch'], 2) return features
def main(argv): parser = argparse.ArgumentParser( description='Compute codes and reconstructions for image.') parser.add_argument('export_dir', type=str, help='Path to saved model.') parser.add_argument( 'mean', type=str, help='Path to npy file holding mean for normalization.') parser.add_argument( 'variance', type=str, help='Path to npy file holding variance for normalization.') parser.add_argument('source_filename', type=str, help='Image file from which to extract patch.') parser.add_argument('source_image_size', type=int, nargs=2, help='Size of the input image, HW.') parser.add_argument('target_filename', type=str, help='Image file for which to create the heatmap.') parser.add_argument( 'target_image_size', type=int, nargs=2, help='Size of the input image for which to create heatmap, HW.') parser.add_argument('patch_size', type=int, help='Size of image patch.') parser.add_argument( '--method', dest='method', type=str, help= 'Method to use to measure similarity, one of KLD, SKLD, BD, HD, SQHD.') parser.add_argument( '--stain_code_size', type=int, dest='stain_code_size', default=0, help= 'Optional: Size of the stain code to use, which is skipped for similarity estimation' ) parser.add_argument('--rotate', type=float, dest='angle', default=0, help='Optional: rotation angle to rotate target image') parser.add_argument('--subsampling_factor', type=int, dest='subsampling_factor', default=1, help='Factor to subsample source and target image.') args = parser.parse_args() mean = np.load(args.mean) variance = np.load(args.variance) stddev = [np.math.sqrt(x) for x in variance] def denormalize(image): channels = [ np.expand_dims( image[:, :, channel] * stddev[channel] + mean[channel], -1) for channel in range(3) ] denormalized_image = ctfi.rescale(np.concatenate(channels, 2), 0.0, 1.0) return denormalized_image def normalize(image, name=None, num_channels=3): channels = [ tf.expand_dims( (image[:, :, :, channel] - mean[channel]) / stddev[channel], -1) for channel in range(num_channels) ] return tf.concat(channels, num_channels) latest_checkpoint = tf.train.latest_checkpoint(args.export_dir) saver = tf.train.import_meta_graph(latest_checkpoint + '.meta', import_scope='imported') config = tf.ConfigProto() run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_options.report_tensor_allocations_upon_oom = True #config.gpu_options.allow_growth = True # Load image and extract patch from it and create distribution. source_image = ctfi.subsample( ctfi.load(args.source_filename, height=args.source_image_size[0], width=args.source_image_size[1]), args.subsampling_factor) args.source_image_size = list( map(lambda x: int(x / args.subsampling_factor), args.source_image_size)) #Load image for which to create the heatmap target_image = ctfi.subsample( ctfi.load(args.target_filename, height=args.target_image_size[0], width=args.target_image_size[1]), args.subsampling_factor) args.target_image_size = list( map(lambda x: int(x / args.subsampling_factor), args.target_image_size)) heatmap_size = list( map(lambda v: max(v[0], v[1]), zip(args.source_image_size, args.target_image_size))) source_image = tf.expand_dims( tf.image.resize_image_with_crop_or_pad(source_image, heatmap_size[0], heatmap_size[1]), 0) target_image = tf.expand_dims( tf.image.resize_image_with_crop_or_pad(target_image, heatmap_size[0], heatmap_size[1]), 0) num_patches = np.prod(heatmap_size, axis=0) possible_splits = cutil.get_divisors(num_patches) num_splits = possible_splits.pop(0) while num_patches / num_splits > 500 and len(possible_splits) > 0: num_splits = possible_splits.pop(0) split_size = int(num_patches / num_splits) X, Y = np.meshgrid(range(heatmap_size[1]), range(heatmap_size[0])) coords = np.concatenate([ np.expand_dims(Y.flatten(), axis=1), np.expand_dims(X.flatten(), axis=1) ], axis=1) #source_patches_placeholder = tf.placeholder(tf.float32, shape=[num_patches / num_splits, args.patch_size, args.patch_size, 3]) #target_patches_placeholder = tf.placeholder(tf.float32, shape=[num_patches / num_splits, args.patch_size, args.patch_size, 3]) #all_source_patches = ctfi.extract_patches(source_image, args.patch_size, strides=[1,1,1,1], padding='SAME') #all_target_patches = ctfi.extract_patches(target_image, args.patch_size, strides=[1,1,1,1], padding='SAME') #source_patches = tf.split(all_source_patches, num_splits) #target_patches = tf.split(all_target_patches, num_splits) #patches = zip(source_patches, target_patches) coords_placeholder = tf.placeholder(tf.float32, shape=[split_size, 2]) source_patches_placeholder = tf.squeeze( tf.map_fn(lambda x: get_patch_at(x, source_image, args.patch_size), coords_placeholder, parallel_iterations=8, back_prop=False)) target_patches_placeholder = tf.squeeze( tf.map_fn(lambda x: get_patch_at(x, target_image, args.patch_size), coords_placeholder, parallel_iterations=8, back_prop=False)) heatmap = np.ndarray(heatmap_size) with tf.Session(graph=tf.get_default_graph(), config=config).as_default() as sess: source_patches_cov, source_patches_mean = tf.contrib.graph_editor.graph_replace( [ sess.graph.get_tensor_by_name( 'imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], { sess.graph.get_tensor_by_name('imported/patch:0'): normalize(source_patches_placeholder) }) source_patches_distribution = tf.contrib.distributions.MultivariateNormalDiag( source_patches_mean[:, args.stain_code_size:], tf.exp(source_patches_cov[:, args.stain_code_size:])) target_patches_cov, target_patches_mean = tf.contrib.graph_editor.graph_replace( [ sess.graph.get_tensor_by_name( 'imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], { sess.graph.get_tensor_by_name('imported/patch:0'): normalize(target_patches_placeholder) }) target_patches_distribution = tf.contrib.distributions.MultivariateNormalDiag( target_patches_mean[:, args.stain_code_size:], tf.exp(target_patches_cov[:, args.stain_code_size:])) similarity = source_patches_distribution.kl_divergence( target_patches_distribution ) + target_patches_distribution.kl_divergence( source_patches_distribution) #similarity = ctf.bhattacharyya_distance(source_patches_distribution, target_patches_distribution) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) saver.restore(sess, latest_checkpoint) for i in range(num_splits): start = i * split_size end = start + split_size batch_coords = coords[start:end, :] feed_dict = {coords_placeholder: batch_coords} similarity_values = sess.run(similarity, feed_dict=feed_dict, options=run_options) #heatmap.extend(similarity_values) for idx, val in zip(batch_coords, similarity_values): heatmap[idx[0], idx[1]] = val heatmap_sad = sess.run( tf.reduce_mean(tf.squared_difference(source_image, target_image), axis=3))[0] #sim_heatmap = np.reshape(heatmap, heatmap_size, order='C') sim_heatmap = heatmap fig_images, ax_images = plt.subplots(1, 2) ax_images[0].imshow(sess.run(source_image)[0]) ax_images[1].imshow(sess.run(target_image)[0]) fig_similarities, ax_similarities = plt.subplots(1, 2) heatmap_skld_plot = ax_similarities[0].imshow(sim_heatmap, cmap='plasma') heatmap_sad_plot = ax_similarities[1].imshow(heatmap_sad, cmap='plasma') fig_similarities.colorbar(heatmap_skld_plot, ax=ax_similarities[0]) fig_similarities.colorbar(heatmap_sad_plot, ax=ax_similarities[1]) plt.show() sess.close() return 0
def main(argv): parser = argparse.ArgumentParser( description='Register images using keypoints.') parser.add_argument('export_dir', type=str, help='Path to saved model.') parser.add_argument( 'mean', type=str, help='Path to npy file holding mean for normalization.') parser.add_argument( 'variance', type=str, help='Path to npy file holding variance for normalization.') parser.add_argument('patch_size', type=int, help='Size of image patch.') parser.add_argument('source_filename', type=str, help='Image file from which to extract patch.') parser.add_argument('source_image_size', type=int, nargs=2, help='Size of the input image, HW.') parser.add_argument('target_filename', type=str, help='Image file for which to create the heatmap.') parser.add_argument( 'target_image_size', type=int, nargs=2, help='Size of the input image for which to create heatmap, HW.') parser.add_argument('num_keypoints', type=int, help='Number of keypoints to detect.') parser.add_argument('num_matches', type=int, help='Number of matches to keep.') parser.add_argument( '--stain_code_size', type=int, dest='stain_code_size', default=0, help= 'Optional: Size of the stain code to use, which is skipped for similarity estimation' ) parser.add_argument( '--leaf_size', type=int, dest='leaf_size', default=30, help='Number of elements to keep in leaf nodes of search tree.') parser.add_argument( '--method', type=str, dest='method', default='SKLD', help= 'Method to use to measure similarity, one of KLD, SKLD, BD, HD, SQHD.') parser.add_argument('--num_neighbours', type=int, dest='num_neighbours', default=1, help='k for kNN') parser.add_argument('--subsampling_factor', type=int, dest='subsampling_factor', default=1, help='Factor to subsample source and target image.') args = parser.parse_args() mean = np.load(args.mean) variance = np.load(args.variance) stddev = [np.math.sqrt(x) for x in variance] def denormalize(image): channels = [ np.expand_dims( image[:, :, channel] * stddev[channel] + mean[channel], -1) for channel in range(3) ] denormalized_image = ctfi.rescale(np.concatenate(channels, 2), 0.0, 1.0) return denormalized_image def normalize(image, name=None): channels = [ tf.expand_dims( (image[:, :, :, channel] - mean[channel]) / stddev[channel], -1) for channel in range(3) ] return tf.concat(channels, 3, name=name) def get_patch_at(keypoint, image): return tf.image.extract_glimpse(image, [args.patch_size, args.patch_size], [keypoint], normalized=False, centered=False) latest_checkpoint = tf.train.latest_checkpoint(args.export_dir) saver = tf.train.import_meta_graph(latest_checkpoint + '.meta', import_scope='imported') with tf.Session(graph=tf.get_default_graph()).as_default() as sess: #saver.restore(sess,latest_checkpoint) # Load image and extract patch from it and create distribution. source_image = ctfi.subsample( tf.expand_dims( ctfi.load(args.source_filename, height=args.source_image_size[0], width=args.source_image_size[1]), 0), args.subsampling_factor) im_source = (sess.run(source_image[0]) * 255).astype(np.uint8) target_image = ctfi.subsample( tf.expand_dims( ctfi.load(args.target_filename, height=args.target_image_size[0], width=args.target_image_size[1]), 0), args.subsampling_factor) im_target = (sess.run(target_image[0]) * 255).astype(np.uint8) orb = cv2.ORB_create(20000) source_keypoints, source_descriptors_cv = orb.detectAndCompute( im_source, None) target_keypoints, target_descriptors_cv = orb.detectAndCompute( im_target, None) #for keypoint in source_keypoints: # keypoint.pt = (keypoint.pt[1], keypoint.pt[0]) #for keypoint in target_keypoints: # keypoint.pt = (keypoint.pt[1], keypoint.pt[0]) patch_kp_0 = get_patch_at(source_keypoints[0].pt, source_image) #plt.imshow(sess.run(patch_kp_0)[0]) #plt.show() #source_keypoints.sort(key = lambda x: x.response, reverse=False) #target_keypoints.sort(key = lambda x: x.response, reverse=False) def remove_overlapping(x, keypoints): for p in keypoints: if p != x and x.overlap(x, p) > 0.8: keypoints.remove(p) return keypoints def filter_keypoints(keypoints): i = 0 while i < len(keypoints): end_idx = len(keypoints) - 1 - i p = keypoints[end_idx] keypoints = remove_overlapping(p, keypoints) i += 1 return keypoints #source_keypoints = filter_keypoints(source_keypoints) #target_keypoints = filter_keypoints(target_keypoints) source_keypoints.sort(key=lambda x: x.response, reverse=True) target_keypoints.sort(key=lambda x: x.response, reverse=True) source_keypoints = source_keypoints[:args.num_keypoints] target_keypoints = target_keypoints[:args.num_keypoints] source_descriptors_eval = [] target_descriptors_eval = [] #source_patches = normalize(tf.concat(list(map(lambda x: get_patch_at(x, source_image), source_keypoints)),0)) #target_patches = normalize(tf.concat(list(map(lambda x: get_patch_at(x, target_image), target_keypoints)),0)) patches_placeholder = tf.placeholder( tf.float32, shape=[1000, args.patch_size, args.patch_size, 3]) #source_patches_placeholder = tf.placeholder(tf.float32,shape=[1000, args.patch_size, args.patch_size, 3]) #target_patches_placeholder = tf.placeholder(tf.float32,shape=[1000, args.patch_size, args.patch_size, 3]) tf_cov, tf_mean = tf.contrib.graph_editor.graph_replace([ sess.graph.get_tensor_by_name('imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], { sess.graph.get_tensor_by_name('imported/patch:0'): patches_placeholder }) #source_cov, source_mean = tf.contrib.graph_editor.graph_replace([sess.graph.get_tensor_by_name('imported/z_covariance_lower_tri/MatrixBandPart:0'),sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0')] ,{ sess.graph.get_tensor_by_name('imported/patch:0'): source_patches_placeholder }) #target_cov, target_mean = tf.contrib.graph_editor.graph_replace([sess.graph.get_tensor_by_name('imported/z_covariance_lower_tri/MatrixBandPart:0'),sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0')] ,{ sess.graph.get_tensor_by_name('imported/patch:0'): target_patches_placeholder }) batch, latent_code_size = tf_mean.get_shape().as_list() #batch, latent_code_size = target_mean.get_shape().as_list() structure_code_size = latent_code_size - args.stain_code_size descriptors = tf.concat([ tf_mean[:, args.stain_code_size:], tf.layers.flatten(tf_cov[:, args.stain_code_size:]) ], -1) #source_descriptors = tf.concat([source_mean[:,args.stain_code_size:], tf.layers.flatten(source_cov[:,args.stain_code_size:,args.stain_code_size:])], -1) #target_descriptors = tf.concat([target_mean[:,args.stain_code_size:], tf.layers.flatten(target_cov[:,args.stain_code_size:,args.stain_code_size:])], -1) def multi_kl_div(X, Y): X_mean, X_cov = get_mean_and_cov(X, structure_code_size) Y_mean, Y_cov = get_mean_and_cov(Y, structure_code_size) Y_cov_inv = np.linalg.inv(Y_cov) trace_term = np.matrix.trace(np.matmul(Y_cov_inv, X_cov)) diff_mean = np.expand_dims(Y_mean - X_mean, axis=-1) middle_term = np.matmul(np.transpose(diff_mean), np.matmul(Y_cov_inv, diff_mean)) determinant_term = np.log( np.linalg.det(Y_cov) / np.linalg.det(X_cov)) value = 0.5 * (trace_term + middle_term - structure_code_size + determinant_term) return np.squeeze(value) def multi_kl_div_tf(X, Y): X_mean, X_cov = get_mean_and_cov_tf(X, structure_code_size) Y_mean, Y_cov = get_mean_and_cov_tf(Y, structure_code_size) Y_cov_inv = tf.linalg.inv(Y_cov) trace_term = tf.linalg.trace(tf.matmul(Y_cov_inv, X_cov)) diff_mean = tf.expand_dims(Y_mean - X_mean, axis=-1) middle_term = tf.matmul(diff_mean, tf.matmul(Y_cov_inv, diff_mean), transpose_a=True) determinant_term = tf.log( tf.linalg.det(Y_cov) / tf.linalg.det(X_cov)) value = 0.5 * (trace_term + middle_term - structure_code_size + determinant_term) return tf.squeeze(value) def sym_kl_div(X, Y): return multi_kl_div(X, Y) + multi_kl_div(Y, X) def sym_kl_div_tf(X, Y): return multi_kl_div_tf(X, Y) + multi_kl_div_tf(Y, X) def sqhd(X, Y): return multivariate_squared_hellinger_distance( X, Y, structure_code_size) def bd(X, Y): X_mean, X_cov = get_mean_and_cov(X, structure_code_size) Y_mean, Y_cov = get_mean_and_cov(Y, structure_code_size) return bhattacharyya_distance(X_mean, X_cov, Y_mean, Y_cov) def centroid_distance(X, Y): X_mean, X_cov = get_mean_and_cov(X, structure_code_size) Y_mean, Y_cov = get_mean_and_cov(Y, structure_code_size) return np.linalg.norm(X_mean - Y_mean) coords = tf.placeholder(tf.float32, shape=[1000, 2]) source_patches = tf.map_fn(lambda x: get_patch_at(x, source_image), coords) target_patches = tf.map_fn(lambda x: get_patch_at(x, target_image), coords) # Computation of distance metric descriptor_length = descriptors.get_shape().as_list()[1] def cdist_tf(X, Y): X_mean = X[:, args.stain_code_size:] Y_mean = Y[:, args.stain_code_size:] diff_means_einsum = tf.sqrt( tf.einsum('ij,ij->i', X_mean, X_mean)[:, None] + tf.einsum('ij,ij->i', Y_mean, Y_mean) - 2 * tf.matmul(X_mean, Y_mean, transpose_b=True)) return diff_means_einsum def dist_kl(X, Y): X_mean = X[:, 0:structure_code_size] X_cov = tf.exp(X[:, structure_code_size:]) Y_mean = Y[:, 0:structure_code_size] Y_cov = tf.exp(Y[:, structure_code_size:]) return ctf.fast_symmetric_kl_div(X_mean, X_cov, Y_mean, Y_cov) #tf_dist_op = cdist_tf(tf_src_descs, tf_trgt_descs) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) saver.restore(sess, latest_checkpoint) for i in range(int(args.num_keypoints / 1000)): start = i * 1000 end = (i + 1) * 1000 #source_patches = sess.run(normalize(tf.concat(list(map(lambda x: get_patch_at(x, source_image), source_keypoints[start:end])),0))) #target_patches = sess.run(normalize(tf.concat(list(map(lambda x: get_patch_at(x, target_image), target_keypoints[start:end])),0))) #source_coords = tf.convert_to_tensor(np.array([list(key_point.pt) for key_point in source_keypoints])) source_coords_np = np.array( [[key_point.pt[1], key_point.pt[0]] for key_point in source_keypoints[start:end]]) target_coords_np = np.array( [[key_point.pt[1], key_point.pt[0]] for key_point in target_keypoints[start:end]]) source_descriptors_eval.extend( sess.run(descriptors, feed_dict={ patches_placeholder: np.squeeze( sess.run(source_patches, feed_dict={coords: source_coords_np})) })) target_descriptors_eval.extend( sess.run(descriptors, feed_dict={ patches_placeholder: np.squeeze( sess.run(target_patches, feed_dict={coords: target_coords_np})) })) #source_descriptors_eval.extend(sess.run(source_descriptors, feed_dict={source_patches_placeholder : source_patches})) #target_descriptors_eval.extend(sess.run(target_descriptors, feed_dict={target_patches_placeholder : target_patches})) #sess.close() #with tf.Session(graph=tf.get_default_graph()).as_default() as sess: tf_src_descs = tf.placeholder( tf.float32, shape=[args.num_keypoints, descriptor_length]) tf_trgt_descs = tf.placeholder( tf.float32, shape=[args.num_keypoints, descriptor_length]) dist_op = dist_kl(tf_src_descs, tf_trgt_descs) #sess.run(tf.global_variables_initializer()) #matches = match_descriptors(source_descriptors, target_descriptors, metric=lambda x,y: sym_kl_div(x,y), cross_check=True) if args.method == 'SKLD': metric = sym_kl_div elif args.method == 'SQHD': metric = sqhd elif args.method == 'BD': metric = bd elif args.method == 'CD': metric = centroid_distance else: metric = sym_kl_div distances = sess.run(dist_op, feed_dict={ tf_src_descs: np.array(source_descriptors_eval), tf_trgt_descs: np.array(target_descriptors_eval) }) indices = np.expand_dims(np.argmin(distances, axis=1), 1) min_distances = [distances[i, indices[i]] for i in range(len(indices))] #knn_source = sklearn.neighbors.NearestNeighbors(n_neighbors=5, radius=1.0, algorithm='ball_tree', leaf_size=args.leaf_size, metric=metric) #knn_source.fit(target_descriptors_eval) #distances, indices = knn_source.kneighbors(source_descriptors_eval, n_neighbors=args.num_neighbours) matches = list(zip(range(len(indices)), indices, min_distances)) # Sort matches by score matches.sort(key=lambda x: np.min(x[2]), reverse=False) matches = matches[:args.num_matches] def create_dmatch(queryIdx, trainIdx, distance): dmatch = cv2.DMatch(queryIdx, trainIdx, 0, distance) return dmatch def create_cv_matches(match): items = [] for i in range(len(match[1])): items.append(cv2.DMatch(match[0], match[1][i], 0, match[2][i])) return items all_cv_matches = [] for match in matches: all_cv_matches.extend(create_cv_matches(match)) sess.close() #cv_matches = list(map(lambda x: create_dmatch(x[0], x[1], x[2]),matches)) # Draw top matches imMatches = cv2.drawMatches(im_source, source_keypoints, im_target, target_keypoints, all_cv_matches, None) fix, ax = plt.subplots(1) ax.imshow(imMatches) plt.show() print("Detected keypoints!") return 0
def main(argv): parser = argparse.ArgumentParser( description='Compute codes and reconstructions for image.') parser.add_argument('export_dir', type=str, help='Path to saved model.') parser.add_argument( 'mean', type=str, help='Path to npy file holding mean for normalization.') parser.add_argument( 'variance', type=str, help='Path to npy file holding variance for normalization.') parser.add_argument('source_filename', type=str, help='Image file from which to extract patch.') parser.add_argument('source_image_size', type=int, nargs=2, help='Size of the input image, HW.') parser.add_argument( 'source_landmarks', type=str, help='CSV file from which to extract the landmarks for source image.') parser.add_argument('target_filename', type=str, help='Image file for which to create the heatmap.') parser.add_argument( 'target_image_size', type=int, nargs=2, help='Size of the input image for which to create heatmap, HW.') parser.add_argument( 'target_landmarks', type=str, help='CSV file from which to extract the landmarks for target image.') parser.add_argument('patch_size', type=int, help='Size of image patch.') parser.add_argument( '--method', dest='method', type=str, help= 'Method to use to measure similarity, one of KLD, SKLD, BD, HD, SQHD.') parser.add_argument( '--stain_code_size', type=int, dest='stain_code_size', default=0, help= 'Optional: Size of the stain code to use, which is skipped for similarity estimation' ) parser.add_argument('--rotate', type=float, dest='angle', default=0, help='Optional: rotation angle to rotate target image') parser.add_argument('--subsampling_factor', type=int, dest='subsampling_factor', default=1, help='Factor to subsample source and target image.') args = parser.parse_args() mean = np.load(args.mean) variance = np.load(args.variance) stddev = [np.math.sqrt(x) for x in variance] def denormalize(image): channels = [ np.expand_dims( image[:, :, channel] * stddev[channel] + mean[channel], -1) for channel in range(3) ] denormalized_image = ctfi.rescale(np.concatenate(channels, 2), 0.0, 1.0) return denormalized_image def normalize(image, name=None, num_channels=3): channels = [ tf.expand_dims( (image[:, :, :, channel] - mean[channel]) / stddev[channel], -1) for channel in range(num_channels) ] return tf.concat(channels, num_channels) latest_checkpoint = tf.train.latest_checkpoint(args.export_dir) saver = tf.train.import_meta_graph(latest_checkpoint + '.meta', import_scope='imported') config = tf.ConfigProto() config.allow_soft_placement = True #config.log_device_placement=True # Load image and extract patch from it and create distribution. source_image = tf.expand_dims( ctfi.subsample( ctfi.load(args.source_filename, height=args.source_image_size[0], width=args.source_image_size[1]), args.subsampling_factor), 0) args.source_image_size = list( map(lambda x: int(x / args.subsampling_factor), args.source_image_size)) #Load image for which to create the heatmap target_image = tf.expand_dims( ctfi.subsample( ctfi.load(args.target_filename, height=args.target_image_size[0], width=args.target_image_size[1]), args.subsampling_factor), 0) args.target_image_size = list( map(lambda x: int(x / args.subsampling_factor), args.target_image_size)) source_landmarks = get_landmarks(args.source_landmarks, args.subsampling_factor) source_patches = tf.squeeze( tf.map_fn(lambda x: get_patch_at(x, source_image, args.patch_size), source_landmarks)) target_landmarks = get_landmarks(args.target_landmarks, args.subsampling_factor) target_patches = tf.squeeze( tf.map_fn(lambda x: get_patch_at(x, target_image, args.patch_size), target_landmarks)) with tf.Session(config=config).as_default() as sess: saver.restore(sess, latest_checkpoint) source_patches_cov, source_patches_mean = tf.contrib.graph_editor.graph_replace( [ sess.graph.get_tensor_by_name( 'imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], { sess.graph.get_tensor_by_name('imported/patch:0'): normalize(source_patches) }) source_patches_distribution = tf.contrib.distributions.MultivariateNormalDiag( source_patches_mean[:, args.stain_code_size:], tf.exp(source_patches_cov[:, args.stain_code_size:])) target_patches_cov, target_patches_mean = tf.contrib.graph_editor.graph_replace( [ sess.graph.get_tensor_by_name( 'imported/z_log_sigma_sq/BiasAdd:0'), sess.graph.get_tensor_by_name('imported/z_mean/BiasAdd:0') ], { sess.graph.get_tensor_by_name('imported/patch:0'): normalize(target_patches) }) target_patches_distribution = tf.contrib.distributions.MultivariateNormalDiag( target_patches_mean[:, args.stain_code_size:], tf.exp(target_patches_cov[:, args.stain_code_size:])) #similarities = source_patches_distribution.kl_divergence(target_patches_distribution) + target_patches_distribution.kl_divergence(source_patches_distribution) #similarities = ctf.multivariate_squared_hellinger_distance(source_patches_distribution, target_patches_distribution) similarities = ctf.bhattacharyya_distance(source_patches_distribution, target_patches_distribution) #similarities = tf.reduce_sum(tf.abs(source_patches - target_patches), axis=[1,2,3]) sim_vals = sess.run(similarities) min_idx = np.argmin(sim_vals) max_idx = np.argmax(sim_vals) print(sim_vals) print(min_idx, sim_vals[min_idx]) print(max_idx, sim_vals[max_idx]) fig, ax = plt.subplots(2, 3) ax[0, 0].imshow(sess.run(source_image[0])) ax[0, 1].imshow(sess.run(source_patches)[min_idx]) ax[0, 2].imshow(sess.run(source_patches)[max_idx]) ax[1, 0].imshow(sess.run(target_image[0])) ax[1, 1].imshow(sess.run(target_patches)[min_idx]) ax[1, 2].imshow(sess.run(target_patches)[max_idx]) plt.show() sess.close() return 0
def main(argv): parser = argparse.ArgumentParser( description= 'Create tfrecords dataset holding patches of images specified by filename in input dataset.' ) parser.add_argument('input_dataset', type=str, help='Path to dataset holding image filenames') parser.add_argument('output_dataset', type=str, help='Path where to store the output dataset') parser.add_argument( 'patch_size', type=int, help='Patch size which to use in the preprocessed dataset') parser.add_argument('num_samples', type=int, help='Size of output dataset') parser.add_argument( 'labels', type=lambda s: [item for item in s.split(',')], help="Comma separated list of labels to find in filenames.") parser.add_argument('--image_size', type=int, dest='image_size', help='Image size for files pointed to by filename') parser.add_argument( '--no_filter', dest='no_filter', action='store_true', default=False, help='Whether to apply total image variation filtering.') parser.add_argument( '--threshold', type=float, dest='threshold', help='Threshold for filtering the samples according to variation.') parser.add_argument('--subsampling_factor', type=int, dest='subsampling_factor', default=1, help='Subsampling factor to use to downsample images.') args = parser.parse_args() labels_table = tf.contrib.lookup.index_table_from_tensor( mapping=args.labels) filename_dataset = tf.data.TFRecordDataset( args.input_dataset, num_parallel_reads=8).map(_decode_example_filename).shuffle(100000) functions = [ tf.Variable(label, name='const_' + label).value for label in args.labels ] false_fn = tf.Variable('None', name='none_label').value def _extract_label(filename): #base_size = tf.size(tf.string_split([filename],"")) #predicates = [tf.equal(base_size, tf.size(tf.string_split([tf.regex_replace(filename, "/"+ label + "/", "")]))) for label in args.labels] match = [ tf.math.reduce_any( tf.strings.regex_full_match( tf.string_split([filename], '/').values, label)) for label in args.labels ] pred_fn_pairs = list(zip(match, functions)) return tf.case(pred_fn_pairs, default=false_fn, exclusive=True) # Load images and extract the label from the filename if args.image_size is not None: images_dataset = filename_dataset.map( lambda feature: { 'image': ctfi.load(feature['filename'], channels=3, width=args.image_size, height=args.image_size), 'label': labels_table.lookup(_extract_label(feature['filename'])) }) else: images_dataset = filename_dataset.map( lambda feature: { 'image': ctfi.load(feature['filename'], channels=3), 'label': labels_table.lookup( _extract_label(feature['filename'])) }) if args.subsampling_factor > 1: images_dataset = images_dataset.map( lambda feature: { 'image': ctfi.subsample(feature['image'], args. subsampling_factor), 'label': feature['label'] }) def _filter_func_label(features): label = features['label'] result = label > -1 return result images_dataset = images_dataset.filter(_filter_func_label).shuffle(100) # Extract image patches #for sample in tfe.Iterator(images_dataset): # print(sample['label']) def _split_patches(features): patches = ctfi.extract_patches(features['image'], args.patch_size) labels = tf.expand_dims(tf.reshape(features['label'], [1]), 0) labels = tf.tile(labels, tf.stack([tf.shape(patches)[0], 1])) return (patches, labels) patches_dataset = images_dataset.map(_split_patches).apply( tf.data.experimental.unbatch()) patches_dataset = patches_dataset.map(lambda patch, label: { 'patch': patch, 'label': label }) if args.threshold is not None: threshold = args.threshold else: threshold = 0.08 num_filtered_patches = tf.Variable(0) filtered_patch_ratio = 10 # Filter function which filters the dataset after total image variation. # See: https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/image/total_variation def add_background_info(sample): variation = tf.image.total_variation(sample['patch']) num_pixels = sample['patch'].get_shape().num_elements() var_per_pixel = (variation / num_pixels) no_background = var_per_pixel > threshold sample['no_background'] = no_background return sample #def true_fn(): # sample.update({'no_background': True}) # return sample #def false_fn(): # def _true_fn_lvl2(): # sample.update({'label':tf.reshape(tf.convert_to_tensor(len(args.labels), dtype=tf.int64), [1]),'no_background': True}) # return sample # def _false_fn_lvl2(): # sample.update({'no_background': False}) # return sample # pred = tf.equal(num_filtered_patches.assign_add(1) % 10, 0) # return tf.cond(pred,true_fn=_true_fn_lvl2,false_fn=_false_fn_lvl2) #return tf.cond(no_background,true_fn=true_fn, false_fn=false_fn) if args.no_filter == True: dataset = patches_dataset else: dataset = patches_dataset.map(add_background_info) filtered_elements_dataset = dataset.filter( lambda sample: tf.logical_not(sample['no_background'])) def change_label(sample): return { 'patch': sample['patch'], 'label': tf.reshape( tf.convert_to_tensor(len(args.labels), dtype=tf.int64), [1]) } filtered_elements_dataset = filtered_elements_dataset.map(change_label) filtered_dataset = dataset.filter(lambda sample: sample[ 'no_background']).map(lambda sample: { 'patch': sample['patch'], 'label': sample['label'] }) dataset = tf.data.experimental.sample_from_datasets( [filtered_dataset, filtered_elements_dataset], weights=[0.95, 0.05]) dataset = dataset.map(lambda sample: (sample['patch'], sample['label'])) dataset = dataset.take(args.num_samples).shuffle(100000) writer = tf.io.TFRecordWriter(args.output_dataset) # Make file readable for all users cutil.publish(args.output_dataset) def _encode_func(sample): patch_np = sample[0].numpy().flatten() label_np = sample[1].numpy() return ctfd.encode({ 'patch': ctf.float_feature(patch_np), 'label': ctf.int64_feature(label_np) }) # Iterate over whole dataset and write serialized examples to file. # See: https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/contrib/eager/Iterator for sample in tfe.Iterator(dataset): example = _encode_func(sample) writer.write(example.SerializeToString()) # Flush and close the writer. writer.flush() writer.close()