Skip to content

Commit 6c5183f

Browse files
committed
keras in seperate project.
1 parent e43a1c8 commit 6c5183f

18 files changed

+95
-59
lines changed

SciSharp STACK Examples.sln

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpCV.Exmaples", "src\Sha
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Binding", "..\TensorFlow.NET\src\TensorFlowNET.Core\Tensorflow.Binding.csproj", "{F0DEC3F1-418F-46D4-8AC4-A0A49EB79EB4}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras", "..\TensorFlow.NET\src\TensorFlowNET.Keras\Tensorflow.Keras.csproj", "{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -123,6 +125,30 @@ Global
123125
{F0DEC3F1-418F-46D4-8AC4-A0A49EB79EB4}.Release|x64.Build.0 = Release|x64
124126
{F0DEC3F1-418F-46D4-8AC4-A0A49EB79EB4}.Release|x86.ActiveCfg = Release|Any CPU
125127
{F0DEC3F1-418F-46D4-8AC4-A0A49EB79EB4}.Release|x86.Build.0 = Release|Any CPU
128+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
129+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
130+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug|x64.ActiveCfg = Debug|x64
131+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug|x64.Build.0 = Debug|x64
132+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug|x86.ActiveCfg = Debug|Any CPU
133+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug|x86.Build.0 = Debug|Any CPU
134+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
135+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
136+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug-Minimal|x64.ActiveCfg = Debug|x64
137+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug-Minimal|x64.Build.0 = Debug|x64
138+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug-Minimal|x86.ActiveCfg = Debug|Any CPU
139+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Debug-Minimal|x86.Build.0 = Debug|Any CPU
140+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Publish|Any CPU.ActiveCfg = Release|Any CPU
141+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Publish|Any CPU.Build.0 = Release|Any CPU
142+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Publish|x64.ActiveCfg = Debug|x64
143+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Publish|x64.Build.0 = Debug|x64
144+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Publish|x86.ActiveCfg = Release|Any CPU
145+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Publish|x86.Build.0 = Release|Any CPU
146+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
147+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Release|Any CPU.Build.0 = Release|Any CPU
148+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Release|x64.ActiveCfg = Release|x64
149+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Release|x64.Build.0 = Release|x64
150+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Release|x86.ActiveCfg = Release|Any CPU
151+
{520BE7D7-C0DC-4E92-A339-BDF6775E02A7}.Release|x86.Build.0 = Release|Any CPU
126152
EndGlobalSection
127153
GlobalSection(SolutionProperties) = preSolution
128154
HideSolutionNode = FALSE

src/TensorFlowNET.Examples/BasicModels/LinearRegressionEager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using NumSharp;
1818
using static Tensorflow.Binding;
19+
using static Tensorflow.KerasExt;
1920

2021
namespace TensorFlowNET.Examples
2122
{
@@ -64,7 +65,7 @@ public void RunModelInEagerMode()
6465
// var rnd2 = rng.randn<float>();
6566
var W = tf.Variable(-0.06f, name: "weight");
6667
var b = tf.Variable(-0.73f, name: "bias");
67-
var optimizer = tf.optimizers.SGD(learning_rate);
68+
var optimizer = keras.optimizers.SGD(learning_rate);
6869

6970
// Run training for the given number of steps.
7071
foreach (var step in range(1, training_steps + 1))

src/TensorFlowNET.Examples/BasicModels/LogisticRegressionEager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
using System.IO;
2121
using Tensorflow;
2222
using static Tensorflow.Binding;
23+
using static Tensorflow.KerasExt;
2324

2425
namespace TensorFlowNET.Examples
2526
{
@@ -64,7 +65,7 @@ public bool Run()
6465
public void RunEagerMode()
6566
{
6667
// Prepare MNIST data.
67-
var ((x_train, y_train), (x_test, y_test)) = tf.keras.datasets.mnist.load_data();
68+
var ((x_train, y_train), (x_test, y_test)) = keras.datasets.mnist.load_data();
6869
// Flatten images to 1-D vector of 784 features (28*28).
6970
(x_train, x_test) = (x_train.reshape((-1, num_features)), x_test.reshape((-1, num_features)));
7071
// Normalize images value from [0, 255] to [0, 1].
@@ -101,7 +102,7 @@ public void RunEagerMode()
101102
};
102103

103104
// Stochastic gradient descent optimizer.
104-
var optimizer = tf.optimizers.SGD(learning_rate);
105+
var optimizer = keras.optimizers.SGD(learning_rate);
105106

106107
Action<Tensor, Tensor> run_optimization = (x, y) =>
107108
{

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCnnEager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
using Tensorflow;
2020
using Tensorflow.Keras.Optimizers;
2121
using static Tensorflow.Binding;
22+
using static Tensorflow.KerasExt;
2223

2324
namespace TensorFlowNET.Examples
2425
{
@@ -85,7 +86,7 @@ public bool Run()
8586
bout = tf.Variable(tf.zeros(num_classes));
8687

8788
// ADAM optimizer.
88-
var optimizer = tf.optimizers.Adam(learning_rate);
89+
var optimizer = keras.optimizers.Adam(learning_rate);
8990

9091
// Run training for the given number of steps.
9192
foreach (var (step, (batch_x, batch_y)) in enumerate(train_data, 1))
@@ -194,7 +195,7 @@ Tensor accuracy(Tensor y_pred, Tensor y_true)
194195

195196
public override void PrepareData()
196197
{
197-
((x_train, y_train), (x_test, y_test)) = tf.keras.datasets.mnist.load_data();
198+
((x_train, y_train), (x_test, y_test)) = keras.datasets.mnist.load_data();
198199
// Convert to float32.
199200
// (x_train, x_test) = (np.array(x_train, np.float32), np.array(x_test, np.float32));
200201
// Normalize images value from [0, 255] to [0, 1].

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRNN.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
using NumSharp;
1818
using Tensorflow;
1919
using static Tensorflow.Binding;
20+
using static Tensorflow.KerasExt;
2021

2122
namespace TensorFlowNET.Examples
2223
{
@@ -84,7 +85,7 @@ public override Graph BuildGraph()
8485
y = tf.placeholder(tf.int32, new[] { -1 });
8586
var cell = tf.nn.rnn_cell.BasicRNNCell(num_units: n_neurons);
8687
var (output, state) = tf.nn.dynamic_rnn(cell, X, dtype: tf.float32);
87-
var logits = tf.layers.dense(state, n_outputs);
88+
var logits = keras.layers.dense(state, n_outputs);
8889
var cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels: y, logits: logits);
8990
loss = tf.reduce_mean(cross_entropy);
9091
var adam = tf.train.AdamOptimizer(learning_rate: learning_rate);

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRnnKeras.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
using Tensorflow.Keras.Engine;
2222
using Tensorflow.Keras.Optimizers;
2323
using static Tensorflow.Binding;
24+
using static Tensorflow.KerasExt;
2425

2526
namespace TensorFlowNET.Examples
2627
{
@@ -97,7 +98,7 @@ public override void Test()
9798

9899
public override void PrepareData()
99100
{
100-
((x_train, y_train), (x_test, y_test)) = tf.keras.datasets.mnist.load_data();
101+
((x_train, y_train), (x_test, y_test)) = keras.datasets.mnist.load_data();
101102
// Convert to float32.
102103
// (x_train, x_test) = (np.array(x_train, np.float32), np.array(x_test, np.float32));
103104
// Normalize images value from [0, 255] to [0, 1].
@@ -121,9 +122,9 @@ internal class LSTMModel : Model
121122
public LSTMModel(LSTMModelArgs args)
122123
: base(args)
123124
{
124-
optimizer = tf.optimizers.Adam(args.LearningRate);
125+
optimizer = keras.optimizers.Adam(args.LearningRate);
125126

126-
var layers = tf.keras.layers;
127+
var layers = keras.layers;
127128
lstm = layers.LSTM(args.NumUnits);
128129
output = layers.Dense(args.NumClasses);
129130
}

src/TensorFlowNET.Examples/ImageProcessing/ImageClassificationKeras.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System.Collections.Generic;
22
using Tensorflow;
3-
using Tensorflow.Keras.Engine;
3+
using Tensorflow.Keras;
44
using static Tensorflow.Binding;
5+
using static Tensorflow.KerasExt;
56

67
namespace TensorFlowNET.Examples
78
{
@@ -31,14 +32,14 @@ public override void PrepareData()
3132
TensorShape img_dim = (180, 180);
3233

3334
var data_dir = @"C:/Users/haipi/.keras/datasets/flower_photos";
34-
var train_ds = tf.keras.preprocessing.image_dataset_from_directory(data_dir,
35+
var train_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
3536
validation_split: 0.2f,
3637
subset: "training",
3738
seed: 123,
3839
image_size: img_dim,
3940
batch_size: batch_size);
4041

41-
var val_ds = tf.keras.preprocessing.image_dataset_from_directory(data_dir,
42+
var val_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
4243
validation_split: 0.2f,
4344
subset: "validation",
4445
seed: 123,
@@ -56,22 +57,22 @@ public override void PrepareData()
5657

5758
int num_classes = 5;
5859
// var normalization_layer = tf.keras.layers.Rescaling(1.0f / 255);
59-
var layers = tf.keras.layers;
60-
var model = tf.keras.Sequential(new List<Layer>
60+
var layers = keras.layers;
61+
var model = keras.Sequential(new List<ILayer>
6162
{
6263
layers.Rescaling(1.0f / 255, input_shape: (img_dim.dims[0], img_dim.dims[1], 3)),
63-
layers.Conv2D(16, 3, padding: "same", activation: tf.keras.activations.Relu),
64+
layers.Conv2D(16, 3, padding: "same", activation: keras.activations.Relu),
6465
layers.MaxPooling2D(),
6566
/*layers.Conv2D(32, 3, padding: "same", activation: "relu"),
6667
layers.MaxPooling2D(),
6768
layers.Conv2D(64, 3, padding: "same", activation: "relu"),
6869
layers.MaxPooling2D(),*/
6970
layers.Flatten(),
70-
layers.Dense(128, activation: tf.keras.activations.Relu),
71+
layers.Dense(128, activation: keras.activations.Relu),
7172
layers.Dense(num_classes)
7273
});
7374

74-
model.compile("adam", tf.keras.losses.SparseCategoricalCrossentropy(from_logits: true));
75+
model.compile("adam", keras.losses.SparseCategoricalCrossentropy(from_logits: true));
7576
}
7677
}
7778
}

src/TensorFlowNET.Examples/ImageProcessing/MnistCnnKerasSubclass .cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
using Tensorflow.Keras.Engine;
2222
using Tensorflow.Keras.Optimizers;
2323
using static Tensorflow.Binding;
24+
using static Tensorflow.KerasExt;
2425

2526
namespace TensorFlowNET.Examples
2627
{
@@ -67,7 +68,7 @@ public bool Run()
6768
});
6869

6970
// ADAM optimizer.
70-
var optimizer = tf.optimizers.Adam(learning_rate);
71+
var optimizer = keras.optimizers.Adam(learning_rate);
7172

7273
// Run training for the given number of steps.
7374
foreach (var (step, (batch_x, batch_y)) in enumerate(train_data, 1))
@@ -144,7 +145,7 @@ Tensor accuracy(Tensor y_pred, Tensor y_true)
144145

145146
public override void PrepareData()
146147
{
147-
((x_train, y_train), (x_test, y_test)) = tf.keras.datasets.mnist.load_data();
148+
((x_train, y_train), (x_test, y_test)) = keras.datasets.mnist.load_data();
148149
// Convert to float32.
149150
// (x_train, x_test) = (np.array(x_train, np.float32), np.array(x_test, np.float32));
150151
// Normalize images value from [0, 255] to [0, 1].
@@ -172,16 +173,16 @@ public class ConvNet : Model
172173
public ConvNet(ConvNetArgs args)
173174
: base(args)
174175
{
175-
var layers = tf.keras.layers;
176+
var layers = keras.layers;
176177

177178
// Convolution Layer with 32 filters and a kernel size of 5.
178-
conv1 = layers.Conv2D(32, kernel_size: 5, activation: tf.keras.activations.Relu);
179+
conv1 = layers.Conv2D(32, kernel_size: 5, activation: keras.activations.Relu);
179180

180181
// Max Pooling (down-sampling) with kernel size of 2 and strides of 2.
181182
maxpool1 = layers.MaxPooling2D(2, strides: 2);
182183

183184
// Convolution Layer with 64 filters and a kernel size of 3.
184-
conv2 = layers.Conv2D(64, kernel_size: 3, activation: tf.keras.activations.Relu);
185+
conv2 = layers.Conv2D(64, kernel_size: 3, activation: keras.activations.Relu);
185186
// Max Pooling (down-sampling) with kernel size of 2 and strides of 2.
186187
maxpool2 = layers.MaxPooling2D(2, strides: 2);
187188

src/TensorFlowNET.Examples/ImageProcessing/MnistFnnKerasFunctional.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using NumSharp;
18-
using System;
19-
using Tensorflow;
2018
using Tensorflow.Keras.Engine;
2119
using static Tensorflow.Binding;
20+
using static Tensorflow.KerasExt;
2221

2322
namespace TensorFlowNET.Examples
2423
{
@@ -63,10 +62,10 @@ public override void BuildModel()
6362
var inputs = keras.Input(shape: 784);
6463

6564
// 1st dense layer
66-
var outputs = layers.Dense(64, activation: tf.keras.activations.Relu).Apply(inputs);
65+
var outputs = layers.Dense(64, activation: keras.activations.Relu).Apply(inputs);
6766

6867
// 2nd dense layer
69-
outputs = layers.Dense(64, activation: tf.keras.activations.Relu).Apply(outputs);
68+
outputs = layers.Dense(64, activation: keras.activations.Relu).Apply(outputs);
7069

7170
// output layer
7271
outputs = layers.Dense(10).Apply(outputs);

src/TensorFlowNET.Examples/NeuralNetworks/FullyConnectedEager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
using Tensorflow;
2020
using Tensorflow.Keras.Optimizers;
2121
using static Tensorflow.Binding;
22+
using static Tensorflow.KerasExt;
2223

2324
namespace TensorFlowNET.Examples
2425
{
@@ -74,7 +75,7 @@ public bool Run()
7475
var trainable_variables = new IVariableV1[] { h1, h2, wout, b1, b2, bout };
7576

7677
// Stochastic gradient descent optimizer.
77-
var optimizer = tf.optimizers.SGD(learning_rate);
78+
var optimizer = keras.optimizers.SGD(learning_rate);
7879

7980
// Run training for the given number of steps.
8081
foreach (var (step, (batch_x, batch_y)) in enumerate(train_data, 1))
@@ -157,7 +158,7 @@ Tensor neural_net(Tensor x)
157158
public override void PrepareData()
158159
{
159160
// Prepare MNIST data.
160-
((x_train, y_train), (x_test, y_test)) = tf.keras.datasets.mnist.load_data();
161+
((x_train, y_train), (x_test, y_test)) = keras.datasets.mnist.load_data();
161162
// Flatten images to 1-D vector of 784 features (28*28).
162163
(x_train, x_test) = (x_train.reshape((-1, num_features)), x_test.reshape((-1, num_features)));
163164
// Normalize images value from [0, 255] to [0, 1].

0 commit comments

Comments
 (0)