File size: 52,158 Bytes
edcf5ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Experimental Script Generator    Script  ver: Oct 5th 16:30

for linux servers

todo fix train and test alternatively
"""
import argparse
import os.path


def zero_trans_mystrlr_to_float(in_str):
    # EG: '305' -> 0.0005
    front = '0.'
    num_of_zero = int(in_str[0])
    end = in_str[-1]
    for i in range(num_of_zero):
        front = front + '0'
    front = front + end

    out_float = float(front)

    return out_float


def zero_trans_floatlr_to_mystrlr(in_float):
    # EG: 0.0005 -> '305'
    in_string = "%.20f" % in_float
    zero_counts = 0

    for i in range(len(in_string) - 2):
        # print(string[i+2])
        if in_string[i + 2] == '0':
            zero_counts += 1
        else:
            cut = i
            break

    trans_output = str(zero_counts) + '0' + in_string[(cut + 2):]

    last_zeros = 0
    for i in trans_output[::-1]:
        if i == '0':
            last_zeros += 1
        else:
            break
    trans_output = trans_output[0:0 - last_zeros]

    return trans_output


def remove_nohup_ignoring_input_at_first_line(directory='./'):
    """
    read the .sh files at the directory, remove the first line if it's 'nohup: ignoring input\n'
    """
    for root, _, files in os.walk(directory):
        for file_name in files:
            if file_name.endswith(".sh"):
                file_path = os.path.join(root, file_name)

                with open(file_path, 'r') as file:
                    lines = file.readlines()
                    # print(lines)

                modified_lines = [line for line in lines if line != "nohup: ignoring input\n"]
                with open(file_path, 'w') as file:
                    file.writelines(modified_lines)

                print('file_path:', file_path, 'has been cleaned')


def concatenate_the_lines_from_several_files(directory='./', cat_file='0.sh'):
    cat_file_path = os.path.join(directory, cat_file)
    all_lines = ["#!/bin/sh\n", ]

    for root, _, files in os.walk(directory):
        for file_name in files:
            if file_name.endswith(".sh"):
                file_path = os.path.join(root, file_name)

                with open(file_path, 'r') as file:
                    lines = file.readlines()
                    # print(lines)

                modified_lines = [line for line in lines if line != "#!/bin/sh\n"]
                all_lines.extend(modified_lines)
                print('file_path:', file_path, 'has taken')

    with open(cat_file_path, 'w') as file:
        file.writelines(all_lines)


def print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_Token_num=20,
                                           Prompt_input=False):
    Pre_Trained_model_path = os.path.join(Pre_Trained_model_path_PATH, model_weight_name)
    VPT_backbone_model_path = os.path.join(Pre_Trained_model_path_PATH, 'ViT_b16_224_Imagenet.pth')
    if not Prompt_input:
        # send a ViT model inside and then do the ViT + finetuning;
        # In VPT versions: we build VPT backbone with the ViT weight, then do finetuning and prompting
        # ViT + finetuning
        print(
            'python Train.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --model_idx ViT_base_' + model_weight_idx + '_'
            + lr_mystr + '_lf' + lrf_mystr + '_finetuning_' + dataset_name + '_CLS --dataroot ' + str(dataroot)
            + ' --draw_root ' + draw_root + ' --Pre_Trained_model_path ' + Pre_Trained_model_path
            + ' --model_path ' + save_model_PATH)
        print(
            'python Test.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --model_idx ViT_base_' + model_weight_idx + '_' + lr_mystr + '_lf' + lrf_mystr + '_finetuning_'
            + dataset_name + '_CLS --dataroot ' + str(dataroot) + ' --draw_root ' + draw_root + ' --model_path '
            + save_model_PATH)
        # VPT + prompting
        print(
            'python Train.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --model_idx ViT_base_' + model_weight_idx
            + '_PromptDeep_' + str(Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr + '_prompting_' + dataset_name
            + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(Prompt_Token_num) + ' --dataroot ' + str(
                dataroot) + ' --draw_root ' + draw_root
            + ' --Pre_Trained_model_path ' + Pre_Trained_model_path + ' --model_path ' + save_model_PATH)
        print(
            'python Test.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --model_idx ViT_base_' + model_weight_idx + '_PromptDeep_' + str(
                Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr
            + '_prompting_' + dataset_name + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(
                Prompt_Token_num) + ' --dataroot ' + str(dataroot) + ' --draw_root '
            + draw_root + ' --Pre_Trained_model_path ' + Pre_Trained_model_path + ' --model_path ' + save_model_PATH)
        # VPT + finetuning
        print(
            'python Train.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --model_idx ViT_base_' + model_weight_idx
            + '_PromptDeep_' + str(
                Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr + '_finetuning_' + dataset_name
            + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(
                Prompt_Token_num) + ' --PromptUnFreeze --dataroot ' + str(dataroot) + ' --draw_root ' + draw_root
            + ' --Pre_Trained_model_path ' + Pre_Trained_model_path + ' --model_path ' + save_model_PATH)
        print(
            'python Test.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --model_idx ViT_base_' + model_weight_idx + '_PromptDeep_' + str(
                Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr
            + '_finetuning_' + dataset_name + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(
                Prompt_Token_num) + ' --PromptUnFreeze --dataroot ' + str(dataroot)
            + ' --draw_root ' + draw_root + ' --model_path ' + save_model_PATH)
    else:
        # send a VPT prompt state inside to build the prompt tokens
        # we build VPT backbone with the ViT-timm weight, then do finetuning and prompting
        # fixme notice here Pre_Trained_model_path is actually the trained prompt state path
        # VPT + prompting
        print(
            'python Train.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --model_idx ViT_base_' + model_weight_idx
            + '_PromptDeep_' + str(Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr + '_prompting_' + dataset_name
            + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(Prompt_Token_num) + ' --dataroot ' + str(
                dataroot) + ' --draw_root ' + draw_root
            + ' --Pre_Trained_model_path ' + VPT_backbone_model_path + ' --Prompt_state_path ' + Pre_Trained_model_path + ' --model_path ' + save_model_PATH)
        print(
            'python Test.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --model_idx ViT_base_' + model_weight_idx + '_PromptDeep_' + str(
                Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr
            + '_prompting_' + dataset_name + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(
                Prompt_Token_num) + ' --dataroot ' + str(dataroot) + ' --draw_root '
            + draw_root + ' --Pre_Trained_model_path ' + VPT_backbone_model_path + ' --model_path ' + save_model_PATH)
        # VPT + finetuning
        print(
            'python Train.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --model_idx ViT_base_' + model_weight_idx
            + '_PromptDeep_' + str(
                Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr + '_finetuning_' + dataset_name
            + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(
                Prompt_Token_num) + ' --PromptUnFreeze --dataroot ' + str(dataroot) + ' --draw_root ' + draw_root
            + ' --Pre_Trained_model_path ' + VPT_backbone_model_path + ' --Prompt_state_path ' + Pre_Trained_model_path + ' --model_path ' + save_model_PATH)
        print(
            'python Test.py --gpu_idx ' + GPU_idx + ' --edge_size 224 --data_augmentation_mode ' + data_augmentation_mode
            + ' --model_idx ViT_base_' + model_weight_idx + '_PromptDeep_' + str(
                Prompt_Token_num) + '_' + lr_mystr + '_lf' + lrf_mystr
            + '_finetuning_' + dataset_name + '_CLS --PromptTuning Deep --Prompt_Token_num ' + str(
                Prompt_Token_num) + ' --PromptUnFreeze --dataroot ' + str(dataroot)
            + ' --draw_root ' + draw_root + ' --model_path ' + save_model_PATH)

    print('')


def write_PuzzleTuning_comparison_script(lr_mystr, lrf_mystr, data_augmentation_mode, dataset_name, GPU_idx='0'):
    """
    In PuzzleTuning comparison experiments we put
    datasets at: --dataroot /root/autodl-tmp/datasets
    Pre_Trained_model_path /root/autodl-tmp/pre_trained_models  # output_models (not applicable for comparison)
    Prompt_state_path (not applicable for comparison) /root/autodl-tmp/output_models
    save the training model at: model_path /root/autodl-tmp/saved_models
    draw_root /root/autodl-tmp/PuzzleTuning_Comparison/[*lr*_*lrf*_*dataset_name*]

    """
    dataroot_PATH = '/root/autodl-tmp/datasets'
    Pre_Trained_model_path_PATH = '/root/autodl-tmp/pre_trained_models'
    save_model_PATH = '/root/autodl-tmp/saved_models'
    draw_root_PATH = '/root/autodl-tmp/PuzzleTuning_Comparison'

    data_augmentation_mode = str(data_augmentation_mode)
    GPU_idx = str(GPU_idx)

    lr = str(zero_trans_mystrlr_to_float(lr_mystr))
    lrf = '0.' + str(lrf_mystr)

    experiment_idx = lr_mystr + '_lf' + lrf_mystr + '_' + dataset_name

    dataroot = os.path.join(dataroot_PATH, dataset_name + '_CLS')
    draw_root = os.path.join(draw_root_PATH, experiment_idx)

    # PuzzleTuning official version:
    # we pre-trained VPT prompt tokens, and use the timm ViT as backbone
    print('#SAE-timm-start_promptstate')  # SAE+VPT start with timm
    model_weight_idx = 'ViT_base_timm_PuzzleTuning_SAE_E_199_promptstate'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_SAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=True)

    # Comparison methods:

    # For the comparison methods: we trained ViT, so we use ViT + ft first,
    # and then, put it as vpt 's backbone in prompting and VPT finetuning.
    print('#空白对比')
    model_weight_idx = 'random'
    model_weight_name = 'ViT_b16_224_Random_Init.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#timm对比')
    model_weight_idx = 'timm'
    model_weight_name = 'ViT_b16_224_Imagenet.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#MAEImageNet对比')
    model_weight_idx = 'MAEImageNet'
    model_weight_name = 'ViT_b16_224_MAEImageNet_Init.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#mae对比')
    model_weight_idx = 'timm_mae_CPIAm_E100'
    model_weight_name = 'ViT_b16_224_timm_mae_ALL_100.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#moco对比')
    model_weight_idx = 'timm_moco_CPIAm_E100'
    model_weight_name = 'ViT_b16_224_timm_moco_ALL_100.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#dino对比')
    model_weight_idx = 'timm_dino_CPIAm_E100'
    model_weight_name = 'ViT_b16_224_timm_dino_ALL_100.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#BYOL对比')
    model_weight_idx = 'timm_BYOL_CPIAm_E50'
    model_weight_name = 'ViT_b16_224_timm_BYOL_ALL_50.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#GCMAE对比')
    model_weight_idx = 'timm_GCMAE_CPIAm_E80'
    model_weight_name = 'ViT_b16_224_timm_GCMAE_ALL_80.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#SDMAE对比')
    model_weight_idx = 'timm_SDMAE_CPIAm_E80'
    model_weight_name = 'ViT_b16_224_timm_SDMAE_ALL_80.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#SIMMIM对比')
    model_weight_idx = 'timm_SIMMIM_CPIAm_E200'
    model_weight_name = 'ViT_b16_224_timm_SIMMIM_ALL_200.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#SIMCLR对比')
    model_weight_idx = 'timm_SIMCLR_CPIAm_E100'
    model_weight_name = 'ViT_b16_224_timm_SIMCLR_ALL_100.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    # Ablation versions:

    # For ablation SAE-ViT version, we pre-trained ViT, so we use ViT + ft first,
    # and then, put it as vpt 's backbone in prompting and VPT finetuning.
    print('#PuzzleTuning_SAE_ViT-CPIA对比')
    model_weight_idx = 'timm_PuzzleTuning_SAE_E_199'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_SAE_CPIAm_E_199.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#SAE_fixp16fixr25-timm-start')  # SAE_fixp16fixr25+ViT start with timm
    model_weight_idx = 'ViT_base_timm_PuzzleTuning_SAE_fixp16fixr25_E_199'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_SAE_fixp16fixr25_CPIAm_E_199.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    print('#SAE_fixp16ratiodecay-timm-start')  # SAE_fixp16ratiodecay+ViT start with timm
    model_weight_idx = 'ViT_base_timm_PuzzleTuning_SAE_fixp16ratiodecay_E_199'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_SAE_fixp16ratiodecay_CPIAm_E_199.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=False)

    # For ablation SAE-VPT version, we pre-trained VPT prompt tokens, and use the timm ViT as backbone
    print('#MAE-VPT_promptstate')  # MAE+VPT
    model_weight_idx = 'timm_mae_Prompt_CPIAm_E199_promptstate'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_MAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=True)

    print('#SAE-MAE-start_promptstate')  # SAE+VPT start with MAEImageNet
    model_weight_idx = 'ViT_base_MAEImageNet_PuzzleTuning_SAE_E_199_promptstate'
    model_weight_name = 'ViT_b16_224_MAEImageNet_PuzzleTuning_SAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=True)

    print('#SAE-Random-start_promptstate')  # SAE+VPT start with Random
    model_weight_idx = 'ViT_base_Random_PuzzleTuning_SAE_E_199_promptstate'
    model_weight_name = 'ViT_b16_224_Random_PuzzleTuning_SAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=True)

    print('#SAE_fixp16fixr25-timm-start_promptstate')  # SAE_fixp16fixr25+VPT start with timm
    model_weight_idx = 'ViT_base_timm_PuzzleTuning_SAE_fixp16fixr25_E_199_promptstate'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_SAE_fixp16fixr25_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=True)

    print('#SAE_fixp16ratiodecay-timm-start_promptstate')  # SAE_fixp16ratiodecay+VPT start with timm
    model_weight_idx = 'ViT_base_timm_PuzzleTuning_SAE_fixp16ratiodecay_E_199_promptstate'
    model_weight_name = 'ViT_b16_224_timm_PuzzleTuning_SAE_fixp16ratiodecay_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth'
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=True)

    print('')
    print('cd /home/pancreatic-cancer-diagnosis-tansformer/code/utils')
    record_dir = os.path.join(draw_root, 'CSV_logs')
    print('python check_log_json.py --enable_notify --draw_root ' + draw_root + ' --record_dir ' + record_dir)
    print('cd /home/pancreatic-cancer-diagnosis-tansformer/code')


def write_additional_PuzzleTuning_comparison_script(add_idx, lr_mystr, lrf_mystr, data_augmentation_mode, dataset_name,
                                                    model_weight_idx='timm_mae_CPIAm_E100',
                                                    model_weight_name='ViT_b16_224_timm_mae_ALL_100.pth',
                                                    GPU_idx='0', Prompt_input=False):
    """
    In PuzzleTuning comparison experiments we put
    datasets at: --dataroot /root/autodl-tmp/datasets
    Pre_Trained_model_path /root/autodl-tmp/pre_trained_models  # output_models (not applicable for comparison)
    Prompt_state_path (not applicable for comparison) /root/autodl-tmp/output_models
    save the training model at: model_path /root/autodl-tmp/saved_models
    draw_root /root/autodl-tmp/PuzzleTuning_Comparison/[*lr*_*lrf*_*dataset_name*]

    # fixme the additional experiments settings need to manually set!!!
    in the additional experiments, we save the runs to
    draw_root /root/autodl-tmp/runs/[*lr*_*lrf*_*dataset_name*]
    and then copy a duplicates to /root/autodl-tmp/PuzzleTuning_Comparison/[*lr*_*lrf*_*dataset_name*]

    """
    dataroot_PATH = '/root/autodl-tmp/datasets'
    Pre_Trained_model_path_PATH = '/root/autodl-tmp/pre_trained_models'
    save_model_PATH = '/root/autodl-tmp/saved_models'
    draw_root_PATH = '/root/autodl-tmp/runs'
    copy_to_draw_root_PATH = '/root/autodl-tmp/PuzzleTuning_Comparison'

    data_augmentation_mode = str(data_augmentation_mode)
    GPU_idx = str(GPU_idx)

    lr = str(zero_trans_mystrlr_to_float(lr_mystr))
    lrf = '0.' + str(lrf_mystr)

    experiment_idx = lr_mystr + '_lf' + lrf_mystr + '_' + dataset_name
    add_experiment_idx = add_idx + '_' + lr_mystr + '_lf' + lrf_mystr + '_' + dataset_name

    dataroot = os.path.join(dataroot_PATH, dataset_name + '_CLS')
    # additional exp runs path
    draw_root = os.path.join(draw_root_PATH, add_experiment_idx)
    # basic all exp runs path
    copy_draw_root = os.path.join(copy_to_draw_root_PATH, experiment_idx)

    print('# Additional ' + add_idx)
    print_a_PuzzleTuning_comparison_script(model_weight_idx, model_weight_name, lr, lrf, lr_mystr, lrf_mystr,
                                           dataset_name, dataroot, draw_root, Pre_Trained_model_path_PATH,
                                           save_model_PATH, data_augmentation_mode, GPU_idx, Prompt_input=Prompt_input)
    print('')
    print('cd /home/pancreatic-cancer-diagnosis-tansformer/code/utils')
    # update the total record
    print('')
    print('cp -r ' + draw_root + '/*' + ' ' + copy_draw_root)
    record_dir = os.path.join(copy_draw_root, 'CSV_logs')
    print('python check_log_json.py --draw_root ' + copy_draw_root + ' --record_dir ' + record_dir)

    # update the additional runs and send to notify
    record_dir = os.path.join(draw_root, add_experiment_idx)
    print('python check_log_json.py --enable_notify --draw_root ' + draw_root + ' --record_dir ' + record_dir)

    print('cd /home/pancreatic-cancer-diagnosis-tansformer/code')


def write_CLS_script(model_idxs, data_augmentation_mode, edge_size, batch_size, lr, lrf, enable_tensorboard,
                     test_enable_attention_check, dataset_name, dataroot, model_path, draw_root):
    data_augmentation_mode = str(data_augmentation_mode)
    edge_size_ipt = str(edge_size)
    batch_size = str(batch_size)
    lr_name = zero_trans_floatlr_to_mystrlr(lr)
    lr = str(lr)
    lf_name = str(int(100 * lrf))
    lrf = str(lrf)
    dataroot = dataroot + dataset_name + '_CLS'

    for model_idx in model_idxs:

        # alter the edge size for certain models
        if model_idx in ['cross_former', 'convit', 'visformer', 'ViT_h']:
            edge_size = '224'
        else:
            edge_size = edge_size_ipt

        if enable_tensorboard is True:
            print('python Train.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_CLS --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode + ' --batch_size ' + batch_size +
                  ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --dataroot ' + dataroot +
                  ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')
        else:
            print('python Train.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_CLS --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode + ' --batch_size ' + batch_size +
                  ' --lr ' + lr + ' --lrf ' + lrf + ' --dataroot ' + dataroot +
                  ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')

    for model_idx in model_idxs:

        # alter the edge size for certain models
        if model_idx in ['cross_former', 'convit', 'visformer', 'ViT_h']:
            edge_size = '224'
        else:
            edge_size = edge_size_ipt

        if test_enable_attention_check is True:
            print('python Test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_CLS --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode + ' --enable_attention_check --dataroot '
                  + dataroot + ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')
        else:
            print('python Test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_CLS --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode + ' --dataroot '
                  + dataroot + ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')


def write_CLS_AUG_script(model_idx, augmentation_names, data_augmentation_mode, edge_size, batch_size, lr, lrf,
                         test_enable_attention_check, enable_tensorboard, dataset_name, dataroot, model_path,
                         draw_root):
    data_augmentation_mode = str(data_augmentation_mode)
    data_augmentation_mode = str(data_augmentation_mode)
    edge_size = str(edge_size)
    batch_size = str(batch_size)
    lr_name = zero_trans_floatlr_to_mystrlr(lr)
    lr = str(lr)
    lf_name = str(int(100 * lrf))
    lrf = str(lrf)
    dataroot = dataroot + dataset_name + '_CLS'

    for augmentation_name in augmentation_names:
        if enable_tensorboard is True:
            print('python Train.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_' + augmentation_name + '_CLS' +
                  ' --augmentation_name ' + augmentation_name + ' --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode + ' --batch_size ' + batch_size +
                  ' --lr ' + lr + ' --lrf ' + lrf + ' --enable_tensorboard --dataroot ' + dataroot +
                  ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')
        else:
            print('python Train.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_' + augmentation_name + '_CLS' +
                  ' --augmentation_name ' + augmentation_name + ' --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode + ' --batch_size ' + batch_size +
                  ' --lr ' + lr + ' --lrf ' + lrf + ' --dataroot ' + dataroot +
                  ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')

    for augmentation_name in augmentation_names:
        if test_enable_attention_check is True:
            print('python Test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_' + augmentation_name + '_CLS' +
                  ' --edge_size ' + edge_size + ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --enable_attention_check --dataroot ' + dataroot + ' --model_path ' + model_path +
                  ' --draw_root ' + draw_root)
            print('')
        else:
            print('python Test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_' + dataset_name + '_' + augmentation_name + '_CLS' +
                  ' --edge_size ' + edge_size + ' --data_augmentation_mode ' + data_augmentation_mode + ' --dataroot '
                  + dataroot + ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')


def write_MIL_script(model_idxs, data_augmentation_mode, edge_size, batch_size, patch_size, lr, lrf, enable_tensorboard,
                     test_enable_attention_check, dataset_name, dataroot, model_path, draw_root, imaging_root=None):
    # imaging_root 是放画图的检查的路径,可以和draw一样
    if imaging_root == None:
        imaging_root = draw_root

    data_augmentation_mode = str(data_augmentation_mode)
    edge_size = str(edge_size)
    batch_size = str(batch_size)
    patch_size = str(patch_size)
    lr_name = zero_trans_floatlr_to_mystrlr(lr)
    lr = str(lr)
    lf_name = str(int(100 * lrf))
    lrf = str(lrf)
    dataroot = dataroot + dataset_name + '_MIL'
    CLS_dataroot = dataroot + dataset_name + '_CLS'

    for model_idx in model_idxs:
        if enable_tensorboard is True:
            print('python MIL_train.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --edge_size ' + edge_size + ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --batch_size ' + batch_size + ' --patch_size ' + patch_size + ' --lr ' + lr + ' --lrf '
                  + lrf + ' --enable_tensorboard --dataroot ' + dataroot + ' --model_path ' + model_path
                  + ' --draw_root ' + draw_root)
            print('')
        else:
            print('python MIL_train.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --edge_size ' + edge_size + ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --batch_size ' + batch_size + ' --patch_size ' + patch_size + ' --lr ' + lr + ' --lrf '
                  + lrf + ' --dataroot ' + dataroot + ' --model_path ' + model_path + ' --draw_root ' + draw_root)
            print('')

    for model_idx in model_idxs:
        print('python MIL_test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
              + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
              '_MIL --edge_size ' + edge_size + ' --patch_size ' + patch_size +
              ' --batch_size 1 --data_augmentation_mode ' + data_augmentation_mode + ' --dataroot ' +
              dataroot + ' --model_path ' + model_path + ' --draw_root ' + draw_root)
        print('')

        if test_enable_attention_check is True:  # 设置多个batch的实验
            print('python Test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --edge_size ' + edge_size + ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --MIL_Stripe --enable_attention_check --check_minibatch 10' +
                  ' --dataroot ' + CLS_dataroot + ' --model_path ' + model_path +
                  ' --draw_root ' + imaging_root)
            print('')
            print('python MIL_test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --shuffle_attention_check --MIL_Stripe --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --shuffle_dataloader --batch_size 4 --check_minibatch 10' + ' --patch_size ' + patch_size +
                  ' --dataroot ' + dataroot + ' --model_path ' + model_path +
                  ' --draw_root ' + imaging_root)
            print('')
            print('python MIL_test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --shuffle_attention_check --MIL_Stripe --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --batch_size 4 --check_minibatch 10' + ' --patch_size ' + patch_size +
                  ' --dataroot ' + dataroot + ' --model_path ' + model_path +
                  ' --draw_root ' + imaging_root)
            print('')
            print('python MIL_test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --shuffle_attention_check --MIL_Stripe --edge_size ' + edge_size +
                  ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --batch_size 1 --check_minibatch 10' + ' --patch_size ' + patch_size +
                  ' --dataroot ' + dataroot + ' --model_path ' + model_path +
                  ' --draw_root ' + imaging_root)
            print('')

        else:
            print('python Test.py --model_idx ' + model_idx + '_' + edge_size + '_' + lr_name
                  + '_PT_lf' + lf_name + '_b' + batch_size + '_p' + patch_size + '_' + dataset_name +
                  '_MIL --edge_size ' + edge_size + ' --data_augmentation_mode ' + data_augmentation_mode +
                  ' --MIL_Stripe --dataroot ' + CLS_dataroot + ' --model_path ' + model_path +
                  ' --draw_root ' + draw_root)
            print('')


'''
if __name__ == '__main__':

    print('#!/bin/sh')
    print('')
    # CLS-MIL调参的第一步是使用一个经验参数进行简单摸索,看看大家结果大概是多少,同时和文献进行对比
    # 首先摸索CLS对比实验结果
    model_idxs = ['ViT', 'vgg16', 'vgg19', 'mobilenetv3', 'inceptionv3', 'xception',
                  'ResNet50', 'efficientnet_b3', 'swin_b', 'ResN50_ViT', 'conformer', 'cross_former']

    batch_size = 8
    dataset_name = 'NCT-CRC-HE-100K'

    write_CLS_script(model_idxs=model_idxs,
                     data_augmentation_mode=3,
                     edge_size=384,
                     batch_size=batch_size,
                     lr=0.000007,
                     lrf=0.35,
                     enable_tensorboard=True,
                     test_enable_attention_check=True,
                     dataset_name=dataset_name,
                     dataroot='/root/autodl-tmp/datasets/',
                     model_path='/root/autodl-tmp/saved_models',
                     draw_root='/root/autodl-tmp/runs')

    # 正式实验的时候,后面还需要做各种MIL的消融实验
    # TODO 更多write_MIL_script
    # 其次摸索CLS+特定模型vit+不同数据增强 对比实验结果
    augmentation_names = ['Cutout', 'Mixup', 'CutMix']
    write_CLS_AUG_script(model_idx='ViT',
                         augmentation_names=augmentation_names,
                         data_augmentation_mode=3,
                         edge_size=384,
                         batch_size=batch_size,
                         lr=0.000007,
                         lrf=0.35,
                         enable_tensorboard=True,
                         test_enable_attention_check=True,
                         dataset_name=dataset_name,
                         dataroot='/root/autodl-tmp/datasets/',
                         model_path='/root/autodl-tmp/saved_models',
                         draw_root='/root/autodl-tmp/runs')

    # 最后摸索MIL+ViT的实验结果
    MIL_model_idxs = ['ViT', ]
    # MIL ablations
    write_MIL_script(model_idxs=MIL_model_idxs,
                     data_augmentation_mode=3,
                     edge_size=384,
                     batch_size=batch_size,
                     patch_size=16,
                     lr=0.000007,
                     lrf=0.35,
                     enable_tensorboard=True,
                     test_enable_attention_check=False,
                     dataset_name=dataset_name,
                     dataroot='/root/autodl-tmp/datasets/',
                     model_path='/root/autodl-tmp/saved_models',
                     draw_root='/root/autodl-tmp/runs',
                     imaging_root='/root/autodl-tmp/imaging_results')
    write_MIL_script(model_idxs=MIL_model_idxs,
                     data_augmentation_mode=3,
                     edge_size=384,
                     batch_size=batch_size,
                     patch_size=64,
                     lr=0.000007,
                     lrf=0.35,
                     enable_tensorboard=True,
                     test_enable_attention_check=False,
                     dataset_name=dataset_name,
                     dataroot='/root/autodl-tmp/datasets/',
                     model_path='/root/autodl-tmp/saved_models',
                     draw_root='/root/autodl-tmp/runs',
                     imaging_root='/root/autodl-tmp/imaging_results')
    write_MIL_script(model_idxs=MIL_model_idxs,
                     data_augmentation_mode=3,
                     edge_size=384,
                     batch_size=batch_size,
                     patch_size=48,
                     lr=0.000007,
                     lrf=0.35,
                     enable_tensorboard=True,
                     test_enable_attention_check=False,
                     dataset_name=dataset_name,
                     dataroot='/root/autodl-tmp/datasets/',
                     model_path='/root/autodl-tmp/saved_models',
                     draw_root='/root/autodl-tmp/runs',
                     imaging_root='/root/autodl-tmp/imaging_results')
    write_MIL_script(model_idxs=MIL_model_idxs,
                     data_augmentation_mode=3,
                     edge_size=384,
                     batch_size=batch_size,
                     patch_size=96,
                     lr=0.000007,
                     lrf=0.35,
                     enable_tensorboard=True,
                     test_enable_attention_check=False,
                     dataset_name=dataset_name,
                     dataroot='/root/autodl-tmp/datasets/',
                     model_path='/root/autodl-tmp/saved_models',
                     draw_root='/root/autodl-tmp/runs',
                     imaging_root='/root/autodl-tmp/imaging_results')
    write_MIL_script(model_idxs=MIL_model_idxs,
                     data_augmentation_mode=3,
                     edge_size=384,
                     batch_size=batch_size,
                     patch_size=128,
                     lr=0.000007,
                     lrf=0.35,
                     enable_tensorboard=True,
                     test_enable_attention_check=False,
                     dataset_name=dataset_name,
                     dataroot='/root/autodl-tmp/datasets/',
                     model_path='/root/autodl-tmp/saved_models',
                     draw_root='/root/autodl-tmp/runs',
                     imaging_root='/root/autodl-tmp/imaging_results')

    # 调参实验的时候,先调MIL到最好,然后用参数去跑CLS实验看结果

    print('cd /home/pancreatic-cancer-diagnosis-tansformer/code/utils')
    print('')
    print(
        'python check_log_json.py --enable_notify --draw_root /root/autodl-tmp/runs --record_dir /root/autodl-tmp/CSV_logs')
    print('')
    print('shutdown')
'''


def get_args_parser():
    parser = argparse.ArgumentParser(description='Automatically write shell script for training')

    # Model Name or index
    parser.add_argument('--lr_mystr', default=None, type=str, help='Model lr EG: 506 -> 0.000006')
    parser.add_argument('--lrf_mystr', default=None, type=str, help='Model lrf EG: 50 -> cosine decay to 50%')
    parser.add_argument('--data_augmentation_mode', default=None, type=str, help='ROSE,pRCC:0; CAM16,WBC:3')
    parser.add_argument('--dataset_name', default=None, type=str, help='ROSE,pRCC,CAM16,WBC ?')
    parser.add_argument('--GPU_idx', default='0', type=str, help='Experiment GPU_idx EG: 0')

    return parser


if __name__ == '__main__':
    parser = get_args_parser()
    args = parser.parse_args()

    print('#!/bin/sh')
    print('')
    # add DropPos-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='DropPos-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_DropPos_CPIAm_E200',
                                                    model_weight_name='ViT_b16_224_timm_DropPos_ALL_200.pth',
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)

    '''
    # add MAE-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='MAE-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name, 
                                                    model_weight_idx='timm_mae_CPIAm_E100',
                                                    model_weight_name='ViT_b16_224_timm_mae_ALL_100.pth', 
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)
    # add SDMAE-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='SDMAE-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_SDMAE_CPIAm_E80',
                                                    model_weight_name='ViT_b16_224_timm_SDMAE_ALL_80.pth',
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)
    # add GCMAE-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='GCMAE-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_GCMAE_CPIAm_E80',
                                                    model_weight_name='ViT_b16_224_timm_GCMAE_ALL_80.pth',
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)
    # add JIGSAW-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='JIGSAW-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_JIGSAW_CPIAm_E50',
                                                    model_weight_name='ViT_b16_224_timm_JIGSAW_ALL_50.pth',
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)
                                                    
    # add DropPos-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='DropPos-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_DropPos_CPIAm_E200',
                                                    model_weight_name='ViT_b16_224_timm_DropPos_ALL_200.pth',
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)
                                                    
    # add MAE+VPT
    write_additional_PuzzleTuning_comparison_script(add_idx='MAE-VPT_promptstate',
                                                    lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_mae_Prompt_CPIAm_E199_promptstate',
                                                    model_weight_name='ViT_b16_224_timm_PuzzleTuning_MAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth',
                                                    GPU_idx='0', Prompt_input=True)
    # add SAE-MAE-start
    write_additional_PuzzleTuning_comparison_script(add_idx='SAE-MAE-start_promptstate',
                                                    lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='ViT_base_MAEImageNet_PuzzleTuning_SAE_E_199_promptstate',
                                                    model_weight_name='ViT_b16_224_MAEImageNet_PuzzleTuning_SAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth',
                                                    GPU_idx='0', Prompt_input=True)
    # add SAE-Random-start
    write_additional_PuzzleTuning_comparison_script(add_idx='SAE-Random-start_promptstate',
                                                    lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='ViT_base_Random_PuzzleTuning_SAE_E_199_promptstate',
                                                    model_weight_name='ViT_b16_224_Random_PuzzleTuning_SAE_CPIAm_Prompt_Deep_tokennum_20_E_199_promptstate.pth',
                                                    GPU_idx='0', Prompt_input=True)
                                                    
    # add PuzzleTuning_SAE_ViT_to_VPT-CPIA
    write_additional_PuzzleTuning_comparison_script(add_idx='PuzzleTuning_SAE_ViT-CPIA', lr_mystr=args.lr_mystr,
                                                    lrf_mystr=args.lrf_mystr,
                                                    data_augmentation_mode=args.data_augmentation_mode,
                                                    dataset_name=args.dataset_name,
                                                    model_weight_idx='timm_PuzzleTuning_SAE_E_199',
                                                    model_weight_name='ViT_b16_224_timm_PuzzleTuning_SAE_CPIAm_E_199.pth',
                                                    GPU_idx=args.GPU_idx, Prompt_input=False)
    '''

    # rewrite all
    '''
    write_PuzzleTuning_comparison_script(lr_mystr=args.lr_mystr, lrf_mystr=args.lrf_mystr,
                                         data_augmentation_mode=args.data_augmentation_mode,
                                         dataset_name=args.dataset_name, GPU_idx=args.GPU_idx)
    '''


    '''
    we can use the following codes to generates the additional exp scripts
    
    # read and auto generate task info
    import os
    path='/root/autodl-tmp/PuzzleTuning_Comparison'
    data_augmentation_dic = {'ROSE': '0', 'pRCC': '0', 'CAM16': '3', 'WBC': '3'}
    for exp_root in os.listdir(path):
        out_sh_name = exp_root + '.sh'
        lr_mystr = exp_root.split('_')[0]
        lrf_mystr = exp_root.split('_')[1].split('lf')[-1]
        dataset_name = exp_root.split('_')[-1]
        data_augmentation_mode = data_augmentation_dic[dataset_name]
        print('nohup python Experiment_script_helper.py --lr_mystr ' + lr_mystr + ' --lrf_mystr ' + lrf_mystr
              + ' --data_augmentation_mode ' + data_augmentation_mode + ' --dataset_name ' + dataset_name + ' > '
              + out_sh_name + ' 2>&1 &')
    
    # then, we use the shell to run this code with the generated lines
    
    # the generate sh files has a nohup line at their first lines, so we can use this to erase
    remove_nohup_ignoring_input_at_first_line(directory='./')
    
    # we can use the func to combine the sh files:
    concatenate_the_lines_from_several_files(directory='./', cat_file='0.sh')
    '''