4 Create BoF and BoF Histogram dataset 8 import cPickle
as pickle
12 from sklearn.datasets.base
import Bunch
13 from sklearn.preprocessing
import normalize
14 from jsk_recognition_utils
import BagOfFeatures
19 with gzip.open(data_path,
'rb')
as f:
20 descs = pickle.load(f)[
'descriptors']
22 n_data_all = len(descs)
23 n_data =
int(data_size * n_data_all)
24 p = np.random.randint(0, len(descs), n_data)
25 descs = np.array(descs)[p]
26 X = np.vstack(map(
lambda x: np.array(x).reshape((-1, 128)), descs))
29 print(
'fitting bag of features extractor')
33 except MemoryError
as e:
34 print((
'data_size: {} ({} * {})'.format(n_data, data_size, n_data_all)))
38 with gzip.open(output,
'wb')
as f:
44 print(
'creating dataset')
45 with gzip.open(data_path,
'rb')
as f:
46 dataset = pickle.load(f)
47 descs, y, target_names = (dataset[
'descriptors'],
49 dataset[
'target_names'])
51 print(
'extracting feature')
52 with gzip.open(bof_path,
'rb')
as f:
54 X = bof.transform(descs)
56 normalize(X, copy=
False)
57 dataset = Bunch(data=X, target=y, target_names=target_names)
58 print(
'saving dataset')
59 with gzip.open(output,
'wb')
as f:
60 pickle.dump(dataset, f)
65 parser = argparse.ArgumentParser()
66 subparsers = parser.add_subparsers(dest=
'command')
68 bof_parser = subparsers.add_parser(
'extract_bof',
69 help=
'fit dataset and extract Bag of Features')
70 bof_parser.add_argument(
'data_path', help=
'SIFT data path')
71 bof_parser.add_argument(
'-O',
'--output', default=
'bof.pkl.gz',
72 help=
'bof feature extractor instance save path')
73 bof_parser.add_argument(
'-s',
'--data-size', default=1, type=float,
74 help=
'data_size in 0 to 1')
76 dataset_parser = subparsers.add_parser(
'extract_bof_hist',
77 help=
'create BoF histogram dataset')
78 dataset_parser.add_argument(
'data_path', help=
'SIFT data path')
79 dataset_parser.add_argument(
'bof_path',
80 help=
"BoF data path extracted by 'extract_bof' command")
81 dataset_parser.add_argument(
'-O',
'--output', default=
'bof_hist.pkl.gz',
82 help=
'save path of bof histogram (default: bof_hist.pkl.gz)')
83 args = parser.parse_args()
85 if args.command ==
'extract_bof':
88 data_size=args.data_size)
89 elif args.command ==
'extract_bof_hist':
91 bof_path=args.bof_path,
95 if __name__ ==
'__main__':
def cmd_extract_bof(data_path, output, data_size=1)
def cmd_extract_bof_hist(data_path, bof_path, output)