Skip to content

Commit 803bd0d

Browse files
Release the temporary graph early on (migueldeicaza#213)
1 parent bdd2331 commit 803bd0d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Examples/ExampleCommon/ImageUtil.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ public static TFTensor CreateTensorFromImageFile (string file, TFDataType destin
1313
// DecodeJpeg uses a scalar String-valued tensor as input.
1414
var tensor = TFTensor.CreateString (contents);
1515

16-
TFGraph graph;
1716
TFOutput input, output;
1817

1918
// Construct a graph to normalize the image
20-
ConstructGraphToNormalizeImage (out graph, out input, out output, destinationDataType);
21-
22-
// Execute that graph to normalize this one image
23-
using (var session = new TFSession (graph)) {
24-
var normalized = session.Run (
25-
inputs: new [] { input },
26-
inputValues: new [] { tensor },
27-
outputs: new [] { output });
28-
29-
return normalized [0];
19+
using (var graph = ConstructGraphToNormalizeImage (out graph, out input, out output, destinationDataType)){
20+
// Execute that graph to normalize this one image
21+
using (var session = new TFSession (graph)) {
22+
var normalized = session.Run (
23+
inputs: new [] { input },
24+
inputValues: new [] { tensor },
25+
outputs: new [] { output });
26+
27+
return normalized [0];
28+
}
3029
}
3130
}
3231

@@ -37,7 +36,7 @@ public static TFTensor CreateTensorFromImageFile (string file, TFDataType destin
3736
// This function constructs a graph of TensorFlow operations which takes as
3837
// input a JPEG-encoded string and returns a tensor suitable as input to the
3938
// inception model.
40-
private static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float)
39+
private static TFGraph ConstructGraphToNormalizeImage (out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float)
4140
{
4241
// Some constants specific to the pre-trained model at:
4342
// https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
@@ -51,7 +50,7 @@ private static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOut
5150
const float Mean = 117;
5251
const float Scale = 1;
5352

54-
graph = new TFGraph ();
53+
var graph = new TFGraph ();
5554
input = graph.Placeholder (TFDataType.String);
5655

5756
output = graph.Cast (graph.Div (
@@ -64,6 +63,8 @@ private static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOut
6463
size: graph.Const (new int [] { W, H }, "size")),
6564
y: graph.Const (Mean, "mean")),
6665
y: graph.Const (Scale, "scale")), destinationDataType);
66+
67+
return graph;
6768
}
6869
}
6970
}

0 commit comments

Comments
 (0)