def loadDataset(path, label): #Determine number of images count = 0 for dir in os.listdir(path): for file in os.listdir(path + "/" + dir): if file.endswith(".jpg"): count = count + 1 print("loading dataset from " + path + " (%d images) ..." % count) images = np.zeros([count, IMAGESIZE[0] * IMAGESIZE[1]]) labels = np.empty(count) labels.fill(label) start = int(round(time.time())) #import all images to array i = 0 for dir in os.listdir(path): for file in os.listdir(path + "/" + dir): if file.endswith(".jpg"): imgPath = os.path.join(path, dir, file) images[i] = loadBWPic(imgPath) i = i + 1 print_progress(i, count, start_time_seconds=start) return (images, labels)
def extract_features(features_opts, dataset_opts, params): print "# Extracting image features" files1, files2 = dataset(dataset_opts) features = [] for img_file, depth_file in print_progress(files1 + files2): features.append(feature_extraction(img_file, depth_file, features_opts, params)) return files1, features[: len(features) / 2], files2, features[len(features) / 2 :]
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