Skip to content

Commit ea6826a

Browse files
committed
Fix Sequential model.summary missing layers. SciSharp#960
1 parent 3519207 commit ea6826a

File tree

6 files changed

+136
-5
lines changed

6 files changed

+136
-5
lines changed

SciSharp STACK Examples.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpCV", "..\SharpCV\src\S
2323
EndProject
2424
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SciSharp.Models.ImageClassification", "..\SciSharp.Models\SciSharp.Models.ImageClassification\SciSharp.Models.ImageClassification.csproj", "{FA0B3C1B-9364-40D9-A7E8-54A6045F7B67}"
2525
EndProject
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SciSharp.Models.TextClassification", "..\SciSharp.Models\SciSharp.Models.TextClassification\SciSharp.Models.TextClassification.csproj", "{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -216,6 +218,24 @@ Global
216218
{FA0B3C1B-9364-40D9-A7E8-54A6045F7B67}.Release|x64.Build.0 = Release|Any CPU
217219
{FA0B3C1B-9364-40D9-A7E8-54A6045F7B67}.Release|x86.ActiveCfg = Release|Any CPU
218220
{FA0B3C1B-9364-40D9-A7E8-54A6045F7B67}.Release|x86.Build.0 = Release|Any CPU
221+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
222+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
223+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Debug|x64.ActiveCfg = Debug|x64
224+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Debug|x64.Build.0 = Debug|x64
225+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Debug|x86.ActiveCfg = Debug|Any CPU
226+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Debug|x86.Build.0 = Debug|Any CPU
227+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
228+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.GPU|Any CPU.Build.0 = Debug|Any CPU
229+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.GPU|x64.ActiveCfg = Debug|x64
230+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.GPU|x64.Build.0 = Debug|x64
231+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.GPU|x86.ActiveCfg = Debug|Any CPU
232+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.GPU|x86.Build.0 = Debug|Any CPU
233+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
234+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Release|Any CPU.Build.0 = Release|Any CPU
235+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Release|x64.ActiveCfg = Release|x64
236+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Release|x64.Build.0 = Release|x64
237+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Release|x86.ActiveCfg = Release|Any CPU
238+
{D5243F2C-4EAE-45AD-849C-A7FD79B599C5}.Release|x86.Build.0 = Release|Any CPU
219239
EndGlobalSection
220240
GlobalSection(SolutionProperties) = preSolution
221241
HideSolutionNode = FALSE

src/TensorFlowNET.Examples/GAN/MnistGAN.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ namespace TensorFlowNET.Examples.GAN
1616
public class MnistGAN : SciSharpExample, IExample
1717
{
1818
float LeakyReLU_alpha = 0.2f;
19+
20+
#if GPU
21+
int epochs = 2000; // Better effect, but longer time
22+
#else
1923
int epochs = 20;
20-
//int epochs = 2000; // Better effect, but longer time
24+
#endif
2125
int batch_size = 64;
2226

2327
string imgpath = "dcgan\\imgs";
@@ -65,7 +69,8 @@ private Model Make_Generator_model()
6569
Tensorflow.Keras.Activation activation = null;
6670

6771
var model = keras.Sequential();
68-
model.add(keras.layers.Dense(img_rows / 4 * img_cols / 4 * 256, activation: activation, input_shape: 100));
72+
model.add(keras.layers.Input(shape: 100));
73+
model.add(keras.layers.Dense(img_rows / 4 * img_cols / 4 * 256, activation: activation));
6974
model.add(keras.layers.BatchNormalization(momentum: 0.8f));
7075
model.add(keras.layers.LeakyReLU(LeakyReLU_alpha));
7176
model.add(keras.layers.Reshape((7, 7, 256)));
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using SciSharp.Models.TimeSeries;
2+
using SciSharp.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.IO;
9+
using static Tensorflow.Binding;
10+
using static Tensorflow.KerasApi;
11+
12+
namespace TensorFlowNET.Examples
13+
{
14+
public class SentimentClassification : SciSharpExample, IExample
15+
{
16+
ITimeSeriesTask task;
17+
public ExampleConfig InitConfig()
18+
=> Config = new ExampleConfig
19+
{
20+
Name = "Text Sentiment Classification",
21+
Enabled = true
22+
};
23+
24+
public bool Run()
25+
{
26+
var wizard = new ModelWizard();
27+
task = wizard.AddTimeSeriesTask<ConvolutionalModel>(new TaskOptions
28+
{
29+
WeightsPath = @"timeseries_linear_v1\saved_weights.h5"
30+
});
31+
task.SetModelArgs(new TimeSeriesModelArgs
32+
{
33+
});
34+
35+
return true;
36+
}
37+
38+
public override void PrepareData()
39+
{
40+
// tf.debugging.set_log_device_placement(true);
41+
string url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz";
42+
var dataset = keras.utils.get_file("aclImdb_v1.tar.gz", url,
43+
untar: true,
44+
cache_dir: Path.GetTempPath(),
45+
cache_subdir: "aclImdb_v1");
46+
var data_dir = Path.Combine(dataset, "aclImdb");
47+
var train_dir = Path.Combine(data_dir, "train");
48+
49+
int batch_size = 32;
50+
int seed = 42;
51+
var raw_train_ds = keras.preprocessing.text_dataset_from_directory(
52+
train_dir,
53+
batch_size: batch_size,
54+
validation_split: 0.2f,
55+
subset: "training",
56+
seed: seed);
57+
58+
/*foreach (var (text_batch, label_batch) in raw_train_ds.take(1))
59+
{
60+
foreach (var i in range(3))
61+
{
62+
print("Review", text_batch.StringData()[i]);
63+
print("Label", label_batch.numpy()[i]);
64+
}
65+
}
66+
67+
print("Label 0 corresponds to", raw_train_ds.class_names[0]);
68+
print("Label 1 corresponds to", raw_train_ds.class_names[1]);
69+
70+
var raw_val_ds = keras.preprocessing.text_dataset_from_directory(
71+
train_dir,
72+
batch_size: batch_size,
73+
validation_split: 0.2f,
74+
subset: "validation",
75+
seed: seed);
76+
77+
var test_dir = Path.Combine(data_dir, "test");
78+
var raw_test_ds = keras.preprocessing.text_dataset_from_directory(
79+
test_dir,
80+
batch_size: batch_size);
81+
82+
var max_features = 10000;
83+
var sequence_length = 250;
84+
85+
Func<Tensor, Tensor> custom_standardization = input_data =>
86+
{
87+
var lowercase = tf.strings.lower(input_data);
88+
var stripped_html = tf.strings.regex_replace(lowercase, "<br />", " ");
89+
return tf.strings.regex_replace(stripped_html,
90+
"'[!\"\\#\\$%\\&\'\\(\\)\\*\\+,\\-\\./:;<=>\\?@\\[\\\\\\]\\^_`\\{\\|\\}\\~]'",
91+
"");
92+
};
93+
94+
var vectorize_layer = keras.layers.preprocessing.TextVectorization(standardize: custom_standardization,
95+
max_tokens: max_features,
96+
output_mode: "int",
97+
output_sequence_length: sequence_length);
98+
99+
var train_text = raw_train_ds.map(inputs => inputs[0]);
100+
//vectorize_layer.adapt(train_text);*/
101+
}
102+
}
103+
}

src/TensorFlowNET.Examples/ObjectDetection/MnistInYOLOv3.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ public ExampleConfig InitConfig()
3131
=> Config = new ExampleConfig
3232
{
3333
Name = "MNIST in YOLOv3",
34-
Enabled = true
34+
Enabled = false
3535
};
3636

3737
public bool Run()
3838
{
3939
cfg = new YoloConfig("YOLOv3");
4040
(trainingData, testingData) = PrepareData();
4141
Train();
42+
Test();
4243
return true;
4344
}
4445

@@ -67,6 +68,7 @@ public override void Test()
6768
{
6869
ModelPath = @"./YOLOv3/yolov3.h5"
6970
});
71+
task.SetModelArgs(cfg);
7072
var result = task.Test(new TestingOptions
7173
{
7274

src/TensorFlowNET.Examples/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ static void Main(string[] args)
3636
var examples = Assembly.GetEntryAssembly().GetTypes()
3737
.Where(x => x.GetInterfaces().Contains(typeof(IExample)))
3838
//.Where(x => x.Name == nameof(WeatherPrediction))
39-
//.Where(x => x.Name == nameof(NeuralNetXorEager))
40-
.Where(x => x.Name == nameof(MnistInYOLOv3))
39+
//.Where(x => x.Name == nameof(SentimentClassification))
40+
//.Where(x => x.Name == nameof(MnistInYOLOv3))
4141
.ToArray();
4242

4343
Console.WriteLine(Environment.OSVersion, Color.Yellow);

src/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<ItemGroup>
6262
<ProjectReference Include="..\..\..\SciSharp.Models\SciSharp.Models.ImageClassification\SciSharp.Models.ImageClassification.csproj" />
6363
<ProjectReference Include="..\..\..\SciSharp.Models\SciSharp.Models.ObjectDetection\SciSharp.Models.ObjectDetection.csproj" />
64+
<ProjectReference Include="..\..\..\SciSharp.Models\SciSharp.Models.TextClassification\SciSharp.Models.TextClassification.csproj" />
6465
<ProjectReference Include="..\..\..\SciSharp.Models\SciSharp.Models.TimeSeries\SciSharp.Models.TimeSeries.csproj" />
6566
<ProjectReference Include="..\..\..\SharpCV\src\SharpCV\SharpCV.csproj" />
6667
<ProjectReference Include="..\..\..\TensorFlow.NET\src\TensorFlowNET.Core\Tensorflow.Binding.csproj" />

0 commit comments

Comments
 (0)