private void AssertArgumentException(IEvaluateModelManagedF model, Bitmap image, string outputKey, string expectedParameterName, string expectedMessageText, string errorMessage) { bool exception = false; try { model.EvaluateRgbImage(image, outputKey); } catch (ArgumentException ex) { if (ex.ParamName == expectedParameterName && ex.Message.Contains(expectedMessageText)) { exception = true; } } catch { } if (!exception) { throw new Exception(errorMessage); } }
public void EvalManagedImageApiErrorHandling() { // The width and height of the image that will be fed into the network. var expectedSize = 10; // Images with correct size and pixel format. var correctBmp1 = new Bitmap(expectedSize, expectedSize, PixelFormat.Format24bppRgb); var correctBmp2 = new Bitmap(expectedSize, expectedSize, PixelFormat.Format32bppRgb); // Image with correct size, but wrong pixel format var wrongPixelFormat = new Bitmap(expectedSize, expectedSize, PixelFormat.Format16bppRgb565); // Image with wrong size, correct pixel format var wrongSize = new Bitmap(expectedSize * 2, expectedSize, PixelFormat.Format24bppRgb); var inputVectorSize = expectedSize * expectedSize * 3; var modelDefinition = String.Format(@"deviceId = -1 precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder=[ i1 = Input({0}) # Network must have size expectedSize * expectedSize * 3, for 3 channels o1 = Times(Constant(1, rows=2, cols={0}), i1, tag=""output"") FeatureNodes = (i1) ]", inputVectorSize); using (var model = new IEvaluateModelManagedF()) { model.CreateNetwork(modelDefinition); var output = model.EvaluateRgbImage(correctBmp1, "o1"); // Network computes 2 simple dot products. Assert.AreEqual(2, output.Count, "Size of output vector"); // Input image is all zero, output should be too. Assert.AreEqual(0.0f, output[0], "OutputVector[0]"); Assert.AreEqual(0.0f, output[1], "OutputVector[1]"); AssertArgumentException(model, correctBmp1, "No such output key", "outputKey", "not an output node", "Providing a non-existing output node should fail with an ArgumentException."); AssertArgumentException(model, wrongPixelFormat, "o1", "image", "must be one of { Format24bppRgb, Format32bppArgb}", "Images with an unrecognized pixel format should fail with an ArgumentException."); AssertArgumentException(model, wrongSize, "o1", "image", "invalid size", "Calling with a wrongly sized image should fail with an ArgumentException."); } }
/// <summary> /// This method shows how to evaluate a trained image classification model, where the /// creation of the CNTK feature vector is happening in native code inside the EvalWrapper. /// </summary> public static List <float> EvaluateImageInputUsingImageApi() { List <float> outputs = null; try { // This example requires the RestNet_18 model. // The model can be downloaded from <see cref="https://www.cntk.ai/resnet/ResNet_18.model"/> // The model is assumed to be located at: <CNTK>\Examples\Image\Classification\ResNet // along with a sample image file named "zebra.jpg". Environment.CurrentDirectory = initialDirectory; using (var model = new IEvaluateModelManagedF()) { model.CreateNetwork(string.Format("modelPath=\"{0}\"", resnetModelFilePath), deviceId: -1); // Prepare input value in the appropriate structure and size var inDims = model.GetNodeDimensions(NodeGroup.Input); if (inDims.First().Value != resNetImageSize * resNetImageSize * 3) { throw new CNTKRuntimeException(string.Format("The input dimension for {0} is {1} which is not the expected size of {2}.", inDims.First(), inDims.First().Value, 224 * 224 * 3), string.Empty); } // Transform the image Bitmap bmp = new Bitmap(Bitmap.FromFile(imageFileName)); var resized = bmp.Resize(resNetImageSize, resNetImageSize, true); // Now evaluate using the alternative API, where we directly pass the // native bitmap data to the unmanaged code. var outDims = model.GetNodeDimensions(NodeGroup.Output); var outputNodeName = outDims.First().Key; outputs = model.EvaluateRgbImage(resized, outputNodeName); } // Retrieve the outcome index (so we can compare it with the expected index) var max = outputs.Select((value, index) => new { Value = value, Index = index }) .Aggregate((a, b) => (a.Value > b.Value) ? a : b) .Index; Console.WriteLine("EvaluateImageInputUsingImageApi: Outcome = {0}", max); } catch (CNTKException ex) { OnCNTKException(ex); } catch (Exception ex) { OnGeneralException(ex); } return(outputs); }
/// <summary> /// This method shows how to evaluate a trained image classification model, where the /// creation of the CNTK feature vector is happening in native code inside the EvalWrapper. /// </summary> public static List<float> EvaluateImageInputUsingImageApi() { List<float> outputs = null; try { // This example requires the RestNet_18 model. // The model can be downloaded from <see cref="https://www.cntk.ai/resnet/ResNet_18.model"/> // The model is assumed to be located at: <CNTK>\Examples\Image\Classification\ResNet // along with a sample image file named "zebra.jpg". Environment.CurrentDirectory = initialDirectory; using (var model = new IEvaluateModelManagedF()) { model.CreateNetwork(string.Format("modelPath=\"{0}\"", resnetModelFilePath), deviceId: -1); // Prepare input value in the appropriate structure and size var inDims = model.GetNodeDimensions(NodeGroup.Input); if (inDims.First().Value != resNetImageSize * resNetImageSize * 3) { throw new CNTKRuntimeException(string.Format("The input dimension for {0} is {1} which is not the expected size of {2}.", inDims.First(), inDims.First().Value, 224 * 224 * 3), string.Empty); } // Transform the image Bitmap bmp = new Bitmap(Bitmap.FromFile(imageFileName)); var resized = bmp.Resize(resNetImageSize, resNetImageSize, true); // Now evaluate using the alternative API, where we directly pass the // native bitmap data to the unmanaged code. var outDims = model.GetNodeDimensions(NodeGroup.Output); var outputNodeName = outDims.First().Key; outputs = model.EvaluateRgbImage(resized, outputNodeName); } // Retrieve the outcome index (so we can compare it with the expected index) var max = outputs.Select((value, index) => new { Value = value, Index = index }) .Aggregate((a, b) => (a.Value > b.Value) ? a : b) .Index; Console.WriteLine("EvaluateImageInputUsingImageApi: Outcome = {0}", max); } catch (CNTKException ex) { OnCNTKException(ex); } catch (Exception ex) { OnGeneralException(ex); } return outputs; }