Exemplo n.º 1
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",
    )
Exemplo n.º 2
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]))
Exemplo n.º 3
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())