class Novelty: def __init__(self, state_dims, device): self.device = device self.model = FCN(state_dims, state_dims).to(device) self.model_optim = Adam(self.model.parameters(), lr=p.lr) def get_reward(self, state, next_state): with torch.no_grad(): pred_next_state = self.model(state) try: reward = ((pred_next_state - next_state)**2).mean(1) except IndexError: reward = ((pred_next_state - next_state)**2).mean() return reward def update(self, memory): state_batch, action_batch, reward_batch, next_state_batch, mask_batch = memory.sample( p.reward_model_batch_size) state_batch = torch.FloatTensor(state_batch).to(self.device) next_state_batch = torch.FloatTensor(next_state_batch).to(self.device) pred_next_states = self.model(state_batch) loss = F.mse_loss(next_state_batch, pred_next_states) self.model_optim.zero_grad() loss.backward() self.model_optim.step() return loss.item()
class PolicyGradient: def __init__(self, n_states,device='cpu',gamma = 0.99,lr = 0.01,batch_size=5): self.gamma = gamma self.policy_net = FCN(n_states) self.optimizer = torch.optim.RMSprop(self.policy_net.parameters(), lr=lr) self.batch_size = batch_size def choose_action(self,state): state = torch.from_numpy(state).float() state = Variable(state) probs = self.policy_net(state) m = Bernoulli(probs) action = m.sample() action = action.data.numpy().astype(int)[0] # 转为标量 return action def update(self,reward_pool,state_pool,action_pool): # Discount reward running_add = 0 for i in reversed(range(len(reward_pool))): if reward_pool[i] == 0: running_add = 0 else: running_add = running_add * self.gamma + reward_pool[i] reward_pool[i] = running_add # Normalize reward reward_mean = np.mean(reward_pool) reward_std = np.std(reward_pool) for i in range(len(reward_pool)): reward_pool[i] = (reward_pool[i] - reward_mean) / reward_std # Gradient Desent self.optimizer.zero_grad() for i in range(len(reward_pool)): state = state_pool[i] action = Variable(torch.FloatTensor([action_pool[i]])) reward = reward_pool[i] state = Variable(torch.from_numpy(state).float()) probs = self.policy_net(state) m = Bernoulli(probs) loss = -m.log_prob(action) * reward # Negtive score function x reward # print(loss) loss.backward() self.optimizer.step()
class Model: def __init__(self, seq_len=20, learning_rate=3e-4): device = torch.device( "cuda: 0" if torch.cuda.is_available() else "cpu") self.device = device self.seq_len = seq_len time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S", time.localtime()) print("run on device", device, ",current time:", time_stamp) self.writer = SummaryWriter('runs/emb_graph' + time_stamp) # define layers self.categ_embedding = CategoricalEmbedding().to(device) self.r2s_embedding = Route2Stop(vertex_feature=105, edge_feature=112).to(device) self.encoder = Encoder(input_size=100, seq_len=seq_len).to(device) self.fcn = FCN(input_size=100).to(device) self.similarity = Similarity(input_size=30, device=device).to(device) # define training parameters self.criterion = nn.BCELoss() self.optimizer = optim.Adam( [{ 'params': self.categ_embedding.parameters() }, { 'params': self.r2s_embedding.parameters() }, { 'params': self.encoder.parameters() }, { 'params': self.fcn.parameters() }, { 'params': self.similarity.parameters() }], lr=learning_rate) def forward(self, old, real, fake, numer_list, categ_list): old = self.categ_embedding(old, numer_list, categ_list, self.device) real = self.categ_embedding(real, numer_list, categ_list, self.device) fake = self.categ_embedding(fake, numer_list, categ_list, self.device) old = self.r2s_embedding(old) real = self.r2s_embedding(real) fake = self.r2s_embedding(fake) old = self.encoder(old) real = self.fcn(real) fake = self.fcn(fake) score_real = self.similarity(old, real) score_fake = self.similarity(old, fake) return score_real, score_fake def metrics(self, score_real, score_fake, label_real_test, label_fake_test): y_true = np.concatenate( [label_real_test.cpu().numpy(), label_fake_test.cpu().numpy()], axis=0) y_pred = torch.cat([ torch.argmax(score_real, dim=1, keepdim=True), torch.argmax(score_fake, dim=1, keepdim=True) ], dim=0).cpu().numpy() acc = accuracy_score(y_true, y_pred) precision = precision_score(y_true, y_pred) recall = recall_score(y_true, y_pred) f1 = f1_score(y_true, y_pred) return acc, precision, recall, f1 def train_and_test(self, data, batch_size=64, num_epoch=50): #initialize labels before training label_real = torch.cat( [torch.zeros([batch_size, 1]), torch.ones([batch_size, 1])], dim=1).to(self.device) label_fake = torch.cat( [torch.ones([batch_size, 1]), torch.zeros([batch_size, 1])], dim=1).to(self.device) old_test, real_test, fake_test = data.test test_size = real_test.shape[0] label_real_test = torch.ones([test_size, 1]).type(torch.long).to(self.device) label_fake_test = torch.zeros([test_size, 1]).type(torch.long).to(self.device) for epoch in range(num_epoch): total_loss = [0] * len(data) total_loss_real = [0] * len(data) # training first for i, chunk in enumerate(data.train): old_chunk, real_chunk, fake_chunk = chunk num_batch = real_chunk.shape[0] // batch_size for batch in range(num_batch): # get a batch of data pair: (old, real, fake) old_batch = old_chunk.iloc[batch * self.seq_len * batch_size:(batch + 1) * self.seq_len * batch_size, :] real_batch = real_chunk.iloc[batch * batch_size:(batch + 1) * batch_size, :] fake_batch = fake_chunk.iloc[batch * batch_size:(batch + 1) * batch_size, :] score_real, score_fake = self.forward( old_batch, real_batch, fake_batch, data.numer_list, data.categ_list) loss_real = self.criterion(score_real, label_real) loss_fake = self.criterion(score_fake, label_fake) loss = loss_real + loss_fake total_loss[i] += loss.data total_loss_real[i] += loss_real.data self.optimizer.zero_grad() loss.backward() self.optimizer.step() if (batch + 1) % 100 == 0: print( "epoch: %d, chunk: %d, batch: %d, loss: %.3f, real: %.3f, fake: %.3f" % (epoch, i, batch + 1, loss.data, loss_real.data, loss_fake.data)) total_loss[i] = (total_loss[i] / batch).cpu().numpy() total_loss_real[i] = (total_loss_real[i] / batch).cpu().numpy() # testing score_real, score_fake = self.forward(old_test, real_test, fake_test, data.numer_list, data.categ_list) acc, precision, recall, f1 = self.metrics(score_real, score_fake, label_real_test, label_fake_test) print("test acc: %.4f" % acc) self.writer.add_scalar('testing accuracy', acc, epoch) self.writer.close() # print result and save loss in tensorboard print("epoch: %d, average loss: %.4f" % (epoch, np.mean(total_loss))) self.writer.add_scalars('training loss', { 'overall': np.mean(total_loss), 'good': np.mean(total_loss_real) }, epoch) self.writer.close() return acc, precision, recall, f1
class DQN: def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions self.device = device self.gamma = gamma self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity) def select_action(self, state): '''选择工作 Args: state [array]: 状态 Returns: [array]: 动作 ''' self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.actions_count / self.epsilon_decay) self.actions_count += 1 if random.random() > self.epsilon: with torch.no_grad(): state = torch.tensor( [state], device=self.device, dtype=torch.float32 ) # 先转为张量便于丢给神经网络,state元素数据原本为float64;注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 q_value = self.policy_net( state ) # tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) action = q_value.max(1)[1].item() else: action = random.randrange(self.n_actions) return action def update(self): if len(self.memory) < self.batch_size: return state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) state_batch = torch.tensor( state_batch, device=self.device, dtype=torch.float ) # 例如tensor([[-4.5543e-02, -2.3910e-01, 1.8344e-02, 2.3158e-01],...,[-1.8615e-02, -2.3921e-01, -1.1791e-02, 2.3400e-01]]) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze( 1) # 例如tensor([[1],...,[0]]) reward_batch = torch.tensor( reward_batch, device=self.device, dtype=torch.float) # tensor([1., 1.,...,1]) next_state_batch = torch.tensor(next_state_batch, device=self.device, dtype=torch.float) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze( 1) # 将bool转为float然后转为张量 # Compute Q(s_t, a) - the model computes Q(s_t), then we select the # columns of actions taken. These are the actions which would've been taken # for each batch state according to policy_net q_values = self.policy_net(state_batch).gather( 1, action_batch) # 等价于self.forward # Compute V(s_{t+1}) for all next states. # Expected values of actions for non_final_next_states are computed based # on the "older" target_net; selecting their best reward with max(1)[0]. # This is merged based on the mask, such that we'll have either the expected # state value or 0 in case the state was final. next_state_values = self.target_net(next_state_batch).max( 1)[0].detach() # tensor([ 0.0060, -0.0171,...,]) # Compute the expected Q values expected_q_values = reward_batch + self.gamma * next_state_values * ( 1 - done_batch[0]) # Compute Huber loss # self.loss = nn.MSELoss(q_values, expected_q_values.unsqueeze(1)) self.loss = nn.MSELoss()(q_values, expected_q_values.unsqueeze(1)) # Optimize the model self.optimizer.zero_grad( ) # zero_grad clears old gradients from the last step (otherwise you’d just accumulate the gradients from all loss.backward() calls). self.loss.backward( ) # loss.backward() computes the derivative of the loss w.r.t. the parameters (or anything requiring gradients) using backpropagation. for param in self.policy_net.parameters(): # clip防止梯度爆炸 param.grad.data.clamp_(-1, 1) self.optimizer.step( ) # causes the optimizer to take a step based on the gradients of the parameters.
from model import FCN from synth import Generator # Used to add noise to images G = Generator() from bezier import * from vggnet import * Encoder = VGG(16, 36) #Initializing a VGGnet architecture with 16 depth and 39 (9*4) as the num_outputs. #Now we have to pass in the data writer = TensorBoard('log/') import torch.optim as optim criterion = nn.MSELoss() criterion2 = nn.CrossEntropyLoss() Decoder = FCN(64) #Initializing the FCN network with width 64 (which is useless as the entire thing is hardcoded) optimizerE = optim.Adam(Encoder.parameters(), lr=3e-4) optimizerD = optim.Adam(Decoder.parameters(), lr=3e-4) batch_size = 64 data_size = 100000 generated_size = 0 val_data_size = 512 first_generate = True use_cuda = True step = 0 Train_batch = [None] * data_size Ground_truth = [None] * data_size Label_batch = [None] * data_size Val_train_batch = [None] * val_data_size Val_ground_truth = [None] * val_data_size Val_label_batch = [None] * val_data_size
class DQN: def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions # 总的动作个数 self.device = device # 设备,cpu或gpu等 self.gamma = gamma # e-greedy 策略相关参数 self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) # target_net的初始模型参数完全复制policy_net self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout # 可查parameters()与state_dict()的区别,前者require_grad=True self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity) def select_action(self, state): '''选择动作 Args: state [array]: [description] Returns: action [array]: [description] ''' self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.actions_count / self.epsilon_decay) self.actions_count += 1 if random.random() > self.epsilon: with torch.no_grad(): # 先转为张量便于丢给神经网络,state元素数据原本为float64 # 注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 state = torch.tensor([state], device=self.device, dtype=torch.float32) # 如tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) q_value = self.policy_net(state) # tensor.max(1)返回每行的最大值以及对应的下标, # 如torch.return_types.max(values=tensor([10.3587]),indices=tensor([0])) # 所以tensor.max(1)[1]返回最大值对应的下标,即action action = q_value.max(1)[1].item() else: action = random.randrange(self.n_actions) return action def update(self): if len(self.memory) < self.batch_size: return # 从memory中随机采样transition state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) # 转为张量 # 例如tensor([[-4.5543e-02, -2.3910e-01, 1.8344e-02, 2.3158e-01],...,[-1.8615e-02, -2.3921e-01, -1.1791e-02, 2.3400e-01]]) state_batch = torch.tensor(state_batch, device=self.device, dtype=torch.float) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze( 1) # 例如tensor([[1],...,[0]]) reward_batch = torch.tensor( reward_batch, device=self.device, dtype=torch.float) # tensor([1., 1.,...,1]) next_state_batch = torch.tensor(next_state_batch, device=self.device, dtype=torch.float) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze( 1) # 将bool转为float然后转为张量 # 计算当前(s_t,a)对应的Q(s_t, a) # 关于torch.gather,对于a=torch.Tensor([[1,2],[3,4]]) # 那么a.gather(1,torch.Tensor([[0],[1]]))=torch.Tensor([[1],[3]]) q_values = self.policy_net(state_batch).gather( dim=1, index=action_batch) # 等价于self.forward # 计算所有next states的V(s_{t+1}),即通过target_net中选取reward最大的对应states next_state_values = self.target_net(next_state_batch).max( 1)[0].detach() # 比如tensor([ 0.0060, -0.0171,...,]) # 计算 expected_q_value # 对于终止状态,此时done_batch[0]=1, 对应的expected_q_value等于reward expected_q_values = reward_batch + self.gamma * \ next_state_values * (1-done_batch[0]) # self.loss = F.smooth_l1_loss(q_values,expected_q_values.unsqueeze(1)) # 计算 Huber loss self.loss = nn.MSELoss()(q_values, expected_q_values.unsqueeze(1)) # 计算 均方误差loss # 优化模型 self.optimizer.zero_grad( ) # zero_grad清除上一步所有旧的gradients from the last step # loss.backward()使用backpropagation计算loss相对于所有parameters(需要gradients)的微分 self.loss.backward() for param in self.policy_net.parameters(): # clip防止梯度爆炸 param.grad.data.clamp_(-1, 1) self.optimizer.step() # 更新模型 def save_model(): pass def load_model(): pass
def train_deep(): import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.utils.data import Dataset from model import FCN from torch.optim import lr_scheduler def train(model, device, train_loader, optimizer): model.train() train_loss = 0 for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) train_loss += loss.item() loss.backward() optimizer.step() return train_loss / len(train_loader.dataset) def test(model, device, test_loader): model.eval() test_loss = 0 correct = 0 with torch.no_grad(): for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) test_loss += F.nll_loss( output, target, reduction="sum").item() # sum up batch loss pred = output.argmax( dim=1, keepdim=True) # get the index of the max log-probability correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) test_acc = 100.0 * correct / len(test_loader.dataset) return test_loss, test_acc # training settings batch_size = 32 test_batch_size = 1000 epochs = 500 patience = 30 # for early stopping use_cuda = torch.cuda.is_available() torch.manual_seed(9) device = torch.device("cuda" if use_cuda else "cpu") kwargs = {"num_workers": 1, "pin_memory": True} if use_cuda else {} train_loader = torch.utils.data.DataLoader( PoseDataset([root_dir / d for d in train_data_dirs]), batch_size=batch_size, shuffle=True, **kwargs, ) test_loader = torch.utils.data.DataLoader( PoseDataset([root_dir / d for d in test_data_dirs], mode="test"), batch_size=test_batch_size, shuffle=True, **kwargs, ) model = FCN().to(device) optimizer = optim.Adam(model.parameters(), lr=1e-3, amsgrad=True) early_stopping = utils.EarlyStopping(patience, Path("results")) for epoch in range(1, epochs + 1): train_loss = train(model, device, train_loader, optimizer) test_loss, test_acc = test(model, device, test_loader) print(f"epoch: {epoch:>3}, train_loss: {train_loss:.4f}, ", end="") print(f"test_loss: {test_loss:.4f}, test_acc: {test_acc:.3f}") early_stopping(test_loss, test_acc, model) if early_stopping.early_stop: print("Early stopping activated") break print(f"deep model acc: {early_stopping.best_acc}")
class DQN: def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions # 总的动作个数 self.device = device # 设备,cpu或gpu等 self.gamma = gamma # e-greedy策略相关参数 self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) # target_net的初始模型参数完全复制policy_net self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout # 可查parameters()与state_dict()的区别,前者require_grad=True self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity) def choose_action(self, state, train=True): '''选择动作 ''' if train: self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.actions_count / self.epsilon_decay) self.actions_count += 1 if random.random() > self.epsilon: with torch.no_grad(): # 先转为张量便于丢给神经网络,state元素数据原本为float64 # 注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 state = torch.tensor([state], device=self.device, dtype=torch.float32) # 如tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) q_value = self.policy_net(state) # tensor.max(1)返回每行的最大值以及对应的下标, # 如torch.return_types.max(values=tensor([10.3587]),indices=tensor([0])) # 所以tensor.max(1)[1]返回最大值对应的下标,即action action = q_value.max(1)[1].item() else: action = random.randrange(self.n_actions) return action else: with torch.no_grad(): # 先转为张量便于丢给神经网络,state元素数据原本为float64 # 注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 state = torch.tensor([state], device='cpu', dtype=torch.float32) # 如tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) q_value = self.target_net(state) # tensor.max(1)返回每行的最大值以及对应的下标, # 如torch.return_types.max(values=tensor([10.3587]),indices=tensor([0])) # 所以tensor.max(1)[1]返回最大值对应的下标,即action action = q_value.max(1)[1].item() return action def update(self): if len(self.memory) < self.batch_size: return # 从memory中随机采样transition state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) # 转为张量 # 例如tensor([[-4.5543e-02, -2.3910e-01, 1.8344e-02, 2.3158e-01],...,[-1.8615e-02, -2.3921e-01, -1.1791e-02, 2.3400e-01]]) state_batch = torch.tensor(state_batch, device=self.device, dtype=torch.float) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze( 1) # 例如tensor([[1],...,[0]]) reward_batch = torch.tensor( reward_batch, device=self.device, dtype=torch.float) # tensor([1., 1.,...,1]) next_state_batch = torch.tensor(next_state_batch, device=self.device, dtype=torch.float) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze( 1) # 将bool转为float然后转为张量 # 计算当前(s_t,a)对应的Q(s_t, a) q_values = self.policy_net(state_batch) next_q_values = self.policy_net(next_state_batch) # 代入当前选择的action,得到Q(s_t|a=a_t) q_value = q_values.gather(dim=1, index=action_batch) '''以下是Nature DQN的q_target计算方式 # 计算所有next states的Q'(s_{t+1})的最大值,Q'为目标网络的q函数 next_q_state_value = self.target_net( next_state_batch).max(1)[0].detach() # 比如tensor([ 0.0060, -0.0171,...,]) # 计算 q_target # 对于终止状态,此时done_batch[0]=1, 对应的expected_q_value等于reward q_target = reward_batch + self.gamma * next_q_state_value * (1-done_batch[0]) ''' '''以下是Double DQNq_target计算方式,与NatureDQN稍有不同''' next_target_values = self.target_net(next_state_batch) # 选出Q(s_t‘, a)对应的action,代入到next_target_values获得target net对应的next_q_value,即Q’(s_t|a=argmax Q(s_t‘, a)) next_target_q_value = next_target_values.gather( 1, torch.max(next_q_values, 1)[1].unsqueeze(1)).squeeze(1) q_target = reward_batch + self.gamma * next_target_q_value * ( 1 - done_batch[0]) self.loss = nn.MSELoss()(q_value, q_target.unsqueeze(1)) # 计算 均方误差loss # 优化模型 self.optimizer.zero_grad( ) # zero_grad清除上一步所有旧的gradients from the last step # loss.backward()使用backpropagation计算loss相对于所有parameters(需要gradients)的微分 self.loss.backward() for param in self.policy_net.parameters(): # clip防止梯度爆炸 param.grad.data.clamp_(-1, 1) self.optimizer.step() # 更新模型 def save_model(self, path): torch.save(self.target_net.state_dict(), path) def load_model(self, path): self.target_net.load_state_dict(torch.load(path))
def main(config): ## ================ Load data ============================================================= dataloader_train, dataloader_test = get_dataloader(config, config.tforms) data, labels, fname, raw_audio = next(iter(dataloader_train)) tn = tforms_mine.ApplyReverb(config.resampling_rate) tmp = tn(data[0]) print("Data shape pre module = %s" % str(data.shape)) transformer = get_time_frequency_transform(config) out = do_time_frequency_transform(data, transformer, config) print("Data shape after transformer = %s" % str(out.shape)) # Visualize some of the data tmp = out tmp2 = {'spectrogram': tmp, 'labels': labels, 'fname': fname} AudioTaggingPreProcessing.show_spectrogram_batch( tmp2, config, filepath=config.result_path) tmp3 = {'audio': raw_audio, 'labels': labels, 'fname': fname} AudioTaggingPreProcessing.show_waveforms_from_audio_batch( tmp3, config, filepath=config.result_path, saveWavs=False) ## ================ Model ================================================================= device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) # input shape (batch, 1, 128, 1366) ss model = FCN(ConvBlock, output_shape=dataloader_train.dataset.num_classes, max_pool=[(2, 4), (2, 4), (2, 4), (3, 5), (4, 4)]).to(device) summary(model, input_size=(1, 128, 1366)) # input shape (batch, 1, 128, 256) # model = FCN(ConvBlock, output_shape=dataloader_train.dataset.num_classes, # max_pool=[(2, 2), (2, 2), (2, 4), (3, 3), (4, 4)], # filters_num=[64, 128, 256, 512, 1024]).to(device) # # summary(model, input_size=(1, 128, 256)) # Loss and optimizer learning_rate = 0.001 criterion = model.loss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Another hacky way to get the iterator for the validation set def loopy(dl): while True: for x in iter(dl): yield x ## ================ Training loop ========================================================= t_sum = 0 t_epoch = 0 last_time = time.time() last_epoch = time.time() all_time = [] all_time_epoch = [] total_step = len(dataloader_train) loss_history_train = [] train_loss_step = 0.0 valid_loss_step = 0.0 train_loss_epoch = 0.0 valid_loss_epoch = 0.0 train_loss_history = [] valid_loss_history = [] train_steps = 0 valid_steps = 0 myIter = loopy(dataloader_test) for epoch in range(config.num_epochs): train_loss_epoch = 0.0 valid_loss_epoch = 0.0 train_loss_step = 0.0 valid_loss_step = 0.0 # Training loop pbar = tqdm(enumerate(dataloader_train)) for i, sample in pbar: t = time.time() t_diff = t - last_time t_sum += t_diff last_time = t all_time.append(t_diff) audio = sample[ 0] # Sample is (transformed_audio, labels, fname, raw_audio) labels = sample[1].to(device) # Forward pass if config.tforms == TformsSet.TorchAudio: # Torchaudio supports full GPU audio = audio.to(device) specs = do_time_frequency_transform( audio, transformer.to(device), config) # time-freq transform outputs = model(specs) else: # Audtorch uses numpy, so no support for GPU specs = do_time_frequency_transform( audio, transformer, config) # time-freq transform outputs = model(specs.to(device)) loss = criterion(outputs, labels) train_loss_epoch += loss.item() train_loss_step = loss.item() train_steps += 1 train_loss_history.append( train_loss_step) # Append losses for plotting # Backward and optimize optimizer.zero_grad() loss.backward() optimizer.step() if train_steps % config.print_every == 0: model.eval() with torch.no_grad(): # Hacky way to get a single validation batch, see above for comments. try: sample = next(myIter) except StopIteration: myIter = loopy(dataloader_test) sample = next(myIter) audio = sample[0] labels = sample[1].to(device) # Forward pass | time-freq transform if config.tforms == TformsSet.TorchAudio: # Torchaudio supports full GPU specs = do_time_frequency_transform( audio.to(device), transformer.to(device), config) else: # Audtorch uses numpy, so no support for GPU specs = do_time_frequency_transform( audio, transformer, config).to(device) outputs = model(specs) loss = criterion(outputs, labels) valid_loss_step = loss.item() valid_loss_epoch += valid_loss_step valid_steps += 1 valid_loss_history.append( valid_loss_step) # Append losses for plotting pbar.set_description( "Epoch [{}/{}], Step [{}/{}], T_diff {} , T_total {} , train_Loss: {:.4f} , valid_Loss {:.4f}" .format(epoch + 1, config.num_epochs, i + 1, total_step, t_diff, t_sum, train_loss_step, valid_loss_step)) t = time.time() t_epoch = t - last_epoch last_epoch = t all_time_epoch.append(t_epoch) print( "--------- Epoch [{}/{}] Summary , time per epoch {} , train_loss {} , valid_loss {}" .format(epoch + 1, config.num_epochs, t_epoch, train_loss_epoch / len(dataloader_train), valid_loss_epoch / max(valid_steps, 1))) ## ================ Plot training ========================================================= ## Plot time for each step t = np.linspace(0, config.num_epochs, num=len(all_time), endpoint=False) plt.figure(figsize=(16, 8)) plt.plot(t, all_time, 'r-+', label='Time per step') plt.legend() plt.savefig(os.path.join(config.result_path, 'Output_time_per_step.png')) plt.xlabel('Epochs') plt.ylabel('Time (s)') plt.title("Train data = %d" % (len(dataloader_train))) plt.show() ## Plot time per epoch t = np.linspace(0, config.num_epochs, num=len(all_time_epoch), endpoint=False) plt.figure(figsize=(16, 8)) plt.plot(t, all_time_epoch, 'r-+', label='Time per step') plt.legend() plt.savefig(os.path.join(config.result_path, 'Output_time_per_epoch.png')) plt.xlabel('Epochs') plt.ylabel('Time (s)') plt.title("Train data = %d" % (len(dataloader_train))) plt.show() ## Plot loss t = np.linspace(0, config.num_epochs, num=train_steps, endpoint=False) t_valid = np.linspace(0, config.num_epochs, num=valid_steps, endpoint=False) plt.figure(figsize=(20, 8)) plt.plot(t, train_loss_history, 'r-+', label='Train') plt.plot(t_valid, valid_loss_history, 'b--o', label='Valid') plt.legend() plt.yscale('log') plt.ylabel('Loss') plt.title("Train data = %d" % (len(dataloader_train))) plt.savefig(os.path.join(config.result_path, 'Output_loss.png')) plt.show() ## ================ Evaluation ============================================================ def get_auc(y_true, y_preds, labels_list): from sklearn import metrics score_accuracy = 0 score_lwlrap = 0 score_roc_auc_macro = 0 score_pr_auc_macro = 0 score_roc_auc_micro = 0 score_pr_auc_micro = 0 score_mse = 0 score_roc_auc_all = np.zeros((len(labels_list), 1)).squeeze() score_pr_auc_all = np.zeros((len(labels_list), 1)).squeeze() try: # for accuracy, lets pretend this is a single label problem, so assign only one label to every prediction score_accuracy = metrics.accuracy_score( y_true, indices_to_one_hot(y_preds.argmax(axis=1), dataloader_train.dataset.num_classes)) score_lwlrap = metrics.label_ranking_average_precision_score( y_true, y_preds) score_mse = math.sqrt(metrics.mean_squared_error(y_true, y_preds)) # Average precision is a single number used to approximate the integral of the PR curve # Macro is average over all clases, without considering class imbalances score_pr_auc_macro = metrics.average_precision_score( y_true, y_preds, average="macro") score_roc_auc_macro = metrics.roc_auc_score(y_true, y_preds, average="macro") # Micro, considers class imbalances score_pr_auc_micro = metrics.average_precision_score( y_true, y_preds, average="micro") score_roc_auc_micro = metrics.roc_auc_score(y_true, y_preds, average="micro") except ValueError as e: print("Soemthing wrong with evaluation") print(e) print("Accuracy = %f" % (score_accuracy)) print("Label ranking average precision = %f" % (score_lwlrap)) print("ROC_AUC macro score = %f" % (score_roc_auc_macro)) print("PR_AUC macro score = %f" % (score_pr_auc_macro)) print("ROC_AUC_micro score = %f" % (score_roc_auc_micro)) print("PR_AUC_micro score = %f" % (score_pr_auc_micro)) print("MSE score for train = %f" % (score_mse)) # These are per tag try: score_roc_auc_all = metrics.roc_auc_score(y_true, y_preds, average=None) score_pr_auc_all = metrics.average_precision_score(y_true, y_preds, average=None) except ValueError as e: print("Something wrong with evaluation") print(e) print("") print("Per tag, roc_auc, pr_auc") for i in range(len(labels_list)): print('%s \t\t\t\t\t\t %.4f \t%.4f' % (labels_list[i], score_roc_auc_all[i], score_pr_auc_all[i])) tmp = { 'accuracy': score_accuracy, 'lwlrap': score_lwlrap, 'mse': score_mse, 'roc_auc_macro': score_roc_auc_macro, 'pr_auc_macro': score_pr_auc_macro, 'roc_auc_micro': score_roc_auc_micro, 'pr_auc_micro': score_pr_auc_micro, 'roc_auc_all': score_roc_auc_all, 'pr_auc_all': score_pr_auc_all } scores = edict(tmp) try: plt.figure(figsize=(20, 8)) plt.bar(np.arange(len(labels_list)), scores.roc_auc_all[:]) plt.ylabel('ROC_AUC') plt.title("Train data = %d" % (len(dataloader_train))) plt.savefig( os.path.join(config.result_path, 'Output_scores_per_label.png')) plt.show() # Plot only a few of the scores aa = { 'accuracy': scores.accuracy, 'lwlrap': scores.lwlrap, 'mse': scores.mse, 'roc_auc_macro': scores.roc_auc_macro, 'roc_auc_micro': scores.roc_auc_micro, 'pr_auc_macro': scores.pr_auc_macro, 'pr_auc_micro': scores.pr_auc_micro } plt.figure(figsize=(10, 6)) plt.bar(range(len(aa)), list(aa.values()), align='center') plt.xticks(range(len(aa)), list(aa.keys())) plt.ylabel('Scores') plt.savefig(os.path.join(config.result_path, 'Outputs_scores.png')) plt.show() except IndexError as e: print(e) return scores del myIter model.eval() with torch.no_grad(): total = 0 numBatches = 0 allPreds = np.empty((0, dataloader_test.dataset.num_classes), float) allLabels = np.empty((0, dataloader_test.dataset.num_classes), int) for batch in dataloader_test: audio = batch[0] labels = batch[1].cpu().numpy() # Forward pass | time-freq transform if config.tforms == TformsSet.TorchAudio: # Torchaudio supports full GPU specs = do_time_frequency_transform(audio.to(device), transformer.to(device), config) else: # Audtorch uses numpy, so no support for GPU specs = do_time_frequency_transform(audio, transformer, config).to(device) outputs = model.forward_with_evaluation( specs.to(device)).cpu().numpy() if not check_outputs(outputs, True): warnings.warn( "Warning, the ouputs appear to have wrong values!!!!!") allPreds = np.append(allPreds, outputs, axis=0) allLabels = np.append(allLabels, labels, axis=0) total += labels.shape[0] numBatches += 1 print("Evaluated on {} validation batches".format(numBatches)) scores = get_auc(allLabels, allPreds, dataloader_test.dataset.taglist) # Save scores to disk import pickle with open(os.path.join(config.result_path, 'Scores.pkl')) as f: pickle.dump(scores, f, pickle.HIGHEST_PROTOCOL) return # Load the results like this: with open('obj/' + name + '.pkl', 'rb') as f: return pickle.load(f)
def train(data_loader, model_index, x_eval_train, y_eval_train): ### Model Initiation fcn = FCN() #print (fcn.b_1_conv_1[0].weight.data) d = tor.load("./models/vgg16_pretrained.pkl") fcn.vgg16_load(d) #d = tor.load("./models/fcn_model_1_1.pkl") #fcn.load_state_dict(d) fcn.cuda() #loss_func = tor.nn.CrossEntropyLoss(weight=w) loss_func = tor.nn.CrossEntropyLoss() #loss_func = tor.nn.MSELoss() #optim = tor.optim.SGD(fcn.parameters(), lr=LR, momentum=MOMENTUM) optim1 = tor.optim.Adam(fcn.b_6_conv_1.parameters(), lr=LR) optim2 = tor.optim.Adam(fcn.b_6_conv_2.parameters(), lr=LR) optim3 = tor.optim.Adam(fcn.b_6_conv_3.parameters(), lr=LR) optim4 = tor.optim.Adam(fcn.b_7_trans_1.parameters(), lr=LR) optim = tor.optim.Adam(fcn.parameters(), lr=LR) ### Training for epoch in range(EPOCH): print("|Epoch: {:>4} |".format(epoch + 1), end="") ### Training for step, (x_batch, y_batch) in enumerate(data_loader): x = Variable(x_batch).type(tor.FloatTensor).cuda() y = Variable(y_batch).type(tor.LongTensor).cuda() pred = fcn(x) optim1.zero_grad() optim2.zero_grad() optim3.zero_grad() optim4.zero_grad() optim.zero_grad() loss = loss_func(pred, y) loss.backward() optim1.step() optim2.step() optim3.step() optim4.step() print(pred[:2]) print(tor.max(pred[:5], 1)[1]) ### Evaluation loss = float(loss.data) acc = evaluate(fcn, x_eval_train, y_eval_train) print("|Loss: {:<8} |Acc: {:<8}".format(loss, acc)) ### Save model if epoch % RECORD_MODEL_PERIOD == 0: tor.save( fcn.state_dict(), os.path.join(MODEL_ROOT, "fcn_model_{}_{}.pkl".format(model_index, epoch))) ### Record record_data = dict() if epoch == 0: record_data["model_name"] = "fcn_model_{}.pkl".format(model_index) record_data["data_size"] = AVAILABLA_SIZE record_data["batch_size"] = BATCHSIZE record_data["decay"] = str((LR_STEPSIZE, LR_GAMMA)) record_data["lr_init"] = float(optim1.param_groups[0]["lr"]) record_data["lr"] = float(optim1.param_groups[0]["lr"]) record_data["record_epoch"] = RECORD_MODEL_PERIOD record_data["loss"] = loss record_data["acc"] = acc else: record_data["model_name"] = "fcn_model_{}.pkl".format(model_index) record_data["lr"] = float(optim1.param_groups[0]["lr"]) record_data["loss"] = loss record_data["acc"] = acc record(RECORD_FP, record_data)
indices = [] mapLoc = None if haveCuda else {'cuda:0': 'cpu'} if haveCuda: model = model.cuda() criterion = CrossEntropyLoss2d() epochs = 200 lr = 1e-1 weight_decay = 1e-3 momentum = 0.5 patience = 20 optimizer = torch.optim.SGD([ { 'params': model.parameters() }, ], lr=lr, momentum=momentum, weight_decay=weight_decay) scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=patience, verbose=True) ploter = LinePlotter() bestLoss = 100 bestAcc = 0
torch.nn.init.normal(m.weight.data,mean=0,std=1) torch.nn.init.normal(m.bias.data,mean=0,std=1) with open("/home/yuchen/Programs/cancer-prognosis/best.txt", 'r') as file: best = float(file.readline()) epoch0 = int(file.readline()) print("Last Train: accu %f , epoch0 %d" % (best, epoch0)) try: net = torch.load('/home/yuchen/Programs/cancer-prognosis/seg_model.pkl') except: net = FCN() net.apply(weights_init) net.cuda() optimizer = torch.optim.Adam(net.parameters(),lr=LR, weight_decay=0.5) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.9) loss_func = nn.NLLLoss2d(weight=torch.FloatTensor([1,8000]).cuda()) data_loder = DataLoader() zeros = np.zeros((512,512)) for epoch in range(epoch0, epoch0+EPOCH): scheduler.step() train_step = 0 test_step = 0 train_loss = 0 test_loss = 0 train_accu = 0 test_accu = 0 for train, test in data_loder.gen(BATCH_SIZE, get_stored=True):
}) net = FCN() net.cuda() data = ImageDataset('./train.zarr', transform=train_tr) val = ImageDataset('./test.zarr', transform=val_tr) loader = DataLoader(dataset=data, batch_size=batch_size, shuffle=True, num_workers=6) val_loader = DataLoader(dataset=val, batch_size=batch_size, shuffle=False) loss_fn = nn.CrossEntropyLoss() optimizer = Adam(params=net.parameters(), lr=lr, weight_decay=weight_decay) scaler = GradScaler() scheduler = StepLR(optimizer=optimizer, step_size=step_size, gamma=gamma) trainer = Engine(update) Accuracy(output_transform=output_transform).attach(engine=trainer, name='train_acc') Loss(loss_fn=loss_fn, output_transform=output_transform).attach(engine=trainer, name='loss') trainer.add_event_handler(event_name=Events.EPOCH_COMPLETED, handler=log_training) trainer.add_event_handler(event_name=Events.EPOCH_COMPLETED, handler=scheduler_step) trainer.add_event_handler(event_name=Events.COMPLETED,
ax3.set_title("Predicted") ax1.tick_params(labelbottom=False, labelleft=False) ax2.tick_params(labelbottom=False, labelleft=False) ax3.tick_params(labelbottom=False, labelleft=False) fig.savefig('viz_results/viz_{}.png'.format(epoch_idx), bbox_inches='tight') plt.close(fig) # Instantiate the model and its hyperparameters. n_species = 4 n_epochs = 501 model = FCN(n_classes=n_species, img_size=256).to(device) optimizer = optim.Adam(model.parameters(), lr=1e-3) criterion = nn.NLLLoss() # Our main training loop. for epoch_i in range(n_epochs): # Training consists of just these 4 lines. loss = criterion(model(train_imgs), train_masks) optimizer.zero_grad() loss.backward() optimizer.step() # Visualize validation performance every 50 epochs. if epoch_i % 50 == 0: print("Epoch: {}/{}, Loss: {:.4f}".format(epoch_i + 1, n_epochs, loss.item())) with torch.no_grad():