<jupyter_start><jupyter_text>CHECK IF AN IMAGE IS CONTESTED<jupyter_code>allTopTrain = pickle.load(open('train_cond.p','rb'))
allTop = []
for Class in allTopTrain:
    allTop.append(layerTopCond(Class, 2, convLayers))
array = x_test[0]
res = model.predict(np.expand_dims(array,0))
pred = np.argmax(res)
fm = list(allTop[pred]['conv8'].keys())
heatmaps = condHeat(array, model, num_class, convLayers, fm = fm[0])
condViz(heatmaps,array, num_class, index_to_label, figsize = (38,25),xAxis = 4)
binary = load_binary('verif_weight_train_cond_data_augment.h5')
test_cond = pickle.load(open('test_cond.p', 'rb'))
test_index = pickle.load(open('test_index.p', 'rb'))
L = []
for c in range(num_class):
    L.extend(test_index[c])
#rank = [4,4,4,4,4,4,4,4,4]
# topFMs, topMeans = extractTop(rank, convLayers, allTop, num_class)
# finalTestCond = process_cond(convLayers,topFMs, topMeans, test_cond, num_class)
    array = load_array(class_ind, img_ind, path)
    pred = int(train_pred[class_ind, img_ind])
    prob = train_proba[class_ind, img_ind]
    
    sess = K.get_session()
    input_tensor = model.layers[0].input
    inter_tensors = [convOut.output]
    output_tensor = model.layers[-2].output
    outputs = [tf.gradients(output_tensor[:,pred],inter_tensors), inter_tensors]
    
    grads, activs = integrated_gradient(array, input_tensor, outputs, sess)
    grads, activs = np.array(grads), np.array(activs)
    delta_activs = activs[:,1:,:,:,:] - activs[:,:-1,:,:,:] #shape = (n-1,128,128,3)
    contribs = np.sum(grads[:,:,1:,:,:,:]*delta_activs, axis = 2)
    contribs = np.squeeze(contribs, axis = (0,1))
    
    topFMs_pred = topFMs[layer,pred,:]
    contribs_pred = [contribs[:,:,fm] for fm in topFMs_pred]
    
    print("predicted as \'"+index_to_label[pred]+"\' with proba "+str(prob))
    print("is actually \'"+index_to_label[class_ind]+'\'')
    fig, axes = plt.subplots(1,len(topFMs_pred)+1, figsize = figsize)
    axes[0].imshow(image.array_to_img(array))
    axes[0].grid(False)
    axes[0].set_xticks([])
    axes[0].set_yticks([])

    for i in range(len(topFMs_pred)):
        sns.heatmap(contribs_pred[i],center = 0, annot = False, yticklabels = False, ax = axes[i+1])
        axes[i+1].set_title("feature map "+str(topFMs_pred[i]), fontsize = 16)
    
    contribs = np.mean(contribs, axis = (0,1))
    contribs = list(contribs - allClassMeans[layer])
    contribs = [(i, contribs[i]) for i in range(len(contribs))]
    contribs = sorted(contribs, key = lambda x: x[1], reverse = True)
def predCheck(model, layer, x, y, img_num, topFMs, predi, labeli, figsize1 = (20,2)):
    array = x[img_num]
    res = model.predict(x[img_num][None])[0]
    pred = np.argmax(res)
    label = labeli[img_num]
    
    input_tensor, outputs, sess = layerInit(model, num_class, [convLayers[layer]], True)
    grads,activs = integratedGrad(array, input_tensor, [outputs[pred], outputs[-1]], sess)
    grads, activs = np.array(grads), np.array(activs)
    delta_activs = activs[:,1:,:,:,:] - activs[:,:-1,:,:,:]
    contribs = np.sum(grads[:,:,1:,:,:]*delta_activs, axis = 2)
    contribs = np.squeeze(contribs, axis = (0,1))

    topFMs_pred = topFMs[layer,pred,:]
    contribs_pred = [contribs[:,:,fm] for fm in topFMs_pred]
    
#     data = finalTestCond[img_num].reshape((1,-1))
#     score = binary.predict(data).item()
    print("predicted as "+index_to_label[pred]+' with proba '+str(res[pred]))
    print('Is actually a '+str(index_to_label[label]))
#    print("the score is: "+str(score)+ ' the threshold is: '+str(0.477))

    fig, axes = plt.subplots(1, 4+1, figsize = figsize1)
    axes[0].imshow(image.array_to_img(array))
    axes[0].grid(False)
    axes[0].set_xticks([])
    axes[0].set_yticks([])

    for i in range(4):
        sns.heatmap(contribs_pred[i],center = 0, annot = False, yticklabels = False, ax = axes[i+1])
        axes[i+1].set_title("feature map "+str(topFMs_pred[i]), fontsize = 16)
    
    contribs = list(np.mean(contribs, axis = (0,1)))
    contribs = [(i, contribs[i]) for i in range(len(contribs))]
    contribs = sorted(contribs, key = lambda x: x[1], reverse = True)
    return contribs
x_cond, y_cond = x_test[L], y_test[L]
pred = np.argmax(model.predict(x_cond), axis = 1)
label = np.argmax(y_cond, axis = 1)
w = pred != label 
w_ind = np.arange(len(w))[w]
r = pred == label 
r_ind = np.arange(len(r))[r]
contribsW = predCheck(model, -2, x_cond, y_cond, w_ind[6], topFMs2, pred, label, figsize1 = (20,2))
contribsR = predCheck(model, -2, x_cond, y_cond, r_ind[0], topFMs2, pred, label, figsize1 = (20,2))
contribsW
contribsR<jupyter_output><empty_output><jupyter_text>check if sum of cond is equal to pre softmax value<jupyter_code>def pre_softmax(x, ind):
    array = np.expand_dims(x[ind],0)
    ref_array = np.zeros_like(array)
    sess = K.get_session()
    input_tensor = model.input
    feed_dict = {input_tensor: np.concatenate((array, ref_array), 0)}
    out = model.layers[-2].output
    weights, bias = model.layers[-1].weights
    output_tensor = tf.add(tf.matmul(out,weights),bias)
    Fx, FxPrime = sess.run(output_tensor, feed_dict = feed_dict)
    res = model.predict(array)[0]
    pred = np.argmax(res)
    diff = Fx - FxPrime
    return Fx, FxPrime, pred, res
FxW, FxPrimeW, predW, resW = pre_softmax(x_train, 0)
diffW = FxW - FxPrimeW
diffW
def cond_to_softmax(FxPrime, contribs):
    soft = []
    for c in range(num_class):
        score = np.exp(FxPrime[c])
        for i in range(contribs[c].shape[0]):
            for j in range(contribs[c].shape[1]):
                for k in range(contribs[c].shape[2]):
                    score *= np.exp(contribs[c][i,j,k])
        soft.append(score)
    exps = np.sum(soft)
    soft = [sof/exps for sof in soft]
    return soft
def softmax(entry):
    exps = np.sum([np.exp(x) for x in entry])
    entry = np.exp(entry)/exps
    return entry
softmax(FxW)
model.predict(x_train[:1])
def quickCond(model, num_class, interLayer, x, ind):
    array = x[ind]
    pred = np.argmax(model.predict(array[None]))
    
    input_tensor, outputs, sess = layerInit(model, num_class, interLayer, True)

    grads,activs = integratedGrad(array, input_tensor, outputs, sess)
    grads, activs = np.array(grads), np.array(activs)
    delta_activs = activs[:,1:,:,:,:] - activs[:,:-1,:,:,:]
    contribs = np.sum(grads[:,:,1:,:,:]*delta_activs, axis = 2)
    contribs = np.squeeze(contribs, axis = 1)
    
    return contribs
contribs = quickCond(model, num_class, [convLayers[-9]], x_train,0)
a = np.sum(contribs, axis = (1,2,3))
a
diffW
softmax(a)
softmax(diffW)
pred = np.argmax(model.predict(x_train), axis = 1)
label = np.argmax(y_train, axis = 1)
w = pred != label 
w_ind = np.arange(len(w))[w]
FxW, FxPrimeW, predW, resW = pre_softmax(x_train, w_ind[0])
diffW = FxW - FxPrimeW
diffW
#all layer prediction with conductance
fg = []
for l in range(len(convLayers)):
    contribs = quickCond(model, num_class, [convLayers[l]], x_train, w_ind[2])
    a = np.sum(contribs, axis = (1,2,3))
    qs = softmax(a+FxPrimeW)
    fg.append(np.argmax(qs))
fg
pred[w_ind[2]], label[w_ind[2]]<jupyter_output><empty_output><jupyter_text>DISPLAY OF CONTESTED IMAGES<jupyter_code>wrongs = pickle.load(open("wrong_train_cond.p", "rb"))
finalWrongs = process_cond(convLayers, topFMs, topMeans, wrongs, num_class)
balanced_rights = pickle.load(open('balanced_rights.p', 'rb'))
finalRights = process_cond(convLayers, topFMs, topMeans, balanced_rights, num_class)
LCRW = pickle.load(open('LCRW.p', 'rb'))
LCRR = pickle.load(open('LCRR.p', 'rb'))
more_rights = pickle.load(open('more_rights.p', 'rb'))
more_finalRights = process_cond(convLayers, topFMs, topMeans, more_rights, num_class)
more_LCRR = pickle.load(open('more_LCRR.p', 'rb'))
more_finalWrongs, _ = augment(finalWrongs, LCRW, more_finalRights, more_LCRR, num_class)
finalWrongs = np.concatenate((finalWrongs, more_finalWrongs), axis = 0)
finalRights = np.concatenate((finalRights, more_finalRights), axis = 0)
brut_data = np.concatenate((finalWrongs,finalRights), axis = 0)
y_wrongs = np.zeros(len(finalWrongs))
y_rights = np.ones(len(finalRights))
brut_labels = np.concatenate((y_wrongs,y_rights), axis = 0)
trainD, trainL, testD, testL,rnd = verif_data(9, rank, num_class, brut_data, brut_labels, False)  #wrongs is 0, rights is 1
rnd
verif_pred = verif.predict(testD)
thresh = find_opti_thresh(testL, verif_pred, plot = True)
print(thresh)
binary_res = verif.predict(finalTestCond)
binary_lab = pred == label
w_contested_ind = binary_res[w_ind] < thresh
w_contested_ind = np.squeeze(w_contested_ind, 1)
w_ncontested_ind = binary_res[w_ind] >= thresh
w_ncontested_ind = np.squeeze(w_ncontested_ind, 1)
w_contested = w_ind[w_contested_ind]
w_ncontested = w_ind[w_ncontested_ind]
r_contested_ind = binary_res[r_ind] < thresh
r_contested_ind = np.squeeze(r_contested_ind, 1)
r_contested = r_ind[r_contested_ind]
def print_img(contested_list, ind, L, figsize):
    array = x_test[L][contested_list[ind]]
    res = model.predict(np.expand_dims(array, axis = 0))[0]
    pred = np.argmax(res)
    label = np.argmax(y_test[L][contested_list[ind]])
    img = image.array_to_img(array)
    
    data = finalTestCond[contested_list[ind]].reshape((1,-1))
    score = verif.predict(data).item()
    
    print("predicted as "+index_to_label[pred]+' with proba '+str(res[pred]))
    print('Is actually a '+str(index_to_label[label]))
    print("the score is: "+str(score)+ ' the threshold is: '+str(0.477))
    fig, axes = plt.subplots(1,1, figsize = figsize)
    axes.imshow(img)
print_img(w_ncontested, 19, L, figsize = (8,3))<jupyter_output>predicted as airplane with proba 0.8458881
Is actually a horse
the score is: 0.5501914024353027 the threshold is: 0.477<jupyter_text>BARPLOTS<jupyter_code># allTopTrain = pickle.load(open('train_cond.p','rb'))
# wrongs = pickle.load(open("wrong_train_cond.p", "rb"))
allTopTrain = pickle.load(open('allClassCond2.p', 'rb'))
wrongs = pickle.load(open('wrongs2.p', 'rb'))
layer_num = len(convLayers) - 1
lastRLayer = [[allTopTrain[c][i][layer_num] for i in range(len(allTopTrain[c]))] for c in range(num_class)]
lastWLayer = [[wrongs[c][i][layer_num] for i in range(len(wrongs[c]))] for c in range(num_class)]
for c in range(num_class):
    classL = lastRLayer[c]
#     classL = [sorted(ov, key = lambda x: x[0]) for ov in classL]
#     classL = [[ovv[1] for ovv in ov] for ov in classL]
    lastRLayer[c] = np.array(classL)
    lastWLayer[c] = np.array(lastWLayer[c])
meanLastRLayer = [np.mean(classL, axis = 0) for classL in lastRLayer]
meanLastWLayer = [np.mean(classL, axis = 0) for classL in lastWLayer]
for c in range(num_class):
    lastRLayer[c] = np.array(lastRLayer[c])
    lastWLayer[c] = np.array(lastWLayer[c])
meanLastRLayer = [np.mean(classL, axis = 0) for classL in lastRLayer]
meanLastWLayer = [np.mean(classL, axis = 0) for classL in lastWLayer]
maxRMean = [np.max(m) for m in meanLastRLayer]
maxWMean = [np.max(m) for m in meanLastWLayer]
maxMean = [max(maxRMean[i], maxWMean[i]) for i in range(len(maxRMean))]
minRMean = [np.min(m) for m in meanLastRLayer]
minWMean = [np.min(m) for m in meanLastWLayer]
minMean = [min(minRMean[i], minWMean[i]) for i in range(len(minRMean))]
check = list(meanLastRLayer[0])
check = [(i,check[i]) for i in range(len(check))]
check = sorted(check, key = lambda x: x[1], reverse = True)
check
class_ind = 0
fig, axes = plt.subplots(2,1, figsize = (16,10))
axes[0].yaxis.grid(True)
axes[0].set_title('True positives\' mean conductance')
axes[0].set_ylabel('value')
axes[0].set_xlabel('feature maps')
axes[0].set_ylim(minMean[class_ind]+minMean[class_ind]/10, maxMean[class_ind]+maxMean[class_ind]/10)
axes[1].yaxis.grid(True)
axes[1].set_title('False positives\' mean condutance')
axes[1].set_ylabel('value')
axes[1].set_xlabel('feature maps')
axes[1].set_ylim(minMean[class_ind]+minMean[class_ind]/10, maxMean[class_ind]+maxMean[class_ind]/10)
sns.barplot(x = np.arange(lastRLayer[c].shape[1]), y = meanLastRLayer[class_ind], ax = axes[0])
sns.barplot(x = np.arange(lastWLayer[c].shape[1]), y = meanLastWLayer[class_ind], ax = axes[1])
class_ind = 0
fig, axes = plt.subplots(2,1, figsize = (16,10))
axes[0].xaxis.grid(True)
axes[0].yaxis.grid(True)
axes[1].xaxis.grid(True)
axes[1].yaxis.grid(True)
sns.boxplot(data = lastRLayer[class_ind], ax = axes[0])
sns.boxplot(data = lastWLayer[class_ind], ax = axes[1])<jupyter_output><empty_output><jupyter_text>CALC THE COND OF AN IMAGE<jupyter_code>#calc_the_cond_of_an_image.py<jupyter_output><empty_output><jupyter_text>BUILD COND STAT FOR WRONG IMG<jupyter_code>#build_cond_stat_for_wrong.py<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16216430955029398, 'fraction_numerical': 0.018750993167010965, 'mean_word_length': 5.116132167152576, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '323754', 'license': 'MIT', 'n_tokens_mistral': 5262, 'n_tokens_neox': 5038, 'n_words': 966, 'path': 'cifar10/cifar10_visualization.ipynb', 'repo_name': 'feiwu77777/Leveraging-Model-Interpretability-and-Stability-to-increase-Model-Robustness', 'size': '12586'}