def normalize_factory(computer_func_params): norm = computer_func_params["norm"] def computer_(X): return normalize(X, norm=norm) return Computer(computer_, None)
def lbp_computer_factory(computer_func_params): def computer_(img_matrix): img_gray_matrix = iu.img_matrix_to_gray_img_matrix(img_matrix) img_gray_matrix = img_gray_matrix.squeeze() kwargs = computer_func_params["library_func_kwargs"] lbp = local_binary_pattern(img_gray_matrix, **kwargs) return lbp return Computer(computer_, None)
def rect_tiles_generator_factory(computer_func_params): rect_size = computer_func_params["rect_size"] tile_size = computer_func_params["tile_size"] tile_step = computer_func_params["tile_step"] def computer_(): tile_generator = generate_tiles_rects(rect_size, tile_size, tile_step, True) return tile_generator return Computer(computer_)
def knn_class_computer_factory(computer_func_params): # print(computer_func_params) true_classes_model = computer_func_params["true_classes_model"] # compute_model(true_classes_model), а сейчас предполагаем, что это обязательно ds true_classes = ds_utils.read_array(true_classes_model) def computer_(nearest_indices): return knn_compute_classes(nearest_indices, true_classes) return Computer(computer_)
def pq_cluster_centers_computer_factory(computer_func_params): def computer_(descriptors): n_quantizers = computer_func_params["n_quantizers"] n_clusters = computer_func_params["n_clusters"] kwargs = computer_func_params["library_func_kwargs"] pq = PQQuantizer(n_quantizers=n_quantizers, n_clusters=n_clusters, **kwargs) pq.fit(descriptors) return pq.get_cluster_centers() return Computer(computer_)
def openslide_tiles_generator_factory(computer_func_params): # tiles_rects = computer_func_params["tiles_rects"] downsample = computer_func_params["downsample"] image_path = computer_func_params["image_model"]["string"] slide = openslide.OpenSlide(image_path) level = slide.get_best_level_for_downsample(downsample) def computer_(tile_rect): tile = read_openslide_tile(slide, level, tile_rect) return tile return Computer(computer_)
def accuracy_score_computer_factory(computer_func_params): def computer_(queries_classes): true_classes_model = computer_func_params["true_classes_model"] # compute_model(true_classes_model), а сейчас предполагаем, что это обязательно ds true_classes = ds_utils.read_array(true_classes_model) assert len(queries_classes.shape) == 2 n_nearest = queries_classes.shape[1] scores_arr = np.empty((n_nearest, )) for n_nearest_ in range(n_nearest): scores_arr[n_nearest_] = accuracy_score( true_classes, queries_classes[:, n_nearest_]) return scores_arr return Computer(computer_)
def pqcode_computer_factory(computer_func_params): cluster_centers_ds = computer_func_params["quantization_model"][ "output_model"] cluster_centers = ds_utils.read_array(cluster_centers_ds) assert len(cluster_centers.shape) == 3 pqquantizer = restore_from_clusters(cluster_centers) def computer_(descriptors_chunks): pqcodes = pqquantizer.predict_subspace_indices(descriptors_chunks) return pqcodes n_quantizers = cluster_centers.shape[0] shape = [n_quantizers] return Computer(computer_, shape)
def asymmetric_distance_matrix_computer_factory(computer_func_params): quantization_model = computer_func_params["base_model"]["computer_func_params"]["quantization_model"] # print(quantization_model) X_ds = computer_func_params["base_model"]["output_model"] # print("X_DS", X_ds) X = ds_utils.read_array(X_ds) cluster_centers_ds = quantization_model["output_model"] cluster_centers = ds_utils.read_array(cluster_centers_ds) def computer_(Q): distances_matrix = asymmetric_distance_matrix(cluster_centers, Q, X) return distances_matrix return Computer(computer_)
def glcm_computer_factory(computer_func_params): kwargs = computer_func_params["library_func_kwargs"] def computer_(img_matrix): img_gray_matrix = iu.img_matrix_to_gray_img_matrix(img_matrix) img_gray_matrix = img_gray_matrix.squeeze() glcm = greycomatrix(img_gray_matrix, **kwargs) return glcm shape = [ kwargs["levels"], kwargs["levels"], len(kwargs["distances"]), len(kwargs["angles"]) ] return Computer(computer_, shape)
def argsort_nearest_indices_computer_factory(computer_func_params): def computer_(distance_matrix): assert len(distance_matrix.shape) == 2 n_nearest = computer_func_params["n_nearest"] if n_nearest == -1: n_nearest = distance_matrix.shape[1] nearest_indices = np.argpartition(distance_matrix, axis=1, kth=n_nearest - 1)[:, :n_nearest] rows_range = np.arange(len(distance_matrix)).reshape((-1, 1)) nearest_indices = nearest_indices[rows_range, np.argsort(distance_matrix[ rows_range, nearest_indices])] return nearest_indices return Computer(computer_)
def l2_distance_matrix_computer_factory(computer_func_params): # X_ds = computer_func_params["base_model"]["output_model"] # X = ds_utils.read_array(X_ds) X = computer_utils.compute_model(computer_func_params["base_model"]) # TODO consider all cases if isinstance(X, np.ndarray): pass elif isinstance(X, Iterable): X = list(X) def computer_(Q): Q = np.array(Q, copy=False) Q = Q.reshape((len(Q), -1)) distances_matrix = l2_distance_matrix(Q, X) return distances_matrix return Computer(computer_)
def accuracy_KNeighbors_computer_factory(computer_func_params): # if computer_func_params["base_model"]["computer_func_name"] == "pqcode": # X_ds = computer_func_params["base_model"]["output_model"] # # print("X_DS", X_ds) # X = ds_utils.read_array(X_ds) # quantization_model = computer_func_params["base_model"]["computer_func_params"]["quantization_model"] # # print(quantization_model) # cluster_centers_ds = quantization_model["output_model"] # cluster_centers = ds_utils.read_array(cluster_centers_ds) # metric = unoptimzed_asymmetric_distance # metric_params = {"cluster_centers": cluster_centers} # else: X_ds = computer_func_params["base_model"]["output_model"] X = ds_utils.read_array(X_ds) metric = "l2" metric_params = None n_neighbors = computer_func_params["n_nearest"] true_classes_model = computer_func_params["true_classes_model"] # compute_model(true_classes_model), а сейчас предполагаем, что это обязательно ds true_classes = ds_utils.read_array(true_classes_model) def computer_(Q): Q = Q.reshape((len(Q), -1)) k_arr = [k for k in range(1, n_neighbors + 1)] accuracy_arr = np.empty(len(k_arr)) predicted_classes = np.empty((len(k_arr), len(true_classes)), dtype=int) for i, k in enumerate(k_arr): neigh = KNeighborsClassifier(n_neighbors=k, metric=metric, metric_params=metric_params, algorithm="brute") neigh.fit(X, true_classes) pred = neigh.predict(Q) # print(pred) predicted_classes[i] = pred accuracy_arr[i] = accuracy_score(true_classes, pred) return accuracy_arr return Computer(computer_)
def vgg16_computer_factory(computer_func_params): vgg16_model = getVGG16() layer_name = computer_func_params["layer_name"] def computer_(img_matrix_chunks): # return None kwargs = computer_func_params["library_func_kwargs"] # if img_matrix_chunks.shape[1] != 224 or img_matrix_chunks.shape[2] != 224: # return np.zeros((len(img_matrix_chunks), 4096), dtype=float) activations = keras_utils.get_activations(vgg16_model, img_matrix_chunks, layer_name=layer_name, **kwargs) return activations[0] if layer_name == "fc1": shape = [4096] elif layer_name == "fc2": shape = [4096] return Computer(computer_, shape)
def symmetric_distance_matrix_computer_factory(computer_func_params): quantization_model = computer_func_params["base_model"]["computer_func_params"]["quantization_model"] # print(quantization_model) X_ds = computer_func_params["base_model"]["output_model"] # print("X_DS", X_ds) X = ds_utils.read_array(X_ds) cluster_centers_ds = quantization_model["output_model"] cluster_centers = ds_utils.read_array(cluster_centers_ds) pq_quantizer = restore_from_clusters(cluster_centers) n_quantizers, n_clusters, subvector_length = cluster_centers.shape print("n_quantizers, n_clusters, subvector_length", n_quantizers, n_clusters, subvector_length) cluster_centers_distance_matrix = np.empty((n_quantizers, n_clusters, n_clusters), dtype=float) for i in range(n_quantizers): cluster_centers_distance_matrix[i] = pairwise_distances(cluster_centers[i], cluster_centers[i]) def computer_(Q): distances_matrix = symmetric_distance_matrix(cluster_centers_distance_matrix, Q, X, pq_quantizer) return distances_matrix return Computer(computer_)
def gray_histogram_computer_factory(computer_func_params): n_bins = computer_func_params["n_bins"] density = computer_func_params["density"] n_values = 256 bin_size = n_values // n_bins # n_edges must be = n_bins + 1 edges = np.arange(0, n_values + 1, bin_size) edges[n_bins] = n_values - 1 def computer_(img_matrix: np.ndarray): img_gray_matrix = iu.img_matrix_to_gray_img_matrix(img_matrix) img_gray_matrix = img_gray_matrix.ravel() # iu.img_matrix_to_pil_image(img_gray_matrix).show() kwargs = computer_func_params["library_func_kwargs"] if bin_size == 1: histogram = np.empty((n_values, ), dtype=float) histogram[...] = np.bincount(img_gray_matrix, minlength=n_values) if density: histogram /= img_gray_matrix.size else: histogram, edges_ = np.histogram(img_gray_matrix, bins=edges, density=density, **kwargs) # if first_hist is None: # first_img = iu.img_matrix_to_pil_image(img_gray_matrix) # first_img.show() # else: # dist = pairwise_distances(first_hist.reshape(1, -1), histogram.reshape(1, -1)) # iu.img_matrix_to_pil_image(img_gray_matrix).show() # print(dist) return histogram shape = [n_bins] return Computer(computer_, shape)
def plot_sublots_model_factory(computer_func_params): def computer_(): return plot_sublots_model(computer_func_params) return Computer(computer_)
def factory(computer_func_params=None): def computer_(X): return func(X, **computer_func_params) return Computer(computer_)
def factory(computer_func_params=None): return Computer(func)