Esempio n. 1
0
  def test_formulate_smt_constraints_convolution_layer(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_cnn(self.test_model_path)
    image_edge_length = 4
    image_channels = 3
    # The 1st convolution layer has 48 neurons.
    top_k = np.random.randint(low=1, high=48)
    image = np.ones((image_edge_length, image_edge_length, image_channels))
    tensor_names = {
        'input': 'conv2d_input:0',
        'first_layer': 'conv2d/BiasAdd:0',
        'first_layer_relu': 'conv2d/Relu:0',
        'logits': 'dense/BiasAdd:0',
        'softmax': 'dense/Softmax:0',
        'weights_layer_1': 'conv2d/Conv2D/ReadVariableOp:0',
        'biases_layer_1': 'conv2d/bias/Read/ReadVariableOp:0'}
    session = utils.restore_model(self.test_model_path)
    cnn_predictions = session.run(
        tensor_names,
        feed_dict={
            tensor_names['input']: image.reshape(
                (1, image_edge_length, image_edge_length, image_channels))})
    z3_mask = [_get_z3_var(index=i) for i in range(image_edge_length ** 2)]
    first_layer_activations = masking._reorder(masking._remove_batch_axis(
        cnn_predictions['first_layer'])).reshape(-1)
    masked_input = masking._encode_input(
        image=image, z3_mask=z3_mask, window_size=1)

    z3_optimizer = masking._formulate_smt_constraints_convolution_layer(
        z3_optimizer=utils.ImageOptimizer(
            z3_mask=z3_mask,
            window_size=1,
            edge_length=image_edge_length),
        kernels=masking._reorder(cnn_predictions['weights_layer_1']),
        biases=cnn_predictions['biases_layer_1'],
        chosen_indices=first_layer_activations.argsort()[-top_k:],
        conv_activations=first_layer_activations,
        input_activation_maps=masked_input,
        output_activation_map_shape=(image_edge_length, image_edge_length),
        strides=1,
        padding=(0, 1),
        gamma=0.5)
    mask, result = z3_optimizer.generate_mask()

    self.assertEqual(result, 'sat')
    self.assertEqual(mask.shape, (image_edge_length, image_edge_length))
    session.close()
Esempio n. 2
0
    def test_encode_input(self, image_channels):
        # Creates a random image and checks if the encoded image, after multiplying
        # the mask bits (set to 1) is the same as the original image.
        image_edge_length = 2
        image = np.random.rand(image_edge_length, image_edge_length,
                               image_channels)
        z3_var = _get_z3_var(index=0)

        # encoded_image has dimensions
        # (image_channels, image_edge_length, image_edge_length)
        encoded_image = masking._encode_input(
            image=image, z3_mask=[z3_var for _ in range(image_edge_length**2)])
        solver = z3.Solver()
        solver.add(z3_var == 1)
        # Swap the axes of the image so that it has the same dimensions as the
        # encoded image.
        image = masking._reorder(image).reshape(-1)
        encoded_image = utils.flatten_nested_lists(encoded_image)
        for i in range(image_channels * image_edge_length**2):
            solver.add(encoded_image[i] == image[i])

        self.assertEqual(str(solver.check()), 'sat')