def get_per_video_mbh_data(split, suffix=''): """ Returns the 'train' or 'test' video descriptors. The `suffix` argument can be '_morenull' to load the data with 5296 null samples. """ base_path = ('/home/clear/oneata/data/trecvid12/features/' 'dense5.track15mbh.small.skip_1/') sstats_fn = os.path.join( base_path, 'statistics_k_256', '%s%s.dat' % (split, suffix)) labels_fn = os.path.join( base_path, 'statistics_k_256', 'labels_%s%s.info' % (split, suffix)) info_fn = os.path.join( base_path, 'statistics_k_256', 'info_%s%s.info' % (split, suffix)) gmm_fn = os.path.join(base_path, 'gmm', 'gmm_256') sstats = np.fromfile(sstats_fn, dtype=np.float32) labels = np.array([tuple_label[0] for tuple_label in cPickle.load(open(labels_fn, 'r'))]) video_names = cPickle.load(open(info_fn, 'r'))['video_names'] # Convert sufficient statistics to Fisher vectors. gmm = gmm_read(open(gmm_fn, 'r')) data = FVModel.sstats_to_features(sstats, gmm) return data, labels, video_names
def double_normalization(filenames, sstats_in , sstats_out, N, len_sstats, gmm): """ The slices in each sample are converted to Fisher vectors, square- rooted, L2 normalized, and then aggregated together. Inputs ------ filenames: list of str The names of the files to be aggregated. Usually, this should be the entire dataset, i.e. dataset.get_data('train')[0] + dataset.get_data('test')[0]. sstats_in: SstatsMap instance The sufficient statistics that we operate on. sstats_out: SstatsMap instance The resulting sufficient statistics. N: int The number of slices that are aggregated together. If N is -1 all the slices in the clip are aggregated together. len_sstats: int The length of a typical sufficient statistics vector. gmm: yael.gmm object The Gaussian mixture model for the current sufficient statistics. """ assert len_sstats == gmm.k + 2 * gmm.k * gmm.d, ( "GMM and len_sstats don't match") for filename in filenames: if sstats_out.exists(filename): continue if not sstats_in.exists(filename): print 'Not found ' + filename continue if sstats_in.getsize(filename) == 0: print 'Not computed ' + filename continue sstats = sstats_in.read(filename).reshape((-1, len_sstats)) info = sstats_in.read_info(filename) fv = FVModel.sstats_to_features(sstats, gmm) fv = power_normalize(fv, 0.5) fv = L2_normalize(fv) agg_sstats, agg_info = _aggregate(fv, info, N) sstats_out.write(filename, agg_sstats, info=agg_info)
def double_normalization(filenames, sstats_in, sstats_out, N, len_sstats, gmm): """ The slices in each sample are converted to Fisher vectors, square- rooted, L2 normalized, and then aggregated together. Inputs ------ filenames: list of str The names of the files to be aggregated. Usually, this should be the entire dataset, i.e. dataset.get_data('train')[0] + dataset.get_data('test')[0]. sstats_in: SstatsMap instance The sufficient statistics that we operate on. sstats_out: SstatsMap instance The resulting sufficient statistics. N: int The number of slices that are aggregated together. If N is -1 all the slices in the clip are aggregated together. len_sstats: int The length of a typical sufficient statistics vector. gmm: yael.gmm object The Gaussian mixture model for the current sufficient statistics. """ assert len_sstats == gmm.k + 2 * gmm.k * gmm.d, ( "GMM and len_sstats don't match") for filename in filenames: if sstats_out.exists(filename): continue if not sstats_in.exists(filename): print 'Not found ' + filename continue if sstats_in.getsize(filename) == 0: print 'Not computed ' + filename continue sstats = sstats_in.read(filename).reshape((-1, len_sstats)) info = sstats_in.read_info(filename) fv = FVModel.sstats_to_features(sstats, gmm) fv = power_normalize(fv, 0.5) fv = L2_normalize(fv) agg_sstats, agg_info = _aggregate(fv, info, N) sstats_out.write(filename, agg_sstats, info=agg_info)
def get_per_video_mbh_data_given_list(list_name, **kwargs): """ Loads the Fisher vectors corresponding to the samples found in the list specified by `list_name`. """ K = kwargs.get('K', 256) sstats_path = ('/home/clear/oneata/data/trecvid12/features' '/dense5.track15mbh.small.skip_1/statistics_k_%d' % K) # Default base directories. list_base_path = kwargs.get( 'list_base_path', '/home/lear/douze/src/experiments/trecvid12/data') cache_base_path = kwargs.get( 'cache_base_path', sstats_path) double_norm = kwargs.get('double_norm', False) suffix = '.double_norm' if double_norm else '' # If this file exists load them directly. cache_filename = os.path.join(cache_base_path, 'mbh_' + list_name + suffix + '.raw') if os.path.exists(cache_filename): print "Loading Fisher vectors from MBH descriptors for list %s..." % ( list_name) with open(cache_filename, 'r') as ff: data = np.load(ff) labels = np.load(ff) names = cPickle.load(ff) return data, labels, names D = 64 data, labels, names = [], [], [] sstats_generic_name = os.path.join(sstats_path, 'stats.tmp' + suffix, '%s.dat') list_path = os.path.join(list_base_path, list_name + '.list') gmm_path = os.path.join(sstats_path, 'gmm_%d' % K) gmm = gmm_read(open(gmm_path, 'r')) # Get descriptors for the files in list. print "Creating cache file from list %s..." % list_name for line in open(list_path, 'r'): fields = line.split() sstats_filename = fields[0] video_name = sstats_filename.split('-')[0] sys.stdout.write("%s\t\r" % video_name) try: class_name = fields[1] except IndexError: class_name = 'unknown' try: sstats = np.fromfile(sstats_generic_name % sstats_filename, dtype=np.float32) if double_norm: fv = sstats else: fv = FVModel.sstats_to_features(sstats, gmm) except IOError: print ('Sufficient statistics for video %s are missing;' 'replacing with zeros.') % video_name fv = np.zeros(K + 2 * K * D) data.append(fv) names.append(video_name) labels.append(rev_label_map[class_name]) data = np.vstack(data) labels = np.array(labels) assert data.shape[0] == len(labels), "Data size doesn't match nr of labels" # Cache results to file. with open(cache_filename, 'w') as ff: np.save(ff, data) np.save(ff, labels) cPickle.dump(names, ff) return data, labels, names