Skip to content

Commit

Permalink
Add imdb sample data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Jan 9, 2021
1 parent 0316fde commit 4c814d6
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 88 deletions.
Binary file added data/imdb.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override void PrepareData()
{
string fileName = "flower_photos.tgz";
string url = $"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz";
string data_dir = Path.GetTempPath();
string data_dir = Path.Combine(Path.GetTempPath(), "flower_photos");
Web.Download(url, data_dir, fileName);
Compress.ExtractTGZ(Path.Join(data_dir, fileName), data_dir);
data_dir = Path.Combine(data_dir, "flower_photos");
Expand All @@ -90,25 +90,19 @@ public override void PrepareData()
batch_size: batch_size);

val_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
validation_split: 0.2f,
subset: "validation",
seed: 123,
image_size: img_dim,
batch_size: batch_size);
validation_split: 0.2f,
subset: "validation",
seed: 123,
image_size: img_dim,
batch_size: batch_size);

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size: -1);
val_ds = val_ds.cache().prefetch(buffer_size: -1);

foreach (var (img, label) in train_ds)
{
print($"images: {img.TensorShape}");
var nd = label.numpy();
print($"labels: {nd}");
var data = nd.Data<int>();
if (data.Max() > 4 || data.Min() < 0)
{
// exception
}
print($"labels: {label.numpy()}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Examples/ImageProcessing/ToyResNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ExampleConfig InitConfig()
public bool Run()
{
tf.enable_eager_execution();

BuildModel();
PrepareData();
Train();
Expand Down
2 changes: 2 additions & 0 deletions src/TensorFlowNET.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
using System.Reflection;
using Tensorflow;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
using Console = Colorful.Console;

namespace TensorFlowNET.Examples
Expand Down Expand Up @@ -100,6 +101,7 @@ private static void RunExamples(string key, IExample[] examples)
}

finished++;
keras.backend.clear_session();
Console.WriteLine($"{DateTime.UtcNow} Completed {example.Config.Name}", Color.White);
}

Expand Down
1 change: 1 addition & 0 deletions src/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
<PackageReference Include="SharpCV" Version="0.6.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
<PackageReference Include="TensorFlow.Keras" Version="0.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Tensorflow.Keras.Utils;
using static Tensorflow.KerasApi;

namespace TensorFlowNET.Examples
{
/// <summary>
/// https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/nlp/ipynb/text_classification_from_scratch.ipynb#scrollTo=qqTCrB7SmJv9
/// </summary>
public class CnnTextClassificationKeras : SciSharpExample, IExample
{
public ExampleConfig InitConfig()
=> Config = new ExampleConfig
{
Name = "CNN Text Classification (Keras)",
Enabled = false
};

public bool Run()
{
return true;
}

public override void PrepareData()
{
string fileName = "aclImdb_v1.tar.gz";
string url = $"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz";
string data_dir = Path.GetTempPath();
Web.Download(url, data_dir, fileName);
Compress.ExtractGZip(Path.Join(data_dir, fileName), data_dir);
data_dir = Path.Combine(data_dir, "aclImdb_v1");
}
}
}
2 changes: 1 addition & 1 deletion src/tensorflow2.x-python-tutorial/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/image_classification.py",
"program": "${workspaceFolder}/keras_mnist.py",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"justMyCode": false
Expand Down
34 changes: 0 additions & 34 deletions src/tensorflow2.x-python-tutorial/keras-basic.py

This file was deleted.

39 changes: 0 additions & 39 deletions src/tensorflow2.x-python-tutorial/keras_basic.py

This file was deleted.

51 changes: 51 additions & 0 deletions src/tensorflow2.x-python-tutorial/keras_mnist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

from tensorflow.python.platform import gfile
# GRAPH_PB_PATH = 'D:/tmp/TensorflowIssue/TensorflowIssue/model/saved_model.pb'
GRAPH_PB_PATH = 'D:/tmp/TensorFlow.NET/data/saved_model.pb'
with tf.compat.v1.Session() as sess:
print("load graph")
with tf.io.gfile.GFile(GRAPH_PB_PATH,'rb') as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read())
sess.graph.as_default()
tf.import_graph_def(graph_def, name='')
graph_nodes=[n for n in graph_def.node]
names = []
for t in graph_nodes:
names.append(t.name)
print(names)

inputs = keras.Input(shape=(784,))

dense = layers.Dense(64, activation="relu")
x = dense(inputs)

dense = layers.Dense(64, activation="relu")
x = dense(x)

dense = layers.Dense(10)
outputs = dense(x)

model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
model.summary();

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255

model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.RMSprop(),
metrics=["accuracy"],
)

history = model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2)

test_scores = model.evaluate(x_test, y_test, verbose=2)
print("Test loss:", test_scores[0])
print("Test accuracy:", test_scores[1])
24 changes: 24 additions & 0 deletions src/tensorflow2.x-python-tutorial/transformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

physical_devices = tf.config.list_physical_devices('CPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)

tf.config.run_functions_eagerly(True)
tf.debugging.set_log_device_placement(True)

a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
print(a)
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
print(b)
c = tf.matmul(a, b)
print(c)

vocab_size = 20000 # Only consider the top 20k words
maxlen = 200 # Only consider the first 200 words of each movie review
(x_train, y_train), (x_val, y_val) = keras.datasets.imdb.load_data(num_words=vocab_size)
print(len(x_train), "Training sequences")
print(len(x_val), "Validation sequences")
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen)
x_val = keras.preprocessing.sequence.pad_sequences(x_val, maxlen=maxlen)

0 comments on commit 4c814d6

Please sign in to comment.