Beispiel #1
0
def test_mix_new_and_get():
    from kwiver.sprokit.pipeline import datum
    from kwiver.vital import types as kvt

    # Try creating with generic constructor first, retrieving with
    # type specific get function
    datum_inst = datum.new("string_value")
    check_same_type(datum_inst.get_string(), "string_value")

    datum_inst = datum.new(kvt.Timestamp(1000000000, 10))
    check_same_type(datum_inst.get_timestamp(), kvt.Timestamp(1000000000, 10))

    datum_inst = datum.new(datum.VectorString(["element1", "element2"]))
    check_same_type(datum_inst.get_string_vector(),
                    datum.VectorString(["element1", "element2"]))

    # Now try the opposite
    datum_inst = datum.new_string("string_value")
    check_same_type(datum_inst.get_datum(), "string_value")

    datum_inst = datum.new_timestamp(kvt.Timestamp(1000000000, 10))
    check_same_type(datum_inst.get_datum(), kvt.Timestamp(1000000000, 10))

    datum_inst = datum.new_string_vector(
        datum.VectorString(["element1", "element2"]))
    check_same_type(datum_inst.get_datum(),
                    datum.VectorString(["element1", "element2"]))
Beispiel #2
0
def test_add_get_cpp_types_with_datum():
    from kwiver.sprokit.adapters import adapter_data_set

    ads = adapter_data_set.AdapterDataSet.create()
    add_get_helper(
        ads,
        ads.add_datum,
        ads._get_port_data_double_vector,
        datum.new_double_vector(datum.VectorDouble([6.3, 8.9])),
        "datum_double_vector",
    )
    add_get_helper(
        ads,
        ads.add_datum,
        ads._get_port_data_string_vector,
        datum.new_string_vector(datum.VectorString(["foo", "bar"])),
        "datum_string_vector",
    )
    add_get_helper(
        ads,
        ads.add_datum,
        ads._get_port_data_uchar_vector,
        datum.new_uchar_vector(datum.VectorUChar([102, 103])),
        "datum_uchar_vector",
    )
Beispiel #3
0
def test_add_get_cpp_types():
    from kwiver.sprokit.pipeline import datum

    datum_inst = datum.new_double_vector(datum.VectorDouble([3.14, 4.14]))
    retrieved_val = datum_inst.get_double_vector()
    check_same_type(retrieved_val, datum.VectorDouble([3.14, 4.14]))

    datum_inst = datum.new_string_vector(datum.VectorString(["s00", "s01"]))
    retrieved_val = datum_inst.get_string_vector()
    check_same_type(retrieved_val, datum.VectorString(["s00", "s01"]))

    datum_inst = datum.new_uchar_vector(datum.VectorUChar([100, 101]))
    retrieved_val = datum_inst.get_uchar_vector()
    check_same_type(retrieved_val, datum.VectorUChar([100, 101]))

    check_automatic_conversion(datum.VectorDouble([3.14, 4.14]))
    check_automatic_conversion(datum.VectorString(["s00", "s01"]))
    check_automatic_conversion(datum.VectorUChar([100, 101]))
Beispiel #4
0
def test_iter():
    from kwiver.vital import types as kvt
    from kwiver.sprokit.adapters import adapter_data_set

    ads = _create_ads()

    for port, dat in ads:
        if port == "string_port":
            if dat.get_datum() != "string_value":
                test_error("Didn't retrieve correct string value on first iteration")
        elif port == "timestamp_port":
            if dat.get_datum() != kvt.Timestamp(1000000000, 10):
                test_error(
                    "Didn't retrieve correct timestamp value on second iteration"
                )
        elif port == "vector_string_port":
            if dat.get_datum() != datum.VectorString(["element1", "element2"]):
                test_error("Didn't retrieve correct string vector on third iteration")
        else:
            test_error("unknown port: {}".format(port))
Beispiel #5
0
    def _step(self):
        try:
            print("Stepping SMQTK Query Process")
            #
            # Grab input values from ports using traits.
            #
            # Set/vector of descriptors to perform query off of
            #
            #: :type: kwiver.vital.types.DescriptorSet
            vital_positive_descriptor_set = self.grab_input_using_trait(
                'positive_descriptor_set')
            vital_positive_descriptor_uids = self.grab_input_using_trait(
                'positive_exemplar_uids')
            #
            # Set/vector of descriptors to use as negative examples
            #
            #: :type: kwiver.vital.types.DescriptorSet
            vital_negative_descriptor_set = self.grab_input_using_trait(
                'negative_descriptor_set')
            vital_negative_descriptor_uids = self.grab_input_using_trait(
                'negative_exemplar_uids')
            #
            # Vector of UIDs for vector of descriptors in descriptor_set.
            #
            #: :type: list[str]
            iqr_positive_tuple = self.grab_input_using_trait(
                'iqr_positive_uids')
            iqr_negative_tuple = self.grab_input_using_trait(
                'iqr_negative_uids')
            #
            # Optional input SVM model
            #
            #: :type: kwiver.vital.types.UCharVector
            iqr_query_model = self.grab_input_using_trait('iqr_query_model')

            # Reset index on new query, a new query is one without IQR feedback
            if len(iqr_positive_tuple) == 0 and len(iqr_negative_tuple) == 0:
                self.iqr_session = smqtk.iqr.IqrSession(
                    self.pos_seed_neighbors)

            # Convert descriptors to SMQTK elements.
            #: :type: list[DescriptorElement]
            user_pos_elements = []
            z = zip(vital_positive_descriptor_set.descriptors(),
                    vital_positive_descriptor_uids)
            for vital_descr, uid_str in z:
                smqtk_descr = self.smqtk_descriptor_element_factory.new_descriptor(
                    'from_sprokit', uid_str)
                # A descriptor may already exist in the backend (if its persistant)
                # for the given UID. We currently always overwrite.
                smqtk_descr.set_vector(vital_descr.todoublearray())
                # Queue up element for adding to set.
                user_pos_elements.append(smqtk_descr)

            user_neg_elements = []
            z = zip(vital_negative_descriptor_set.descriptors(),
                    vital_negative_descriptor_uids)
            for vital_descr, uid_str in z:
                smqtk_descr = self.smqtk_descriptor_element_factory.new_descriptor(
                    'from_sprokit', uid_str)
                # A descriptor may already exist in the backend (if its persistant)
                # for the given UID. We currently always overwrite.
                smqtk_descr.set_vector(vital_descr.todoublearray())
                # Queue up element for adding to set.
                user_neg_elements.append(smqtk_descr)

            # Get SMQTK descriptor elements from index for given pos/neg UUID-
            # values.
            #: :type: collections.Iterator[DescriptorElement]
            pos_descrs = self.descriptor_set.get_many_descriptors(
                iqr_positive_tuple)
            #: :type: collections.Iterator[DescriptorElement]
            neg_descrs = self.descriptor_set.get_many_descriptors(
                iqr_negative_tuple)

            self.iqr_session.adjudicate(user_pos_elements, user_neg_elements)
            self.iqr_session.adjudicate(set(pos_descrs), set(neg_descrs))

            # Update iqr working index for any new positives
            self.iqr_session.update_working_index(self.neighbor_index)

            self.iqr_session.refine()

            ordered_results = self.iqr_session.ordered_results()
            ordered_feedback = self.iqr_session.ordered_feedback()
            if self.query_return_n > 0:
                if ordered_feedback is not None:
                    ordered_feedback_results = ordered_feedback[:self.
                                                                query_return_n]
                else:
                    ordered_feedback_results = []
                ordered_results = ordered_results[:self.query_return_n]

            return_elems, return_dists = zip(*ordered_results)
            return_uuids = [e.uuid() for e in return_elems]
            ordered_feedback_uuids = [
                e[0].uuid() for e in ordered_feedback_results
            ]
            ordered_feedback_distances = [
                e[1] for e in ordered_feedback_results
            ]
            # Just creating the scores to preserve the order in case of equal floats
            # because we're passing the distances explicity anyway
            ordered_feedback_scores = numpy.linspace(
                1, 0, len(ordered_feedback_distances))

            # Retrive IQR model from class
            try:
                return_model = self.get_svm_bytes()
            except:
                return_model = []

            # Pass on input descriptors and UIDs
            self.push_to_port_using_trait('result_uids',
                                          datum.VectorString(return_uuids))
            self.push_to_port_using_trait(
                'feedback_uids', datum.VectorString(ordered_feedback_uuids))
            self.push_to_port_using_trait('result_scores',
                                          datum.VectorDouble(return_dists))
            self.push_to_port_using_trait(
                'feedback_distances',
                datum.VectorDouble(ordered_feedback_distances))
            self.push_to_port_using_trait(
                'feedback_scores', datum.VectorDouble(ordered_feedback_scores))
            self.push_to_port_using_trait('result_model',
                                          datum.VectorUChar(return_model))

            self._base_step()

        except BaseException as e:
            print(repr(e))
            import traceback
            print(traceback.format_exc())
    def _step(self):
        try:
            # Grab image container from port using traits
            in_img_c = self.grab_input_using_trait('image')
            dos_ptr = self.grab_input_using_trait('detected_object_set')

            # Declare outputs
            new_positive_descriptors = []
            new_positive_ids = []
            new_negative_descriptors = []
            new_negative_ids = []

            # Get detection bbox
            dos = dos_ptr.select(self._select_threshold)
            bbox_num = dos.size()

            # Make sure we have at least some detections
            app_f_begin = timer()
            if bbox_num != 0:
                im = get_pil_image(in_img_c.image())
                self._app_feature_extractor.frame = im
                pt_app_features = self._app_feature_extractor(dos)
                for item in pt_app_features:
                    new_positive_descriptors.append(item.numpy())
                    new_positive_ids.append(self._get_uid("scale_and_shift"))

            app_f_end = timer()
            print('%%%aug app feature eclapsed time: {}'.format(app_f_end -
                                                                app_f_begin))

            # Get negative descriptors via distance function
            neg_per_pos = int(self._negative_sample_count /
                              len(new_positive_descriptors))

            if neg_per_pos <= 0:
                neg_per_pos = 1

            if self._use_hist:
                for nd in new_positive_descriptors:
                    # Do distance func in database
                    output, indxs = self._hist_tree.query(np.reshape(
                        nd, np.size(nd)),
                                                          k=neg_per_pos)
                    # Make new negative entries
                    for ind in indxs:
                        new_negative_descriptors.append(self._hist_data[ind])
                        new_negative_ids.append(self._get_uid("db_neg"))

            # push outputs
            vital_pos_descriptors = to_vital(new_positive_descriptors)
            vital_neg_descriptors = to_vital(new_negative_descriptors)

            self.push_to_port_using_trait('new_positive_descriptors',
                                          vital_pos_descriptors)
            self.push_to_port_using_trait('new_positive_ids',
                                          datum.VectorString(new_positive_ids))
            self.push_to_port_using_trait('new_negative_descriptors',
                                          vital_neg_descriptors)
            self.push_to_port_using_trait('new_negative_ids',
                                          datum.VectorString(new_negative_ids))
            self._base_step()

        except BaseException as e:
            print(repr(e))
            import traceback
            print(traceback.format_exc())
            sys.stdout.flush()
            raise