def get_output_from_cntk_model(cntk_model, cntk_input_tensor, testing_info): # Get output from CNTK model _logger = logger.get() _logger.info("Getting CNTK results") if (len(cntk_model.arguments) > 1): arg1_output = np.zeros(cntk_model.arguments[1].shape).astype(np.float32) cntk_output = cntk_model.eval({cntk_model.arguments[0]: [cntk_input_tensor], cntk_model.arguments[1]: arg1_output}) else: cntk_output = cntk_model.eval({cntk_model.arguments[0]: [cntk_input_tensor]}) size = 0 if isinstance(cntk_output, dict): for key in cntk_model.outputs: shape = key.shape if len(shape) > 0: s = np.max(shape) if (s > size): size = s cntk_output = cntk_output[key][0] else: cntk_output = cntk_output[0] # Check whether softmax needs to be applied or not. if testing_info["apply_softmax"]: cntk_output = softmax(cntk_output).eval() # Reorder cntk node output cntk_output = get_node_output_in_ell_order(cntk_output) return cntk_output
def _eval_single_image(self, image_path, image_width=_image_width, image_height=_image_height): # load and format image (resize, RGB -> BGR, CHW -> HWC) img = Image.open(image_path) if image_path.endswith("png"): temp = Image.new("RGB", img.size, (255, 255, 255)) temp.paste(img, img) img = temp re_sized = img.resize((image_width, image_height), Image.ANTIALIAS) bgr_image = np.asarray(re_sized, dtype=np.float32)[..., [2, 1, 0]] hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) # Alternatively: if you want to use opencv-python # cv_img = cv2.imread(image_path) # resized = cv2.resize(cv_img, (image_width, image_height), interpolation=cv2.INTER_NEAREST) # bgr_image = np.asarray(resized, dtype=np.float32) # hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) # compute model output arguments = {self.trained_model.arguments[0]: [hwc_format]} output = self.trained_model.eval(arguments) # return softmax probabilities sm = softmax(output[0]) return sm.eval()
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.FloatNeuralNetworkPredictor(ellLayers) shape = predictor.GetInputShape() # to CNTK (channel, rows, columns) order self.input_shape = (shape.channels, shape.rows, shape.columns) self.data = self.get_input_data() if len(self.cntk_model.arguments) > 1: output = np.zeros(self.cntk_model.arguments[1].shape, dtype=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]}) size = 0 cntk_output = None if isinstance(predictions, dict): for key in self.cntk_model.outputs: shape = key.shape if shape: s = np.max(shape) if s > size: size = s # CNTK models currently don't have softmax operations # right now, so we work around it by including it # explicitly cntk_output = softmax(predictions[key][0]).eval() else: cntk_output = softmax(predictions).eval() self.verify_ell("Softmax", predictor, self.data, cntk_output)
def eval_and_write(network, test_mapFiles, output_file): # Model dimensions image_height = 224 image_width = 224 num_classes = 101 num_channels = 3 num_inputs = 5 # 20 # Create test reader test_reader = create_video_mb_source(test_mapFiles, num_channels, image_height, image_width, num_classes) OF_input_map = {} for i in range(num_inputs): OF_input_map[network.find_by_name( "input_" + str(i))] = test_reader.streams["feature" + str(i)] with open(test_mapFiles[0], 'r') as file: lines = file.readlines() max_samples = len(lines) correctLabels = [0] * int(max_samples / 25) for i in range(int(max_samples / 25)): label = lines[i * 25].replace('\n', '').split('\t')[-1] correctLabels[i] = int(label) sample_count = 0.0 results = '{:^15} | {:^15} | {:^15}\n'.format('Correct label', 'Predicted label', 'Confidence') while sample_count < max_samples: mb = test_reader.next_minibatch(25, input_map=OF_input_map) predictedLabels = dict((key, 0) for key in range(num_classes)) labelsConfidence = dict((key, 0) for key in range(num_classes)) id_correctLabel = int(sample_count / 25) sample_count += 25 output = network.eval(mb) predictions = softmax(np.squeeze(output)).eval() top_classes = [np.argmax(p) for p in predictions] for i, c in enumerate(top_classes): predictedLabels[c] += 1 # Melhorar labelsConfidence[c] += predictions[i][c] label, confidence = getFinalLabel(predictedLabels, labelsConfidence) results += '{:^15} | {:^15} | {:^15.2f}%\n'.format( correctLabels[id_correctLabel], label, confidence) if sample_count % 100 == 0: print("{:.2f}".format(float(sample_count) / max_samples)) with open(output_file, 'w') as file: file.write(results)
def softmax(cntk_layer, inputs): ''' Setup softmax op with given parameters Args: cntk_layer (:class:`~cntk.contrib.crosstalkcaffe.unimodel.cntkmodel.CntkLayersDefinition`): the layer definition of softmax op inputs (list): a list contains all :class:`~cntk.ops.functions.Function` or :class:`~cntk.input` Return: :func:`~cntk.ops.functions.Function`: instaced cntk softmax op ''' sanitize_output = ops.sanitize_input(inputs[0]) return ops.softmax(sanitize_output, name=cntk_layer.op_name)
def eval_single_image(loaded_model, image_path, image_width, image_height): # load and format image img = Image.open(image_path) if image_path.endswith("png"): temp = Image.new("RGB", img.size, (255, 255, 255)) temp.paste(img, img) img = temp resized = img.resize((image_width, image_height), Image.ANTIALIAS) hwc_format = np.ascontiguousarray(np.array(resized, dtype=np.float32).transpose(2, 0, 1)) # compute model output arguments = {loaded_model.arguments[0]: [hwc_format]} output = loaded_model.eval(arguments) # return softmax probabilities sm = softmax(output[0, 0]) return sm.eval()
def eval_single_image(loaded_model, image_path, image_width, image_height): # load and format image img = Image.open(image_path) if image_path.endswith("png"): temp = Image.new("RGB", img.size, (255, 255, 255)) temp.paste(img, img) img = temp resized = img.resize((image_width, image_height), Image.ANTIALIAS) hwc_format = np.ascontiguousarray( np.array(resized, dtype=np.float32).transpose(2, 0, 1)) # compute model output arguments = {loaded_model.arguments[0]: [hwc_format]} output = loaded_model.eval(arguments) # return softmax probabilities sm = softmax(output[0, 0]) return sm.eval()
def eval_single_image(loaded_model, image_path, image_width, image_height): # load and format image (resize, RGB -> BGR, CHW -> HWC) img = Image.open(image_path) if image_path.endswith("png"): temp = Image.new("RGB", img.size, (255, 255, 255)) temp.paste(img, img) img = temp resized = img.resize((image_width, image_height), Image.ANTIALIAS) bgr_image = np.asarray(resized, dtype=np.float32)[..., [2, 1, 0]] hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) ## Alternatively: if you want to use opencv-python # cv_img = cv2.imread(image_path) # resized = cv2.resize(cv_img, (image_width, image_height), interpolation=cv2.INTER_NEAREST) # bgr_image = np.asarray(resized, dtype=np.float32) # hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) # compute model output arguments = {loaded_model.arguments[0]: [hwc_format]} output = loaded_model.eval(arguments) # return softmax probabilities sm = softmax(output[0]) return sm.eval()
def negative_of_entropy_with_softmax(p): """See https://en.wikipedia.org/wiki/Entropy_(information_theory).""" return C.reduce_sum(C.softmax(p) * p) - C.reduce_log_sum_exp(p)
def compare_predictor_output(modelFile, labels, modelTestInput=None, maxLayers=None): """Compares an ELL.NeuralNetworkPredictor against its equivalent CNTK model. Parameters: modelFile -- path to the CNTK model file labels -- array of labels modelTestInput -- input data in row, column, channel ordering maxLayers -- integer to indicate how many layers to run before stopping. Setting to None will run all layers and compare against the original model. """ z = load_model(modelFile) 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, maxLayers) if not layersToConvert: raise RuntimeError("No layers are converted, nothing to test") # Create a list of ELL layers from the relevant CNTK layers print("\nCreating ELL predictor...") ellLayers = cntk_layers.convert_cntk_layers_to_ell_layers(layersToConvert) # Create an ELL neural network predictor from the relevant CNTK layers predictor = ELL.FloatNeuralNetworkPredictor(ellLayers) if not modelTestInput: inputShape = predictor.GetInputShape() modelTestInput = np.random.uniform( low=0, high=255, size=(inputShape.rows, inputShape.columns, inputShape.channels)).astype(np.float_) ellTestInput = modelTestInput.ravel() # rows, columns, channels ellResults = predictor.Predict(ellTestInput) # rows, columns, channels => channels, rows, columns cntkTestInput = np.moveaxis(modelTestInput, -1, 0).astype(np.float32) cntkTestInput = np.ascontiguousarray(cntkTestInput) # Get the equivalent CNTK model if not maxLayers: print("\nRunning original CNTK model...") _, out = z.forward({ z.arguments[0]: [cntkTestInput], z.arguments[1]: [list(range(len(labels)))] }) for output in z.outputs: if (output.shape == (len(labels), )): out = out[output] cntkResults = cntk.ops.softmax(out[0]).eval() # For the full model, we compare prediction output instead of layers np.testing.assert_array_almost_equal( cntkResults, ellResults, 5, 'prediction outputs do not match! (for model ' + modelFile + ')') else: print("\nRunning partial CNTK model...") if (layersToConvert[-1].layer.op_name == 'CrossEntropyWithSoftmax' and len(layersToConvert) > 2): # ugly hack for CrossEntropyWithSoftmax from cntk.ops import softmax zz = as_composite(layersToConvert[-2].layer) zz = softmax(zz) else: zz = as_composite(layersToConvert[-1].layer) out = zz(cntkTestInput) orderedCntkModelResults = cntk_converters.\ get_float_vector_from_cntk_array(out) np.testing.assert_array_almost_equal( orderedCntkModelResults, ellResults, 5, ('prediction outputs do not match! (for partial model ' + modelFile + ')'))
def verify_ell_output_in_vision_model(ell_map, cntk_model, testing_info): _logger.info("Verification of model output starting") try: cntk_input_tensor = np.random.random((cntk_model.arguments[0].shape)).astype(np.float32) * 255 ell_input_tensor = memory_shapes.get_tensor_in_ell_order(cntk_input_tensor, "channel_row_column").ravel().astype(np.float32) # Get output from CNTK model _logger.info("Getting CNTK results") if (len(cntk_model.arguments) > 1): arg1_output = np.zeros(cntk_model.arguments[1].shape).astype(np.float32) cntk_output = cntk_model.eval({cntk_model.arguments[0]:[cntk_input_tensor], cntk_model.arguments[1]:arg1_output}) else: cntk_output = cntk_model.eval({cntk_model.arguments[0]:[cntk_input_tensor]}) size = 0 if isinstance(cntk_output,dict): for key in cntk_model.outputs: shape = key.shape if len(shape) > 0: s = np.max(shape) if (s > size): size = s cntk_output = cntk_output[key][0] else: cntk_output = cntk_output[0] # Check whether softmax needs to be applied or not. if testing_info["apply_softmax"]: cntk_output = softmax(cntk_output).eval() # Get computed ELL result _logger.info("Getting computed ELL results") result_from_compute = np.array(ell_map.Compute(ell_input_tensor, dtype=np.float32)) # Get compiled ELL result _logger.info("Getting compiled ELL results") compiler_options = ell.model.MapCompilerOptions() compiler_options.useBlas = True compiled_ell_map = ell_map.Compile("host", "model", "predict", compilerOptions=compiler_options, dtype=np.float32) result_from_compiled = np.array(compiled_ell_map.Compute(ell_input_tensor, dtype=np.float32)) # Verify the computed result against the cntk result np.testing.assert_array_almost_equal( cntk_output, result_from_compute, decimal=4, err_msg=( 'results for computed ELL model do not match CNTK output!')) _logger.info("Verified computed result against CNTK") # Verify the compiled result against the cntk result np.testing.assert_array_almost_equal( cntk_output, result_from_compiled, decimal=4, err_msg=( 'results for compiled ELL model do not match CNTK output!')) _logger.info("Verified compiled result against CNTK") # Verify the compiled result agrees with the computed result np.testing.assert_array_almost_equal( result_from_compute, result_from_compiled, decimal=4, err_msg=( 'results for computed ELL model do not match results from compiled ELL model!')) _logger.info("Verified compiled result against computed result") # Get timing info total_time = 0 num_frames = 50 _logger.info("Sending {} frames through model...".format(num_frames)) for i in range(num_frames): cntk_input_tensor = np.random.random((cntk_model.arguments[0].shape)).astype(np.float32) * 255 ell_input_tensor = memory_shapes.get_tensor_in_ell_order(cntk_input_tensor, "channel_row_column").ravel().astype(np.float32) start = time.time() result_from_compiled = np.array(compiled_ell_map.Compute(ell_input_tensor, dtype=np.float32)) end = time.time() total_time += end - start total_time /= num_frames _logger.info("Average speed: {:.0f}ms/frame".format(total_time * 1000)) except BaseException as exception: _logger.error("Verification of model output failed") raise exception _logger.info("Verification of model output complete")
print("Loading model: ", model_file) trained_model = load_model(model_file) if trained_model is not None: img = Image.open(image_name) if image_name.endswith("png"): temp = Image.new("RGB", img.size, (255, 255, 255)) temp.paste(img, img) img = temp re_sized = img.resize((image_width, image_height), Image.ANTIALIAS) bgr_image = np.asarray(re_sized, dtype=np.float32)[..., [2, 1, 0]] hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) # Alternatively: if you want to use opencv-python # cv_img = cv2.imread(image_path) # resized = cv2.resize(cv_img, (image_width, image_height), interpolation=cv2.INTER_NEAREST) # bgr_image = np.asarray(resized, dtype=np.float32) # hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) # compute model output arguments = {trained_model.arguments[0]: [hwc_format]} output = trained_model.eval(arguments) # return softmax probabilities sm = softmax(output[0]) prob = sm.eval() for probability in prob: print("{:f}".format(probability))