Beispiel #1
0
 def testMultipleColumnsRetrieval(self):
     metadata = projector_plugin.EmbeddingMetadata(3)
     metadata.add_column('Sizes', [1, 2, 3])
     metadata.add_column('Labels', ['a', 'b', 'c'])
     self.assertEqual(metadata.column_names, ['Sizes', 'Labels'])
     self.assertEqual(metadata.name_to_values['Labels'], ['a', 'b', 'c'])
     self.assertEqual(metadata.name_to_values['Sizes'], [1, 2, 3])
Beispiel #2
0
    def testAddEmbeddingWithTwoMetadataColumns(self):
        manager = plugin_asset.get_plugin_asset(
            projector_plugin.ProjectorPluginAsset)

        metadata = projector_plugin.EmbeddingMetadata(3)
        metadata.add_column('labels', ['a', 'b', 'друг јазик'])
        metadata.add_column('sizes', [10, 20, 30])
        manager.add_embedding('test', np.array([[1], [2], [3]]), metadata)

        config = projector_config_pb2.ProjectorConfig()
        embedding = config.embeddings.add()
        embedding.tensor_name = 'test'
        embedding.tensor_shape.extend([3, 1])
        embedding.tensor_path = 'test_values.tsv'
        embedding.metadata_path = 'test_metadata.tsv'
        expected_config_pbtxt = text_format.MessageToString(config)

        self.assertEqual(
            manager.assets(), {
                'projector_config.pbtxt':
                expected_config_pbtxt,
                'test_values.tsv':
                b'1\n2\n3\n',
                'test_metadata.tsv':
                'labels\tsizes\na\t10\nb\t20\nдруг јазик\t30\n'
            })
    def testEndpointsMetadataForVariableAssets(self):
        self._GenerateProjectorTestData()
        g = ops.Graph()
        with g.as_default():
            manager = plugin_asset.get_plugin_asset(
                projector_plugin.ProjectorPluginAsset)

        metadata = projector_plugin.EmbeddingMetadata(3)
        metadata.add_column('labels', ['a', 'b', 'c'])
        manager.add_metadata_for_embedding_variable('test', metadata)

        fw = writer.FileWriter(self.log_dir, graph=g)
        fw.close()

        self._SetupWSGIApp()
        run_json = self._GetJson('/data/plugin/projector/runs')
        self.assertTrue(run_json)

        run = run_json[0]
        metedata_query = '/data/plugin/projector/metadata?run=%s&name=test' % run
        metadata_tsv = self._Get(metedata_query).data
        self.assertEqual(metadata_tsv, b'a\nb\nc\n')

        unk_tensor_query = '/data/plugin/projector/tensor?run=%s&name=test' % run
        response = self._Get(unk_tensor_query)
        self.assertEqual(response.status_code, 400)

        expected_tensor = np.array([[6, 6]], dtype=np.float32)
        tensor_query = '/data/plugin/projector/tensor?run=%s&name=var1' % run
        tensor_bytes = self._Get(tensor_query).data
        self._AssertTensorResponse(tensor_bytes, expected_tensor)
    def testEndpointsComboTensorAssetsAndCheckpoint(self):
        self._GenerateProjectorTestData()
        g = ops.Graph()
        with g.as_default():
            manager = plugin_asset.get_plugin_asset(
                projector_plugin.ProjectorPluginAsset)

        metadata = projector_plugin.EmbeddingMetadata(3)
        metadata.add_column('labels', ['a', 'b', 'c'])
        manager.add_metadata_for_embedding_variable('var1', metadata)

        new_tensor_values = np.array([[1, 2], [3, 4], [5, 6]])
        manager.add_embedding('new_tensor', new_tensor_values)

        fw = writer.FileWriter(self.log_dir, graph=g)
        fw.close()

        self._SetupWSGIApp()
        run_json = self._GetJson('/data/plugin/projector/runs')
        self.assertTrue(run_json)

        run = run_json[0]
        var1_values = np.array([[6, 6]], dtype=np.float32)
        var1_tensor_query = '/data/plugin/projector/tensor?run=%s&name=var1' % run
        tensor_bytes = self._Get(var1_tensor_query).data
        self._AssertTensorResponse(tensor_bytes, var1_values)

        metadata_query = '/data/plugin/projector/metadata?run=%s&name=var1' % run
        metadata_tsv = self._Get(metadata_query).data
        self.assertEqual(metadata_tsv, b'a\nb\nc\n')

        tensor_query = '/data/plugin/projector/tensor?run=%s&name=new_tensor' % run
        tensor_bytes = self._Get(tensor_query).data
        self._AssertTensorResponse(tensor_bytes, new_tensor_values)
Beispiel #5
0
    def testAddEmbeddingWithMetadataOfIncorrectLength(self):
        manager = plugin_asset.get_plugin_asset(
            projector_plugin.ProjectorPluginAsset)

        metadata = projector_plugin.EmbeddingMetadata(3)
        metadata.add_column('labels', ['a', 'b', 'c'])
        # values has length 2, while metadata has length 3.
        values = np.array([[1], [2]])

        with self.assertRaises(ValueError):
            manager.add_embedding('test', values, metadata)
    def testEndpointsTensorAndMetadataAssets(self):
        g = ops.Graph()
        with g.as_default():
            manager = plugin_asset.get_plugin_asset(
                projector_plugin.ProjectorPluginAsset)

        metadata = projector_plugin.EmbeddingMetadata(3)
        metadata.add_column('labels', ['a', 'b', 'c'])
        manager.add_metadata_for_embedding_variable('test', metadata)
        expected_tensor = np.array([[1, 2], [3, 4], [5, 6]])
        image1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
        image2 = np.array([[[10, 20, 30], [40, 50, 60]],
                           [[70, 80, 90], [100, 110, 120]]])
        manager.add_embedding('emb', expected_tensor, metadata,
                              [image1, image2], [2, 2])

        fw = writer.FileWriter(self.log_dir, graph=g)
        fw.close()

        self._SetupWSGIApp()
        run_json = self._GetJson('/data/plugin/projector/runs')
        self.assertTrue(run_json)

        run = run_json[0]
        metadata_query = '/data/plugin/projector/metadata?run=%s&name=emb' % run
        metadata_tsv = self._Get(metadata_query).data
        self.assertEqual(metadata_tsv, b'a\nb\nc\n')

        unk_metadata_query = '/data/plugin/projector/metadata?run=%s&name=q' % run
        response = self._Get(unk_metadata_query)
        self.assertEqual(response.status_code, 400)

        tensor_query = '/data/plugin/projector/tensor?run=%s&name=emb' % run
        tensor_bytes = self._Get(tensor_query).data
        self._AssertTensorResponse(tensor_bytes, expected_tensor)

        unk_tensor_query = '/data/plugin/projector/tensor?run=%s&name=var1' % run
        response = self._Get(unk_tensor_query)
        self.assertEqual(response.status_code, 400)

        image_query = '/data/plugin/projector/sprite_image?run=%s&name=emb' % run
        image_bytes = self._Get(image_query).data
        with ops.Graph().as_default():
            s = session.Session()
            image_array = image_ops.decode_png(image_bytes).eval(
                session=s).tolist()
        expected_sprite_image = [[[1, 2, 3], [4, 5, 6], [10, 20, 30],
                                  [40, 50, 60]],
                                 [[7, 8, 9], [10, 11, 12], [70, 80, 90],
                                  [100, 110, 120]],
                                 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                                 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]
        self.assertEqual(image_array, expected_sprite_image)
    def testEndpointsMetadataForVariableAssetsButNoCheckpoint(self):
        g = ops.Graph()
        with g.as_default():
            manager = plugin_asset.get_plugin_asset(
                projector_plugin.ProjectorPluginAsset)

        metadata = projector_plugin.EmbeddingMetadata(3)
        metadata.add_column('labels', ['a', 'b', 'c'])
        manager.add_metadata_for_embedding_variable('test', metadata)

        fw = writer.FileWriter(self.log_dir, graph=g)
        fw.close()

        self._SetupWSGIApp()
        run_json = self._GetJson('/data/plugin/projector/runs')
        self.assertEqual(run_json, [])
  def testAddMetadataForVariable(self):
    manager = plugin_asset.get_plugin_asset(
        projector_plugin.ProjectorPluginAsset)
    metadata = projector_plugin.EmbeddingMetadata(3)
    metadata.add_column('Labels', ['a', 'b', 'c'])
    manager.add_metadata_for_embedding_variable('test', metadata)

    config = projector_config_pb2.ProjectorConfig()
    embedding = config.embeddings.add()
    embedding.tensor_name = 'test'
    embedding.metadata_path = 'test_metadata.tsv'
    expected_config_pbtxt = text_format.MessageToString(config)

    self.assertEqual(manager.assets(), {
        'projector_config.pbtxt': expected_config_pbtxt,
        'test_metadata.tsv': 'a\nb\nc\n'
    })
Beispiel #9
0
 def testDuplicateColumnName(self):
     metadata = projector_plugin.EmbeddingMetadata(2)
     metadata.add_column('Labels', np.array(['a', 'b']))
     with self.assertRaises(ValueError):
         metadata.add_column('Labels', np.array(['a', 'b']))
Beispiel #10
0
 def testStringNdArrayRetrieval(self):
     metadata = projector_plugin.EmbeddingMetadata(2)
     metadata.add_column('Labels', np.array(['a', 'b']))
     self.assertEqual(metadata.name_to_values['Labels'].tolist(),
                      ['a', 'b'])
Beispiel #11
0
 def testNumericNdArrayRetrieval(self):
     metadata = projector_plugin.EmbeddingMetadata(3)
     metadata.add_column('Labels', np.array([1, 2, 3]))
     self.assertEqual(metadata.name_to_values['Labels'].tolist(), [1, 2, 3])
Beispiel #12
0
 def testNumericListRetrieval(self):
     metadata = projector_plugin.EmbeddingMetadata(3)
     metadata.add_column('Labels', [1, 2, 3])
     self.assertEqual(metadata.name_to_values['Labels'], [1, 2, 3])
Beispiel #13
0
 def testStringListRetrieval(self):
     metadata = projector_plugin.EmbeddingMetadata(3)
     metadata.add_column('Labels', ['a', 'B', 'c'])
     self.assertEqual(metadata.name_to_values['Labels'], ['a', 'B', 'c'])
     self.assertEqual(metadata.column_names, ['Labels'])
Beispiel #14
0
    def testValuesNot1D(self):
        metadata = projector_plugin.EmbeddingMetadata(3)
        values = np.array([[1, 2, 3]])

        with self.assertRaises(ValueError):
            metadata.add_column('Labels', values)
Beispiel #15
0
    def testLengthDoesNotMatch(self):
        metadata = projector_plugin.EmbeddingMetadata(10)

        with self.assertRaises(ValueError):
            metadata.add_column('Labels', [''] * 11)
Beispiel #16
0
 def testAddMetadataForVariableButNoColumns(self):
     manager = plugin_asset.get_plugin_asset(
         projector_plugin.ProjectorPluginAsset)
     metadata = projector_plugin.EmbeddingMetadata(3)
     with self.assertRaises(ValueError):
         manager.add_metadata_for_embedding_variable('test', metadata)
Beispiel #17
0
 def testValuesAreListofLists(self):
     metadata = projector_plugin.EmbeddingMetadata(3)
     values = [[1, 2, 3], [4, 5, 6]]
     with self.assertRaises(ValueError):
         metadata.add_column('Labels', values)