33using Tensorflow . Keras ;
44using static Tensorflow . Binding ;
55using static Tensorflow . KerasApi ;
6+ using System . Linq ;
7+ using Tensorflow . Keras . Utils ;
8+ using System . IO ;
9+ using Tensorflow . Keras . Engine ;
610
711namespace 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}
0 commit comments