-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpretrain_reps.py
161 lines (131 loc) · 6.56 KB
/
pretrain_reps.py
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
import logging
import pickle
import os
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from envs.MVBipedal import MVBipedalDataset, MVBipedalSeqDataset
from reps.MVTCAE import MVTCAE
from reps.CMC import CMC
from reps.MVSSM import MVSSM
from reps.trainer_tester import trainer, tester
def train(config):
env_name = config['env_name']
method_name = config['method']
cuda = config['cuda']
if env_name in ['BipedalWalker-v3'] and method_name in ["MVTCAE", "MVSSM", "SLAC", "CMC"]:
root_dir = './'
log_dir = f'representations/{method_name}/'
else:
raise ValueError(f'Invalid environment name / method name: {env_name} / {method_name}')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
trained_model_dir = os.path.join(log_dir, '{}_pretrained'.format(config['rl_algorithm']))
if not os.path.exists(trained_model_dir):
os.makedirs(trained_model_dir)
should_resume = False
trained_model_filename = '_'.join([str(config[k]) for k in config['save_keys_reps']]) + '.pth'
best_trained_model_filename = '_'.join([str(config[k]) for k in config['save_keys_reps']]) + '_best.pth'
trained_model_path = os.path.join(trained_model_dir, trained_model_filename)
best_trained_model_path = os.path.join(trained_model_dir, best_trained_model_filename)
if os.path.isfile(trained_model_path):
checkpoint = torch.load(trained_model_path)
if checkpoint['epoch'] == config['epochs']:
print('Fully trained Representation already exists!!!')
return
else:
print('Resuming Representation Pretraining!!')
should_resume = True
else:
print('Pretraining Representation from the scratch!')
train_data_filename = 'dataset/pretrain_train_data_PPO.pkl'
train_data_dir = root_dir + train_data_filename
valid_data_filename = 'dataset/pretrain_valid_data_PPO.pkl'
valid_data_dir = root_dir + valid_data_filename
### Load precollected trajectories & Generate multi-view data ##
if env_name in ['BipedalWalker-v3']:
logging.info('Reading `{}` file...'.format(train_data_dir))
with open(train_data_dir, 'rb') as file:
input_train = pickle.load(file)
logging.info('Reading `{}` file...'.format(valid_data_dir))
with open(valid_data_dir, 'rb') as file:
input_valid = pickle.load(file)
if method_name in ['MVTCAE', 'CMC']:
MVBipedal_train = MVBipedalDataset(input_train, cuda)
MVBipedal_valid = MVBipedalDataset(input_valid, cuda)
action_size = None
elif method_name in ['MVSSM', 'SLAC']:
MVBipedal_train = MVBipedalSeqDataset(input_train, 8, cuda)
MVBipedal_valid = MVBipedalSeqDataset(input_valid, 8, cuda)
action_size = MVBipedal_train.get_action_size()
else:
raise ValueError(f'Invalid method name ', method_name)
view_sizes = MVBipedal_train.get_view_sizes()
view_binaries = MVBipedal_train.get_view_binaries()
train_loader = DataLoader(MVBipedal_train, batch_size=config['batch_size'],
shuffle=True,
num_workers=0, drop_last=True)
valid_loader = DataLoader(MVBipedal_valid, batch_size=config['batch_size'],
shuffle=False,
num_workers=0, drop_last=True)
print("Num train : ", MVBipedal_train.num_samples)
print("Num valid : ", MVBipedal_valid.num_samples)
else:
raise ValueError(f'Invalid environment name: {env_name}')
if config['even_dim_recon']:
max_view_size = max(view_sizes)
config['lambda_views'] = [max_view_size / float(view_size) for view_size in view_sizes]
else:
config['lambda_views'] = [1.0]*len(view_sizes)
N_mini_batches = len(train_loader)
### Create a model ###
if method_name == 'MVTCAE':
config['use_prior_expert'] = False
model = MVTCAE(config['rec_coeff'], config['activation'], config['n_latents'], view_sizes, config['hidden_size'],
view_binaries, use_prior_expert=config['use_prior_expert'])
elif method_name == 'CMC':
model = CMC(config['rec_coeff'], config['activation'], config['n_latents'], view_sizes, config['hidden_size'],
view_binaries, use_prior_expert=config['use_prior_expert'])
elif method_name in ['MVSSM', 'SLAC']:
config['use_prior_expert'] = False
model = MVSSM(config['rec_coeff'], config['activation'], config['n_latents'], view_sizes, config['hidden_size'],
action_size, view_binaries, use_prior_expert=config['use_prior_expert'], method=method_name)
else:
raise ValueError(f'Invalid method name: {method_name}')
optimizer = optim.Adam(model.parameters(), lr=config['lr'])
start_epoch = 0
training_info = {
'epoch': 0,
'logs': [],
}
min_valid_loss = 100000.
if should_resume:
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch']
min_valid_loss = checkpoint['min_valid_loss']
print('-- resumed training information -- ')
print(' ** start_epoch : ', start_epoch)
print(' ** min_valid_loss : ', min_valid_loss)
if config['cuda']:
model.cuda()
for training_info['epoch'] in range(start_epoch+1, config['epochs'] + 1):
loss_dict = trainer(training_info['epoch'], model, train_loader, config['cuda'], optimizer,
config['lambda_views'], config['anneal_epochs'], N_mini_batches, config['log_interval'])
_, avg_valid_loss = tester(training_info['epoch'], model, valid_loader, config['lambda_views'])
print(f'---- Save latest model ----')
torch.save({
'epoch': training_info['epoch'],
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'min_valid_loss': min_valid_loss
}, trained_model_path)
if min_valid_loss >= avg_valid_loss:
min_valid_loss = avg_valid_loss
print(f'---- Save best model (valid loss : ', min_valid_loss, ' )')
torch.save({
'epoch': training_info['epoch'],
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'min_valid_loss': min_valid_loss
}, best_trained_model_path)