def train_svm(C=0.1, grid=False): pascal = PascalSegmentation() files_train = pascal.get_split("kTrain") superpixels = [slic_n(pascal.get_image(f), n_superpixels=100, compactness=10) for f in files_train] bow = SiftBOW(pascal, n_words=1000, color_sift=True) data_train = bow.fit_transform(files_train, superpixels) data_train = add_global_descriptor(data_train) svm = LinearSVC(C=C, dual=False, class_weight='auto') chi2 = AdditiveChi2Sampler() X, y = np.vstack(data_train.X), np.hstack(data_train.Y) X = chi2.fit_transform(X) svm.fit(X, y) print(svm.score(X, y)) eval_on_sp(pascal, data_train, [svm.predict(chi2.transform(x)) for x in data_train.X], print_results=True) files_val = pascal.get_split("kVal") superpixels_val = [slic_n(pascal.get_image(f), n_superpixels=100, compactness=10) for f in files_val] data_val = bow.transform(files_val, superpixels_val) data_val = add_global_descriptor(data_val) eval_on_sp(pascal, data_val, [svm.predict(chi2.transform(x)) for x in data_val.X], print_results=True) tracer()
def load_single_file(dataset, file_name, n_sp=300, sp='rgb', reorder=None): print(file_name) image = dataset.get_image(file_name) if sp == 'rgb': sps = slic_n(image, n_superpixels=n_sp, compactness=10) elif sp == 'rgb-skimage': sps = slic(image, n_segments=n_sp, compactness=10, multichannel=True, sigma=0.1) sps = merge_small_sp(image, morphology.label(sps))[0] elif sp == 'rgbd': depth = dataset.get_depth(file_name) #depth -= depth.min() #depth /= depth.max() rgbd = np.dstack([image / 255., depth]) sps = slic(rgbd, n_segments=n_sp, compactness=.1, convert2lab=False, multichannel=True, sigma=0) sps = merge_small_sp(image, morphology.label(sps))[0] else: raise ValueError("Expected sp to be 'rgb' or 'rgbd' got %d" % sp) gt = gt_in_sp(dataset, file_name, sps) probs = get_probabilities(file_name, dataset.directory) if reorder is not None: probs = probs[:, :, reorder] probs_sp = probabilities_on_sp(dataset, probs, sps) return probs_sp, gt, sps
def train_svm(C=0.1, grid=False): pascal = PascalSegmentation() files_train = pascal.get_split("kTrain") superpixels = [ slic_n(pascal.get_image(f), n_superpixels=100, compactness=10) for f in files_train ] bow = SiftBOW(pascal, n_words=1000, color_sift=True) data_train = bow.fit_transform(files_train, superpixels) data_train = add_global_descriptor(data_train) svm = LinearSVC(C=C, dual=False, class_weight='auto') chi2 = AdditiveChi2Sampler() X, y = np.vstack(data_train.X), np.hstack(data_train.Y) X = chi2.fit_transform(X) svm.fit(X, y) print(svm.score(X, y)) eval_on_sp(pascal, data_train, [svm.predict(chi2.transform(x)) for x in data_train.X], print_results=True) files_val = pascal.get_split("kVal") superpixels_val = [ slic_n(pascal.get_image(f), n_superpixels=100, compactness=10) for f in files_val ] data_val = bow.transform(files_val, superpixels_val) data_val = add_global_descriptor(data_val) eval_on_sp(pascal, data_val, [svm.predict(chi2.transform(x)) for x in data_val.X], print_results=True) tracer()
def load_pascal_single(f, sp_type, which, pascal): print(f) image = pascal.get_image(f) if sp_type == "slic": sp = slic_n(image, n_superpixels=100, compactness=10) segments = None elif sp_type == "cpmc": segments, sp = superpixels_segments(f) sp, _ = merge_small_sp(image, sp) sp = morphological_clean_sp(image, sp, 4) else: raise ValueError("Expected sp to be 'slic' or 'cpmc', got %s" % sp_type) x = get_kraehenbuehl_pot_sp(f, sp) if which != "test": y = gt_in_sp(pascal, f, sp) return x, y, sp, segments