def test_formulate_smt_constraints_convolution_layer_text(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) # The 1st convolution layer has 12 neurons. image = np.ones(5) 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' } session = utils.restore_model(self.test_model_path) cnn_predictions = session.run( tensor_names, feed_dict={tensor_names['input']: image.reshape(1, 5)}) text_embedding = masking._remove_batch_axis( cnn_predictions['embedding']) z3_mask = [ z3.Int('mask_%d' % i) for i in range(text_embedding.shape[0]) ] masked_input = [] for mask_bit, embedding_row in zip(z3_mask, text_embedding): masked_input.append( [z3.ToReal(mask_bit) * i for i in embedding_row]) first_layer_activations = masking._reorder( masking._remove_batch_axis( cnn_predictions['first_layer'])).reshape(-1) z3_optimizer = masking._formulate_smt_constraints_convolution_layer( z3_optimizer=utils.TextOptimizer(z3_mask=z3_mask), kernels=masking._reshape_kernels( kernels=cnn_predictions['weights_layer_1'], model_type='text_cnn'), biases=cnn_predictions['biases_layer_1'], chosen_indices=first_layer_activations.argsort()[-5:], conv_activations=first_layer_activations, input_activation_maps=[masked_input], output_activation_map_shape=masking._get_activation_map_shape( activation_maps_shape=cnn_predictions['first_layer'].shape, model_type='text_cnn'), strides=1, padding=(0, 0), gamma=0.5) mask, result = z3_optimizer.generate_mask() self.assertEqual(result, 'sat') self.assertEqual(mask.shape, (5, )) session.close()
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()