-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNN-Algorithm.cs
More file actions
42 lines (42 loc) · 1.41 KB
/
CNN-Algorithm.cs
File metadata and controls
42 lines (42 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Drawing;
using TensorFlow;
using TensorFlow.Image;
using TensorFlow.NN;
using TensorFlow.NN.Functions;
using TensorFlow.NN.Layers;
using TensorFlow.NN.Pooling;
using TensorFlow.NN.Activation;
using TensorFlow.NN.Convolution;
using TensorFlow.NN.DataTypes;
using TensorFlow.NN.IO;
class Program
{
static void Main()
{
var kernel = new TFTensor(new float[,,] {
{ { -1, -1, -1 },
{ -1, 8, -1 },
{ -1, -1, -1 } }});
var imageBytes = System.IO.File.ReadAllBytes("RealTime.jpg");
var image = TFImage.DecodeJpeg(imageBytes, 1);
image = TFImage.Resize(image, new int[] { 300, 300 });
var img = image.ToArray<float>();
var imageFloat = TF.Cast(image, TFDataType.Float);
imageFloat = TF.ExpandDims(imageFloat, 0);
kernel = TF.Reshape(kernel, new
TFShape(kernel.Shape.Dimensions.Append(1).Append(1)));
kernel = TF.Cast(kernel, TFDataType.Float);
var imageFilter = TF.Conv2D(
input: imageFloat,
filter: kernel,
strides: new int[] { 1, 1 },
padding: "SAME");
var imageDetect = TF.Relu(imageFilter);
var imageCondense = TF.Pool(
input: imageDetect,
windowShape: new int[] { 2, 2 },
poolingType: PoolingType.Max,
strides: new int[] { 2, 2 },
padding: "SAME");
// Display images or further processing here}}