forked from aws-deepracer-community/deepracer-on-the-spot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot-instance.yaml
1224 lines (1104 loc) · 57 KB
/
spot-instance.yaml
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
AWSTemplateFormatVersion: "2010-09-09"
Description: Setup a spot EC2 Autoscaling Group for deep racer
Parameters:
InstanceType:
Type: String
Default: g4dn.2xlarge
ResourcesStackName:
Type: String
DeepRacerImportName:
Type: String
TimeToLiveInMinutes:
Type: Number
Description: timeout in minutes after which training is stopped and this stack is deleted
Default: 60
MinValue: 2
MaxValue: 1440 # 24 hours
AmiId:
Type: String
Description: the AMI we want to launch an ec2 instance against. By default this is the image created by central account 747447086422 owned by Tyler Wooten
BUCKET:
Type: String
CUSTOMFILELOCATION:
Type: String
Outputs:
Instance:
Value: !Ref ASG
Resources:
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: !Sub ${AWS::StackName}-launch-template
LaunchTemplateData:
IamInstanceProfile:
Name:
!ImportValue
'Fn::Sub': '${ResourcesStackName}-InstanceProfile'
ImageId: !Ref AmiId
InstanceType: !Ref InstanceType
InstanceMarketOptions:
MarketType: spot
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeType: gp3
VolumeSize: 60
DeleteOnTermination: 'true'
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
/usr/local/bin/cfn-init --stack ${AWS::StackName} --resource ASG --region ${AWS::Region}
bash
sudo su ubuntu
cd /home/ubuntu/deepracer-for-cloud/
source bin/activate.sh
export DEEPRACER_S3_URI=${BUCKET}
export CUSTOM_FILE_LOCATION="${CUSTOMFILELOCATION}"
SecurityGroupIds:
- !ImportValue
'Fn::Sub': '${ResourcesStackName}-SecurityGroup'
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}'
- ResourceType: volume
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}'
ASG:
Type: AWS::AutoScaling::AutoScalingGroup
CreationPolicy:
ResourceSignal:
Count: '1'
Timeout: PT30M
Metadata:
AWS::CloudFormation::Init:
config:
commands:
1-append-fstab:
command: "bash -c 'cat /tmp/fstabcontent.txt >> /etc/fstab'"
2-mount-fstab:
command: "bash -c 'mount /home/ubuntu/efs'"
3-signal-cfn:
command:
!Sub "bash -c '/usr/local/bin/cfn-signal -s true -e 0 --stack ${AWS::StackName} --resource ASG --region ${AWS::Region}'"
4-start-train:
command: "su -l ubuntu bash -c '/home/ubuntu/bin/start_training.sh'"
files:
/tmp/fstabcontent.txt:
content:
Fn::Sub:
- "${EFS} /home/ubuntu/efs efs _netdev,tls,accesspoint=${EFSAP} 0 0"
- EFS:
Fn::ImportValue:
!Sub "${ResourcesStackName}-EFS"
EFSAP:
Fn::ImportValue:
!Sub "${ResourcesStackName}-EFSAccessPoint"
mode : "000755"
owner: root
/etc/profile.d/my_efs.sh:
content:
Fn::Sub:
- "export MY_EFS=${EFS}"
- EFS:
Fn::ImportValue:
!Sub "${ResourcesStackName}-EFS"
mode : "000755"
owner: root
group: root
/etc/profile.d/my_sns_topic.sh:
content:
Fn::Sub:
- "export MY_SNS_TOPIC=${SNS}"
- SNS:
Fn::ImportValue:
!Sub "${ResourcesStackName}-InterruptionNotification"
/etc/profile.d/my_public_ip.sh:
content: "export PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)"
mode : "000755"
owner: root
group: root
/etc/profile.d/my_bucket.sh:
content:
Fn::Sub:
- "export MY_BUCKET=${BUCKET};export DR_S3_URI=${BUCKET};export DEEPRACER_S3_URI=${BUCKET}"
- BUCKET:
Fn::ImportValue:
!Sub "${ResourcesStackName}-Bucket"
mode : "000755"
owner: root
group: root
/etc/profile.d/my_custom_files.sh:
content:
Fn::Sub: |
export CUSTOM_FILE_LOCATION="${CUSTOMFILELOCATION}"
mode : "000755"
owner: root
group: root
/etc/profile.d/my_region.sh:
content: "export DEEPRACER_REGION=$(curl http://169.254.169.254/latest/meta-data/placement/region)"
mode : "000755"
owner: root
group: root
/home/ubuntu/deepracer-for-cloud/import_model.sh:
content:
Fn::Sub:
- sudo aws deepracer import-model --name '${DeepRacerImportName}' --description "$DR_WORLD_NAME imported from s3://$DR_UPLOAD_S3_BUCKET/$DR_UPLOAD_S3_PREFIX by DeepRacer on the Spot" --model-artifacts-s3-path s3://$DR_UPLOAD_S3_BUCKET/$DR_UPLOAD_S3_PREFIX --role-arn ${DR_IMPORT_ROLENAME} --type REINFORCEMENT_LEARNING --region us-east-1
- DR_IMPORT_ROLENAME:
Fn::ImportValue:
!Sub "${ResourcesStackName}-DeepRacerServiceRole"
mode : "000755"
owner: root
group: root
/home/ubuntu/deepracer-for-cloud/interrupt_spot.sh:
content: |
date >> /tmp/interrupt.log
sudo su ubuntu
INTERRUPT_TIME=$(date '+%Y%m%d-%H%M')
cd ~/deepracer-for-cloud
source bin/activate.sh
source /etc/profile.d/my_bucket.sh
dr-reload
dr-upload-model -bfw
dr-stop-training
sed -i '/DR_LOCAL_S3_PRETRAINED/d' ~/deepracer-for-cloud/run.env
sed -i -e '$aDR_LOCAL_S3_PRETRAINED=True' ~/deepracer-for-cloud/run.env
PRE_TRAINED_LOCATION=$(cat run.env | grep DR_LOCAL_S3_MODEL_PREFIX= | awk -F'=' '{print $2}')
sed -i 's|-continued-.*||' ~/deepracer-for-cloud/run.env
TRAINING_LOCATION=$(cat run.env | grep DR_LOCAL_S3_MODEL_PREFIX= | awk -F'=' '{print $2}')
UPLOAD_LOCATION=$(cat run.env | grep DR_UPLOAD_S3_PREFIX= | awk -F'=' '{print $2}')
sed -i -e '$aDR_LOCAL_S3_PRETRAINED_PREFIX=PLACEHOLDER' ~/deepracer-for-cloud/run.env
sed -i "s|DR_LOCAL_S3_PRETRAINED_PREFIX=PLACEHOLDER|DR_LOCAL_S3_PRETRAINED_PREFIX=$PRE_TRAINED_LOCATION|" ~/deepracer-for-cloud/run.env
sed -i "s|DR_LOCAL_S3_MODEL_PREFIX=$TRAINING_LOCATION|DR_LOCAL_S3_MODEL_PREFIX=$TRAINING_LOCATION-continued-$INTERRUPT_TIME|" ~/deepracer-for-cloud/run.env
sed -i "s|DR_UPLOAD_S3_PREFIX=$UPLOAD_LOCATION|DR_UPLOAD_S3_PREFIX=$UPLOAD_LOCATION-continued-$INTERRUPT_TIME|" ~/deepracer-for-cloud/run.env
sed -i -e '$aDR_LOCAL_S3_PRETRAINED_CHECKPOINT=last' ~/deepracer-for-cloud/run.env
aws s3 cp ~/deepracer-for-cloud/run.env s3://$DEEPRACER_S3_URI/$DR_LOCAL_S3_CUSTOM_FILES_PREFIX/
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/bin/menu.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Video Feed</h2>
<p><a href="/?robo=all&camera=kvs_stream&quality=75&width=480">Watch Live Video</a></p>
<h2>All logs summary in one view</h2>
<p><a href="output.txt">Output</a></p>
<h2>Docker logs (last 1000 lines)</h2>
<p><a href="sagemaker.txt">Sagemaker</a></p>
<p><a href="robomaker.txt">Robomaker (Main worker)</a></p>
<p><a href="dockerstatus.txt">docker ps -a (command output)</a></p>
<h2>Nvidia GPU status</h2>
<p><a href="nvidia-smi.txt">nvidia-smi (command output)</a></p>
<h2>Custom logs (last 1000 lines)</h2>
<p><a href="OutputLog.txt">OutputLog</a></p>
<p><a href="completedlaps.txt">Completed Laps - last step from Robomaker output (all Workers)</a></p>
<h2>Configuration files</h2>
<p><a href="run.env.txt">run.env</a></p>
<p><a href="system.env.txt">system.env</a></p>
<p><a href="hyperparameters.json">hyperparameters.json</a></p>
<p><a href="model_metadata.json">model_metadata.json</a></p>
<p><a href="reward_function.py.txt">reward_function.py</a></p>
<h2>Training metrics</h2>
<p><a href="TrainingMetrics.json">TrainingMetrics.json</a></p>
<p><a href="deepracer_checkpoints.json">deepracer_checkpoints.json</a></p>
<p><a download href="robomaker1.log">robomaker1.log</a></p>
<h2>Training/Evaluation monitoring graphs</h2>
<p><a href="Training_and_Evaluation_Overview.html">Training_and_Evaluation_Overview</a></p>
<p><a href="Training_progress.html">Training_progress</a></p>
<p><a href="Quintiles.html">Quintiles</a></p>
<p><a href="Heatmap.html">Reward Heatmap</a></p>
<p><a href="Data_tables.html">Data in tables</a></p>
<p><a href="Path_for_complete_laps.html">Path_for_complete_laps</a></p>
<p><a href="update_to_jupyter_url">Access Jupyter Notebook</a></p>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Training_and_Evaluation_Overview.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Training analysis is typically available 20-30 minutes into training. Please refresh in a few minutes. </h2>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Training_progress.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Training analysis is typically available 20-30 minutes into training. Please refresh in a few minutes. </h2>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Quintiles.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Training analysis is typically available 20-30 minutes into training. Please refresh in a few minutes. </h2>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Heatmap.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Training analysis is typically available 20-30 minutes into training. Please refresh in a few minutes. </h2>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Data_tables.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Training analysis is typically available 20-30 minutes into training. Please refresh in a few minutes. </h2>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Path_for_complete_laps.html:
content: |
<!DOCTYPE html>
<html>
<body>
<h2>Training analysis is typically available 20-30 minutes into training. Please refresh in a few minutes. </h2>
</body>
</html>
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/bin/web_monitoring.sh:
content: |
#!/bin/bash
/home/ubuntu/bin/start_analysis.sh
USAGE_OUTPUT=output.txt
cd ~/deepracer-for-cloud
while [ true ]
do
# This loop collects training data available and publishes it on the nginx docker. accessible through Public_IP:8100/menu.html
# Update variable references before every iteration in case of any change on the config files, this is similar to dr-reload
source ~/deepracer-for-cloud/bin/activate.sh > /dev/null 2>&1
echo "-----------------------------------" > $USAGE_OUTPUT
# Get model name being trained
cat ~/deepracer-for-cloud/run.env | egrep "^DR_LOCAL_S3_MODEL_PREFIX" >> $USAGE_OUTPUT
# get timestamp to know if the data published is current
date --utc +%F_%T_UTC >> $USAGE_OUTPUT
# known training issues # 1 - GPU ran out of memory
outofmemoryerrors=$(docker logs $(dr-find-sagemaker) 2>&1 | grep "ran out of memory"|wc -l)
if [[ $outofmemoryerrors -ge 1 ]];then
echo " ########### ERROR ------> GPU RAN OUT OF MEMORY !!!!!! ###########" >> $USAGE_OUTPUT
fi
# get Checkpoint status (best checkpoint, last checkpoint, current checkpoint)
docker logs $(dr-find-sagemaker) 2>&1 | grep "Best checkpoint" | tail -n 1 >> $USAGE_OUTPUT
docker logs $(dr-find-sagemaker) 2>&1 | grep Checkpoint | tail -n 1 >> $USAGE_OUTPUT
echo "=====Robomaker (main Worker)=====" >> $USAGE_OUTPUT
docker logs $(dr-find-robomaker) 2>&1 | egrep '^(SIM_TRACE_LOG.*(omplete|off_)|^reward_output)' | tail -n 10 | grep "omplete\|off_\|reward_output\|checkpoint" >> $USAGE_OUTPUT
echo "=====Sagemaker policy training=====" >> $USAGE_OUTPUT
docker logs $(dr-find-sagemaker) 2>&1 | egrep '^Policy training' | tail -n 1 >> $USAGE_OUTPUT
echo "=====GPU performance=====" >> $USAGE_OUTPUT
nvidia-smi > nvidia-smi.txt 2>&1
grep Default nvidia-smi.txt >> $USAGE_OUTPUT 2>&1
echo "=====Docker containers status=====" >> $USAGE_OUTPUT
docker ps -a > dockerstatus.txt 2>&1
cat dockerstatus.txt >> $USAGE_OUTPUT 2>&1
# known training issues # 2 - At least one required DOCKER CONTAINER EXITED
dockererrors=$(grep "exited" dockerstatus.txt | egrep 'deepracer-(sagemaker|rlcoach|robomaker)' | wc -l)
if [[ $dockererrors -ge 1 ]];then
echo " ########### ERROR ------> At least one required DOCKER CONTAINER EXITED !!!!!! ###########" >> $USAGE_OUTPUT
fi
echo "=====CPU average load (1min / 5min / 15min avg)=====" >> $USAGE_OUTPUT
cat /proc/loadavg >> $USAGE_OUTPUT 2>&1
echo "=====Memory usage=====" >> $USAGE_OUTPUT
cat /proc/meminfo | egrep '(^MemTotal|^MemFree|^SwapTotal|^SwapFree)' >> $USAGE_OUTPUT 2>&1
echo "=====Robomaker Testing result logs (all Workers)=====" >> $USAGE_OUTPUT
for name in `docker ps --format "{{.Names}}" | grep obomaker`
do
docker logs ${name} 2>&1 | egrep '^Testing>' | tail -n 10 >> $USAGE_OUTPUT
docker logs ${name} >& ${name}.log
done
mv deepracer-0_robomaker.1.*.log robomaker1.log
echo "=====Sagemaker training logs=====" >> $USAGE_OUTPUT
docker logs $(dr-find-sagemaker) 2>&1 | egrep '^Training>' | tail -n 10 >> $USAGE_OUTPUT
echo "=====Robomaker Top 10 completed laps (all Workers)=====" >> $USAGE_OUTPUT
if [ -f $USAGE_OUTPUT.tmp ] ;then
rm "$USAGE_OUTPUT.tmp" > /dev/null 2>&1
fi
for name in `docker ps --format "{{.Names}}"`
do
docker logs ${name} 2>&1 | egrep '^SIM_TRACE_LOG.*(omplete)' | sort --field-separator=',' --key=2 | head -n 10000 >> $USAGE_OUTPUT.tmp
done
echo "Number of completed laps: $(cat $USAGE_OUTPUT.tmp | wc -l)" >> $USAGE_OUTPUT 2>&1
cat $USAGE_OUTPUT.tmp | sort --field-separator=',' --key=2 | head -n 1000 > completedlaps.txt 2>&1
head completedlaps.txt -n 10 >> $USAGE_OUTPUT 2>&1
echo "=====Robomaker (main Worker) - OutputLog: =====" >> $USAGE_OUTPUT
docker logs $(dr-find-robomaker) 2>&1 | egrep '^OutputLog' | tail -n 1000 > OutputLog.txt
tail OutputLog.txt -n 10 >> $USAGE_OUTPUT
rm $USAGE_OUTPUT.tmp > /dev/null 2>&1
echo "###################" >> $USAGE_OUTPUT
# Collecting remaining common output files, metrics and uploading them to website
docker logs $(dr-find-sagemaker) 2>&1 | tail -n 1000 > sagemaker.txt
docker logs $(dr-find-robomaker) 2>&1 | tail -n 1000 > robomaker.txt
aws s3 cp s3://$DR_LOCAL_S3_BUCKET/$DR_LOCAL_S3_MODEL_PREFIX/metrics/TrainingMetrics.json . > /dev/null 2>&1
aws s3 cp s3://$DR_LOCAL_S3_BUCKET/$DR_LOCAL_S3_MODEL_PREFIX/model/deepracer_checkpoints.json . > /dev/null 2>&1
for ID in `docker ps --filter name=viewer --format "{{.ID}}"`
do
docker cp $USAGE_OUTPUT $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp nvidia-smi.txt $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp dockerstatus.txt $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp completedlaps.txt $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp OutputLog.txt $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp sagemaker.txt $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp robomaker.txt $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp robomaker1.log $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp TrainingMetrics.json $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp deepracer_checkpoints.json $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp ~/deepracer-for-cloud/run.env $ID:/usr/share/nginx/html/run.env.txt > /dev/null 2>&1
docker cp ~/deepracer-for-cloud/system.env $ID:/usr/share/nginx/html/system.env.txt > /dev/null 2>&1
docker cp ~/deepracer-for-cloud/custom_files/hyperparameters.json $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp ~/deepracer-for-cloud/custom_files/model_metadata.json $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker cp ~/deepracer-for-cloud/custom_files/reward_function.py $ID:/usr/share/nginx/html/reward_function.py.txt > /dev/null 2>&1
docker cp /home/ubuntu/bin/menu.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
#Update training analysis
ANALYSIS_ID=$(docker ps --filter name=deepracer-analysis --format "{{.ID}}")
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute import_from_s3.ipynb
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute Training_progress.ipynb
docker cp $ANALYSIS_ID:/workspace/Training_progress.html .
docker cp Training_progress.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute Heatmap.ipynb
docker cp $ANALYSIS_ID:/workspace/Heatmap.html .
docker cp Heatmap.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute Quintiles.ipynb
docker cp $ANALYSIS_ID:/workspace/Quintiles.html .
docker cp Quintiles.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute Data_tables.ipynb
docker cp $ANALYSIS_ID:/workspace/Data_tables.html .
docker cp Data_tables.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute Path_for_complete_laps.ipynb
docker cp $ANALYSIS_ID:/workspace/Path_for_complete_laps.html .
docker cp Path_for_complete_laps.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
docker exec $ANALYSIS_ID jupyter nbconvert --no-input --to html --execute Training_and_Evaluation_Overview.ipynb
docker cp $ANALYSIS_ID:/workspace/Training_and_Evaluation_Overview.html .
docker cp Training_and_Evaluation_Overview.html $ID:/usr/share/nginx/html/ > /dev/null 2>&1
done
# if the EC2 has started the termination process we do not want to upload $USAGE_OUTPUT to S3
if [[ ! -f /home/ubuntu/bin/termination.started ]];then
cp $USAGE_OUTPUT /tmp/logs/ > /dev/null 2>&1
fi
sleep 300
done
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/bin/start_training.sh:
content: |
#!/bin/bash
source /etc/profile.d/my_custom_files.sh
aws sns publish --topic-arn $MY_SNS_TOPIC --message "Training has initiated on a new instance. The new url to monitor progress is http://$PUBLIC_IP:8100/menu.html" --region $DEEPRACER_REGION
cd ~/deepracer-for-cloud
git pull
sed -i '/DR_AWS_APP_REGION=/d' /home/ubuntu/deepracer-for-cloud/system.env
sed -i -e '$aDR_AWS_APP_REGION=$DEEPRACER_REGION' /home/ubuntu/deepracer-for-cloud/system.env
sed -i "s/DR_UPLOAD_S3_BUCKET=not-defined/DR_UPLOAD_S3_BUCKET=$DEEPRACER_S3_URI/" ~/deepracer-for-cloud/system.env
sed -i "s/DR_LOCAL_S3_BUCKET=bucket/DR_LOCAL_S3_BUCKET=$DEEPRACER_S3_URI/" ~/deepracer-for-cloud/system.env
sed -i "s/DR_UPLOAD_S3_PREFIX=upload/DR_UPLOAD_S3_PREFIX=$DR_LOCAL_S3_MODEL_PREFIX-upload/" ~/deepracer-for-cloud/run.env
sed -i "s|DR_LOCAL_S3_CUSTOM_FILES_PREFIX=custom_files|DR_LOCAL_S3_CUSTOM_FILES_PREFIX=$CUSTOM_FILE_LOCATION|" ~/deepracer-for-cloud/run.env
source bin/activate.sh
dr-download-custom-files
cp custom_files/*.env .
dr-reload
# Setup required config if using OpenGL training
if [[ $DR_HOST_X == True ]];then
distribution=$(. /etc/os-release;echo $ID$VERSION_ID | sed 's/\.//')
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$distribution/x86_64/3bf863cc.pub
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/$distribution/x86_64/7fa2af80.pub
echo "deb http://developer.download.nvidia.com/compute/cuda/repos/$distribution/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list
echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/$distribution/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda_learn.list
sudo apt install -y nvidia-driver-525-server --no-install-recommends -o Dpkg::Options::="--force-overwrite"
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y --no-install-recommends nvidia-docker2 nvidia-container-toolkit nvidia-container-runtime xserver-xorg-dev xutils-dev
./utils/setup-xorg.sh
./utils/start-xorg.sh
sleep 15
export DISPLAY=$DR_DISPLAY
sudo nohup xinit /usr/bin/jwm -- /usr/lib/xorg/Xorg $DISPLAY -config $DR_DIR/tmp/xorg.conf > $DR_DIR/tmp/xorg.log 2>&1 & sleep 1
nohup xrandr -s 1400x900
nohup x11vnc -bg -forever -no6 -nopw -rfbport 5901 -rfbportv6 -1 -display WAIT$DISPLAY & sleep 1
xauth generate $DISPLAY
export XAUTHORITY=~/.Xauthority
sudo DISPLAY=:$DISPLAY XAUTHORITY=$(ps aux | grep "X.*\-auth" | grep -v grep | sed -n 's/.*-auth \([^ ]\+\).*/\1/p') xhost +
fi
# There is a bug where at some times the training fails to start, so we start, stop and start it again to reduce the occurrences of this issue.
nohup /bin/bash -lc 'cd ~/deepracer-for-cloud/; dr-start-training -qw; sleep 120; dr-stop-training; sleep 60; echo y | docker container prune; dr-reload; dr-start-training -qwv' &
mkdir -p /tmp/logs/
# We want to be able to monitor our EC2 training without needing to connect to console, so we upload all needed info to Public_IP:8100/menu.html using this script
nohup /bin/bash -lc 'source /home/ubuntu/bin/web_monitoring.sh >/dev/null 2>&1' &
sleep 180 > /dev/null
while [ True ]; do
# if the EC2 started termination process upon interruption notification, this file should exist, hence we leave termination process to manage final uploads without conflict
if [[ -f /home/ubuntu/bin/termination.started ]];then
break
fi
# Update variable references before every iteration in case of any change on the config files
source ~/deepracer-for-cloud/bin/activate.sh
for name in `docker ps -a --format "{{.Names}}"`; do
docker logs ${name} > /tmp/logs/${name}.log 2>&1
done
# Only upload best Checkpoint if best Checkpoint has changed
bestcheckpoint=$(echo n | dr-upload-model -b 2>&1 | grep "checkpoint:")
aws s3 cp /tmp/logs/ s3://$DEEPRACER_S3_URI/$DR_LOCAL_S3_MODEL_PREFIX/logs/ --recursive
rm -rf /tmp/logs/*.* > /dev/null 2>&1
if [ [ "$bestcheckpoint" != "$lastbestcheckpoint" ] && [ "$bestcheckpoint" != "" ] ];then
# update file timestamp just to avoid conflict with termination process
touch /home/ubuntu/bin/uploading_best_model.timestamp 2>&1
dr-upload-model -bfw > /dev/null 2>&1
lastbestcheckpoint=$bestcheckpoint
fi
sleep 120
done
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/bin/start_analysis.sh:
content: |
#!/bin/bash
cd ~/deepracer-for-cloud
source bin/activate.sh
docker run -d -p 8888:8888 --name deepracer-analysis awsdeepracercommunity/deepracer-analysis:cpu
while [ "$TOKEN" == "" ]
do
TOKEN=$(docker logs deepracer-analysis 2>&1 | grep -o -E "token=[0-9a-f]+" | head -n 1)
done
PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
JUPYTER_URL=http://$PUBLIC_IP:8888/?$TOKEN
sudo sed -i "s|update_to_jupyter_url|${JUPYTER_URL}|" /home/ubuntu/bin/menu.html
S3_PREFIX_FOR_ANALYSIS=$(cat run.env | grep DR_LOCAL_S3_MODEL_PREFIX= | awk -F'=' '{print $2}')
DEEPRACER_TRACK=$(cat run.env | grep DR_WORLD_NAME= | awk -F'=' '{print $2}')
DEEPRACER_WORKERS=$(cat system.env | grep DR_WORKERS= | awk -F'=' '{print $2}')
S3_REGION=$(curl http://169.254.169.254/latest/meta-data/placement/region)
sed -i "s|DR_LOCAL_S3_MODEL_PREFIX|${S3_PREFIX_FOR_ANALYSIS}|" import_from_s3.py
sed -i "s|DEEPRACER_S3_URI|${DEEPRACER_S3_URI}|" import_from_s3.py
sed -i "s|DEEPRACER_TRACK|${DEEPRACER_TRACK}|" import_from_s3.py
sed -i "s|S3_REGION|${S3_REGION}|" import_from_s3.py
sed -i "s|DR_LOCAL_S3_MODEL_PREFIX|${S3_PREFIX_FOR_ANALYSIS}|" Training_and_Evaluation_Overview.py
sed -i "s|DEEPRACER_S3_URI|${DEEPRACER_S3_URI}|" Training_and_Evaluation_Overview.py
sed -i "s|DEEPRACER_WORKERS|${DEEPRACER_WORKERS}|" Training_and_Evaluation_Overview.py
ANALYSIS_ID=$(docker ps --filter name=deepracer-analysis --format "{{.ID}}")
docker cp import_from_s3.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook import_from_s3.py
docker cp Training_progress.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook Training_progress.py
docker cp Quintiles.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook Quintiles.py
docker cp Heatmap.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook Heatmap.py
docker cp Data_tables.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook Data_tables.py
docker cp Path_for_complete_laps.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook Path_for_complete_laps.py
docker cp Training_and_Evaluation_Overview.py $ANALYSIS_ID:/workspace/
docker exec $ANALYSIS_ID jupytext --to notebook Training_and_Evaluation_Overview.py
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/import_from_s3.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from deepracer.tracks import TrackIO, Track
from deepracer.tracks.track_utils import track_breakdown, track_meta
from deepracer.logs import \
SimulationLogsIO as slio, \
NewRewardUtils as nr, \
AnalysisUtils as au, \
PlottingUtils as pu, \
ActionBreakdownUtils as abu, \
DeepRacerLog, \
S3FileHandler
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
# -
# +
#Get logs from S3
fh = S3FileHandler(bucket="DEEPRACER_S3_URI",
prefix="DR_LOCAL_S3_MODEL_PREFIX", region="S3_REGION")
#Attempt to load logs but catch error if training not yet far enough advanced
try:
log = DeepRacerLog(filehandler=fh)
log.load_training_trace()
df = log.dataframe()
simulation_agg = au.simulation_agg(df, secondgroup="unique_episode")
complete_ones = simulation_agg[simulation_agg['progress']==100]
%store df
%store simulation_agg
%store complete_ones
except Exception:
print("Logs are not yet available. It typically takes 25 minutes from the start of training for them to be available.")
tu = TrackIO()
try:
track: Track = tu.load_track("DEEPRACER_TRACK")
%store track
except Exception:
print("Track not currently included in the solution. Copy track into the tracks folder or check you're using the latest deepracer-analysis image.")
# -
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Training_progress.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from deepracer.tracks import TrackIO, Track
from deepracer.tracks.track_utils import track_breakdown, track_meta
from deepracer.logs import \
SimulationLogsIO as slio, \
NewRewardUtils as nr, \
AnalysisUtils as au, \
PlottingUtils as pu, \
ActionBreakdownUtils as abu, \
DeepRacerLog, \
S3FileHandler
%store -r simulation_agg
%store -r df
%store -r track
%store -r complete_ones
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
# -
# ## Training Progress Graphs
# +
try:
au.analyze_training_progress(simulation_agg, title='Training progress')
au.scatter_aggregates(simulation_agg, 'Stats for all laps')
complete_ones = simulation_agg[simulation_agg['progress']==100]
if complete_ones.shape[0] > 0:
au.scatter_aggregates(complete_ones, 'Stats for complete laps')
else:
print('Stats for complete laps - No complete laps yet.')
try:
au.analyze_training_progress(complete_ones, title='Complete lap training progress')
except Exception:
print('Complete lap training progress - No complete laps yet.')
except Exception:
print("Logs are not yet available. It typically takes 25 minutes from the start of training for them to be available.")
# -
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Quintiles.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from deepracer.tracks import TrackIO, Track
from deepracer.tracks.track_utils import track_breakdown, track_meta
from deepracer.logs import \
SimulationLogsIO as slio, \
NewRewardUtils as nr, \
AnalysisUtils as au, \
PlottingUtils as pu, \
ActionBreakdownUtils as abu, \
DeepRacerLog, \
S3FileHandler
%store -r simulation_agg
%store -r df
%store -r track
%store -r complete_ones
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
# -
# ## Training Progress Graphs
# +
try:
complete_ones = simulation_agg[simulation_agg['progress']==100]
au.scatter_by_groups(simulation_agg, title='Quintiles')
au.scatter_by_groups(complete_ones, title='Complete Lap Quintiles')
except Exception:
print("Logs are not yet available. It typically takes 25 minutes from the start of training for them to be available.")
# -
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Heatmap.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from deepracer.tracks import TrackIO, Track
from deepracer.tracks.track_utils import track_breakdown, track_meta
from deepracer.logs import \
SimulationLogsIO as slio, \
NewRewardUtils as nr, \
AnalysisUtils as au, \
PlottingUtils as pu, \
ActionBreakdownUtils as abu, \
DeepRacerLog, \
S3FileHandler
%store -r simulation_agg
%store -r df
%store -r track
%store -r complete_ones
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
# -
# ## Training Heatmap
# +
try:
pu.plot_track(df, track)
except Exception:
print("Logs are not yet available. It typically takes 25 minutes from the start of training for them to be available.")
# -
# ## Waypoint Map
# +
try:
pu.plot_trackpoints(track)
except Exception:
print("Logs are not yet available. It typically takes 25 minutes from the start of training for them to be available.")
# -
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Data_tables.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from deepracer.tracks import TrackIO, Track
from deepracer.tracks.track_utils import track_breakdown, track_meta
from deepracer.logs import \
SimulationLogsIO as slio, \
NewRewardUtils as nr, \
AnalysisUtils as au, \
PlottingUtils as pu, \
ActionBreakdownUtils as abu, \
DeepRacerLog, \
S3FileHandler
%store -r simulation_agg
%store -r df
%store -r track
%store -r complete_ones
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
# -
# ## Data in tables
# ## Ten best rewarded episodes in the training
# +
simulation_agg.nlargest(10, 'reward')
# -
# ## Ten fastest complete laps in the training
# +
complete_ones.nsmallest(10, 'time')
# -
# ## Ten fastest complete laps from the start/finish line in training
# +
complete_ones_from_start = complete_ones[complete_ones['start_at']==1]
complete_ones_from_start.nsmallest(10, 'time')
# -
# ## Ten fastest incomplete laps in the training
# +
simulation_agg.nsmallest(10, 'time_if_complete')
# -
# ## Ten best rewarded complete laps in the training
# +
complete_ones.nlargest(10, 'reward')
# -
# ## Ten most progressed episodes in the training
# +
simulation_agg.nlargest(10, 'progress')
# -
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Path_for_complete_laps.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from deepracer.tracks import TrackIO, Track
from deepracer.tracks.track_utils import track_breakdown, track_meta
from deepracer.logs import \
SimulationLogsIO as slio, \
NewRewardUtils as nr, \
AnalysisUtils as au, \
PlottingUtils as pu, \
ActionBreakdownUtils as abu, \
DeepRacerLog, \
S3FileHandler
%store -r simulation_agg
%store -r df
%store -r track
%store -r complete_ones
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
print("Number of completed laps so far:", len(complete_ones))
# -
# ## Path taken for quickest complete laps
# +
episodes_to_plot = complete_ones.nsmallest(5, 'time')
pu.plot_selected_laps(episodes_to_plot, df, track, section_to_plot="unique_episode")
# -
# ## Path taken for highest rewarded complete laps
# +
episodes_to_plot = complete_ones.nlargest(5,'reward')
pu.plot_selected_laps(episodes_to_plot, df, track, section_to_plot="unique_episode")
# -
mode : "000755"
owner: ubuntu
group: ubuntu
/home/ubuntu/deepracer-for-cloud/Training_and_Evaluation_Overview.py:
content: |
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
#Import block to bring in dependencies
from deepracer.logs import metrics
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Ignore deprecation warnings we have no power over
import warnings
warnings.filterwarnings('ignore')
# -
# ## Completion of Training and Evaluation per Iteration
# +
try: