File size: 1,880 Bytes
6fc683c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import numpy as np
import torch
import os
import shutil
# import logging
import sys


# def set_logging(args):
#     '''
#     Set logger for recording
#     '''
#     logging.basicConfig(filename="./output/{}/log.txt".format(args.exp_name), level=logging.INFO,
#                         format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
#     logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
#     logging.info(str(args))



def set_seed(args):
    '''
    Set seed for reproducibility
    '''
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    # if args.n_gpu > 0: 
    #     torch.cuda.manual_seed_all(args.seed)


def set_exp_folder(args):
    '''
    Create a folder to store experimental results e.g., checkpoints or log
    '''
    os.makedirs(os.path.join(args.output_dir, 'output'), exist_ok=True) 

    if os.path.exists(os.path.join('output', args.exp_name)):
        if not args.overwrite_output_dir: 
            assert False, 'The exp_name is already used. Please modify the experiment name or use --overwrite_output_dir'
        else:
            print('Remove original directories.')
            shutil.rmtree(os.path.join('output', args.exp_name))
            print('Remove successfully.')
    
    os.makedirs(os.path.join(args.output_dir, 'output', args.exp_name), exist_ok=True)
    exp_path = os.path.join(args.output_dir, 'output', args.exp_name)
    print(f'Path [{exp_path}] has been created')


def check_screen():
    '''
    Check whether the experiment is in screen
    '''
    text = os.popen('echo $STY').readlines()
    string = ''
    for line in text:
        string += line
    if len(string.strip()) == 0:
        print("**** Attention Please! The code is not executed in Screen! ****")
    else:
        print(f'**** Screen Name : {string} ****')