Skip to content

Open In Colab

How to parse a voc dataset using predefined splits

Installing IceVision and IceData

If on Colab run the following cell, else check the installation instructions

Install from pypi...

# Torch - Torchvision - IceVision - IceData - MMDetection - YOLOv5 - EfficientDet Installation
!wget https://raw.githubusercontent.com/airctic/icevision/master/icevision_install.sh

# Choose your installation target: cuda11 or cuda10 or cpu
!bash icevision_install.sh cuda11

... or from icevision master

# # Torch - Torchvision - IceVision - IceData - MMDetection - YOLOv5 - EfficientDet Installation
# !wget https://raw.githubusercontent.com/airctic/icevision/master/icevision_install.sh

# # Choose your installation target: cuda11 or cuda10 or cpu
# !bash icevision_install.sh cuda11 master
# Restart kernel after installation
import IPython
IPython.Application.instance().kernel.do_shutdown(True)

Imports

from icevision.all import * 

Load Pascal VOC 2012 dataset

path = icedata.voc.load_data()

Set images, annotations and imagesets directories

annotations_dir = path / "Annotations"
images_dir = path / "JPEGImages"
imagesets_dir = path / "ImageSets/Main"

Define class_map

class_map = icedata.voc.class_map()

Split data using imagesets

ImageSets directory contains text files containing subsets of the dataset. We will split our dataset using the train and validation sets for aeroplanes.

ImageSets directory contains multiple text files containing subsets of images from JPEGImages. We can use these files to select subsets of our data ie aeroplanes. The values we need to pass to FixedSplitter are the values returned by record_id in our parser rather than the filenames.

train = [(line.split(" ",1)[0]) for line in open(imagesets_dir / "aeroplane_train.txt")]
val = [(line.split(" ",1)[0]) for line in open(imagesets_dir / "aeroplane_val.txt")]
presplits = [train, val]
data_splitter = FixedSplitter(presplits)

Parser: use icevision predefined VOC parser

parser = parsers.VOCBBoxParser(annotations_dir=annotations_dir, images_dir=images_dir, class_map=class_map)

Train and validation records

train_records, valid_records = parser.parse(data_splitter, autofix=False)
show_records(train_records[:2], ncols=2)
  0%|          | 0/17125 [00:00<?, ?it/s]

png