Example #1
0
 def _get_meta(self, data: Data, **kwargs) -> None:
     # pylint: disable=arguments-differ
     """Get metadata for some data.
     """
     data.add_attribute('frame', batch=True)  # usually the same a index
     data.add_attribute('time', batch=True)
     super()._get_meta(data, **kwargs)
Example #2
0
 def _get_meta(self, data: Data, **kwargs) -> None:
     # pylint: disable=arguments-differ
     """Get metadata for some data.
     """
     if self._label_from_directory:
         data.add_attribute('label', batch=True)
     super()._get_meta(data, **kwargs)
Example #3
0
 def _get_label_for_filename(self, data: Data, filename: str) -> None:
     """Get a class label for the file from the directory name.
     """
     if self._label_from_directory is True:
         data.label = os.path.dirname(filename)
     elif self._label_from_directory:
         data.label = \
             self._scheme.identifier(os.path.dirname(filename),
                                     lookup=self._label_from_directory)
Example #4
0
    def _read_frame_at(self, data: Data, time: float):
        """Fetch a video frame at a given timepoint from this
        :py:class:`Video`.

        Arguments
        ---------
        time: float
            The temporal position (point in time) of the frame
            to fetch in seconds (fractions may be given).
        """
        data.array = self._backend.read_frame(time=time)
        data.index = self._backend.frame
Example #5
0
    def _get_index(self, data: Data, index: int, **kwargs) -> None:
        """Implementation of the :py:class:`Indexed` datasource interface.
        The index will be interpreted as frame number.

        Arguments
        ---------
        index: int
            The frame number of the frame to fetch from the video.
            If no frame is given, the next frame of the video will
            be fetched (advancing the frame number by 1).
        """
        data.array = self._backend[index]
        data.frame = self._backend.frame
        data.index = index or data.frame
        data.time = self._backend.time
Example #6
0
 def _get_data(self,
               data: Data,
               frame: int = None,
               time: float = None,
               index: int = None,
               **kwargs) -> None:
     # pylint: disable=arguments-differ
     if frame is not None and not data:
         data.datasource_argument = 'frame'
         data.datasource_value = frame
         index = frame
     elif time is not None and not data:
         data.datasource_argument = 'time'
         data.datasource_value = time
         index = self._backend.frame_at(time)
     super()._get_data(data, index=index, **kwargs)
Example #7
0
    def _get_label_for_filename(self, data: Data, filename: str) -> None:
        """Get the (internal) label for a given filename from the ImageNet
        dataset.  The way to determine the label depends on what section
        of the dataset ('train', 'val', or 'test') we are working on and
        requires additional data.  If this data is not available,
        the method will simply return None.

        Parameters
        ----------
        filename: str
            The filename of the image, relative to the base directory
            imagenet_data/{train,val,test}/. That is for example
            'n01440764/n01440764_3421.JPEG' in case of 'train' or
            'ILSVRC2012_val_00031438.JPEG' in case of 'val' images.

        Returns
        ------
        label: int
            A number from 0-999 indicating the class to which the image
            belongs or None if no class could be determined.
        """
        label = None

        if self._section == 'train':
            super()._get_data_from_file(data, filename)
        elif self._section == 'val' and self._val_labels is not None:
            match_object = re.search('ILSVRC2012_val_([0-9]*).JPEG', filename)
            if match_object is not None:
                label = self._val_labels[int(match_object.group(1))-1]
                data.label = self._scheme.identifier(label)
Example #8
0
 def test_instantiate_data(self):
     """Instantiating the (abstract) Data class should yield a
     DataDict object.
     """
     data = Data()
     self.assertTrue(isinstance(data, Data))
     self.assertTrue(isinstance(data, DataDict))
Example #9
0
 def test_batch(self):
     """Data objects initialized with the `batch` argument should
     be batches.
     """
     data = Data(batch=3)
     self.assertTrue(data.is_batch)
     self.assertEqual(len(data), 3)
Example #10
0
 def test_single_tool_02(self) -> None:
     """Call the :py:class:`SingleTestTool` with a single
     :py:class:`Data` argument.
     """
     data = Data(array=[1, 2, 3], batch=False)
     tool, batch = SingleTestTool(data)
     result = tool(data)
     self.assertIsInstance(result, int)
     self.assertIsFalse(batch)
Example #11
0
 def test_batch_tool_02(self) -> None:
     """Call the :py:class:`BatchTestTool` with a single (non-batch)
     :py:class:`Data` argument.
     """
     data = Data(array=[1, 2, 3], batch=False)
     tool = BatchTestTool()
     result, batch = tool(data)
     self.assertEqual(result, 6)
     self.assertIsTrue(batch)
Example #12
0
 def _get_index(self, data: Data, index: int, **kwargs) -> None:
     """Implementation of the :py:class:`Indexed` interface. Lookup
     up data point by its index in the file list.
     """
     if self._filenames is None:
         raise ValueError(f"Access by index ({index}) is disabled: "
                          "no filename register was provided.")
     data.index = index
     self._get_data_from_file(data, self._filenames[index])
Example #13
0
 def _get_meta(self, data: Data, filename: str = None, **kwargs) -> None:
     # pylint: disable=arguments-differ
     if filename is not None and not data.datasource_argument:
         data.datasource_argument = 'filename'
         data.datasource_value = filename
     data.add_attribute('filename', batch=True)
     if self._filenames is not None:
         data.add_attribute('index', batch=True)
     if self._loader_kind != 'array':
         data.add_attribute(self._loader_kind, batch=True)
     super()._get_meta(data, **kwargs)
Example #14
0
 def _get_meta(self, data: Data, **kwargs) -> None:
     # pylint: disable=arguments-differ
     if self._identities is not None:
         data.add_attribute('identity', batch=True)
     if self._landmarks is not None:
         data.add_attribute('landmarks', batch=True)
     if self._bboxes is not None:
         data.add_attribute('bbox', batch=True)
     if self._attr_names is not None:
         for name in self._attr_names:
             data.add_attribute(name, batch=True)
     super()._get_meta(data, **kwargs)
def demo_image_activations3(network, image) -> None:
    """Show activations for an `network` when applied to a given `image`.
    Activations are obtained by applying an :py:class:`ActivationTool`,
    passing the image as :py:class:`Data` object.
    """
    tool = ActivationTool(network)
    data = Data(image)
    activations3 = tool(data)  # dict
    show_activations(activations3,
                     title="Demo 3: "
                     "ActivationTool(network)(data):")
def demo_image_activations4(network, image) -> None:
    """Show activations for an `network` when applied to a given `image`.
    Activations are obtained using an :py:class:`ActivationTool`,
    that is applied to a :py:class:`Data` object, encapsulating the image.
    """
    tool = ActivationTool(network)
    data = Data(image)
    tool.apply(data)
    activations4 = tool.data_activations(data)  # dict
    show_activations(activations4,
                     title="Demo 4: "
                     "ActivationTool(network).apply(data):")
Example #17
0
 def _get_data(self, data: Data, **kwargs) -> None:
     # pylint: disable=arguments-differ
     """
     Raises
     ------
     ValueError:
         If the index is present, but the :py:class:`DataFiles`
         has no filename register.
     """
     super()._get_data(data, **kwargs)
     if self._ffhq_meta is not None:
         data.url = self._ffhq_meta[data.filename]['metadata']['photo_url']
Example #18
0
    def _get_data_from_file(self, data: Data, filename: str):
        """Get data from a given file.

        Arguments
        ---------
        filename: str
            The filename (relative to this directory).

        Notes
        -----
        Subclasses implementing this method may utilize
        :py:meth:`load_datapoint_from_file` to load the actual data.

        """
        abs_filename = os.path.join(self.directory, filename)
        setattr(data, self._loader_kind,
                self.load_datapoint_from_file(abs_filename))
        data.filename = abs_filename
        if self._filenames is not None and not hasattr(data, 'index'):
            # FIXME[todo]: This can be improved by reverse lookup table
            data.index = self._filenames.index(filename)
    def onShow(self, checked: bool):
        """Respond to the 'show' button.  Set the current image as
        input data to the controller.  This causes the activations
        (and the classification result) to be displayed in the
        'Activations' panel.
        """
        if not self._toolbox:
            return

        #image = self._maximization.get_snapshot()
        image = self._imageView.getImage()

        if image is None:
            return

        #a,b = self._maximization.get_min(), self._maximization.get_max()
        #image2 = (image-a)*255/(b-a)
        description = ("Artificial input generated to maximize "
                       f"activation of unit {self._config.UNIT_INDEX} "
                       f"in layer {self._config.LAYER_KEY} "
                       f"of network {self._config.NETWORK_KEY}")
        # FIXME[check]: could we use self._maximization.description here?
        # FIXME[hack]: label is only self._config.UNIT_INDEX, if the
        # network is a classifier an we have selected the last
        # (i.e. output) layer.
        data = Data()
        data.array = image
        data.image = True
        data.description = description
        data.label = self._config.UNIT_INDEX
        self._toolbox.set_input(data)
def demo_image_activations5(network, image) -> None:
    """Show activations for an `network` when applied to a given `image`.
    Activations are obtained using an :py:class:`ActivationTool`,
    applied by an :py:class:`ActivationWorker`.
    """
    tool = ActivationTool(network)
    worker = ActivationWorker(tool=tool)
    data = Data(image)
    worker.work(data, run=False)
    activations5 = tool.data_activations(data)  # dict
    show_activations(activations5,
                     title="Demo 5: "
                     "ActivationWorker(tool).work(data):")
Example #21
0
    def test_batch3(self):
        """Testing attributes with `initalize` argument.
        """
        data = Data(batch=3)
        data.add_attribute('a')
        data.add_attribute('b', batch=True, initialize=True)
        data.a = 5
        data[1].b = 3
        data.b[2] = 4

        self.assertEqual(data.a, 5)
        self.assertEqual(data[1].a, 5)
        self.assertEqual(data[1].b, 3)
        self.assertEqual(data.b[1], 3)
        self.assertEqual(data[2].b, 4)
        self.assertEqual(data.b[2], 4)
Example #22
0
 def _get_data(self, data: Data, **kwargs) -> None:
     # pylint: disable=arguments-differ
     """
     Raises
     ------
     ValueError:
         If the index is present, but the :py:class:`DataFiles`
         has no filename register.
     """
     super()._get_data(data, **kwargs)
     if self._identities is not None:
         data.identity = self._identities[data.index]
     if self._bboxes is not None:
         box = self._bboxes[data.index]
         data.bbox = Region(
             BoundingBox(x=box[0], y=box[1], width=box[2], height=box[3]))
     if self._landmarks is not None:
         landmarks = \
             FacialLandmarks(self._landmarks[data.index].reshape((-1, 2)))
         data.landmarks = landmarks  # Region(landmarks)
     if self._attr_names is not None:
         for index, name in enumerate(self._attr_names):
             setattr(data, name, bool(self._attributes[data.index, index]))
Example #23
0
    def _get_data_from_file(self, data: Data, filename: str) -> None:
        super()._get_data_from_file(data, filename)

        # provide the metadata (landmarks)
        basename = os.path.basename(filename)
        if basename.endswith('.jpg'):
            basename = basename[:-len('.jpg')]
        if self._load_annotations:
            # self._annotations[basename] is the landmark object
            landmarks = self._annotations[basename]
        else:
            # self._annotations[basename] is the name of the annotation
            # file (relative to the annotation directory)
            annotation_filename = os.path.join(self.directory, 'annotation',
                                               self._annotations[basename])
            _, landmarks = self._load_annotation(annotation_filename)

        data.label = Region(landmarks)
Example #24
0
    def _get_random(self,
                    data: Data,
                    shape: Tuple[int, ...] = None,
                    distribution: str = None,
                    **kwargs) -> None:
        # pylint: disable=arguments-differ
        """Generate a random datapoint. Parameters can be given as arguments
        or will be taken from object attributes.

        Arguments
        ---------
        shape: tuple
            The shape of the data to be generated.

        distribution: str
            The distribution (either `uniform` or `normal`).
        """
        shape = shape or self.shape
        distribution = distribution or self.distribution
        data.array = (np.random.rand(
            *shape) if distribution == 'uniform' else np.random.randn(*shape))
Example #25
0
 def _get_meta(self, data: Data, **kwargs) -> None:
     # pylint: disable=arguments-differ
     if self._ffhq_meta is not None:
         data.add_attribute('url', batch=True)
     super()._get_meta(data, **kwargs)
Example #26
0
    def test_batch2(self):
        """Using batch and non-batch attributes.
        """
        data = Data(batch=3)
        data.add_attribute('a')
        data.add_attribute('b', batch=True)
        data.add_attribute('c', batch=False)

        self.assertFalse(data.is_batch_attribute('a'))
        self.assertTrue(data.is_batch_attribute('b'))
        self.assertFalse(data.is_batch_attribute('c'))
        self.assertFalse(data.is_batch_attribute('d'))
Example #27
0
 def test_data1(self):
     """Ensure that default data objects are not batches.
     """
     data = Data()
     self.assertFalse(data.is_batch)
 def _get_meta(self, data: Data, **kwargs) -> None:
     data.add_attribute('label', batch=True)
     super()._get_meta(data, **kwargs)