Skip to content

Commit 8347f61

Browse files
committed
RetrainImageClassifier.cs: Changed cache_bottlenecks to use Parallel.For
1 parent 33286b9 commit 8347f61

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

test/TensorFlowNET.Examples/ImageProcessing/RetrainImageClassifier.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
using System.Diagnostics;
2222
using System.IO;
2323
using System.Linq;
24+
using System.Threading.Tasks;
2425
using Tensorflow;
2526
using TensorFlowNET.Examples.Utility;
2627
using static Tensorflow.Binding;
@@ -381,10 +382,15 @@ private void cache_bottlenecks(Session sess, Dictionary<string, Dictionary<strin
381382
Tensor resized_input_tensor, Tensor bottleneck_tensor, string module_name)
382383
{
383384
int how_many_bottlenecks = 0;
384-
foreach (var (label_name, label_lists) in image_lists)
385+
var kvs = image_lists.ToArray();
386+
var categories = new string[] {"training", "testing", "validation"};
387+
Parallel.For(0, kvs.Length, i =>
385388
{
386-
foreach (var category in new string[] { "training", "testing", "validation" })
389+
var (label_name, label_lists) = kvs[i];
390+
391+
Parallel.For(0, categories.Length, j =>
387392
{
393+
var category = categories[j];
388394
var category_list = label_lists[category];
389395
foreach (var (index, unused_base_name) in enumerate(category_list))
390396
{
@@ -395,8 +401,8 @@ private void cache_bottlenecks(Session sess, Dictionary<string, Dictionary<strin
395401
if (how_many_bottlenecks % 300 == 0)
396402
print($"{how_many_bottlenecks} bottleneck files created.");
397403
}
398-
}
399-
}
404+
});
405+
});
400406
}
401407

402408
private float[] get_or_create_bottleneck(Session sess, Dictionary<string, Dictionary<string, string[]>> image_lists,

0 commit comments

Comments
 (0)