Ejemplo n.º 1
0
 def test_find_mask_first_layer_text_cnn(self):
     with self.test_session():
         # Temporary graphs should be created inside a session. Notice multiple
         # graphs are being created in this particular code. So, if each graph
         # isn't created inside a separate session, the tensor names will have
         # unwanted integer suffices, which then would cause problems while
         # accessing tensors by name.
         _create_temporary_tf_graph_text_cnn(self.test_model_path)
     result = masking.find_mask_first_layer(
         image=np.zeros(5),
         run_params=masking.RunParams(
             **{
                 'model_path': self.test_model_path,
                 'tensor_names': {
                     'input': 'input_1:0',
                     'embedding': 'embedding/embedding_lookup/Identity_1:0',
                     'first_layer': 'conv1d/BiasAdd:0',
                     'first_layer_relu': 'conv1d/Relu:0',
                     'logits': 'dense/BiasAdd:0',
                     'softmax': 'dense/Sigmoid:0',
                     'weights_layer_1': 'conv1d/conv1d/ExpandDims_1:0',
                     'biases_layer_1': 'conv1d/BiasAdd/ReadVariableOp:0',
                 },
                 'image_placeholder_shape': (1, 5),
                 'model_type': 'text_cnn',
                 'padding': (0, 0),
                 'strides': 1,
                 'activations': None,
                 'pixel_range': (0, 1),
             }),
         window_size=1,
         label_index=0,
         score_method='activations',
         top_k=4,
         gamma=0.5,
         timeout=600,
         num_unique_solutions=5)
     self.assertEqual(result['image'].shape, (5, ))
     self.assertEqual(result['unmasked_logits'].shape, (1, ))
     self.assertEqual(result['unmasked_first_layer'].shape, (12, ))
     self.assertEqual(result['masks'][0].shape, (5, ))
     self.assertLen(result['masks'], 5)
     self.assertLen(result['masked_first_layer'], 5)
     self.assertLen(result['inv_masked_first_layer'], 5)
     self.assertLen(result['masked_images'], 5)
     self.assertLen(result['inv_masked_images'], 5)
     self.assertLen(result['masked_logits'], 5)
     self.assertLen(result['inv_masked_logits'], 5)
     self.assertLen(result['solver_outputs'], 5)
Ejemplo n.º 2
0
 def test_find_mask_full_encoding(self):
     with self.test_session():
         # Temporary graphs should be created inside a session. Notice multiple
         # graphs are being created in this particular code. So, if each graph
         # isn't created inside a separate session, the tensor names will have
         # unwanted integer suffices, which then would cause problems while
         # accessing tensors by name.
         weights, biases = _create_temporary_tf_graph_fully_connected(
             self.test_model_path)
     result = masking.find_mask_full_encoding(
         image=np.zeros((2, 2, 1)),
         weights=weights,
         biases=biases,
         run_params=masking.RunParams(
             **{
                 'model_path': self.test_model_path,
                 'tensor_names': {
                     'input': 'dense_input:0',
                     'first_layer': 'dense/BiasAdd:0',
                     'first_layer_relu': 'dense/Relu:0',
                     'softmax': 'dense_1/Softmax:0',
                     'logits': 'dense_1/BiasAdd:0',
                     'weights_layer_1': 'dense/MatMul/ReadVariableOp:0',
                     'biases_layer_1': 'dense/BiasAdd/ReadVariableOp:0'
                 },
                 'image_placeholder_shape': (1, 4),
                 'model_type': 'fully_connected',
                 'padding': (0, 0),
                 'strides': 0,
                 'activations': ['relu', 'linear'],
                 'pixel_range': (0, 1),
             }),
         window_size=1,
         label_index=0,
         delta=0,
         timeout=600,
         num_unique_solutions=5)
     self.assertEqual(result['image'].shape, (4, ))
     self.assertEqual(result['unmasked_logits'].shape, (2, ))
     self.assertEqual(result['unmasked_first_layer'].shape, (4, ))
     self.assertEqual(result['masks'][0].shape, (4, ))
     self.assertLen(result['masks'], 5)
     self.assertLen(result['masked_first_layer'], 5)
     self.assertLen(result['inv_masked_first_layer'], 5)
     self.assertLen(result['masked_images'], 5)
     self.assertLen(result['inv_masked_images'], 5)
     self.assertLen(result['masked_logits'], 5)
     self.assertLen(result['inv_masked_logits'], 5)
     self.assertLen(result['solver_outputs'], 5)