Esempio n. 1
0
def get_train_length(wordnet_ids):
    mode = get_mode('train')
    n = 0
    logger.info('Getting number of train examples...')
    bar = IncrementalBar(max=len(wordnet_ids))
    for wordnet_id in wordnet_ids:
        n += get_tar_length(get_tar_path(mode, wordnet_id))
        bar.next()
    bar.finish()
    return n
Esempio n. 2
0
def get_test_records(delete_tar):
    mode = get_mode('test')

    with tarfile.TarFile(get_tar_path(mode)) as tar:
        for member in tar.getmembers():
            filename = member.name
            image = tar.extractfile(member)
            yield image, filename
            image.close()

    maybe_delete(mode, None, delete_tar)
Esempio n. 3
0
def get_train_records(wordnet_ids, delete_tar=False):
    class_indices = meta.get_wordnet_indices(zero_based=ZERO_BASED)
    mode = get_mode('train')
    for wordnet_id in wordnet_ids:
        target = class_indices[wordnet_id]

        with tarfile.TarFile(get_tar_path(mode, wordnet_id)) as tar:
            for member in tar.getmembers():
                filename = member.name
                image = tar.extractfile(member)
                yield image, filename, target
                image.close()

        maybe_delete(mode, wordnet_id, delete_tar)
Esempio n. 4
0
def get_val_records(delete_tar=False):
    from imagenet.meta import load_val_labels
    mode = get_mode('val')
    labels = load_val_labels(zero_based=ZERO_BASED)

    with tarfile.TarFile(get_tar_path(mode)) as tar:
        for member in tar.getmembers():
            filename = member.name
            index = filename.split('_')[-1][:-5]
            target = labels[int(index)-1]
            image = tar.extractfile(member)
            yield image, filename, target
            image.close()

    maybe_delete(mode, None, delete_tar)
Esempio n. 5
0
def convert_other(mode, delete_tar, overwrite=False):
    mode = get_mode(mode)
    check_overwrite(mode, overwrite)
    n_examples = get_tar_length(get_tar_path(mode))

    with get_file(mode, 'w') as fp:
        if mode == 'val':
            write_examples(
                fp, n_examples, include_targets=True,
                records=get_val_records(delete_tar), shuffle=False)
        elif mode == 'test':
            write_examples(
                fp, n_examples, include_targets=False,
                records=get_test_records(delete_tar), shuffle=False)
        else:
            raise ValueError('Invalid mode: "%s"' % mode)
Esempio n. 6
0
def main(_):
    mode = FLAGS.mode
    with get_file(get_mode(mode), 'r') as fp:
        images = fp['encoded_images']
        filenames = fp['filenames']
        targets = fp['targets']
        if 'bounding_boxes' in fp:
            bbs = fp['bounding_boxes']
        else:
            bbs = None
        n = len(images)
        print('number of examples: %d' % n)
        indices = list(range(n))
        print('shuffling...')
        random.shuffle(indices)
        print('Done!')
        for i in indices:
            vis(images[i], filenames[i], targets[i],
                None if bbs is None else bbs[i])
Esempio n. 7
0
def main(_):
    add_bbox(get_mode(FLAGS.mode), FLAGS.overwrite)
Esempio n. 8
0
def main(_):
    compute_bbox(get_mode(FLAGS.mode), FLAGS.overwrite)
Esempio n. 9
0
def _main(mode, delete_tar, overwrite):
    mode = get_mode(mode)
    if mode == 'train':
        convert_train(delete_tar=delete_tar, overwrite=overwrite)
    else:
        convert_other(mode, delete_tar=delete_tar, overwrite=overwrite)