Ejemplo n.º 1
0
    def clone_cntk_layer(self, feature):
        """Returns a clone of the CNTK layer for per-layer forward prop validation"""

        weightsShape = self.weights_parameter.shape  # filters, channels, rows, columns
        pad = self.attributes['autoPadding'][0] or (
            self.attributes['autoPadding'][1] and self.attributes['autoPadding'][2])

        # Bias is a separate layer and not processed by this class
        # Activation is a separate layer and not processed by this class

        x = CustomSign(feature)
        return BinaryConvolution((weightsShape[2], weightsShape[3]), num_filters=weightsShape[0],
                                 channels=weightsShape[1], init=self.weights_parameter.value,
                                 pad=pad, activation=False, bias=False, init_bias=0)(x)
Ejemplo n.º 2
0
    def test_binary_convolution_layer(self):
        """Test a model with a single CNTK Binary Convolution layer against the
        equivalent ELL predictor. This verifies that the import functions
        reshape and reorder values appropriately and that the equivalent ELL
        layer produces comparable output
        """

        # Create a test set of weights to use for both CNTK and ELL layers
        # CNTK has these in filters, channels, rows, columns order
        weightValues = np.random.uniform(low=-5, high=5,
                                         size=(5, 2, 3, 3)).astype(np.float32)

        # create an ELL Tensor from the cntk weights, which re-orders the
        # weights and produces an appropriately dimensioned tensor
        weightTensor = cntk_converters.\
            get_float_tensor_from_cntk_convolutional_weight_value_shape(
                weightValues, weightValues.shape)

        # Create a Binary Convolution CNTK layer with no bias, no activation,
        # stride 1
        # Input order for CNTK is channels, rows, columns
        x = input((2, 10, 10))
        cntkModel = CustomSign(x)

        cntkModel = BinaryConvolution((10, 10),
                                      num_filters=5,
                                      channels=2,
                                      init=weightValues,
                                      pad=True,
                                      bias=False,
                                      init_bias=0,
                                      activation=False)(cntkModel)

        # Create the equivalent ELL predictor
        layerParameters = ell.neural.LayerParameters(
            # Input order for ELL is rows, columns, channels. Account for
            # padding.
            ell.math.TensorShape(10 + 2, 10 + 2, 2),
            ell.neural.ZeroPadding(1),
            ell.math.TensorShape(10, 10, 5),
            ell.neural.NoPadding())

        convolutionalParameters = ell.neural.BinaryConvolutionalParameters(
            3, 1, ell.neural.BinaryConvolutionMethod.bitwise,
            ell.neural.BinaryWeightsScale.none)

        layer = ell.neural.FloatBinaryConvolutionalLayer(
            layerParameters, convolutionalParameters, weightTensor)

        predictor = ell.neural.FloatNeuralNetworkPredictor([layer])

        # Get the results for both
        inputValues = np.random.uniform(low=-50, high=50,
                                        size=(2, 10, 10)).astype(np.float32)

        cntkResults = cntkModel(inputValues)
        orderedCntkResults = cntk_converters.get_float_vector_from_cntk_array(
            cntkResults)

        orderedInputValues = cntk_converters.get_float_vector_from_cntk_array(
            inputValues)
        ellResults = predictor.Predict(orderedInputValues)

        # Compare the results
        np.testing.assert_array_equal(
            orderedCntkResults, ellResults,
            'results for Binary Convolution layer do not match!')

        # now run same over ELL compiled model
        self.verify_compiled(predictor, orderedInputValues, orderedCntkResults,
                             "binary_convolution", "test")