f = np.random.uniform(0, 1, 9)
        train_batch.append(f)
        ground_truth.append(draw(f))

    train_batch = torch.tensor(train_batch).float()
    ground_truth = torch.tensor(ground_truth).float()
    if use_cuda:
        Decoder = Decoder.cuda()
        train_batch = train_batch.cuda()
        ground_truth = ground_truth.cuda()
    gen = Decoder(train_batch)
    optimizer.zero_grad()
    loss = criterion(gen, ground_truth)
    loss.backward()
    optimizer.step()
    print(step, loss.item())
    writer.add_scalar('train/loss', loss.item(), step)
    if step % 100 == 0:
        Decoder.eval()
        gen = Decoder(train_batch)
        loss = criterion(gen, ground_truth)
        writer.add_scalar('validate/loss', loss.item(), step)
        for i in range(64):
            G = gen[i].cpu().data.numpy()
            GT = ground_truth[i].cpu().data.numpy()
            writer.add_image(str(step) + '/train/gen.png', G, step)
            writer.add_image(str(step) + '/train/ground_truth.png', GT, step)
    if step % 10000 == 0:
        save_model()
    step += 1
Example #2
0
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.
Example #3
0
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
Example #4
0
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))
Example #5
0
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)
Example #6
0
    scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,
                                               'min',
                                               factor=0.5,
                                               patience=patience,
                                               verbose=True)
    ploter = LinePlotter()

    bestLoss = 100
    bestAcc = 0
    bestIoU = 0
    bestTAcc = 0
    bestConf = torch.zeros(numClass, numClass)

    for epoch in range(epochs):

        model.eval()
        running_loss = 0.0
        running_acc = 0.0
        imgCnt = 0
        bar = progressbar.ProgressBar(0,
                                      len(trainloader),
                                      redirect_stdout=False)
        for i, (images, labels) in enumerate(trainloader):
            if torch.cuda.is_available():
                images = images.cuda()
                labels = labels.cuda()

            optimizer.zero_grad()

            pred = model(images)
            loss = criterion(pred, labels)
Example #7
0
            output = net(X)
            output = F.log_softmax(output,dim=1)
            loss = loss_func(output, y)
            train_loss += loss.data[0]
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            train_step += 1

            label_pred = output.max(dim=1)[1].data.squeeze().cpu().numpy()
            label_true = y.data.squeeze().cpu().numpy()
            accu = sum(sum(label_pred == label_true))/512.0/512.0
            train_accu += accu
        

        net = net.eval()
        for X_, y_ in test:
            X_ = torch.FloatTensor(X_[:,np.newaxis,:,:])
            y_ = torch.LongTensor(y_)
            X_ = Variable(X_).cuda()
            y_ = Variable(y_).cuda()
            output = net(X_)
            output = F.log_softmax(output,dim=1)
            loss = loss_func(output, y_)
            test_loss += loss.data[0]
            test_step += 1

            label_pred = output.max(dim=1)[1].data.squeeze().cpu().numpy()
            label_true = y_.data.squeeze().cpu().numpy()
            accu = sum(sum(label_pred == label_true))/512.0/512.0
            test_accu += accu