Skip to content

Commit 0316fde

Browse files
committed
image classification #23
1 parent 43c06d5 commit 0316fde

File tree

8 files changed

+135
-43
lines changed

8 files changed

+135
-43
lines changed

README.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SciSharp STACK Examples
2-
This repo contains many practical examples written in SciSharp's machine learning libraries. If you still don't know how to use .NET for deep learning, getting started from here is your best choice.
2+
This repo contains many practical examples written in SciSharp's machine learning libraries. If you still don't know how to use .NET for deep learning, getting started from these examples is your best choice.
33

44
[![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
55

@@ -30,6 +30,8 @@ dotnet TensorFlowNET.Examples.dll -ex "MNIST CNN (Eager)"
3030

3131
Example runner will download all the required files like training data and model pb files.
3232

33+
#### Basic Model
34+
3335
* Hello World [C#](src/TensorFlowNET.Examples/HelloWorld.cs)
3436
* Basic Operations [C#](src/TensorFlowNET.Examples/BasicOperations.cs)
3537
* Linear Regression in Graph mode [C#](src/TensorFlowNET.Examples/BasicModels/LinearRegression.cs)
@@ -38,22 +40,29 @@ Example runner will download all the required files like training data and model
3840
* Logistic Regression in Eager mode [C#](src/TensorFlowNET.Examples/BasicModels/LogisticRegressionEager.cs)
3941
* Nearest Neighbor [C#](src/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs)
4042
* Naive Bayes Classification [C#](src/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs)
41-
* Full Connected Neural Network in Eager mode [C#](src/TensorFlowNET.Examples/\NeuralNetworks/FullyConnectedEager.cs)
4243
* K-means Clustering [C#](src/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs)
44+
45+
#### Neural Network
46+
47+
* Full Connected Neural Network in Eager mode [C#](src/TensorFlowNET.Examples/\NeuralNetworks/FullyConnectedEager.cs)
4348
* NN XOR [C#](src/TensorFlowNET.Examples/NeuralNetworks/NeuralNetXor.cs)
4449
* Object Detection in MobileNet [C#](src/TensorFlowNET.Examples/ObjectDetection/DetectInMobilenet.cs)
45-
* Binary Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
46-
* CNN Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
4750
* MNIST FNN in Keras Functional API [C#](src/TensorFlowNET.Examples/ImageProcessing/MnistFnnKerasFunctional.cs)
4851
* MNIST CNN in Graph mode [C#](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs)
4952
* MNIST RNN [C#](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRNN.cs)
5053
* MNIST LSTM [C#](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionLSTM.cs)
51-
* Named Entity Recognition [C#](src/TensorFlowNET.Examples/TextProcessing/NER)
54+
* Image Classification in Keras Sequential API [C#](src/TensorFlowNET.Examples/ImageProcessing/ImageClassificationKeras.cs)
55+
* Toy ResNet in Keras Functional API [C#](src/TensorFlowNET.Examples/ImageProcessing/ToyResNet.cs)
5256
* Transfer Learning for Image Classification in InceptionV3 [C#](src/TensorFlowNET.Examples/ImageProcessing/TransferLearningWithInceptionV3.cs)
5357
* CNN In Your Own Dataset [C#](src/TensorFlowNET.Examples/ImageProcessing/CnnInYourOwnData.cs)
5458

59+
#### Natural Language Processing
60+
* Binary Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
61+
* CNN Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
62+
* Named Entity Recognition [C#](src/TensorFlowNET.Examples/TextProcessing/NER)
63+
5564

56-
### Welcome to PR your example to us.
65+
#### Welcome to PR your example to us.
5766
Your contribution will make .NET community better than ever.
5867
<br>
5968
<a href="http://scisharpstack.org"><img src="https://github.com/SciSharp/SciSharp/blob/master/art/scisharp-stack.png" width="391" height="100" /></a>

src/TensorFlowNET.Examples/ImageProcessing/ImageClassificationKeras.cs

+65-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
using Tensorflow.Keras;
44
using static Tensorflow.Binding;
55
using static Tensorflow.KerasApi;
6+
using System.Linq;
7+
using Tensorflow.Keras.Utils;
8+
using System.IO;
9+
using Tensorflow.Keras.Engine;
610

711
namespace TensorFlowNET.Examples
812
{
@@ -12,67 +16,100 @@ namespace TensorFlowNET.Examples
1216
/// </summary>
1317
public class ImageClassificationKeras : SciSharpExample, IExample
1418
{
19+
int batch_size = 32;
20+
int epochs = 10;
21+
TensorShape img_dim = (180, 180);
22+
IDatasetV2 train_ds, val_ds;
23+
Model model;
24+
1525
public ExampleConfig InitConfig()
1626
=> Config = new ExampleConfig
1727
{
1828
Name = "Image Classification (Keras)",
19-
Enabled = false,
29+
Enabled = true,
2030
Priority = 18
2131
};
2232

2333
public bool Run()
2434
{
35+
tf.enable_eager_execution();
36+
2537
PrepareData();
38+
BuildModel();
39+
Train();
40+
2641
return true;
2742
}
2843

44+
public override void BuildModel()
45+
{
46+
int num_classes = 5;
47+
// var normalization_layer = tf.keras.layers.Rescaling(1.0f / 255);
48+
var layers = keras.layers;
49+
model = keras.Sequential(new List<ILayer>
50+
{
51+
layers.Rescaling(1.0f / 255, input_shape: (img_dim.dims[0], img_dim.dims[1], 3)),
52+
layers.Conv2D(16, 3, padding: "same", activation: keras.activations.Relu),
53+
layers.MaxPooling2D(),
54+
/*layers.Conv2D(32, 3, padding: "same", activation: "relu"),
55+
layers.MaxPooling2D(),
56+
layers.Conv2D(64, 3, padding: "same", activation: "relu"),
57+
layers.MaxPooling2D(),*/
58+
layers.Flatten(),
59+
layers.Dense(128, activation: keras.activations.Relu),
60+
layers.Dense(num_classes)
61+
});
62+
63+
model.compile(optimizer: keras.optimizers.Adam(),
64+
loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
65+
metrics: new[] { "accuracy" });
66+
67+
model.summary();
68+
}
69+
70+
public override void Train()
71+
{
72+
model.fit(train_ds, validation_data: val_ds, epochs: epochs);
73+
}
74+
2975
public override void PrepareData()
3076
{
31-
int batch_size = 32;
32-
TensorShape img_dim = (180, 180);
77+
string fileName = "flower_photos.tgz";
78+
string url = $"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz";
79+
string data_dir = Path.GetTempPath();
80+
Web.Download(url, data_dir, fileName);
81+
Compress.ExtractTGZ(Path.Join(data_dir, fileName), data_dir);
82+
data_dir = Path.Combine(data_dir, "flower_photos");
3383

34-
var data_dir = @"C:/Users/haipi/.keras/datasets/flower_photos";
35-
var train_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
84+
// convert to tensor
85+
train_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
3686
validation_split: 0.2f,
3787
subset: "training",
3888
seed: 123,
3989
image_size: img_dim,
4090
batch_size: batch_size);
4191

42-
var val_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
92+
val_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
4393
validation_split: 0.2f,
4494
subset: "validation",
4595
seed: 123,
4696
image_size: img_dim,
4797
batch_size: batch_size);
4898

49-
train_ds = train_ds.cache().shuffle(100).prefetch(buffer_size: -1);
99+
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size: -1);
50100
val_ds = val_ds.cache().prefetch(buffer_size: -1);
51101

52102
foreach (var (img, label) in train_ds)
53103
{
54-
print("batch images: " + img.TensorShape);
55-
print("labels: " + label);
104+
print($"images: {img.TensorShape}");
105+
var nd = label.numpy();
106+
print($"labels: {nd}");
107+
var data = nd.Data<int>();
108+
if (data.Max() > 4 || data.Min() < 0)
109+
{
110+
// exception
111+
}
56112
}
57-
58-
int num_classes = 5;
59-
// var normalization_layer = tf.keras.layers.Rescaling(1.0f / 255);
60-
var layers = keras.layers;
61-
var model = keras.Sequential(new List<ILayer>
62-
{
63-
layers.Rescaling(1.0f / 255, input_shape: (img_dim.dims[0], img_dim.dims[1], 3)),
64-
layers.Conv2D(16, 3, padding: "same", activation: keras.activations.Relu),
65-
layers.MaxPooling2D(),
66-
/*layers.Conv2D(32, 3, padding: "same", activation: "relu"),
67-
layers.MaxPooling2D(),
68-
layers.Conv2D(64, 3, padding: "same", activation: "relu"),
69-
layers.MaxPooling2D(),*/
70-
layers.Flatten(),
71-
layers.Dense(128, activation: keras.activations.Relu),
72-
layers.Dense(num_classes)
73-
});
74-
75-
model.compile("adam", keras.losses.SparseCategoricalCrossentropy(from_logits: true));
76113
}
77114
}
78115
}

src/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1111
<DefineConstants>DEBUG;TRACE</DefineConstants>
12-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
12+
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
1313
<PlatformTarget>AnyCPU</PlatformTarget>
1414
</PropertyGroup>
1515

@@ -38,13 +38,12 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41-
<PackageReference Include="Colorful.Console" Version="1.2.11" />
41+
<PackageReference Include="Colorful.Console" Version="1.2.15" />
4242
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
4343
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.4.0.20200915" />
4444
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
4545
<PackageReference Include="SharpCV" Version="0.6.0" />
46-
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
47-
<PackageReference Include="TensorFlow.Keras" Version="0.2.1" />
46+
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
4847
</ItemGroup>
4948

5049
<ItemGroup>

src/TensorFlowNET.Examples/TextProcessing/Word2Vec.cs

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public ExampleConfig InitConfig()
5050

5151
public bool Run()
5252
{
53+
tf.compat.v1.disable_eager_execution();
54+
5355
PrepareData();
5456

5557
var graph = tf.Graph().as_default();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/image_classification.py",
12+
"console": "integratedTerminal",
13+
"cwd": "${workspaceFolder}",
14+
"justMyCode": false
15+
}
16+
]
17+
}

src/tensorflow2.x-python-tutorial/MapDataset.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
# Import TensorFlow v2.
44
import tensorflow as tf
5+
from tensorflow.python.data.experimental.ops import cardinality
56

67
tf.autograph.set_verbosity(10, alsologtostdout=True)
8+
tf.config.run_functions_eagerly(True)
79

810
def add(x):
911
print("debug test test")
1012
return x + 1
1113

12-
dataset = tf.data.Dataset.range(1, 3) # ==> [ 1, 2, 3, 4, 5 ]
14+
dataset = tf.data.Dataset.range(10) # ==> [ 1, 2, 3, 4, 5 ]
1315
dataset = dataset.map(add)
16+
card = cardinality.cardinality(dataset)
1417
for item in dataset:
15-
print(item)
18+
print(item)

src/tensorflow2.x-python-tutorial/image_classification.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
from tensorflow.keras.models import Sequential
1313
from tensorflow.python.ops import bitwise_ops
1414

15+
"""
16+
y_true = np.array([[0., 1.], [0., 0.]],dtype='float64')
17+
y_pred = np.array([[1., 1.], [1., 0.]],dtype='float64')
18+
mse = tf.keras.losses.MeanSquaredError()
19+
print(mse(y_true, y_pred).numpy())
20+
"""
21+
1522
import pathlib
1623
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
1724
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
@@ -24,14 +31,21 @@
2431
img_height = 180
2532
img_width = 180
2633

34+
tf.config.run_functions_eagerly(True)
35+
2736
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
2837
data_dir,
2938
validation_split=0.2,
3039
subset="training",
3140
seed=123,
41+
shuffle=False,
3242
image_size=(img_height, img_width),
3343
batch_size=batch_size)
3444

45+
for img, label in train_ds:
46+
print("batch images: ", img.shape)
47+
print("labels: ", label)
48+
3549
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
3650
data_dir,
3751
validation_split=0.2,
@@ -47,7 +61,7 @@
4761

4862
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
4963
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
50-
64+
5165
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
5266

5367
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
@@ -82,8 +96,7 @@
8296
history = model.fit(
8397
train_ds,
8498
validation_data=val_ds,
85-
epochs=epochs
86-
)
99+
epochs=epochs)
87100

88101
acc = history.history['accuracy']
89102
val_acc = history.history['val_accuracy']
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
import tensorflow as tf
3+
import tensorflow_text as text
4+
5+
docs = tf.constant([u'Everything not saved will be lost.'])
6+
tokenizer = text.WhitespaceTokenizer()
7+
tokens = tokenizer.tokenize(docs)
8+
f1 = text.wordshape(tokens, text.WordShape.HAS_TITLE_CASE)
9+
bigrams = text.ngrams(tokens, 2, reduction_type=text.Reduction.STRING_JOIN)
10+
11+
print(docs)
12+
a = input()

0 commit comments

Comments
 (0)