forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetrainImageClassifier.cs
789 lines (681 loc) · 36.9 KB
/
RetrainImageClassifier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using Google.Protobuf;
using NumSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using static Tensorflow.Python;
namespace TensorFlowNET.Examples
{
/// <summary>
/// In this tutorial, we will reuse the feature extraction capabilities from powerful image classifiers trained on ImageNet
/// and simply train a new classification layer on top. Transfer learning is a technique that shortcuts much of this
/// by taking a piece of a model that has already been trained on a related task and reusing it in a new model.
///
/// https://www.tensorflow.org/hub/tutorials/image_retraining
/// </summary>
public class RetrainImageClassifier : IExample
{
public int Priority => 16;
public bool Enabled { get; set; } = true;
public bool IsImportingGraph { get; set; } = true;
public string Name => "Retrain Image Classifier";
const string data_dir = "retrain_images";
string summaries_dir = Path.Join(data_dir, "retrain_logs");
string image_dir = Path.Join(data_dir, "flower_photos");
string bottleneck_dir = Path.Join(data_dir, "bottleneck");
string output_graph = Path.Join(data_dir, "output_graph.pb");
string output_labels = Path.Join(data_dir, "output_labels.txt");
// The location where variable checkpoints will be stored.
string CHECKPOINT_NAME = Path.Join(data_dir, "_retrain_checkpoint");
string tfhub_module = "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/3";
string input_tensor_name = "Placeholder";
string final_tensor_name = "Score";
float testing_percentage = 0.1f;
float validation_percentage = 0.1f;
float learning_rate = 0.01f;
Tensor resized_image_tensor;
Dictionary<string, Dictionary<string, string[]>> image_lists;
int how_many_training_steps = 100;
int eval_step_interval = 10;
int train_batch_size = 100;
int test_batch_size = -1;
int validation_batch_size = 100;
int intermediate_store_frequency = 0;
int class_count = 0;
const int MAX_NUM_IMAGES_PER_CLASS = 134217727;
Operation train_step;
Tensor final_tensor;
Tensor bottleneck_input;
Tensor cross_entropy;
Tensor ground_truth_input;
Tensor bottleneck_tensor;
bool wants_quantization;
float test_accuracy;
NDArray predictions;
public bool Run()
{
PrepareData();
#region For debug purpose
// predict images
// Predict(null);
// load saved pb and test new images.
// Test(null);
#endregion
var graph = IsImportingGraph ? ImportGraph() : BuildGraph();
using (var sess = tf.Session(graph))
{
Train(sess);
}
return test_accuracy > 0.75f;
}
/// <summary>
/// Runs a final evaluation on an eval graph using the test data set.
/// </summary>
/// <param name="train_session"></param>
/// <param name="module_spec"></param>
/// <param name="class_count"></param>
/// <param name="image_lists"></param>
/// <param name="jpeg_data_tensor"></param>
/// <param name="decoded_image_tensor"></param>
/// <param name="resized_image_tensor"></param>
/// <param name="bottleneck_tensor"></param>
private (float, NDArray) run_final_eval(Session train_session, object module_spec, int class_count,
Dictionary<string, Dictionary<string, string[]>> image_lists,
Tensor jpeg_data_tensor, Tensor decoded_image_tensor,
Tensor resized_image_tensor, Tensor bottleneck_tensor)
{
var (test_bottlenecks, test_ground_truth, test_filenames) = get_random_cached_bottlenecks(train_session, image_lists,
test_batch_size, "testing", bottleneck_dir, image_dir, jpeg_data_tensor,
decoded_image_tensor, resized_image_tensor, bottleneck_tensor, tfhub_module);
var (eval_session, _, bottleneck_input, ground_truth_input, evaluation_step,
prediction) = build_eval_session(class_count);
var results = eval_session.run(new Tensor[] { evaluation_step, prediction },
new FeedItem(bottleneck_input, test_bottlenecks),
new FeedItem(ground_truth_input, test_ground_truth));
print($"final test accuracy: {((float)results[0] * 100).ToString("G4")}% (N={len(test_bottlenecks)})");
return (results[0], results[1]);
}
private (Session, Tensor, Tensor, Tensor, Tensor, Tensor)
build_eval_session(int class_count)
{
// If quantized, we need to create the correct eval graph for exporting.
var (eval_graph, bottleneck_tensor, resized_input_tensor, wants_quantization) = create_module_graph();
var eval_sess = tf.Session(graph: eval_graph);
Tensor evaluation_step = null;
Tensor prediction = null;
var graph = eval_graph.as_default();
// Add the new layer for exporting.
var (_, _, bottleneck_input, ground_truth_input, final_tensor) =
add_final_retrain_ops(class_count, final_tensor_name, bottleneck_tensor,
wants_quantization, is_training: false);
// Now we need to restore the values from the training graph to the eval
// graph.
tf.train.Saver().restore(eval_sess, CHECKPOINT_NAME);
(evaluation_step, prediction) = add_evaluation_step(final_tensor,
ground_truth_input);
return (eval_sess, resized_input_tensor, bottleneck_input, ground_truth_input,
evaluation_step, prediction);
}
/// <summary>
/// Adds a new softmax and fully-connected layer for training and eval.
///
/// We need to retrain the top layer to identify our new classes, so this function
/// adds the right operations to the graph, along with some variables to hold the
/// weights, and then sets up all the gradients for the backward pass.
///
/// The set up for the softmax and fully-connected layers is based on:
/// https://www.tensorflow.org/tutorials/mnist/beginners/index.html
/// </summary>
/// <param name="class_count"></param>
/// <param name="final_tensor_name"></param>
/// <param name="bottleneck_tensor"></param>
/// <param name="quantize_layer"></param>
/// <param name="is_training"></param>
/// <returns></returns>
private (Operation, Tensor, Tensor, Tensor, Tensor) add_final_retrain_ops(int class_count, string final_tensor_name,
Tensor bottleneck_tensor, bool quantize_layer, bool is_training)
{
var (batch_size, bottleneck_tensor_size) = (bottleneck_tensor.TensorShape.Dimensions[0], bottleneck_tensor.TensorShape.Dimensions[1]);
tf_with(tf.name_scope("input"), scope =>
{
bottleneck_input = tf.placeholder_with_default(
bottleneck_tensor,
shape: bottleneck_tensor.TensorShape.Dimensions,
name: "BottleneckInputPlaceholder");
ground_truth_input = tf.placeholder(tf.int64, new TensorShape(batch_size), name: "GroundTruthInput");
});
// Organizing the following ops so they are easier to see in TensorBoard.
string layer_name = "final_retrain_ops";
Tensor logits = null;
tf_with(tf.name_scope(layer_name), scope =>
{
RefVariable layer_weights = null;
tf_with(tf.name_scope("weights"), delegate
{
var initial_value = tf.truncated_normal(new int[] { bottleneck_tensor_size, class_count }, stddev: 0.001f);
layer_weights = tf.Variable(initial_value, name: "final_weights");
variable_summaries(layer_weights);
});
RefVariable layer_biases = null;
tf_with(tf.name_scope("biases"), delegate
{
layer_biases = tf.Variable(tf.zeros(class_count), name: "final_biases");
variable_summaries(layer_biases);
});
tf_with(tf.name_scope("Wx_plus_b"), delegate
{
logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases;
tf.summary.histogram("pre_activations", logits);
});
});
final_tensor = tf.nn.softmax(logits, name: final_tensor_name);
// The tf.contrib.quantize functions rewrite the graph in place for
// quantization. The imported model graph has already been rewritten, so upon
// calling these rewrites, only the newly added final layer will be
// transformed.
if (quantize_layer)
{
throw new NotImplementedException("quantize_layer");
/*if (is_training)
tf.contrib.quantize.create_training_graph();
else
tf.contrib.quantize.create_eval_graph();*/
}
tf.summary.histogram("activations", final_tensor);
// If this is an eval graph, we don't need to add loss ops or an optimizer.
if (!is_training)
return (null, null, bottleneck_input, ground_truth_input, final_tensor);
Tensor cross_entropy_mean = null;
tf_with(tf.name_scope("cross_entropy"), delegate
{
cross_entropy_mean = tf.losses.sparse_softmax_cross_entropy(
labels: ground_truth_input, logits: logits);
});
tf.summary.scalar("cross_entropy", cross_entropy_mean);
tf_with(tf.name_scope("train"), delegate
{
var optimizer = tf.train.GradientDescentOptimizer(learning_rate);
train_step = optimizer.minimize(cross_entropy_mean);
});
return (train_step, cross_entropy_mean, bottleneck_input, ground_truth_input,
final_tensor);
}
private void variable_summaries(RefVariable var)
{
tf_with(tf.name_scope("summaries"), delegate
{
var mean = tf.reduce_mean(var);
tf.summary.scalar("mean", mean);
Tensor stddev = null;
tf_with(tf.name_scope("stddev"), delegate
{
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)));
});
tf.summary.scalar("stddev", stddev);
tf.summary.scalar("max", tf.reduce_max(var));
tf.summary.scalar("min", tf.reduce_min(var));
tf.summary.histogram("histogram", var);
});
}
private (Graph, Tensor, Tensor, bool) create_module_graph()
{
var (height, width) = (299, 299);
var graph = tf.Graph().as_default();
tf.train.import_meta_graph("graph/InceptionV3.meta");
Tensor resized_input_tensor = graph.OperationByName(input_tensor_name); //tf.placeholder(tf.float32, new TensorShape(-1, height, width, 3));
// var m = hub.Module(module_spec);
Tensor bottleneck_tensor = graph.OperationByName("module_apply_default/hub_output/feature_vector/SpatialSqueeze");// m(resized_input_tensor);
var wants_quantization = false;
return (graph, bottleneck_tensor, resized_input_tensor, wants_quantization);
}
private (NDArray, long[], string[]) get_random_cached_bottlenecks(Session sess, Dictionary<string, Dictionary<string, string[]>> image_lists,
int how_many, string category, string bottleneck_dir, string image_dir,
Tensor jpeg_data_tensor, Tensor decoded_image_tensor, Tensor resized_input_tensor,
Tensor bottleneck_tensor, string module_name)
{
var bottlenecks = new List<float[]>();
var ground_truths = new List<long>();
var filenames = new List<string>();
class_count = image_lists.Keys.Count;
if (how_many >= 0)
{
// Retrieve a random sample of bottlenecks.
foreach (var unused_i in range(how_many))
{
int label_index = new Random().Next(class_count);
string label_name = image_lists.Keys.ToArray()[label_index];
int image_index = new Random().Next(MAX_NUM_IMAGES_PER_CLASS);
string image_name = get_image_path(image_lists, label_name, image_index,
image_dir, category);
var bottleneck = get_or_create_bottleneck(
sess, image_lists, label_name, image_index, image_dir, category,
bottleneck_dir, jpeg_data_tensor, decoded_image_tensor,
resized_input_tensor, bottleneck_tensor, module_name);
bottlenecks.Add(bottleneck);
ground_truths.Add(label_index);
filenames.Add(image_name);
}
}
else
{
// Retrieve all bottlenecks.
foreach (var (label_index, label_name) in enumerate(image_lists.Keys.ToArray()))
{
foreach (var (image_index, image_name) in enumerate(image_lists[label_name][category]))
{
var bottleneck = get_or_create_bottleneck(
sess, image_lists, label_name, image_index, image_dir, category,
bottleneck_dir, jpeg_data_tensor, decoded_image_tensor,
resized_input_tensor, bottleneck_tensor, module_name);
bottlenecks.Add(bottleneck);
ground_truths.Add(label_index);
filenames.Add(image_name);
}
}
}
return (bottlenecks.ToArray(), ground_truths.ToArray(), filenames.ToArray());
}
/// <summary>
/// Inserts the operations we need to evaluate the accuracy of our results.
/// </summary>
/// <param name="result_tensor"></param>
/// <param name="ground_truth_tensor"></param>
/// <returns></returns>
private (Tensor, Tensor) add_evaluation_step(Tensor result_tensor, Tensor ground_truth_tensor)
{
Tensor evaluation_step = null, correct_prediction = null, prediction = null;
tf_with(tf.name_scope("accuracy"), scope =>
{
tf_with(tf.name_scope("correct_prediction"), delegate
{
prediction = tf.argmax(result_tensor, 1);
correct_prediction = tf.equal(prediction, ground_truth_tensor);
});
tf_with(tf.name_scope("accuracy"), delegate
{
evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32));
});
});
tf.summary.scalar("accuracy", evaluation_step);
return (evaluation_step, prediction);
}
/// <summary>
/// Ensures all the training, testing, and validation bottlenecks are cached.
/// </summary>
/// <param name="sess"></param>
/// <param name="image_lists"></param>
/// <param name="image_dir"></param>
/// <param name="bottleneck_dir"></param>
/// <param name="jpeg_data_tensor"></param>
/// <param name="decoded_image_tensor"></param>
/// <param name="resized_image_tensor"></param>
/// <param name="bottleneck_tensor"></param>
/// <param name="tfhub_module"></param>
private void cache_bottlenecks(Session sess, Dictionary<string, Dictionary<string, string[]>> image_lists,
string image_dir, string bottleneck_dir, Tensor jpeg_data_tensor, Tensor decoded_image_tensor,
Tensor resized_input_tensor, Tensor bottleneck_tensor, string module_name)
{
int how_many_bottlenecks = 0;
foreach (var (label_name, label_lists) in image_lists)
{
foreach (var category in new string[] { "training", "testing", "validation" })
{
var category_list = label_lists[category];
foreach (var (index, unused_base_name) in enumerate(category_list))
{
get_or_create_bottleneck(sess, image_lists, label_name, index, image_dir, category,
bottleneck_dir, jpeg_data_tensor, decoded_image_tensor,
resized_input_tensor, bottleneck_tensor, module_name);
how_many_bottlenecks++;
if (how_many_bottlenecks % 300 == 0)
print($"{how_many_bottlenecks} bottleneck files created.");
}
}
}
}
private float[] get_or_create_bottleneck(Session sess, Dictionary<string, Dictionary<string, string[]>> image_lists,
string label_name, int index, string image_dir, string category, string bottleneck_dir,
Tensor jpeg_data_tensor, Tensor decoded_image_tensor, Tensor resized_input_tensor,
Tensor bottleneck_tensor, string module_name)
{
var label_lists = image_lists[label_name];
var sub_dir_path = Path.Join(bottleneck_dir, label_name);
Directory.CreateDirectory(sub_dir_path);
string bottleneck_path = get_bottleneck_path(image_lists, label_name, index,
bottleneck_dir, category, module_name);
if (!File.Exists(bottleneck_path))
create_bottleneck_file(bottleneck_path, image_lists, label_name, index,
image_dir, category, sess, jpeg_data_tensor,
decoded_image_tensor, resized_input_tensor,
bottleneck_tensor);
var bottleneck_string = File.ReadAllText(bottleneck_path);
var bottleneck_values = Array.ConvertAll(bottleneck_string.Split(','), x => float.Parse(x));
return bottleneck_values;
}
private void create_bottleneck_file(string bottleneck_path, Dictionary<string, Dictionary<string, string[]>> image_lists,
string label_name, int index, string image_dir, string category, Session sess,
Tensor jpeg_data_tensor, Tensor decoded_image_tensor, Tensor resized_input_tensor, Tensor bottleneck_tensor)
{
// Create a single bottleneck file.
print("Creating bottleneck at " + bottleneck_path);
var image_path = get_image_path(image_lists, label_name, index, image_dir, category);
if (!File.Exists(image_path))
print($"File does not exist {image_path}");
var image_data = File.ReadAllBytes(image_path);
var bottleneck_values = run_bottleneck_on_image(
sess, image_data, jpeg_data_tensor, decoded_image_tensor,
resized_input_tensor, bottleneck_tensor);
var values = bottleneck_values.Data<float>();
var bottleneck_string = string.Join(",", values);
File.WriteAllText(bottleneck_path, bottleneck_string);
}
/// <summary>
/// Runs inference on an image to extract the 'bottleneck' summary layer.
/// </summary>
/// <param name="sess">Current active TensorFlow Session.</param>
/// <param name="image_data">Data of raw JPEG data.</param>
/// <param name="image_data_tensor">Input data layer in the graph.</param>
/// <param name="decoded_image_tensor">Output of initial image resizing and preprocessing.</param>
/// <param name="resized_input_tensor">The input node of the recognition graph.</param>
/// <param name="bottleneck_tensor">Layer before the final softmax.</param>
/// <returns></returns>
private NDArray run_bottleneck_on_image(Session sess, byte[] image_data, Tensor image_data_tensor,
Tensor decoded_image_tensor, Tensor resized_input_tensor, Tensor bottleneck_tensor)
{
// First decode the JPEG image, resize it, and rescale the pixel values.
var resized_input_values = sess.run(decoded_image_tensor, new FeedItem(image_data_tensor, new Tensor(image_data, TF_DataType.TF_STRING)));
// Then run it through the recognition network.
var bottleneck_values = sess.run(bottleneck_tensor, new FeedItem(resized_input_tensor, resized_input_values));
bottleneck_values = np.squeeze(bottleneck_values);
return bottleneck_values;
}
private string get_bottleneck_path(Dictionary<string, Dictionary<string, string[]>> image_lists, string label_name, int index,
string bottleneck_dir, string category, string module_name)
{
module_name = (module_name.Replace("://", "~") // URL scheme.
.Replace('/', '~') // URL and Unix paths.
.Replace(':', '~').Replace('\\', '~')); // Windows paths.
return get_image_path(image_lists, label_name, index, bottleneck_dir,
category) + "_" + module_name + ".txt";
}
private string get_image_path(Dictionary<string, Dictionary<string, string[]>> image_lists, string label_name,
int index, string image_dir, string category)
{
if (!image_lists.ContainsKey(label_name))
print($"Label does not exist {label_name}");
var label_lists = image_lists[label_name];
if (!label_lists.ContainsKey(category))
print($"Category does not exist {category}");
var category_list = label_lists[category];
if (category_list.Length == 0)
print($"Label {label_name} has no images in the category {category}.");
var mod_index = index % len(category_list);
var base_name = category_list[mod_index].Split(Path.DirectorySeparatorChar).Last();
var sub_dir = label_name;
var full_path = Path.Join(image_dir, sub_dir, base_name);
return full_path;
}
/// <summary>
/// Saves an graph to file, creating a valid quantized one if necessary.
/// </summary>
/// <param name="graph_file_name"></param>
/// <param name="class_count"></param>
private void save_graph_to_file(string graph_file_name, int class_count)
{
var (sess, _, _, _, _, _) = build_eval_session(class_count);
var graph = sess.graph;
var output_graph_def = tf.graph_util.convert_variables_to_constants(
sess, graph.as_graph_def(), new string[] { final_tensor_name });
File.WriteAllBytes(graph_file_name, output_graph_def.ToByteArray());
}
public void PrepareData()
{
// get a set of images to teach the network about the new classes
string fileName = "flower_photos.tgz";
string url = $"http://download.tensorflow.org/example_images/{fileName}";
Web.Download(url, data_dir, fileName);
Compress.ExtractTGZ(Path.Join(data_dir, fileName), data_dir);
// download graph meta data
url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/InceptionV3.meta";
Web.Download(url, "graph", "InceptionV3.meta");
// download variables.data checkpoint file.
url = "https://github.com/SciSharp/TensorFlow.NET/raw/master/data/tfhub_modules.zip";
Web.Download(url, data_dir, "tfhub_modules.zip");
Compress.UnZip(Path.Join(data_dir, "tfhub_modules.zip"), "tfhub_modules");
// Prepare necessary directories that can be used during training
Directory.CreateDirectory(summaries_dir);
Directory.CreateDirectory(bottleneck_dir);
// Look at the folder structure, and create lists of all the images.
image_lists = create_image_lists();
class_count = len(image_lists);
if (class_count == 0)
print($"No valid folders of images found at {image_dir}");
if (class_count == 1)
print("Only one valid folder of images found at " +
image_dir +
" - multiple classes are needed for classification.");
}
private (Tensor, Tensor) add_jpeg_decoding()
{
// height, width, depth
var input_dim = (299, 299, 3);
var jpeg_data = tf.placeholder(tf.@string, name: "DecodeJPGInput");
var decoded_image = tf.image.decode_jpeg(jpeg_data, channels: input_dim.Item3);
// Convert from full range of uint8 to range [0,1] of float32.
var decoded_image_as_float = tf.image.convert_image_dtype(decoded_image, tf.float32);
var decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0);
var resize_shape = tf.stack(new int[] { input_dim.Item1, input_dim.Item2 });
var resize_shape_as_int = tf.cast(resize_shape, dtype: tf.int32);
var resized_image = tf.image.resize_bilinear(decoded_image_4d, resize_shape_as_int);
return (jpeg_data, resized_image);
}
/// <summary>
/// Builds a list of training images from the file system.
/// </summary>
private Dictionary<string, Dictionary<string, string[]>> create_image_lists()
{
var sub_dirs = tf.gfile.Walk(image_dir)
.Select(x => x.Item1)
.OrderBy(x => x)
.ToArray();
var result = new Dictionary<string, Dictionary<string, string[]>>();
foreach (var sub_dir in sub_dirs)
{
var dir_name = sub_dir.Split(Path.DirectorySeparatorChar).Last();
print($"Looking for images in '{dir_name}'");
var file_list = Directory.GetFiles(sub_dir);
if (len(file_list) < 20)
print($"WARNING: Folder has less than 20 images, which may cause issues.");
var label_name = dir_name.ToLower();
result[label_name] = new Dictionary<string, string[]>();
int testing_count = (int)Math.Floor(file_list.Length * testing_percentage);
int validation_count = (int)Math.Floor(file_list.Length * validation_percentage);
result[label_name]["testing"] = file_list.Take(testing_count).ToArray();
result[label_name]["validation"] = file_list.Skip(testing_count).Take(validation_count).ToArray();
result[label_name]["training"] = file_list.Skip(testing_count + validation_count).ToArray();
}
return result;
}
public Graph ImportGraph()
{
Graph graph;
// Set up the pre-trained graph.
(graph, bottleneck_tensor, resized_image_tensor, wants_quantization) =
create_module_graph();
// Add the new layer that we'll be training.
(train_step, cross_entropy, bottleneck_input,
ground_truth_input, final_tensor) = add_final_retrain_ops(
class_count, final_tensor_name, bottleneck_tensor,
wants_quantization, is_training: true);
return graph;
}
public Graph BuildGraph()
{
throw new NotImplementedException();
}
public void Train(Session sess)
{
var sw = new Stopwatch();
// Initialize all weights: for the module to their pretrained values,
// and for the newly added retraining layer to random initial values.
var init = tf.global_variables_initializer();
sess.run(init);
var (jpeg_data_tensor, decoded_image_tensor) = add_jpeg_decoding();
// We'll make sure we've calculated the 'bottleneck' image summaries and
// cached them on disk.
cache_bottlenecks(sess, image_lists, image_dir,
bottleneck_dir, jpeg_data_tensor,
decoded_image_tensor, resized_image_tensor,
bottleneck_tensor, tfhub_module);
// Create the operations we need to evaluate the accuracy of our new layer.
var (evaluation_step, _) = add_evaluation_step(final_tensor, ground_truth_input);
// Merge all the summaries and write them out to the summaries_dir
var merged = tf.summary.merge_all();
var train_writer = tf.summary.FileWriter(summaries_dir + "/train", sess.graph);
var validation_writer = tf.summary.FileWriter(summaries_dir + "/validation", sess.graph);
// Create a train saver that is used to restore values into an eval graph
// when exporting models.
var train_saver = tf.train.Saver();
train_saver.save(sess, CHECKPOINT_NAME);
sw.Restart();
for (int i = 0; i < how_many_training_steps; i++)
{
var (train_bottlenecks, train_ground_truth, _) = get_random_cached_bottlenecks(
sess, image_lists, train_batch_size, "training",
bottleneck_dir, image_dir, jpeg_data_tensor,
decoded_image_tensor, resized_image_tensor, bottleneck_tensor,
tfhub_module);
// Feed the bottlenecks and ground truth into the graph, and run a training
// step. Capture training summaries for TensorBoard with the `merged` op.
var results = sess.run(
new ITensorOrOperation[] { merged, train_step },
new FeedItem(bottleneck_input, train_bottlenecks),
new FeedItem(ground_truth_input, train_ground_truth));
var train_summary = results[0];
// TODO
train_writer.add_summary(train_summary, i);
// Every so often, print out how well the graph is training.
bool is_last_step = (i + 1 == how_many_training_steps);
if ((i % eval_step_interval) == 0 || is_last_step)
{
results = sess.run(
new Tensor[] { evaluation_step, cross_entropy },
new FeedItem(bottleneck_input, train_bottlenecks),
new FeedItem(ground_truth_input, train_ground_truth));
(float train_accuracy, float cross_entropy_value) = (results[0], results[1]);
print($"{DateTime.Now}: Step {i + 1}: Train accuracy = {train_accuracy * 100}%, Cross entropy = {cross_entropy_value.ToString("G4")}");
var (validation_bottlenecks, validation_ground_truth, _) = get_random_cached_bottlenecks(
sess, image_lists, validation_batch_size, "validation",
bottleneck_dir, image_dir, jpeg_data_tensor,
decoded_image_tensor, resized_image_tensor, bottleneck_tensor,
tfhub_module);
// Run a validation step and capture training summaries for TensorBoard
// with the `merged` op.
results = sess.run(new Tensor[] { merged, evaluation_step },
new FeedItem(bottleneck_input, validation_bottlenecks),
new FeedItem(ground_truth_input, validation_ground_truth));
(string validation_summary, float validation_accuracy) = (results[0], results[1]);
validation_writer.add_summary(validation_summary, i);
print($"{DateTime.Now}: Step {i + 1}: Validation accuracy = {validation_accuracy * 100}% (N={len(validation_bottlenecks)}) {sw.ElapsedMilliseconds}ms");
sw.Restart();
}
// Store intermediate results
int intermediate_frequency = intermediate_store_frequency;
if (intermediate_frequency > 0 && i % intermediate_frequency == 0 && i > 0)
{
}
}
// After training is complete, force one last save of the train checkpoint.
train_saver.save(sess, CHECKPOINT_NAME);
// We've completed all our training, so run a final test evaluation on
// some new images we haven't used before.
(test_accuracy, predictions) = run_final_eval(sess, null, class_count, image_lists,
jpeg_data_tensor, decoded_image_tensor, resized_image_tensor,
bottleneck_tensor);
// Write out the trained graph and labels with the weights stored as
// constants.
print($"Save final result to : {output_graph}");
save_graph_to_file(output_graph, class_count);
File.WriteAllText(output_labels, string.Join("\n", image_lists.Keys));
}
/// <summary>
/// Prediction
/// labels mapping, it's from output_lables.txt
/// 0 - daisy
/// 1 - dandelion
/// 2 - roses
/// 3 - sunflowers
/// 4 - tulips
/// </summary>
/// <param name="sess_"></param>
public void Predict(Session sess_)
{
if (!File.Exists(output_graph))
return;
var labels = File.ReadAllLines(output_labels);
// predict image
var img_path = Path.Join(image_dir, "daisy", "5547758_eea9edfd54_n.jpg");
var fileBytes = ReadTensorFromImageFile(img_path);
// import graph and variables
var graph = new Graph();
graph.Import(output_graph, "");
Tensor input = graph.OperationByName(input_tensor_name);
Tensor output = graph.OperationByName(final_tensor_name);
using (var sess = tf.Session(graph))
{
var result = sess.run(output, new FeedItem(input, fileBytes));
var prob = np.squeeze(result);
var idx = np.argmax(prob);
print($"Prediction result: [{labels[idx]} {prob[idx][0]}] for {img_path}.");
}
}
private NDArray ReadTensorFromImageFile(string file_name,
int input_height = 299,
int input_width = 299,
int input_mean = 0,
int input_std = 255)
{
var graph = tf.Graph().as_default();
var file_reader = tf.read_file(file_name, "file_reader");
var image_reader = tf.image.decode_jpeg(file_reader, channels: 3, name: "jpeg_reader");
var caster = tf.cast(image_reader, tf.float32);
var dims_expander = tf.expand_dims(caster, 0);
var resize = tf.constant(new int[] { input_height, input_width });
var bilinear = tf.image.resize_bilinear(dims_expander, resize);
var sub = tf.subtract(bilinear, new float[] { input_mean });
var normalized = tf.divide(sub, new float[] { input_std });
using (var sess = tf.Session(graph))
return sess.run(normalized);
}
public void Test(Session sess_)
{
if (!File.Exists(output_graph))
return;
var graph = new Graph();
graph.Import(output_graph);
var (jpeg_data_tensor, decoded_image_tensor) = add_jpeg_decoding();
tf_with(tf.Session(graph), sess =>
{
(test_accuracy, predictions) = run_final_eval(sess, null, class_count, image_lists,
jpeg_data_tensor, decoded_image_tensor, resized_image_tensor,
bottleneck_tensor);
});
}
}
}