create_bof_dataset.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Create BoF and BoF Histogram dataset
5 """
6 
7 import gzip
8 import cPickle as pickle
9 import argparse
10 
11 import numpy as np
12 from sklearn.datasets.base import Bunch
13 from sklearn.preprocessing import normalize
14 from jsk_recognition_utils import BagOfFeatures
15 
16 
17 def cmd_extract_bof(data_path, output, data_size=1):
18  print('loading data')
19  with gzip.open(data_path, 'rb') as f:
20  descs = pickle.load(f)['descriptors']
21  # limit to specified data size to avoid memory error
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))
27  del descs
28  # extract feature
29  print('fitting bag of features extractor')
30  bof = BagOfFeatures()
31  try:
32  bof.fit(X)
33  except MemoryError as e:
34  print(('data_size: {} ({} * {})'.format(n_data, data_size, n_data_all)))
35  print(e)
36  # save bof extractor
37  print('saving bof')
38  with gzip.open(output, 'wb') as f:
39  pickle.dump(bof, f)
40  print('done')
41 
42 
43 def cmd_extract_bof_hist(data_path, bof_path, output):
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'],
48  dataset['target'],
49  dataset['target_names'])
50  del dataset
51  print('extracting feature')
52  with gzip.open(bof_path, 'rb') as f:
53  bof = pickle.load(f)
54  X = bof.transform(descs)
55  del 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)
61  print('done')
62 
63 
64 def main():
65  parser = argparse.ArgumentParser()
66  subparsers = parser.add_subparsers(dest='command')
67  # extract_bof 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')
75  # extract_bof_hist command
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()
84 
85  if args.command == 'extract_bof':
86  cmd_extract_bof(data_path=args.data_path,
87  output=args.output,
88  data_size=args.data_size)
89  elif args.command == 'extract_bof_hist':
90  cmd_extract_bof_hist(data_path=args.data_path,
91  bof_path=args.bof_path,
92  output=args.output)
93 
94 
95 if __name__ == '__main__':
96  main()
def cmd_extract_bof(data_path, output, data_size=1)
def cmd_extract_bof_hist(data_path, bof_path, output)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27