def predictor_from_cntk_model(modelFile, plotModel=False): """Loads a CNTK model and returns an ell.neural.NeuralNetworkPredictor""" _logger = logger.get() _logger.info("Loading...") z = load_model(modelFile) _logger.info("\nFinished loading.") if plotModel: filename = os.path.join(os.path.dirname(modelFile), os.path.basename(modelFile) + ".svg") cntk_utilities.plot_model(z, filename) _logger.info("Pre-processing...") modelLayers = cntk_utilities.get_model_layers(z) # Get the relevant CNTK layers that we will convert to ELL layersToConvert = cntk_layers.get_filtered_layers_list(modelLayers) _logger.info("\nFinished pre-processing.") predictor = None try: # Create a list of ELL layers from the CNTK layers ellLayers = cntk_layers.convert_cntk_layers_to_ell_layers( layersToConvert) # Create an ELL neural network predictor from the layers predictor = ell.neural.NeuralNetworkPredictor(ellLayers) except BaseException as exception: _logger.error("Error occurred attempting to convert cntk layers to ELL layers: " + str(exception)) raise exception return predictor
def compare_model(self, layers): ellLayers = cntk_layers.convert_cntk_layers_to_ell_layers(layers) # Create an ELL neural network predictor from the layers predictor = ell.neural.NeuralNetworkPredictor(ellLayers) shape = predictor.GetInputShape() self.input_shape = (shape.channels, shape.rows, shape.columns ) # to CNTK (channel, rows, columns) order self.data = self.get_input_data() if (len(self.cntk_model.arguments) > 1): output = np.zeros(self.cntk_model.arguments[1].shape).astype( np.float32) predictions = self.cntk_model.eval({ self.cntk_model.arguments[0]: [self.data], self.cntk_model.arguments[1]: output }) else: predictions = self.cntk_model.eval( {self.cntk_model.arguments[0]: [self.data.ravel()]}) size = 0 output = None if isinstance(predictions, dict): for key in self.cntk_model.outputs: shape = key.shape if len(shape) > 0: s = np.max(shape) if (s > size): size = s output = predictions[key][0] / 100 else: output = predictions[0] self.verify_ell("Softmax", predictor, self.data, output) self.data = output # make this the input to the next layer. self.save_layer_outputs("Softmax")