|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Tensorflow.Keras.Layers; |
| 5 | +using NumSharp; |
| 6 | +using Tensorflow.Keras; |
| 7 | +using static Tensorflow.Binding; |
| 8 | +using static Tensorflow.KerasApi; |
| 9 | + |
| 10 | +namespace Tensorflow.Benchmark.Leak |
| 11 | +{ |
| 12 | + class GpuLeakByCNN |
| 13 | + { |
| 14 | + protected static LayersApi layers = new LayersApi(); |
| 15 | + |
| 16 | + public static void Test() |
| 17 | + { |
| 18 | + int num = 50, width = 64, height = 64; |
| 19 | + // if width = 128, height = 128, the exception occurs faster |
| 20 | + |
| 21 | + var bytes = new byte[num * width * height * 3]; |
| 22 | + var inputImages = np.array(bytes) / 255.0f; |
| 23 | + inputImages = inputImages.reshape(num, height, width, 3); |
| 24 | + |
| 25 | + bytes = new byte[num]; |
| 26 | + var outLables = np.array(bytes); |
| 27 | + Console.WriteLine("Image.Shape={0}", inputImages.Shape); |
| 28 | + Console.WriteLine("Label.Shape={0}", outLables.Shape); |
| 29 | + |
| 30 | + tf.enable_eager_execution(); |
| 31 | + |
| 32 | + var inputss = keras.Input((height, width, 3)); |
| 33 | + |
| 34 | + var inputs = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputss); |
| 35 | + inputs = layers.MaxPooling2D((2, 2)).Apply(inputs); |
| 36 | + |
| 37 | + inputs = layers.Flatten().Apply(inputs); |
| 38 | + |
| 39 | + var outputs = layers.Dense(10).Apply(inputs); |
| 40 | + |
| 41 | + var model = keras.Model(inputss, outputs, "gpuleak"); |
| 42 | + |
| 43 | + model.summary(); |
| 44 | + |
| 45 | + model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true), |
| 46 | + optimizer: keras.optimizers.RMSprop(), |
| 47 | + metrics: new[] { "accuracy" }); |
| 48 | + |
| 49 | + model.fit(inputImages, outLables, epochs: 200); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments