示例#1
0
    def generate_saliencies(self, state, contrastive = False):
        eb.use_eb(True)

        state = Variable(torch.Tensor(state)).unsqueeze(0)

        saliencies = {}
        for idx, choice in enumerate(self.adaptive.choices):
            choice_saliencies = {}
            prob_outputs = Variable(torch.zeros((len(self.adaptive.choices),)))
            prob_outputs[idx] = 1

            for reward_idx, reward_type in enumerate(self.adaptive.reward_types):
                #TODO better way to do this
                explainable_model = HRAModel(self.adaptive.name + "_explain", self.adaptive.network_config, restore = False)
                explainable_model.replace(self.adaptive.eval_model)

                explainable_model.clear_weights(reward_idx)

                layer_top = explainable_model.top_layer(reward_idx)

                saliency = eb.excitation_backprop(explainable_model.model, state, prob_outputs, contrastive = contrastive, layer_top = layer_top, target_layer = 0)

                choice_saliencies[reward_type] = np.squeeze(saliency.view(*state.shape).data.numpy())

            # for overall reward
            saliency = eb.excitation_backprop(self.adaptive.eval_model.model, state, prob_outputs, contrastive = contrastive, target_layer = 0)
            choice_saliencies["all"] = np.squeeze(saliency.view(*state.shape).data.numpy())

            saliencies[choice] = choice_saliencies

        eb.use_eb(False)
        return saliencies
示例#2
0
def gen_eb(path, model_name, show=True):

    # get model
    model = models.__dict__[model_name](pretrained=True)
    #model = models.vgg19(pretrained=True)
    _ = model.train(False)  # put model in evaluation mode

    # get imagenet class labels from local file
    class_labels = []
    with open('techniques/excitationbp/data/imagenet/labels.txt', 'r') as f:
        for line in f:
            class_labels.append(line[:-1])

    eb.use_eb(True)
    inputs = read_tensor(path)
    inputs = Variable(inputs.resize_(1, 3, 224, 224), requires_grad=True)
    y_hat = model(inputs)
    class_id = y_hat.max(1)[1].data.numpy()[0]

    # compute excitation backprop for correct label
    prob_outputs_cat = Variable(torch.zeros(1, 1000))
    prob_outputs_cat.data[:, class_id] += 1

    prob_inputs_cat = eb.excitation_backprop(model,
                                             inputs,
                                             prob_outputs_cat,
                                             contrastive=False)

    beta = 0.5  # adjust the 'peakiness' of data
    cat_img = prob_inputs_cat.clamp(min=0).sum(0).sum(0).data.numpy()
    print(cat_img.shape)

    if show:
        plt.imshow(cat_img**beta, cmap='gray')
    return cat_img
def obtain_ceb_prob_inputs(inputs, y, p, hl, model, pyes=True):
    eb.use_eb(True)
    true_id_0 = y.data[0]
    true_id_1 = y.data[1]
    parity = ["even", "odd"]
    true_p_0 = p.data[0]
    true_p_1 = p.data[1]
    highlow = ["low", "high"]
    true_hl_0 = hl.data[0]
    true_hl_1 = hl.data[1]

    prob_outputs_true_p_even = Variable(torch.zeros(1, 4))
    prob_outputs_true_p_even.data[:, 0] += 1
    prob_outputs_true_p_even.data[:, 2] += 1

    prob_outputs_true_p_odd = Variable(torch.zeros(1, 4))
    prob_outputs_true_p_odd.data[:, 1] += 1
    prob_outputs_true_p_odd.data[:, 3] += 1

    prob_outputs_true_hl_low = Variable(torch.zeros(1, 4))
    prob_outputs_true_hl_low.data[:, 0] += 1
    prob_outputs_true_hl_low.data[:, 2] += 1

    prob_outputs_true_hl_high = Variable(torch.zeros(1, 4))
    prob_outputs_true_hl_high.data[:, 1] += 1
    prob_outputs_true_hl_high.data[:, 3] += 1

    if pyes:
        prob_outputs_true_0 = prob_outputs_true_p_even
        prob_outputs_true_1 = prob_outputs_true_p_odd
        top_layer_num = -3
    else:
        prob_outputs_true_0 = prob_outputs_true_hl_low
        prob_outputs_true_1 = prob_outputs_true_hl_high
        top_layer_num = -1

    if useCuda:
        prob_outputs_true_0 = prob_outputs_true_0.type(torch.cuda.FloatTensor)
        prob_outputs_true_1 = prob_outputs_true_1.type(torch.cuda.FloatTensor)

    prob_inputs_true_0 = eb.excitation_backprop(model,
                                                inputs,
                                                prob_outputs_true_0,
                                                contrastive=True,
                                                top_layer=top_layer_num)
    prob_inputs_true_1 = eb.excitation_backprop(model,
                                                inputs,
                                                prob_outputs_true_1,
                                                contrastive=True,
                                                top_layer=top_layer_num)

    if useCuda:
        prob_inputs_true_0 = prob_inputs_true_0.type(torch.cuda.FloatTensor)
        prob_inputs_true_1 = prob_inputs_true_1.type(torch.cuda.FloatTensor)

    return prob_inputs_true_0, prob_inputs_true_1
示例#4
0
    def predict(self, state):
        self.steps += 1
        saliencies = []

        # add to experience
        if self.previous_state is not None:
            experience = Experience(self.previous_state, self.previous_action,
                                    self.current_reward, state)
            self.replay_memory.add(experience)

        if self.learning and self.should_explore():
            action = np.random.choice(len(self.choices))
            q_values = [None] * len(
                self.choices
            )  # TODO should it be output shape or from choices?
            choice = self.choices[action]
        else:
            _state = Variable(torch.Tensor(state)).unsqueeze(0)
            q_values = self.eval_model.predict(_state)
            q_values = q_values.data.numpy()[0]
            action = np.argmax(q_values)
            choice = self.choices[action]

        if self.explanation:
            eb.use_eb(True)
            prob_outputs = Variable(torch.zeros((len(self.choices), )))
            for action in range(len(self.choices)):
                prob_outputs[action] = 1
                saliency = eb.excitation_backprop(self.eval_model.model,
                                                  _state,
                                                  prob_outputs,
                                                  contrastive=False)
                saliency = np.squeeze(
                    saliency.view(*_state.shape).data.numpy())
                saliencies.append(saliency)

        if self.learning and self.steps % self.update_frequency == 0:
            logger.debug("Replacing target model for %s" % self.name)
            self.target_model.replace(self.eval_model)

        self.update()

        self.current_reward = 0

        self.previous_state = state
        self.previous_action = action

        return choice, q_values, saliencies
示例#5
0
    def generate_saliency(self, input, target):
        eb.use_eb(True)
        inputs = Variable(torch.Tensor(input))
        prob_outputs_zero = Variable(torch.zeros(1, 4))
        zero_id = target
        prob_outputs_zero.data[0:1, zero_id] += 1
        prob_inputs_zero = eb.excitation_backprop(net,
                                                  inputs,
                                                  prob_outputs_zero,
                                                  contrastive=True)
        ebX = prob_inputs_zero.view(inputs.shape).data.abs()
        #print(input)
        #torch.set_printoptions(threshold=10000)
        #print(input.numpy().shape)
        #f = open('input.txt', 'w+')
        #f.write(str(input.numpy()))
        #f.close()

        return ebX
    def forward(self, x, label):
        h1 = self.maxpool1(F.relu(self.cnn1(x)))
        h2 = self.maxpool2(F.relu(self.cnn2(h1)))
        h3 = self.maxpool3(F.relu(self.cnn3(h2)))
        h4 = self.flatten(h3)
        h5 = F.relu(self.fc1(h4))

        model2 = latterModel(copy.deepcopy(self.fc2), copy.deepcopy(self.fc3))
        eb.use_eb(True, verbose=False)
        peb_list = []
        mask = None
        retain_p = None
        if self.training:
            data = h5.clone()
            for i in range(self.batch_size):
                prob_outputs = Variable(torch.zeros(1, 10)).to(device)
                prob_outputs.data[:, label[i]] += 1

                prob_inputs = eb.excitation_backprop(
                    model2, data[i:i + 1, :], prob_outputs, 
                    contrastive=False, target_layer=0)
                peb_list.append(prob_inputs)
        
            pebs = torch.cat(peb_list, dim=0) # calc peb
            mask, retain_p = DropoutMask.mask(pebs) # calc mask
            peb_entropy = Entropy_Calc.peb_entropy_calc(pebs) # calc peb entropy val
            self.ed.peb_entropy = peb_entropy
            self.ed.peek_peb = torch.max(pebs) # update peek peb
            eb.use_eb(False, verbose=False)

        self.ed.train = self.training  # ugly code!
        self.ed.mask =  mask
        self.ed.retain_p = retain_p
        h_ed = self.ed(h5)

        h6 = F.relu(self.fc2(h_ed))
        h7 = self.fc3(h6)

        return h7
def ceb_pred_accuracy(prob_input, prob_0_1, y, p, hl, model, pyes=True):
    eb.use_eb(False)
    ceb_input = (prob_input).clamp(min=0)
    newX = Variable(ceb_input / ceb_input.max(1)[0] * 1.7)
    if useCuda:
        newX = newX.type(torch.cuda.FloatTensor)
        model = model.cuda()
    logits = model(newX)

    if pyes:
        target_ind = (p == prob_0_1).nonzero()  # 0 or 1
        target_digit = y[p == prob_0_1]  #0~9
        target_parity = prob_0_1
        target_highlow = hl[p == prob_0_1]
    else:
        target_ind = (hl == prob_0_1).nonzero()  # 0 or 1
        target_digit = y[hl == prob_0_1]  #0~9
        target_parity = p[hl == prob_0_1]
        target_highlow = prob_0_1

    if pyes:
        target_y_hat = F.softmax(
            logits[0][:, (0 + target_ind * 10):(10 + target_ind * 10)], dim=-1)
    else:
        target_y_hat = F.softmax(
            logits[2][:, (0 + target_ind * 10):(10 + target_ind * 10)], dim=-1)
    #target_y_hat = F.softmax(logits[0][:,(0+target_ind*10):(10+target_ind*10)], dim=-1)
    target_digit_prob = target_y_hat[:, target_digit].data

    target_p_hat = F.softmax(
        logits[1][:, (0 + target_ind * 2):(2 + target_ind * 2)], dim=-1)
    target_parity_prob = target_p_hat[:, target_parity].data

    target_hl_hat = F.softmax(
        logits[3][:, (0 + target_ind * 2):(2 + target_ind * 2)], dim=-1)
    target_highlow_prob = target_hl_hat[:, target_highlow].data

    pred_digit = target_y_hat.data.max(1)[1]
    pred_digit_prob = target_y_hat.data.max(1)[0]

    pred_parity = target_p_hat.data.max(1)[1]
    pred_parity_prob = target_p_hat.data.max(1)[0]

    pred_highlow = target_hl_hat.data.max(1)[1]
    pred_highlow_prob = target_hl_hat.data.max(1)[0]

    digit_correct = Variable(target_digit == pred_digit).type(
        torch.LongTensor).data
    parity_correct = Variable(target_parity == pred_parity).type(
        torch.LongTensor).data
    highlow_correct = Variable(target_highlow == pred_highlow).type(
        torch.LongTensor).data

    nontarget_ind = 1 - target_ind
    if pyes:
        nontarget_y_hat = F.softmax(
            logits[0][:, (0 + nontarget_ind * 10):(10 + nontarget_ind * 10)],
            dim=-1)
    else:
        nontarget_y_hat = F.softmax(
            logits[2][:, (0 + nontarget_ind * 10):(10 + nontarget_ind * 10)],
            dim=-1)
    #nontarget_y_hat = F.softmax(logits[0][:,(0+nontarget_ind*10):(10+nontarget_ind*10)], dim=-1)
    nontarget_max_digit = nontarget_y_hat.data.max(1)[1]
    nontarget_max_digit_prob = nontarget_y_hat.data.max(1)[0]

    nontarget_p_hat = F.softmax(
        logits[1][:, (0 + nontarget_ind * 2):(2 + nontarget_ind * 2)], dim=-1)
    nontarget_max_parity = nontarget_p_hat.data.max(1)[1]
    nontarget_max_parity_prob = nontarget_p_hat.data.max(1)[0]

    nontarget_hl_hat = F.softmax(
        logits[3][:, (0 + nontarget_ind * 2):(2 + nontarget_ind * 2)], dim=-1)
    nontarget_max_highlow = nontarget_hl_hat.data.max(1)[1]
    nontarget_max_highlow_prob = nontarget_hl_hat.data.max(1)[0]

    return_list = [digit_correct, parity_correct, highlow_correct, target_digit_prob, target_parity_prob, target_highlow_prob, \
                   pred_digit_prob, pred_parity_prob, pred_highlow_prob, \
                   nontarget_max_digit_prob, nontarget_max_parity_prob, nontarget_max_highlow_prob, \
                   target_digit, target_parity, target_highlow, \
                   pred_digit, pred_parity, pred_highlow, \
                   nontarget_max_digit, nontarget_max_parity, nontarget_max_highlow]

    return return_list