def test_simple_xor_model(self):
        predictor = cntk_to_ell.predictor_from_cntk_model('xorModel1.dnn')
        result = predictor.Predict([0, 0])
        self.assertAlmostEqual(result[0],
                               0,
                               msg='incorrect prediction for [0, 0]')
        result = predictor.Predict([0, 1])
        self.assertAlmostEqual(result[0],
                               1,
                               msg='incorrect prediction for [0, 1]')
        result = predictor.Predict([1, 0])
        self.assertAlmostEqual(result[0],
                               1,
                               msg='incorrect prediction for [1, 0]')
        result = predictor.Predict([1, 1])
        self.assertAlmostEqual(result[0],
                               0,
                               msg='incorrect prediction for [1, 1]')

        # create a map and save to file
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        ell_map.Save("xor_test.map")

        # create a map and save to file
        ell_map = ell_utilities.ell_map_from_float_predictor(
            predictor,
            step_interval_msec=500,
            lag_threshold_msec=750,
            function_prefix="XorTest")
        ell_map.Save("xor_test_steppable.map")
    def verify_compiled(self,
                        predictor,
                        input,
                        expectedOutput,
                        module_name,
                        method_name,
                        precision=5):
        map = ell_utilities.ell_map_from_float_predictor(predictor)

        # Note: for testing purposes, callback functions assume the "model" namespace
        compiled = map.Compile("host",
                               "model",
                               method_name,
                               False,
                               dtype=np.float32)
        compiledResults = compiled.Compute(input, dtype=np.float32)

        # Compare compiled results
        if precision > 0:
            np.testing.assert_array_almost_equal(
                expectedOutput, compiledResults, precision,
                'results for %s layer do not match ELL compiled output !' %
                (module_name))
        else:
            np.testing.assert_array_equal(
                expectedOutput, compiledResults,
                'results for %s layer do not match ELL compiled output !' %
                (module_name))
    def test_simple_xor_model(self):
        predictor = cntk_to_ell.predictor_from_cntk_model('xorModel1.dnn')
        result = predictor.Predict([0, 0])
        self.assertAlmostEqual(result[0],
                               0,
                               msg='incorrect prediction for [0, 0]')
        result = predictor.Predict([0, 1])
        self.assertAlmostEqual(result[0],
                               1,
                               msg='incorrect prediction for [0, 1]')
        result = predictor.Predict([1, 0])
        self.assertAlmostEqual(result[0],
                               1,
                               msg='incorrect prediction for [1, 0]')
        result = predictor.Predict([1, 1])
        self.assertAlmostEqual(result[0],
                               0,
                               msg='incorrect prediction for [1, 1]')

        # create a map and save to file
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        ell_map.Save("xor_test.map")

        # create a steppable map and save to file
        ell_steppable_map = ell_utilities.\
            ell_steppable_map_from_float_predictor(
                predictor, 100, "XorInputCallback", "XorOutputCallback")
        ell_steppable_map.Save("xor_steppable_test.map")
    def test_darknet_model(self):
        # Create synthetic input data
        input1 = np.arange(28 * 28, dtype=np.float).reshape(28, 28, 1)
        # Create an ELL predictor from the darknet model files
        predictor = darknet_to_ell.predictor_from_darknet_model(
            'unittest.cfg', 'unittest.weights')

        # Feed the input through the model
        result1 = predictor.Predict(input1.ravel())
        # Verify its what we expect
        expectedResult1 = [0.09134083986282349, 0.09748589247465134, 0.09064911305904388, 0.13794259727001190, 0.16832095384597778,
                           0.08976214379072190, 0.06458559632301330, 0.07894224673509598, 0.12377665191888809, 0.05719388648867607]
        np.testing.assert_array_almost_equal(
            result1, expectedResult1, 5, 'prediction of first input does not match expected results!')

        # Feed the next input through the model
        input2 = np.flipud(input1)
        result2 = predictor.Predict(input2.ravel())
        # Verify its what we expect
        expectedResult2 = [0.08052270114421844, 0.08739096671342850, 0.08180813491344452, 0.24630726873874664, 0.12944690883159637,
                           0.08548084646463394, 0.06091265007853508, 0.07173667103052139, 0.11159289628267288, 0.04480091854929924]
        np.testing.assert_array_almost_equal(
            result2, expectedResult2, 5, 'prediction of second input does not match expected results!')

        # create a map and save to file
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        ell_map.Save("darknet_test.map")

        # create a steppable map and save to file
        ell_steppable_map = ell_utilities.ell_steppable_map_from_float_predictor(
            predictor, 100, "DarknetTestInputCallback", "DarknetTestOutputCallback")
        ell_steppable_map.Save("darknet_steppable_test.map")

        return
Beispiel #5
0
    def verify_compiled(self,
                        predictor,
                        input,
                        expectedOutput,
                        module_name,
                        precision=1e-4):
        map = ell_utilities.ell_map_from_float_predictor(predictor)

        # Note: for testing purposes, callback functions assume the "model" namespace
        compiled = map.Compile("host",
                               "model",
                               "test" + str(self.method_index),
                               False,
                               dtype=np.float32)

        self.method_index += 1
        compiledResults = compiled.Compute(input, dtype=np.float32)
        ca = np.array(compiledResults)  # convert back to numpy
        self.compiled_data = ca
        expectedFloats = expectedOutput.astype(dtype=np.float32)
        # Compare compiled results
        self.compare_arrays(
            expectedFloats, ca,
            'results for %s layer do not match ELL compiled output !' %
            (module_name), precision)
    def test_simple_xor_model(self):
        predictor = cntk_to_ell.predictor_from_cntk_model('xorModel1.dnn')
        result = predictor.Predict([0, 0])
        self.assertAlmostEqual(
            result[0], 0, msg='incorrect prediction for [0, 0]')
        result = predictor.Predict([0, 1])
        self.assertAlmostEqual(
            result[0], 1, msg='incorrect prediction for [0, 1]')
        result = predictor.Predict([1, 0])
        self.assertAlmostEqual(
            result[0], 1, msg='incorrect prediction for [1, 0]')
        result = predictor.Predict([1, 1])
        self.assertAlmostEqual(
            result[0], 0, msg='incorrect prediction for [1, 1]')

        # create a map and save to file
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        ell_map.Save("xor_test.map")

        # create a steppable map and save to file
        ell_steppable_map = ell_utilities.ell_steppable_map_from_float_predictor(
            predictor, 100, "XorInputCallback", "XorOutputCallback")
        ell_steppable_map.Save("xor_steppable_test.map")

        return
Beispiel #7
0
def main(argv):
    arg_parser = argparse.ArgumentParser(
        "Converts CNTK model to ELL model\n"
        "Example:\n"
        "    cntk_import.py VGG16_ImageNet_Caffe.model\n"
        "This outputs 'VGG16_ImageNet_Caffe.ell' and 'VGG16_ImageNet_Caffe_config.json'\n"
    )

    arg_parser.add_argument(
        "cntk_model_file",
        help="path to a CNTK model file, or a zip archive of a CNTK model file"
    )
    arg_parser.add_argument("--zip_ell_model",
                            help="zips the output ELL model if set",
                            action="store_true")

    model_options = arg_parser.add_argument_group('model_options')
    model_options.add_argument(
        "--step_interval",
        help="produce a steppable ELL model for a millisecond interval",
        default=0)
    model_options.add_argument(
        "--lag_threshold",
        help=
        "number of step intervals to fall behind before notifying the caller.\n"
        "used when step_interval is set\n",
        default=5)

    args = vars(arg_parser.parse_args(argv))

    model_options = args.get('model_options', {})
    step_interval = model_options.get('step_interval')
    lag_threshold = model_options.get('lag_threshold')

    # extract the model if it's in an archive
    unzip = ziptools.Extractor(args['cntk_model_file'])
    success, filename = unzip.extract_file(".cntk")
    if success:
        print("extracted: " + filename)
    else:
        # not a zip archive
        filename = args['cntk_model_file']

    predictor = cntk_to_ell.predictor_from_cntk_model(filename)

    model_file_name = os.path.splitext(filename)[0] + '.ell'

    ell_map = ell_utilities.ell_map_from_float_predictor(
        predictor, step_interval, lag_threshold)

    print("Saving model file: '" + model_file_name + "'")
    ell_map.Save(model_file_name)

    if args['zip_ell_model']:
        print("Zipping model file: '" + model_file_name + ".zip'")
        zipper = ziptools.Zipper()
        zipper.zip_file(model_file_name, model_file_name + ".zip")
        os.remove(model_file_name)
Beispiel #8
0
 def save_ell_predictor_to_file(self, predictor, filePath, intervalMs = 0):
     """Saves an ELL predictor to file so that it can be compiled to run on a device, with an optional stepInterval in milliseconds"""
     name = self.model_name
     if (intervalMs > 0):
         ell_map = ell_utilities.ell_steppable_map_from_float_predictor(
             predictor, intervalMs, name + "InputCallback", name + "OutputCallback")
         ell_map.Save(filePath)
     else:
         ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
         ell_map.Save(filePath)
    def get_predictor_map(self, predictor, intervalMs):
        """Creates an ELL map from an ELL predictor"""
        import ell_utilities

        name = self.model_name
        if (intervalMs > 0):
            ell_map = ell_utilities.ell_steppable_map_from_float_predictor(
                predictor, intervalMs, name + "InputCallback", name + "OutputCallback")
        else:
            ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        return ell_map
Beispiel #10
0
 def save_ell_predictor_to_file(self, predictor, filePath, intervalMs=0):
     """Saves an ELL predictor to file so that it can be compiled to run on a device, with an optional stepInterval in milliseconds"""
     name = self.model_name
     if (intervalMs > 0):
         ell_map = ell_utilities.ell_steppable_map_from_float_predictor(
             predictor, intervalMs, name + "InputCallback",
             name + "OutputCallback")
         ell_map.Save(filePath)
     else:
         ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
         ell_map.Save(filePath)
Beispiel #11
0
    def run(self):
        predictor = darknet_to_ell.predictor_from_darknet_model(
            self.config_file, self.weights_file)

        weights_directory, weights_filename = os.path.split(self.weights_file)
        if self.output_directory:
            output_directory = self.output_directory
        else:
            output_directory = weights_directory

        filename_base = os.path.splitext(weights_filename)[0]
        model_file_name = filename_base + '.ell'
        model_file_path = os.path.join(output_directory, model_file_name)
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        print("Saving model file: '" + model_file_name + "'")
        ell_map.Save(model_file_path)
Beispiel #12
0
 def verify_compiled(self, predictor, input, expectedOutput, module_name,
                     method_name, precision=5):
     # now run same over ELL compiled model
     map = ell_utilities.ell_map_from_float_predictor(predictor)
     compiled = map.Compile("host", module_name, method_name)
     compiledResults = compiled.ComputeFloat(input)
     # Compare compiled results
     if precision > 0:
         np.testing.assert_array_almost_equal(
             expectedOutput, compiledResults, precision,
             'results for %s layer do not match ELL compiled output !' %
             (module_name))
     else:
         np.testing.assert_array_equal(
             expectedOutput, compiledResults,
             'results for %s layer do not match ELL compiled output !' %
             (module_name))
Beispiel #13
0
    def verify_compiled(
            self, predictor, inputData, expectedOutput, module_name):
        """Takes the input data and passes it through a compiled ELL model
        and compare it against the CNTK output, `expectedOutput`.
        """
        # now run same over ELL compiled model
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        compiled = ell_map.Compile("host", module_name, "test{}".format(
            self.method_index))
        self.method_index += 1

        compiledResults = np.array(compiled.ComputeFloat(inputData))

        # Compare compiled results
        np.testing.assert_array_almost_equal(
                expectedOutput, compiledResults, decimal=4, err_msg=(
                    'results for {} layer do not match ELL compiled output!'
                ).format(module_name))
Beispiel #14
0
def main(argv):
    arg_parser = argparse.ArgumentParser(
        "Converts CNTK model to ELL model\n"
        "Example:\n"
        "    cntk_import.py VGG16_ImageNet_Caffe.model\n"
        "This outputs 'VGG16_ImageNet_Caffe.ell' and 'VGG16_ImageNet_Caffe_config.json'\n"
    )

    arg_parser.add_argument(
        "cntk_model_file",
        help="path to a CNTK model file, or a zip archive of a CNTK model file"
    )
    arg_parser.add_argument("--zip_ell_model",
                            help="zips the output ELL model if set",
                            action="store_true")

    args = arg_parser.parse_args(argv)

    # extract the model if it's in an archive
    unzip = ziptools.Extractor(args.cntk_model_file)
    success, filename = unzip.extract_file(".cntk")
    if (success):
        print("extracted: " + filename)
    else:
        # not a zip archive
        filename = args.cntk_model_file

    predictor = cntk_to_ell.predictor_from_cntk_model(filename)

    input_shape = predictor.GetInputShape()
    output_shape = predictor.GetOutputShape()

    model_file_name = os.path.splitext(filename)[0] + '.ell'
    head, tail = os.path.split(model_file_name)

    ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
    print("Saving model file: '" + model_file_name + "'")
    ell_map.Save(model_file_name)

    if (args.zip_ell_model):
        print("Zipping model file: '" + model_file_name + ".zip'")
        zipper = ziptools.Zipper()
        zipper.zip_file(model_file_name, model_file_name + ".zip")
        os.remove(model_file_name)
Beispiel #15
0
 def verify_compiled(self,
                     predictor,
                     input,
                     expectedOutput,
                     module_name,
                     precision=1e-4):
     # now run same over ELL compiled model
     map = ell_utilities.ell_map_from_float_predictor(predictor)
     compiled = map.Compile("host", module_name,
                            "test" + str(self.method_index))
     self.method_index += 1
     compiledResults = compiled.ComputeFloat(input)
     ca = np.array(compiledResults)  # convert back to numpy
     self.compiled_data = ca
     expectedFloats = expectedOutput.astype(dtype=np.float32)
     # Compare compiled results
     self.compare_arrays(
         expectedFloats, ca,
         'results for %s layer do not match ELL compiled output !' %
         (module_name), precision)
Beispiel #16
0
    def verify_compiled(self, predictor, inputData, expectedOutput,
                        module_name):
        """Takes the input data and passes it through a compiled ELL model
        and compare it against the CNTK output, `expectedOutput`.
        """
        # Note: for testing purposes, callback functions assume the "model" namespace
        ell_map = ell_utilities.ell_map_from_float_predictor(predictor)
        compiled = ell_map.Compile("host",
                                   "model",
                                   "test{}".format(self.method_index),
                                   False,
                                   dtype=np.float32)
        self.method_index += 1

        compiledResults = np.array(
            compiled.Compute(inputData, dtype=np.float32))

        # Compare compiled results
        np.testing.assert_array_almost_equal(
            expectedOutput,
            compiledResults,
            decimal=4,
            err_msg=('results for {} layer do not match ELL compiled output!'
                     ).format(module_name))
Beispiel #17
0
    def model_test_impl(self, modelName):
        with self.subTest(modelName=modelName):
            _logger.info('Testing {0}.cntk vs ELL ({0})'.format(modelName))
            # Load the cntk model
            cntkModel = load_model(modelName + '.cntk')

            # Import the model into an ELL map live, without unarchiving
            predictor = cntk_to_ell.predictor_from_cntk_model(modelName +
                                                              '.cntk')
            ellMap = ell_utilities.ell_map_from_float_predictor(predictor)

            # Load the map from archive
            ellMapFromArchive = ell.model.Map(modelName + '.ell')

            inputShape = ellMap.GetInputShape()
            outputShape = ellMap.GetOutputShape()

            # Compile the live map
            # Note: for testing purposes, callback functions assume the "model" namespace
            ellCompiledMap = ellMap.Compile('host',
                                            'model',
                                            'predict',
                                            False,
                                            dtype=np.float32)

            # Compile the unarchived map
            # Note: for testing purposes, callback functions assume the "model" namespace
            ellCompiledMapFromArchive = ellMapFromArchive.Compile(
                'host', 'model', 'predict', False, dtype=np.float32)

            cntkInput = np.random.uniform(
                high=255,
                size=(inputShape.channels, inputShape.rows,
                      inputShape.columns)).astype(np.float)
            ellOrderedInput = cntk_converters.\
                get_float_vector_from_cntk_array(cntkInput)
            cntkInput = np.ascontiguousarray(cntkInput)

            # Get the CNTK results
            _, out = cntkModel.forward({
                cntkModel.arguments[0]: [cntkInput],
                cntkModel.arguments[1]: [list(range(len(self.categories)))]
            })
            for output in cntkModel.outputs:
                if (output.shape == (len(self.categories), )):
                    out = out[output]
            cntkResults = softmax(out[0]).eval()

            _logger.info('Comparing predictor output (reference)')
            sys.stdout.flush()

            ellPredictorResults = predictor.Predict(ellOrderedInput)

            # Verify CNTK results and predictor results match
            np.testing.assert_array_almost_equal(
                cntkResults,
                ellPredictorResults,
                decimal=5,
                err_msg=('results for CNTK and ELL predictor (' + modelName +
                         ') do not match!'))

            _logger.info('Comparing map output (reference)')
            sys.stdout.flush()

            ellMapResults = ellMap.Compute(ellOrderedInput, dtype=np.float32)

            # Verify CNTK results and ELL map results match
            np.testing.assert_array_almost_equal(
                cntkResults,
                ellMapResults,
                decimal=5,
                err_msg=('results for CNTK and ELL map reference (' +
                         modelName + ') do not match!'))

            _logger.info('Comparing unarchived map output (reference)')
            sys.stdout.flush()

            ellMapFromArchiveResults = self.compute_ell_map(
                ellMapFromArchive, ellOrderedInput, cntkResults, modelName)

            _logger.info('Comparing map output (compiled)')
            sys.stdout.flush()

            ellCompiledMapResults = ellCompiledMap.Compute(ellOrderedInput,
                                                           dtype=np.float32)

            # Verify CNTK results and unarchived ELL model results match
            np.testing.assert_array_almost_equal(
                cntkResults,
                ellCompiledMapResults,
                decimal=5,
                err_msg=('results for CNTK and ELL map compiled (' +
                         modelName + ') do not match!'))

            _logger.info('Comparing unarchived map output (compiled)')
            sys.stdout.flush()

            ellCompiledMapFromArchiveResults = ellCompiledMapFromArchive.\
                Compute(ellOrderedInput, dtype=np.float32)

            # Verify CNTK results and unarchived ELL model results match
            np.testing.assert_array_almost_equal(
                cntkResults,
                ellCompiledMapFromArchiveResults,
                decimal=5,
                err_msg=('results for CNTK and ELL unarchived map compiled (' +
                         modelName + ') do not match!'))

            _logger.info(('Testing output of {0}.cntk vs ELL for model {0}'
                          ' passed!').format(modelName))
Beispiel #18
0
    def get_predictor_map(self, predictor, intervalMs):
        """Creates an ELL map from an ELL predictor"""
        import ell_utilities

        name = self.model_name
        return ell_utilities.ell_map_from_float_predictor(predictor)