3
3
using Tensorflow . Keras ;
4
4
using static Tensorflow . Binding ;
5
5
using static Tensorflow . KerasApi ;
6
+ using System . Linq ;
7
+ using Tensorflow . Keras . Utils ;
8
+ using System . IO ;
9
+ using Tensorflow . Keras . Engine ;
6
10
7
11
namespace TensorFlowNET . Examples
8
12
{
@@ -12,67 +16,100 @@ namespace TensorFlowNET.Examples
12
16
/// </summary>
13
17
public class ImageClassificationKeras : SciSharpExample , IExample
14
18
{
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
+
15
25
public ExampleConfig InitConfig ( )
16
26
=> Config = new ExampleConfig
17
27
{
18
28
Name = "Image Classification (Keras)" ,
19
- Enabled = false ,
29
+ Enabled = true ,
20
30
Priority = 18
21
31
} ;
22
32
23
33
public bool Run ( )
24
34
{
35
+ tf . enable_eager_execution ( ) ;
36
+
25
37
PrepareData ( ) ;
38
+ BuildModel ( ) ;
39
+ Train ( ) ;
40
+
26
41
return true ;
27
42
}
28
43
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
+
29
75
public override void PrepareData ( )
30
76
{
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" ) ;
33
83
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 ,
36
86
validation_split : 0.2f ,
37
87
subset : "training" ,
38
88
seed : 123 ,
39
89
image_size : img_dim ,
40
90
batch_size : batch_size ) ;
41
91
42
- var val_ds = keras . preprocessing . image_dataset_from_directory ( data_dir ,
92
+ val_ds = keras . preprocessing . image_dataset_from_directory ( data_dir ,
43
93
validation_split : 0.2f ,
44
94
subset : "validation" ,
45
95
seed : 123 ,
46
96
image_size : img_dim ,
47
97
batch_size : batch_size ) ;
48
98
49
- train_ds = train_ds . cache ( ) . shuffle ( 100 ) . prefetch ( buffer_size : - 1 ) ;
99
+ train_ds = train_ds . cache ( ) . shuffle ( 1000 ) . prefetch ( buffer_size : - 1 ) ;
50
100
val_ds = val_ds . cache ( ) . prefetch ( buffer_size : - 1 ) ;
51
101
52
102
foreach ( var ( img , label ) in train_ds )
53
103
{
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
+ }
56
112
}
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 ) ) ;
76
113
}
77
114
}
78
115
}
0 commit comments