def inference(self, x_input, interpolation="nearest"): x_img, x_pass = self.get_features(x_input) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = np.reshape(x_img, (x_height * x_width, -1)) probs = self.clf.predict_proba(x_img) n_classes = probs.shape[1] y_img = [] for i in range(n_classes): y_i_img = np.reshape(probs[:, i], (x_height, x_width, 1)) y_img.append(y_i_img) y_img = np.concatenate(y_img, axis=2) x_img_pass = resize(x_pass, width=o_width, height=o_height, interpolation="nearest") y_img = resize(y_img, width=o_width, height=o_height, interpolation=interpolation) if len(x_img_pass.shape) < 3: x_img_pass = np.expand_dims(x_img_pass, axis=2) if len(y_img.shape) < 3: y_img = np.expand_dims(y_img, axis=2) y_img = np.concatenate([x_img_pass, y_img], axis=2) return y_img
def inference(self, x_input, interpolation="nearest"): segments = generate_segments(x_input, self.opt) x_img, x_pass = self.get_features(x_input) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = get_features_for_segments(x_img, segments, self.opt["feature_aggregation"]) y_pred = self.clf.predict_proba(x_img) segments = resize(segments, width=x_width, height=x_height, interpolation="nearest") y_img = map_segments(segments, y_pred) x_img_pass = resize(x_pass, width=o_width, height=o_height, interpolation="nearest") y_img = resize(y_img, width=o_width, height=o_height, interpolation=interpolation) if len(x_img_pass.shape) < 3: x_img_pass = np.expand_dims(x_img_pass, axis=2) if len(y_img.shape) < 3: y_img = np.expand_dims(y_img, axis=2) y_img = np.concatenate([x_img_pass, y_img], axis=2) return y_img
def predict(self, tag): x_input = tag.load_x() segments = generate_segments(x_input, self.opt) x_img, x_pass = self.get_features(tag) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = get_features_for_segments(x_img, segments, self.opt["feature_aggregation"]) y_img = self.clf.predict(x_img) segments = resize(segments, width=x_width, height=x_height, interpolation="nearest") y_img = map_segments(segments, y_img) y_img = resize(y_img, width=o_width, height=o_height, interpolation="nearest") return y_img
def transform_features(self, x_img): x = resize(x_img, width=self.global_kernel[0], height=self.global_kernel[1], interpolation="linear") x = np.reshape(x, (1, -1)) return x
def inference(self, x_img): if self.parameter is None: return x_img height, width = x_img.shape[:2] return resize(x_img, width=int(width / self.parameter), height=int(height / self.parameter))
def inference(self, x_input, interpolation="nearest"): x_img = self.get_features(x_input) o_h, o_w = x_img.shape[:2] x_img = resize(x_img, height=int(o_h / 2**self.down_scale), width=int(o_w / 2**self.down_scale)) x_img = normalize(x_img) h, w = x_img.shape[:2] if len(x_img.shape) < 3: x_img = np.expand_dims(x_img, axis=2) num_f = x_img.shape[2] x_int_mm = np.zeros((h, w, num_f)) x_int_pp = np.zeros((h, w, num_f)) x_int_mp = np.zeros((h, w, num_f)) x_int_pm = np.zeros((h, w, num_f)) step = w + h for i in range(1, w - 1, 1): for j in range(1, h - 1, 1): n = w - 1 - i k = h - 1 - j i_m = i - 1 j_m = j - 1 n_p = n + 1 k_p = k + 1 x_int_mm[j, i, :] = x_img[j, i, :] / step + ( x_int_mm[j_m, i_m, :] + x_int_mm[j_m, i, :] + x_int_mm[j, i_m, :]) / 3 x_int_pp[k, n, :] = x_img[k, n, :] / step + ( x_int_pp[k_p, n_p, :] + x_int_pp[k_p, n, :] + x_int_pp[k, n_p, :]) / 3 x_int_mp[j, n, :] = x_img[j, n, :] / step + ( x_int_mp[j_m, n_p, :] + x_int_mp[j_m, n, :] + x_int_mp[j, n_p, :]) / 3 x_int_pm[k, i, :] = x_img[k, i, :] / step + ( x_int_pm[k_p, i_m, :] + x_int_pm[k_p, i, :] + x_int_pm[k, i_m, :]) / 3 x_out = [x_img, x_int_mm, x_int_mp, x_int_pm, x_int_pp] x_out = np.concatenate(x_out, axis=2) x_out = resize(x_out, height=o_h, width=o_w, interpolation="linear") return x_out
def get_y_for_segments(y_img, segments): y = [] segments = resize(segments, width=y_img.shape[1], height=y_img.shape[0]) for u in np.unique(segments): counts = np.bincount(y_img[segments == u].astype(np.int)) y_seg = np.argmax(counts) y.append(y_seg) return np.array(y)
def inference(self, tag_3d, interpolation="nearest"): x_img, x_pass = self.get_features(tag_3d) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = self.transform_features(x_img) shape_parameters = self.clf.predict(x_img) y_img = self.shape_parameters_to_label_map(shape_parameters[0], x_height, x_width) x_img_pass = resize(x_pass, width=o_width, height=o_height, interpolation="nearest") y_img = resize(y_img, width=o_width, height=o_height, interpolation=interpolation) if len(x_img_pass.shape) < 3: x_img_pass = np.expand_dims(x_img_pass, axis=2) if len(y_img.shape) < 3: y_img = np.expand_dims(y_img, axis=2) y_img = np.concatenate([x_img_pass, y_img], axis=2) return y_img
def predict(self, x_input): x_img, x_pass = self.get_features(x_input) o_height, o_width = x_pass.shape[:2] y_img = self.pipeline.inference(x_img) y_img = resize(y_img, width=o_width, height=o_height, interpolation="nearest") return y_img
def predict(self, tag_3d): x_img, x_pass = self.get_features(tag_3d) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = self.transform_features(x_img) shape_parameters = self.clf.predict(x_img) y_img = self.shape_parameters_to_label_map(shape_parameters[0], x_height, x_width) y_img = resize(y_img, width=o_width, height=o_height, interpolation="nearest") return y_img
def eval(self, x_img, y_img): p_img = self.inference(x_img) h_img, w_img = p_img.shape[:2] y_img = resize(y_img, w_img, h_img) y_img = np.reshape(y_img, h_img * w_img) p_img = np.reshape(p_img, h_img * w_img) self.stats["TP"] += np.sum(np.logical_and(y_img == 1, p_img == 1)) self.stats["FP"] += np.sum(np.logical_and(y_img != 1, p_img == 1)) self.stats["FN"] += np.sum(np.logical_and(y_img == 1, p_img != 1))
def predict(self, x_input): x_img, x_pass = self.get_features(x_input) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = np.reshape(x_img, (x_height * x_width, -1)) y_img = self.clf.predict(x_img) y_img = np.reshape(y_img, (x_height, x_width)) y_img = resize(y_img, width=o_width, height=o_height, interpolation="nearest") return y_img
def inference(self, x_input, interpolation="nearest"): x_img, x_pass = self.get_features(x_input) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] y_img = self.pipeline.inference(x_img) y_img = np.reshape(y_img, (x_height, x_width, 1)) x_img_pass = resize(x_pass, width=o_width, height=o_height, interpolation="nearest") y_img = resize(y_img, width=o_width, height=o_height, interpolation=interpolation) if len(x_img_pass.shape) < 3: x_img_pass = np.expand_dims(x_img_pass, axis=2) if len(y_img.shape) < 3: y_img = np.expand_dims(y_img, axis=2) y_img = np.concatenate([x_img_pass, y_img], axis=2) return y_img
def get_features_for_segments(tensor, segments, feature_aggregation): if len(tensor.shape) < 3: tensor = np.expand_dims(tensor, axis=2) h, w, num_f = tensor.shape segments = resize(segments, width=w, height=h) f_tensor = np.reshape(tensor, (w * h, num_f)) f_segments = np.reshape(segments, (w * h)) if "quantiles" == feature_aggregation: return get_features_opt_quantiles(f_tensor, f_segments) if "hist" in feature_aggregation: bins = int(feature_aggregation.replace("hist", "")) return get_features_opt_histogram(f_tensor, f_segments, bins) if "sum" == feature_aggregation: return get_features_opt_sum(f_tensor, f_segments) if "gauss" == feature_aggregation: return get_features_opt_gauss(f_tensor, f_segments) if "hog" == feature_aggregation: return get_features_opt_hog(tensor, segments) raise ValueError( "FeatureAggregation - {} - not known!".format(feature_aggregation))
def predict(self, x_input): x_img, x_pass = self.get_features(x_input) o_height, o_width = x_pass.shape[:2] x_height, x_width = x_img.shape[:2] x_img = x_img[:, :, -1] label_img = label(x_img) props = regionprops(label_img) x_props = [] y_img = np.zeros((x_height, x_width)) for p in props: x_p = self.properties_to_features(p) x_props.append(x_p) if len(x_props) == 0: return y_img x_props = np.concatenate(x_props, axis=0) y_props = self.clf.predict(x_props) for i, p in enumerate(props): y_img[label_img == p["label"]] = y_props[i] y_img = resize(y_img, width=o_width, height=o_height, interpolation="nearest") return y_img
def inference(self, x_input, interpolation="nearest"): o_height, o_width = x_input.shape[:2] x_img = self.get_features(x_input) x_height, x_width = x_img.shape[:2] x_img_row = self.format_to_axis_view(np.copy(x_img), axis=1) x_img_row = np.reshape(x_img_row, (x_height, -1)) x_img_col = self.format_to_axis_view(np.copy(x_img), axis=0) x_img_col = np.reshape(x_img_col, (x_width, -1)) y_row = self.clf_row.predict(x_img_row) y_col = self.clf_col.predict(x_img_col) y_row = np.repeat(y_row, axis=1) y_col = np.repeat(y_col, axis=0) n_classes = probs.shape[1] y_img = [] for i in range(n_classes): y_i_img = np.reshape(probs[:, i], (x_height, x_width, 1)) y_img.append(y_i_img) y_img = np.concatenate(y_img, axis=2) y_img = resize(y_img, width=o_width, height=o_height, interpolation=interpolation) if len(x_input.shape) < 3: x_input = np.expand_dims(x_input, axis=2) if len(y_img.shape) < 3: y_img = np.expand_dims(y_img, axis=2) y_img = np.concatenate([x_input, y_img], axis=2) return y_img
def transform_features(self, x_img): assert x_img.shape[2] < 512, "Too many features! - {} -".format(x_img.shape[2]) x = resize(x_img, width=self.global_kernel[0], height=self.global_kernel[1], interpolation="linear") x = np.reshape(x, (1, -1)) return x