class AdaptiveTransformation(): def __init__(self): self.normalizer = Normalizer() def transform(self, tissue, dye): return transforms.Compose( [transforms.ToTensor(), self.normalizer.get(tissue, dye)])
def get_features(arch, size, pooling=True): print("## Starting extracting features...") if pooling: print("## Using pooling..") else: print("## Not using pooling..") # Declare the features extractor extractor = FeaturesExtractor(arch) normalizer = Normalizer() starting = time.time() results_features = dict() with open('./info/project-info.csv', 'r') as csvfile: f_csv = csv.reader(csvfile, delimiter=str(','), quotechar=str('|')) next(f_csv) for row in f_csv: tissue = row[1] dye = row[2] original_name = row[6] if tissue not in results_features: results_features[tissue] = dict() if dye not in results_features[tissue]: results_features[tissue][dye] = None patches = get_patches_from_landmarks(tissue, original_name, size=size) nb_of_landmarks = len(patches) for landmark_nb, (_, _, patch) in enumerate(patches): normalize = normalizer.get(tissue, dye) extractor.set_normalize(normalize) img = Image.fromarray(patch) features = extractor.get_features_from_img( img, size, pooling).cpu().numpy().astype(np.float32) if landmark_nb == 0: results_features[tissue][dye] = np.zeros( (nb_of_landmarks, features.shape[0]), dtype=np.float32) results_features[tissue][dye][landmark_nb] = features print(" Elapsed time : {}".format(time.time() - starting)) return results_features
def main(): """main function""" args = get_args() shift = args.shift normalizer = Normalizer() print("\n#################") print("### Arguments ###") print("#################") for arg in vars(args): print(f"{arg} : {getattr(args, arg)}") print("#################\n") # creating the pairs of dyes to analyze pairs = generate_pairs() for i, (tissue, dye1, dye2, images_path, original_name1, original_name2, extension) in enumerate(pairs): # Each element of the pairs will play the role of the target for s in range(2): if s == 1: dye1, dye2 = dye2, dye1 original_name1, original_name2 = original_name2, original_name1 start_time = time() output_filename = os.path.join( args.output, f"data/{args.distance}/{args.size}/{args.arch}/{tissue}_{dye1}_{dye2}_{args.arch}_{args.size}_{args.pool}_{args.resize}.data" ) if not os.path.exists(output_filename): print(f"File {output_filename} does not exist") mkdir(os.path.dirname(output_filename)) else: print(f"File {output_filename} exists\n") continue # filename1 : reference (comparison from its annotation to those from filename1) -> get the patches # filename2 : image to cut print(tissue, dye1, dye2, Paths.PATH_TO_IMAGES, original_name1, original_name2) # Get patches from image 1 start_time_get_patches1 = time() patches_img1_landmarks = get_patches_from_landmarks( tissue, original_name1, size=get_args().size) time_get_patches1 = time() - start_time_get_patches1 # Get patches from image 2 start_time_get_patches2 = time() patches_img2 = segment_image(os.path.join( images_path, original_name2 + extension), size=get_args().size, shift=shift) time_get_patches2 = time() - start_time_get_patches2 ################# # # Is useful to make to have the results for one pair whose target has to be rotated # angle = -75 # img = img = Image.open(images_path + original_name2 + extension) # im2 = img.convert('RGBA') # # rotated image # rot = im2.rotate(angle, expand=1) # # a white image same size as rotated image # fff = Image.new('RGBA', rot.size, (255,)*4) # # create a composite image using the # out = Image.composite(rot, fff, rot) # out = out.convert(img.mode) # patches_img2 = segment_image(img=out, size=get_args().size, shift=shift) # time_get_patches2 = time() - start_time_get_patches2 ################## # get the features # number of available landmarks for the particular tissue nb_of_landmarks = len(patches_img1_landmarks) print("==> Img1 ({} {}) : {}".format(tissue, dye1, nb_of_landmarks)) print("==> Img2 ({} {}) : {}".format(tissue, dye2, len(patches_img2))) start_time_features_img1_landmarks = time() normalize_dye1 = normalizer.get(tissue, dye1) features_img1_landmarks = get_features(patches_img1_landmarks, normalize_dye1) time_get_features1 = time() - start_time_features_img1_landmarks patches_img1_landmarks = "" del patches_img1_landmarks gc.collect() start_time_features_img2_landmarks = time() normalize_dye2 = normalizer.get(tissue, dye2) features_img2 = get_features(patches_img2, normalize_dye2) time_get_features2 = time() - start_time_features_img2_landmarks feature_size = features_img1_landmarks.shape[1] print("===> Features size : {}".format(feature_size)) # Keep only the center and coordinates of patches_img2 patches_img2 = [(x[0], x[1]) for x in patches_img2] gc.collect() # Compare start_time_comparison = time() results_comparison = compare(features_img1_landmarks, features_img2, args.distance) time_comparison = time() - start_time_comparison features_img2 = "" del features_img2 features_img1_landmarks = "" del features_img1_landmarks gc.collect() # Get the position of the landmarks of dye2 start_time_position_landmarks = time() position_landmarks_dye2 = get_position_landmarks( tissue, original_name2) time_position_landmarks = time() - start_time_position_landmarks # Get top-k accuracy start_time_get_accuracy = time() k_list = [1, 5] # count the landmarks respecting the condition counter = [0] * len(k_list) for i in range(nb_of_landmarks): array = [(k, x) for k, x in enumerate(results_comparison[i])] array.sort(key=lambda x: x[1], reverse=True) for c, k in enumerate(k_list): indices_of_best_matches = None if args.distance == "cos": indices_of_best_matches = [x[0] for x in array[:k]] elif args.distance == "eucl" or args.distance == "eucl-norm": indices_of_best_matches = [x[0] for x in array[-k:]] # get the position of the k centers that best matches centers = [ patches_img2[ind][1] for ind in indices_of_best_matches ] true_position = position_landmarks_dye2[i] distances = [ euclidean_distance(np.array(center), np.array(true_position)) for center in centers ] distances = np.array(distances) # if at least a patch center is within a certain radius around the true landmark if distances[distances <= args.size / 2].shape[0] != 0: counter[c] += 1 table = [] top_accuracy_list = [] for c, k in enumerate(k_list): acc = round(counter[c] / nb_of_landmarks, 4) top_accuracy_list.append((k, acc)) table.append([str(k), str(acc)]) t = tabulate(table, headers=['k', 'Top-k accuracy']) print("\n", t, "\n") time_get_accuracy = time() - start_time_get_accuracy elapsed_time = time() - start_time table = [ [ 'Patches image 1', str(datetime.timedelta(seconds=time_get_patches1)) ], [ 'Patches image 2', str(datetime.timedelta(seconds=time_get_patches2)) ], [ 'Features image 1', str(datetime.timedelta(seconds=time_get_features1)) ], [ 'Features image 2', str(datetime.timedelta(seconds=time_get_features2)) ], [ 'Position landmarks image 2', str(datetime.timedelta(seconds=time_position_landmarks)) ], [ 'Comparison', str(datetime.timedelta(seconds=time_comparison)) ], [ 'Compute accuracy', str(datetime.timedelta(seconds=time_get_accuracy)) ], [ 'Elapsed time', str(datetime.timedelta(seconds=elapsed_time)) ] ] t = tabulate(table, headers=['', 'Time (h:m:s)']) print(t, "\n") info = { "args": vars(args), "pair": (tissue, dye1, dye2, images_path, original_name1, original_name2, extension), "results_comparison": results_comparison, "nb_of_landmarks": nb_of_landmarks, "feature_size": feature_size, "counter": counter, "top_accuracy_list": top_accuracy_list, "time": elapsed_time, "time_get_patches1": time_get_patches1, "time_get_patches2": time_get_patches2, "time_get_features1": time_get_features1, "time_get_features2": time_get_features2, "time_position_landmarks": time_position_landmarks, "time_comparison": time_comparison, "time_get_accuracy": time_get_accuracy, } with open(output_filename, 'wb') as output_file: pickle.dump(info, output_file)