23
23
using static Microsoft . ML . DataOperationsCatalog ;
24
24
using Microsoft . ML . Trainers ;
25
25
using Microsoft . ML . TestFrameworkCommon . Attributes ;
26
+ using Microsoft . ML . Internal . Utilities ;
26
27
27
28
namespace Microsoft . ML . Scenarios
28
29
{
@@ -61,8 +62,34 @@ public void Dispose()
61
62
[ Collection ( "NoParallelization" ) ]
62
63
public sealed class TensorFlowScenariosTests : BaseTestClass , IClassFixture < TensorFlowScenariosTestsFixture >
63
64
{
65
+ private readonly string _fullImagesetFolderPath ;
66
+ private readonly string _finalImagesFolderName ;
67
+ private string _timeOutOldValue ;
68
+
64
69
public TensorFlowScenariosTests ( ITestOutputHelper output ) : base ( output )
65
70
{
71
+ string imagesDownloadFolderPath = Path . Combine ( TensorFlowScenariosTestsFixture . assetsPath , "inputs" ,
72
+ "images" ) ;
73
+
74
+ //Download the image set and unzip
75
+ _finalImagesFolderName = DownloadImageSet (
76
+ imagesDownloadFolderPath ) ;
77
+
78
+ _fullImagesetFolderPath = Path . Combine (
79
+ imagesDownloadFolderPath , _finalImagesFolderName ) ;
80
+ }
81
+
82
+ protected override void Initialize ( )
83
+ {
84
+ // set timeout to 3 minutes, download sometimes will stuck so set smaller timeout to fail fast and retry download
85
+ _timeOutOldValue = Environment . GetEnvironmentVariable ( ResourceManagerUtils . TimeoutEnvVariable ) ;
86
+ Environment . SetEnvironmentVariable ( ResourceManagerUtils . TimeoutEnvVariable , ( 3 * 60 * 1000 ) . ToString ( ) ) ;
87
+ }
88
+
89
+ protected override void Cleanup ( )
90
+ {
91
+ // set back timeout value
92
+ Environment . SetEnvironmentVariable ( ResourceManagerUtils . TimeoutEnvVariable , _timeOutOldValue ) ;
66
93
}
67
94
68
95
private class TestData
@@ -1250,25 +1277,13 @@ public void TensorFlowStringTest()
1250
1277
}
1251
1278
1252
1279
[ TensorFlowFact ]
1253
- // This test hangs occasionally
1254
- [ Trait ( "Category" , "SkipInCI" ) ]
1255
1280
public void TensorFlowImageClassificationDefault ( )
1256
1281
{
1257
- string imagesDownloadFolderPath = Path . Combine ( TensorFlowScenariosTestsFixture . assetsPath , "inputs" ,
1258
- "images" ) ;
1259
-
1260
- //Download the image set and unzip
1261
- string finalImagesFolderName = DownloadImageSet (
1262
- imagesDownloadFolderPath ) ;
1263
-
1264
- string fullImagesetFolderPath = Path . Combine (
1265
- imagesDownloadFolderPath , finalImagesFolderName ) ;
1266
-
1267
1282
MLContext mlContext = new MLContext ( seed : 1 ) ;
1268
1283
1269
1284
//Load all the original images info
1270
1285
IEnumerable < ImageData > images = LoadImagesFromDirectory (
1271
- folder : fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1286
+ folder : _fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1272
1287
1273
1288
IDataView shuffledFullImagesDataset = mlContext . Data . ShuffleRows (
1274
1289
mlContext . Data . LoadFromEnumerable ( images ) , seed : 1 ) ;
@@ -1285,7 +1300,7 @@ public void TensorFlowImageClassificationDefault()
1285
1300
IDataView trainDataset = trainTestData . TrainSet ;
1286
1301
IDataView testDataset = trainTestData . TestSet ;
1287
1302
1288
- var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1303
+ var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1289
1304
. Append ( mlContext . MulticlassClassification . Trainers . ImageClassification ( "Label" , "Image" )
1290
1305
. Append ( mlContext . Transforms . Conversion . MapKeyToValue ( outputColumnName : "PredictedLabel" , inputColumnName : "PredictedLabel" ) ) ) ; ;
1291
1306
@@ -1338,25 +1353,13 @@ internal bool ShouldReuse(string workspacePath, string trainSetBottleneckCachedV
1338
1353
[ InlineData ( ImageClassificationTrainer . Architecture . MobilenetV2 ) ]
1339
1354
[ InlineData ( ImageClassificationTrainer . Architecture . ResnetV250 ) ]
1340
1355
[ InlineData ( ImageClassificationTrainer . Architecture . InceptionV3 ) ]
1341
- //Skipping test temporarily. This test will be re-enabled once the cause of failures has been determined
1342
- [ Trait ( "Category" , "SkipInCI" ) ]
1343
1356
public void TensorFlowImageClassification ( ImageClassificationTrainer . Architecture arch )
1344
1357
{
1345
- string imagesDownloadFolderPath = Path . Combine ( TensorFlowScenariosTestsFixture . assetsPath , "inputs" ,
1346
- "images" ) ;
1347
-
1348
- //Download the image set and unzip
1349
- string finalImagesFolderName = DownloadImageSet (
1350
- imagesDownloadFolderPath ) ;
1351
-
1352
- string fullImagesetFolderPath = Path . Combine (
1353
- imagesDownloadFolderPath , finalImagesFolderName ) ;
1354
-
1355
1358
MLContext mlContext = new MLContext ( seed : 1 ) ;
1356
1359
1357
1360
//Load all the original images info
1358
1361
IEnumerable < ImageData > images = LoadImagesFromDirectory (
1359
- folder : fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1362
+ folder : _fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1360
1363
1361
1364
IDataView shuffledFullImagesDataset = mlContext . Data . ShuffleRows (
1362
1365
mlContext . Data . LoadFromEnumerable ( images ) , seed : 1 ) ;
@@ -1372,13 +1375,13 @@ public void TensorFlowImageClassification(ImageClassificationTrainer.Architectur
1372
1375
1373
1376
IDataView trainDataset = trainTestData . TrainSet ;
1374
1377
IDataView testDataset = trainTestData . TestSet ;
1375
- var validationSet = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1378
+ var validationSet = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1376
1379
. Fit ( testDataset )
1377
1380
. Transform ( testDataset ) ;
1378
1381
1379
1382
// Check if the bottleneck cached values already exist
1380
1383
var ( trainSetBottleneckCachedValuesFileName , validationSetBottleneckCachedValuesFileName ,
1381
- workspacePath , isReuse ) = getInitialParameters ( arch , finalImagesFolderName ) ;
1384
+ workspacePath , isReuse ) = getInitialParameters ( arch , _finalImagesFolderName ) ;
1382
1385
1383
1386
var options = new ImageClassificationTrainer . Options ( )
1384
1387
{
@@ -1401,7 +1404,7 @@ public void TensorFlowImageClassification(ImageClassificationTrainer.Architectur
1401
1404
ValidationSet = validationSet
1402
1405
} ;
1403
1406
1404
- var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1407
+ var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1405
1408
. Append ( mlContext . MulticlassClassification . Trainers . ImageClassification ( options )
1406
1409
. Append ( mlContext . Transforms . Conversion . MapKeyToValue ( outputColumnName : "PredictedLabel" , inputColumnName : "PredictedLabel" ) ) ) ;
1407
1410
@@ -1429,9 +1432,9 @@ public void TensorFlowImageClassification(ImageClassificationTrainer.Architectur
1429
1432
. CreatePredictionEngine < ImageData , ImagePrediction > ( loadedModel ) ;
1430
1433
1431
1434
IEnumerable < ImageData > testImages = LoadImagesFromDirectory (
1432
- fullImagesetFolderPath , true ) ;
1435
+ _fullImagesetFolderPath , true ) ;
1433
1436
1434
- string [ ] directories = Directory . GetDirectories ( fullImagesetFolderPath ) ;
1437
+ string [ ] directories = Directory . GetDirectories ( _fullImagesetFolderPath ) ;
1435
1438
string [ ] labels = new string [ directories . Length ] ;
1436
1439
for ( int j = 0 ; j < labels . Length ; j ++ )
1437
1440
{
@@ -1442,13 +1445,13 @@ public void TensorFlowImageClassification(ImageClassificationTrainer.Architectur
1442
1445
// Test daisy image
1443
1446
ImageData firstImageToPredict = new ImageData
1444
1447
{
1445
- ImagePath = Path . Combine ( fullImagesetFolderPath , "daisy" , "5794835_d15905c7c8_n.jpg" )
1448
+ ImagePath = Path . Combine ( _fullImagesetFolderPath , "daisy" , "5794835_d15905c7c8_n.jpg" )
1446
1449
} ;
1447
1450
1448
1451
// Test rose image
1449
1452
ImageData secondImageToPredict = new ImageData
1450
1453
{
1451
- ImagePath = Path . Combine ( fullImagesetFolderPath , "roses" , "12240303_80d87f77a3_n.jpg" )
1454
+ ImagePath = Path . Combine ( _fullImagesetFolderPath , "roses" , "12240303_80d87f77a3_n.jpg" )
1452
1455
} ;
1453
1456
1454
1457
var predictionFirst = predictionEngine . Predict ( firstImageToPredict ) ;
@@ -1486,21 +1489,11 @@ public void TensorFlowImageClassificationWithPolynomialLRScheduling()
1486
1489
1487
1490
internal void TensorFlowImageClassificationWithLRScheduling ( LearningRateScheduler learningRateScheduler , int epoch )
1488
1491
{
1489
- string imagesDownloadFolderPath = Path . Combine ( TensorFlowScenariosTestsFixture . assetsPath , "inputs" ,
1490
- "images" ) ;
1491
-
1492
- //Download the image set and unzip
1493
- string finalImagesFolderName = DownloadImageSet (
1494
- imagesDownloadFolderPath ) ;
1495
-
1496
- string fullImagesetFolderPath = Path . Combine (
1497
- imagesDownloadFolderPath , finalImagesFolderName ) ;
1498
-
1499
1492
MLContext mlContext = new MLContext ( seed : 1 ) ;
1500
1493
1501
1494
//Load all the original images info
1502
1495
IEnumerable < ImageData > images = LoadImagesFromDirectory (
1503
- folder : fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1496
+ folder : _fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1504
1497
1505
1498
IDataView shuffledFullImagesDataset = mlContext . Data . ShuffleRows (
1506
1499
mlContext . Data . LoadFromEnumerable ( images ) , seed : 1 ) ;
@@ -1516,13 +1509,13 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule
1516
1509
1517
1510
IDataView trainDataset = trainTestData . TrainSet ;
1518
1511
IDataView testDataset = trainTestData . TestSet ;
1519
- var validationSet = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1512
+ var validationSet = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1520
1513
. Fit ( testDataset )
1521
1514
. Transform ( testDataset ) ;
1522
1515
1523
1516
// Check if the bottleneck cached values already exist
1524
1517
var ( trainSetBottleneckCachedValuesFileName , validationSetBottleneckCachedValuesFileName ,
1525
- workspacePath , isReuse ) = getInitialParameters ( ImageClassificationTrainer . Architecture . ResnetV2101 , finalImagesFolderName ) ;
1518
+ workspacePath , isReuse ) = getInitialParameters ( ImageClassificationTrainer . Architecture . ResnetV2101 , _finalImagesFolderName ) ;
1526
1519
1527
1520
var options = new ImageClassificationTrainer . Options ( )
1528
1521
{
@@ -1546,7 +1539,7 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule
1546
1539
LearningRateScheduler = learningRateScheduler
1547
1540
} ;
1548
1541
1549
- var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1542
+ var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1550
1543
. Append ( mlContext . MulticlassClassification . Trainers . ImageClassification ( options ) )
1551
1544
. Append ( mlContext . Transforms . Conversion . MapKeyToValue (
1552
1545
outputColumnName : "PredictedLabel" ,
@@ -1575,9 +1568,9 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule
1575
1568
. CreatePredictionEngine < ImageData , ImagePrediction > ( loadedModel ) ;
1576
1569
1577
1570
IEnumerable < ImageData > testImages = LoadImagesFromDirectory (
1578
- fullImagesetFolderPath , true ) ;
1571
+ _fullImagesetFolderPath , true ) ;
1579
1572
1580
- string [ ] directories = Directory . GetDirectories ( fullImagesetFolderPath ) ;
1573
+ string [ ] directories = Directory . GetDirectories ( _fullImagesetFolderPath ) ;
1581
1574
string [ ] labels = new string [ directories . Length ] ;
1582
1575
for ( int j = 0 ; j < labels . Length ; j ++ )
1583
1576
{
@@ -1588,13 +1581,13 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule
1588
1581
// Test daisy image
1589
1582
ImageData firstImageToPredict = new ImageData
1590
1583
{
1591
- ImagePath = Path . Combine ( fullImagesetFolderPath , "daisy" , "5794835_d15905c7c8_n.jpg" )
1584
+ ImagePath = Path . Combine ( _fullImagesetFolderPath , "daisy" , "5794835_d15905c7c8_n.jpg" )
1592
1585
} ;
1593
1586
1594
1587
// Test rose image
1595
1588
ImageData secondImageToPredict = new ImageData
1596
1589
{
1597
- ImagePath = Path . Combine ( fullImagesetFolderPath , "roses" , "12240303_80d87f77a3_n.jpg" )
1590
+ ImagePath = Path . Combine ( _fullImagesetFolderPath , "roses" , "12240303_80d87f77a3_n.jpg" )
1598
1591
} ;
1599
1592
1600
1593
var predictionFirst = predictionEngine . Predict ( firstImageToPredict ) ;
@@ -1624,25 +1617,13 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule
1624
1617
[ TensorFlowTheory ]
1625
1618
[ InlineData ( ImageClassificationTrainer . EarlyStoppingMetric . Accuracy ) ]
1626
1619
[ InlineData ( ImageClassificationTrainer . EarlyStoppingMetric . Loss ) ]
1627
- // This test hangs ocassionally
1628
- [ Trait ( "Category" , "SkipInCI" ) ]
1629
1620
public void TensorFlowImageClassificationEarlyStopping ( ImageClassificationTrainer . EarlyStoppingMetric earlyStoppingMetric )
1630
1621
{
1631
- string imagesDownloadFolderPath = Path . Combine ( TensorFlowScenariosTestsFixture . assetsPath , "inputs" ,
1632
- "images" ) ;
1633
-
1634
- //Download the image set and unzip
1635
- string finalImagesFolderName = DownloadImageSet (
1636
- imagesDownloadFolderPath ) ;
1637
-
1638
- string fullImagesetFolderPath = Path . Combine (
1639
- imagesDownloadFolderPath , finalImagesFolderName ) ;
1640
-
1641
1622
MLContext mlContext = new MLContext ( seed : 1 ) ;
1642
1623
1643
1624
//Load all the original images info
1644
1625
IEnumerable < ImageData > images = LoadImagesFromDirectory (
1645
- folder : fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1626
+ folder : _fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
1646
1627
1647
1628
IDataView shuffledFullImagesDataset = mlContext . Data . ShuffleRows (
1648
1629
mlContext . Data . LoadFromEnumerable ( images ) , seed : 1 ) ;
@@ -1660,13 +1641,13 @@ public void TensorFlowImageClassificationEarlyStopping(ImageClassificationTraine
1660
1641
IDataView testDataset = trainTestData . TestSet ;
1661
1642
1662
1643
int lastEpoch = 0 ;
1663
- var validationSet = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1644
+ var validationSet = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1664
1645
. Fit ( testDataset )
1665
1646
. Transform ( testDataset ) ;
1666
1647
1667
1648
// Check if the bottleneck cached values already exist
1668
1649
var ( trainSetBottleneckCachedValuesFileName , validationSetBottleneckCachedValuesFileName ,
1669
- workspacePath , isReuse ) = getInitialParameters ( ImageClassificationTrainer . Architecture . ResnetV2101 , finalImagesFolderName ) ;
1650
+ workspacePath , isReuse ) = getInitialParameters ( ImageClassificationTrainer . Architecture . ResnetV2101 , _finalImagesFolderName ) ;
1670
1651
1671
1652
1672
1653
@@ -1692,7 +1673,7 @@ public void TensorFlowImageClassificationEarlyStopping(ImageClassificationTraine
1692
1673
ValidationSet = validationSet
1693
1674
} ;
1694
1675
1695
- var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , fullImagesetFolderPath , "ImagePath" )
1676
+ var pipeline = mlContext . Transforms . LoadRawImageBytes ( "Image" , _fullImagesetFolderPath , "ImagePath" )
1696
1677
. Append ( mlContext . MulticlassClassification . Trainers . ImageClassification ( options ) ) ;
1697
1678
1698
1679
using var trainedModel = pipeline . Fit ( trainDataset ) ;
0 commit comments