def canonization_training(opts): print "# Canonization training" params = caching.nul_repr_dict() # Generate average intensity image from a subset of the dataset. img_avg = np.zeros(opts["img_shape"], dtype=int) files = dataset.training_files(opts["num_train_images"]) for img_file, depth_file in print_progress(files): img = preprocess_image(img_file) segmask = segmentation(depth_file, opts["segmentation"]) img, segmask = _canonize(img, segmask, opts) # Orient correctly if image is upside down if dataset.is_upside_down(img_file): img = np.fliplr(np.flipud(img)) img_avg += img img_avg /= len(files) params["img_avg"] = img_avg params["img_avg_upsidedown"] = np.fliplr(np.flipud(img_avg)) return params
def bow_train(featfunc, opts, canon_opts, params): descs = [] files = dataset.training_files(opts['num_train_images']) for img_file, depth_file in print_progress(files): img, segmask = canonize(img_file, depth_file, canon_opts, params) if featfunc == 'daisy': h = daisy(img_as_float(img), **opts['feature_opts']) if featfunc == 'jet': h = jet(img_as_float(img), **opts['feature_opts']) segmask = sp.misc.imresize(segmask, h.shape[:2]) for i in range(h.shape[0]): for j in range(h.shape[1]): if segmask[i,j] != 0: descs.append(h[i,j,:].flatten()) descs = [descs[i] for i in range(0, len(descs), opts['feature_step'])] descs = np.vstack(tuple(descs)) print '# K-means clustering of %i features of dimensionality %i'%(descs.shape[0], descs.shape[1]) kmeans = KMeans(opts['num_clusters'], n_jobs=options.num_threads) kmeans.fit(descs) return kmeans