Exemple #1
0
 def _loadEntry(self, index):
     image_entry = self.image_entries[index]
     sample = utils.buildImageSample(image_entry, self.c, self.imreader,
                                     self.where_object)
     sample = _filterKeys(self.used_keys, sample)
     sample = utils.applyTransformGroup(self.transform_group, sample)
     return sample
Exemple #2
0
    def __getitem__(self, index):
        '''
        Used to train for classification / segmentation of individual objects.
        Args:
            index:      (int) An index of an image in the dataset.
        Returns a dict with keys:
            image:      (np.uint8) The array corresponding to an image.
            mask:       (np.uint8) The array corresponding to a mask if exists, or None.
            objectid:   (int) The primary key in the "objects" table.
            name:       (string) The name field in the "objects" table.
            score:      (float) The score field in the "objects" table.
            imagefile:  (string) The image id.
            All key-value pairs from the "properties" table for this objectid.
        '''
        if self.preloaded_samples is not None:
            sample = self.preloaded_samples[index]
        else:
            logging.debug('Querying for %d-th object out of %d', index,
                          len(self.object_entries))
            object_entry = self.object_entries[index]
            sample = utils.buildObjectSample(object_entry, self.c,
                                             self.imreader)
            if sample is None:
                logging.warning('Returning None for index %d', index)
                return None

        sample = _filterKeys(self.used_keys, sample)
        sample = utils.applyTransformGroup(self.transform_group, sample)
        return sample
Exemple #3
0
 def test_list(self):
     transform_group = [lambda x: x['image'], lambda x: x['objectid']]
     sample = {'image': np.zeros(3), 'objectid': 1, 'name': 'car'}
     sample = utils.applyTransformGroup(transform_group, sample)
     self.assertTrue(isinstance(sample, list))
     self.assertEqual(len(sample), 2)  # only "image" and 'objectid' left.
     self.assertTrue(isinstance(sample[0], (np.ndarray, np.generic)))
     self.assertTrue(isinstance(sample[1], int))
Exemple #4
0
 def test_dict(self):
     transform_group = {
         'image': lambda x: x[:, :, 0],
         'name': lambda _: 'hi',
         'dummy': lambda x: x,
     }
     sample = {'image': np.zeros((10, 10, 3)), 'objectid': 1, 'name': 'car'}
     sample = utils.applyTransformGroup(transform_group, sample)
     self.assertEqual(len(sample['image'].shape), 2)  # Grayscale image.
     self.assertEqual(sample['name'], 'hi')  # All names replaced to "hi".
     self.assertIsNone(sample['dummy'])  # Returns None for missing keys.
Exemple #5
0
def _object_dataset_function(db_file,
                             rootdir='.',
                             where_object='TRUE',
                             used_keys=None,
                             transform_group=None):
    '''
    Args:
        db_file:        (string) A path to an sqlite3 database file.
        rootdir:        (string) A root path, is pre-appended to "imagefile"
                        entries of "images" table in the sqlite3 database.
        where_object:   (string) The WHERE part of the SQL query on the 
                        "objects" table, as in: 
                        "SELECT * FROM objects WHERE ${where_object};"
                        Allows to query only needed objects.
        used_keys:      (a list of strings or None) If specified, use only
                        these keys for every sample, and discard the rest.
        transform_group: A dict {string: callable}. Each key of this dict
                        should match a key in each sample, and the callables
                        are applied to the respective sample dict values.
    '''

    conn = utils.openConnection(db_file, 'r', copy_to_memory=False)
    c = conn.cursor()
    utils.checkWhereObject(where_object)
    c.execute('SELECT * FROM objects WHERE %s ORDER BY objectid' %
              where_object)
    object_entries = c.fetchall()

    imreader = backendMedia.MediaReader(rootdir=rootdir)

    _checkUsedKeys(used_keys)
    utils.checkTransformGroup(transform_group)

    samples = []
    logging.info('Loading samples...')
    for index in range(len(object_entries)):
        object_entry = object_entries[index]
        sample = utils.buildObjectSample(object_entry, c, imreader)
        if sample is None:
            continue
        sample = _filterKeys(used_keys, sample)
        sample = utils.applyTransformGroup(transform_group, sample)
        samples.append(sample)
    logging.info('Loaded %d samples.', len(object_entries))
    return samples
Exemple #6
0
 def __getitem__(self, index):
     '''
     Used to train for the detection/segmentation task.
     Args:
         index:      An index of an image in the dataset.
     Returns a dict with keys:
         image:      np.uint8 array corresponding to an image.
         mask:       np.uint8 array corresponding to a mask if exists, or None.
         objects:    np.int32 array of shape [Nx5].
                     Each row is (x1, y1, width, height, classname)
         imagefile:  The image id.
     '''
     image_entry = self.image_entries[index]
     sample = utils.buildImageSample(image_entry, self.c, self.imreader,
                                     self.where_object)
     if sample is None:
         logging.warning('Returning None for index %d', index)
         return None
     sample = _filterKeys(self.used_keys, sample)
     sample = utils.applyTransformGroup(self.transform_group, sample)
     return sample
Exemple #7
0
 def _loadEntry(self, index):
     object_entry = self.object_entries[index]
     sample = utils.buildObjectSample(object_entry, self.c, self.imreader)
     sample = _filterKeys(self.used_keys, sample)
     sample = utils.applyTransformGroup(self.transform_group, sample)
     return sample
Exemple #8
0
 def test_callable(self):
     transform_group = lambda x: x['image']
     sample = {'image': np.zeros(3), 'objectid': 1}
     sample = utils.applyTransformGroup(transform_group, sample)
     self.assertTrue(isinstance(sample, (np.ndarray, np.generic)))