def _image_to_internal(self, imagelike: Imagelike) -> np.ndarray:
        # FIXME[todo]: check out the correct preprocessing for AlexNet
        # with the current approach the accuracy is only around 30%

        # get a numpy.ndarray
        image = Image.as_array(imagelike,
                               dtype=np.float32,
                               colorspace=Colorspace.RGB)

        # FIXME: probably we should do center crop here ...
        image = imresize(image, (227, 227))
        # print("Alexnet._image_to_internal:", image.dtype, image.shape)

        # dividing by 256 brings accuracy down to almost 0%.
        # image = image/256.

        # centering slightly improves accuracy
        # FIXME[todo]: we need real means ...
        image = image - image.mean()

        # standardization reduces accuracy to below 3%.
        # image = image / image.std()

        # Caffe Uses BGR Order
        # RGB to BGR: this really boosts performance; from 33% to 55%
        image = image[:, :, ::-1]
        # tmp = image[:, :, 2].copy()
        # image[:, :, 2] = image[:, :, 0]
        # image[:, :, 0] = tmp
        return image
    def resize(self, img):
        from dltb.util.image import imresize

        if self._new_shape is None:
            return img

        if self._new_shape[0:2] == img.shape[0:2]:
            return img

        # return resize(img, self._new_shape, preserve_range=True)
        return imresize(img, self._new_shape[0:2])
    def test_imresize(self) -> None:
        # uint8, RGB
        image = np.random.randint(256, size=(450, 300, 3), dtype=np.uint8)
        new_image = imresize(image, size=(150, 100))
        self.assertEqual(new_image.shape, (100, 150, 3))
        self.assertEqual(new_image.dtype, np.uint8)

        # uint8, grayscale
        image = np.random.randint(256, size=(450, 300), dtype=np.uint8)
        new_image = imresize(image, size=(150, 100))
        self.assertEqual(new_image.shape, (100, 150))
        self.assertEqual(new_image.dtype, np.uint8)

        # float32, RGB
        image = np.random.rand(450, 300, 3)
        new_image = imresize(image, size=(150, 100))
        self.assertEqual(new_image.shape, (100, 150, 3))
        self.assertEqual(new_image.dtype, image.dtype)

        # float32, grayscale
        image = np.random.rand(450, 300)
        new_image = imresize(image, size=(150, 100))
        self.assertEqual(new_image.shape, (100, 150))
        self.assertEqual(new_image.dtype, image.dtype)
Beispiel #4
0
    def test_tensorflow_network_(self):
        self.assertTrue(os.path.isfile(self.checkpoint + '.index'))

        network = TensorFlowNetwork(checkpoint=self.checkpoint)
        network.prepare()
        self.assertTrue(network.prepared)

        # print(network._sess.graph.get_operations())
        # network.summary()

        images = []
        for arg in ('images/elephant.jpg',):
            im = (imread(arg)[:, :, :3]).astype(np.float32)
            im = imresize(im, (227, 227))
            im = im - im.mean()
            im[:, :, 0], im[:, :, 2] = im[:, :, 2], im[:, :, 0]
            images.append(im)

        self.assertEqual(len(images), 1)
        self.assertTrue(isinstance(images[0], np.ndarray))

        # Assuming the first op is the input.
        network_input_tensor = \
            network._session.graph.get_operations()[0].outputs[0]
        network_output_tensor = network['dense_3'].activation_tensor

        in_op = None
        out_op = None
        for op in network._session.graph.get_operations():
            # print(op.type)
            if op.type == 'Placeholder':
                _in_op = op
                print("Heureka: in!")
            if op.type == 'Softmax':
                out_op = op
                print("Heureka: out!")
                break

        if out_op:
            feed_dict = {network_input_tensor: images}
            output = network._session.run(network_output_tensor,
                                          feed_dict=feed_dict)

        for input_im_ind in range(output.shape[0]):
            inds = np.argsort(output)[input_im_ind, :]
            print("Image", input_im_ind)
Beispiel #5
0
 def crop_faces(image: Imagelike) -> np.ndarray:
     """Crop faces in the style of the original LFW dataset.  The procedure
     for obtaining the 250x250 pixel images is as follows: detect
     faces with the OpenCV Haar Cascade detector. Then scale the
     (square-shaped) bounding box by a factor of 2.2 in each
     direction.  Scale the resulting crop to 250x250 pixels.
     """
     opencv_face_module = \
         importlib.import_module('.face', 'dltb.thirdparty.opencv')
     detector = opencv_face_module.DetectorHaar()
     image = Image(image)
     bounding_boxes = list(detector.detect_boxes(image))
     faces = np.ndarray((len(bounding_boxes), 250, 250, 3), dtype=np.uint8)
     for index, box in enumerate(bounding_boxes):
         box = box.scale(2.2, reference='center')
         patch = box.extract.extract_from_image(image)
         faces[index] = imresize(patch, (250, 250))
     return faces