Skip to content

Code snippet disambigation

Carlos de la Torre edited this page Aug 11, 2016 · 2 revisions

What's going on

There's a snippet in the OBIA blog post that's not too clear. To understand it you may need to follow the Jupyter notebook carefully.

training_labels = []
training_objects = []
for klass in classes:
    class_train_objects = [v for i, v in enumerate(objects) if objects_ids[i] in segments_per_klass[klass]]
    training_labels += [klass] * len(class_train_objects)
    print("Training samples for class %i: %i" % (klass, len(class_train_objects)))
    training_objects += class_train_objects

What it finally does is populate two lists, that are matched by position. So, given position i:

training_objects[i] is an object (as defined in the blog post) to be used as a training sample for the class specified in training_labels[i].

But, how...

Let's try to explain it here:

  • objects[i] is the object that represents/models a single segment (computed with the segmentation algorithm).
  • objects_ids[i] stores the segment id (number) corresponding to the previous object.
  • segments_per_klass[klass] is the set of segment ids (numbers) that must be used as train samples for class klass.

Therefore, the line:

    class_train_objects = [v for i, v in enumerate(objects) if objects_ids[i] in segments_per_klass[klass]]

is filtering the items in the objects list to keep only those that are training sample for the class klass. They will be appended to the training_objects list.

The corresponding class label for those samples is the same: klass. So I repeat the klass value as many times as necessary and append them to the training_labels list.

    training_labels += [klass] * len(class_train_objects)

For example, suppose objects

  • A, B, C are samples of class 1
  • M, N are samples of class 2, and
  • Z is a sample of class 3

then, the resulting lists are:

training_objects = [A, B, C, M, N, Z]
training_labels  = [1, 1, 1, 2, 2, 3]

Hope this explanation helps...

Clone this wiki locally