def main():
    import argparse
    from attalos.dataset.mscoco_prep import MSCOCODatasetPrep

    parser = argparse.ArgumentParser(description='Extract text features.')
    parser.add_argument('--dataset_dir',
                        dest='dataset_dir',
                        type=str,
                        help='Directory with input data')
    parser.add_argument('--dataset_type',
                        dest='dataset_type',
                        default='mscoco',
                        choices=[
                            'mscoco', 'visualgenome', 'iaprtc', 'generic',
                            'espgame', 'nuswide'
                        ])
    parser.add_argument('--split',
                        dest='split',
                        default='train',
                        choices=['train', 'test', 'val'])
    parser.add_argument('--output_fname',
                        dest='output_fname',
                        default='captions_text.json.gz',
                        type=str,
                        help='Output json filename')

    args = parser.parse_args()
    if args.dataset_type == 'mscoco':
        from attalos.dataset.mscoco_prep import MSCOCODatasetPrep
        print('Processing MSCOCO Data')
        dataset_prep = MSCOCODatasetPrep(args.dataset_dir, split=args.split)
    elif args.dataset_type == 'visualgenome':
        print('Processing Visual Genome Data')
        from attalos.dataset.vg_prep import VGDatasetPrep
        dataset_prep = VGDatasetPrep(args.dataset_dir, split=args.split)
    elif args.dataset_type == 'iaprtc':
        print('Processing IAPRTC-12 data')
        from attalos.dataset.iaprtc12_prep import IAPRTC12DatasetPrep
        dataset_prep = IAPRTC12DatasetPrep(args.dataset_dir, split=args.split)
    elif args.dataset_type == 'generic':
        print('Processing generic dataset')
        from attalos.dataset.generic_prep import GenericDatasetPrep
        dataset_prep = GenericDatasetPrep(args.dataset_dir, split=args.split)
    elif args.dataset_type == 'espgame':
        print('Processing espgame')
        from attalos.dataset.espgame_prep import ESPGameDatasetPrep
        dataset_prep = ESPGameDatasetPrep(args.dataset_dir, split=args.split)
    elif args.dataset_type == 'nuswide':
        print('Processing nuswide data')
        from attalos.dataset.nuswide_prep import NUSWideDatasetPrep
        dataset_prep = NUSWideDatasetPrep(args.dataset_dir, split=args.split)
    else:
        raise NotImplementedError('Dataset type {} not supported'.format(
            args.dataset_type))

    process_dataset(dataset_prep, args.output_fname)
def main(_):
  import argparse


  parser = argparse.ArgumentParser(description='Extract image features using Inception model.')
  parser.add_argument('--dataset_dir',
                      dest='dataset_dir',
                      type=str,
                      help='Directory with input images')
  parser.add_argument('--dataset_type',
                      dest='dataset_type',
                      default='mscoco',
                      choices=['mscoco', 'visualgenome', 'iaprtc', 'generic', 'espgame'])
  parser.add_argument('--split',
                      dest='split',
                      default='train',
                      choices=['train', 'test', 'val'])
  parser.add_argument('--output_fname',
                      dest='output_fname',
                      default='image_features.hdf5',
                      type=str,
                      help='Output hd5f filename')
  parser.add_argument('--working_dir',
                      dest='working_dir',
                      default=tempfile.gettempdir(),
                      type=str,
                      help='Working directory for hdf5 file creation')
  args = parser.parse_args()

  if args.dataset_type == 'mscoco':
    print('Processing MSCOCO Data')
    from attalos.dataset.mscoco_prep import MSCOCODatasetPrep
    dataset_prep = MSCOCODatasetPrep(args.dataset_dir, split=args.split)
  elif args.dataset_type == 'visualgenome':
    print('Processing Visual Genome Data')
    from attalos.dataset.vg_prep import VGDatasetPrep
    dataset_prep = VGDatasetPrep(args.dataset_dir, split=args.split)
  elif args.dataset_type == 'iaprtc':
    print('Processing IAPRTC-12 data')
    from attalos.dataset.iaprtc12_prep import IAPRTC12DatasetPrep
    dataset_prep = IAPRTC12DatasetPrep(args.dataset_dir, split=args.split)
  elif args.dataset_type == 'generic':
    print('Processing espgame data')
    from attalos.dataset.generic_prep import GenericDatasetPrep
    dataset_prep = GenericDatasetPrep(args.dataset_dir, split=args.split)
  elif args.dataset_type == 'espgame':
    print('Processing espgame data')
    from attalos.dataset.espgame_prep import ESPGameDatasetPrep
    dataset_prep = ESPGameDatasetPrep(args.dataset_dir, split=args.split)
  else:
      raise NotImplementedError('Dataset type {} not supported'.format(args.dataset_type))
  process_dataset(dataset_prep, args.output_fname, working_dir=args.working_dir)
Example #3
0
    def load_metadata(self):
        """
        Load the ESP Game Metadata to allow for efficient iteration
        Returns:

        """
        if self.split == SplitType.TRAIN:
            split_name = 'train'
        elif self.split == SplitType.TEST:
            split_name = 'test'
        else:
            raise NotImplementedError('Split type not yet implemented')

        if self.image_file_handle is None:
            self.image_file_handle = tarfile.open(self.image_filename)
            self.metadata_file_handle = tarfile.open(self.metadata_filename)

        dictionary_lookup = {}
        for i, word in enumerate(
                self.metadata_file_handle.extractfile(
                    'espgame_dictionary.txt')):
            dictionary_lookup[i] = word.strip()

        filenames = []
        filelist_filename = 'espgame_{}_list.txt'.format(split_name)
        for filename in self.metadata_file_handle.extractfile(
                filelist_filename):
            img_id = os.path.basename(filename.strip()) + '.jpg'
            filenames.append(img_id)

        # Get valid tags for files
        item_info = {}
        annotationlist_filename = 'espgame_{}_annot.hvecs'.format(split_name)
        annotationlist_file = self.metadata_file_handle.extractfile(
            annotationlist_filename)
        tag_onehot = IAPRTC12DatasetPrep.parse_LEAR_annotation_file(
            annotationlist_file)
        for img_index in range(tag_onehot.shape[0]):
            image_id = filenames[img_index]
            tags = []
            for tag_id in np.nonzero(tag_onehot[img_index])[0]:
                tags.append(dictionary_lookup[tag_id])
            image_filename = 'ESP-ImageSet/images/{}'.format(image_id)
            item_info[image_id] = {
                'fname': image_filename,
                'id': image_id,
                'tags': tags,
                'captions': []
            }
        return item_info
Example #4
0
    def load_metadata(self):
        """
        Load the ESP Game Metadata to allow for efficient iteration
        Returns:

        """
        if self.split == SplitType.TRAIN:
            split_name = 'train'
        elif self.split == SplitType.TEST:
            split_name = 'test'
        else:
            raise NotImplementedError('Split type not yet implemented')

        if self.image_file_handle is None:
            self.image_file_handle = tarfile.open(self.image_filename)
            self.metadata_file_handle = tarfile.open(self.metadata_filename)

        dictionary_lookup = {}
        for i, word in enumerate(self.metadata_file_handle.extractfile('espgame_dictionary.txt')):
            dictionary_lookup[i] = word.strip()

        filenames = []
        filelist_filename = 'espgame_{}_list.txt'.format(split_name)
        for filename in self.metadata_file_handle.extractfile(filelist_filename):
            img_id = os.path.basename(filename.strip()) + '.jpg'
            filenames.append(img_id)

        # Get valid tags for files
        item_info = {}
        annotationlist_filename = 'espgame_{}_annot.hvecs'.format(split_name)
        annotationlist_file = self.metadata_file_handle.extractfile(annotationlist_filename)
        tag_onehot = IAPRTC12DatasetPrep.parse_LEAR_annotation_file(annotationlist_file)
        for img_index in range(tag_onehot.shape[0]):
            image_id = filenames[img_index]
            tags = []
            for tag_id in np.nonzero(tag_onehot[img_index])[0]:
                tags.append(dictionary_lookup[tag_id])
            image_filename = 'ESP-ImageSet/images/{}'.format(image_id)
            item_info[image_id] = {'fname': image_filename,
                                             'id': image_id,
                                             'tags': tags,
                                             'captions': []}
        return item_info