Exemplo n.º 1
0
    def test_calc_avgpool(self):
        image_data = self._image_data()
        net = self._network('avgpool')
        input_bounds = naive_bounds.input_bounds(image_data.image, delta=.1)
        dual_obj, dual_var_lists = self._build_objective(
            net, input_bounds, image_data.label)

        # Explicitly build the expected TensorFlow graph for calculating objective.
        (
            conv2d_0,
            relu_1,  # pylint:disable=unused-variable
            avgpool_2,
            relu_3,  # pylint:disable=unused-variable
            linear_obj) = self._verifiable_layer_builder(net).build_layers()
        (mu_0, ), (lam_1, ), (mu_2, ), _ = dual_var_lists

        # Expected input bounds for each layer.
        conv2d_0_lb, conv2d_0_ub = self._expected_input_bounds(
            image_data.image, .1)
        relu_1_lb, relu_1_ub = ibp.IntervalBounds(
            conv2d_0_lb, conv2d_0_ub).apply_conv2d(None, conv2d_0.module.w,
                                                   conv2d_0.module.b, 'SAME',
                                                   (1, 1))
        avgpool_2_lb = tf.nn.relu(relu_1_lb)
        avgpool_2_ub = tf.nn.relu(relu_1_ub)
        relu_3_lb = tf.nn.avg_pool(avgpool_2_lb,
                                   ksize=[2, 2],
                                   padding='VALID',
                                   strides=(1, 1))
        relu_3_ub = tf.nn.avg_pool(avgpool_2_ub,
                                   ksize=[2, 2],
                                   padding='VALID',
                                   strides=(1, 1))

        # Expected objective value.
        objective = 0
        act_coeffs_0 = -common.conv_transpose(
            mu_0, conv2d_0.module.w, conv2d_0.input_shape, 'SAME', (1, 1))
        obj_0 = -tf.reduce_sum(mu_0 * conv2d_0.module.b, axis=(2, 3, 4))
        objective += standard_layer_calcs.linear_dual_objective(
            None, act_coeffs_0, obj_0, conv2d_0_lb, conv2d_0_ub)
        objective += standard_layer_calcs.activation_layer_dual_objective(
            tf.nn.relu, mu_0, lam_1, relu_1_lb, relu_1_ub)
        act_coeffs_2 = -common.avgpool_transpose(
            mu_2,
            result_shape=relu_1.output_shape,
            kernel_shape=(2, 2),
            strides=(1, 1))
        objective += standard_layer_calcs.linear_dual_objective(
            lam_1, act_coeffs_2, 0., avgpool_2_lb, avgpool_2_ub)
        objective_w, objective_b = common.targeted_objective(
            linear_obj.module.w, linear_obj.module.b, image_data.label)
        shaped_objective_w = tf.reshape(
            objective_w,
            [self._num_classes(), self._batch_size()] + avgpool_2.output_shape)
        objective += standard_layer_calcs.activation_layer_dual_objective(
            tf.nn.relu, mu_2, -shaped_objective_w, relu_3_lb, relu_3_ub)
        objective += objective_b

        self._assert_dual_objective_close(objective, dual_obj, image_data)
Exemplo n.º 2
0
 def backward_prop(self, y, w_fn=None):
     del w_fn
     return common.avgpool_transpose(y,
                                     result_shape=self.input_shape,
                                     kernel_shape=self.kernel_shape,
                                     strides=self.strides)