In [1]:
import os.path
import matplotlib.pyplot as plt
from tensorflow.python import pywrap_tensorflow
import numpy as np
%matplotlib inline
In [3]:
path = '../activations_original'
num_files = len([f for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f))])
CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
class_to_ind = dict(list(zip(CLASSES, list(range(len(CLASSES))))))
arr_hm = [np.empty([num_files,20,64], dtype=float),
np.empty([num_files,20,64], dtype=float),
np.empty([num_files,20,128], dtype=float),
np.empty([num_files,20,128], dtype=float),
np.empty([num_files,20,256], dtype=float),
np.empty([num_files,20,256], dtype=float),
np.empty([num_files,20,256], dtype=float),
np.empty([num_files,20,512], dtype=float),
np.empty([num_files,20,512], dtype=float),
np.empty([num_files,20,512], dtype=float),
np.empty([num_files,20,512], dtype=float),
np.empty([num_files,20,512], dtype=float),
np.empty([num_files,20,512], dtype=float)]
num_clas = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
In [4]:
print type(os.listdir(path))
In [5]:
# calculate the activation versus classes matrix
print 'loading data from text files'
for file_ind,filename in enumerate(os.listdir(path)):
# print 'processing file {}'.format(filename)
clas = []
acts = []
f = open('/'.join([path,filename]),'r')
act_ind = 0
for line in f.readlines():
if line and line[0].isalpha():
clas.append(line[:-1])
if line.startswith('['):
if not line.endswith(']/n'):
acts.append([])
acts_this_line = line[2:-1].split(' ')
for i in acts_this_line:
if i is not '':
acts[act_ind].append(float(i))
else:
raise IOError('Error line with fewer numbers than expected.')
if line.startswith(' '):
# print 'starts with nothing'
if line.endswith(']\n'):
acts_this_line = line[:-2].split(' ')
for i in acts_this_line:
if i is not '':
acts[act_ind].append(float(i))
act_ind += 1
else:
acts_this_line = line.split(' ')
for i in acts_this_line:
if i is not '':
acts[act_ind].append(float(i))
for item in CLASSES:
ind = class_to_ind[item]
if item in clas:
file_ind = int(filename[:-4])
for j in range(13):
arr_hm[j][file_ind][ind] += acts[j]
num_clas[ind] = num_clas[ind] + 1
print 'loaded'
In [6]:
print num_clas
print len(arr_hm)
print arr_hm[0].shape
num_clas = np.asarray(num_clas)
In [6]:
# only for retrained network's results
num_clas[num_clas == 0] = 1
print num_clas
In [9]:
for i in range(13):
arr_hm_new = np.sum(arr_hm[i], axis=0)
print arr_hm_new.shape
for j in range(len(num_clas)):
arr_hm_new[j] = arr_hm_new[j]/num_clas[j]
fig= plt.figure(figsize=(arr_hm_new.shape[1], 21), dpi= 20, facecolor='w', edgecolor='k')
ax = plt.axes()
plt.subplot()
heatmap = ax.pcolormesh(arr_hm_new,cmap=plt.cm.Blues,alpha=0.8)
plt.savefig('images/{}.png'.format(i))
In [8]:
print len(arr_hm_new)
In [10]:
dic_original_images = {}
for i in range(13):
dic_original_images['%dth_layer'%i] = np.sum(arr_hm[i],axis=0)
print(dic_original_images['%dth_layer'%i].shape)
for j in range(len(num_clas)):
dic_original_images['%dth_layer'%i][j] = dic_original_images['%dth_layer'%i][j]/num_clas[j]
In [12]:
print dic_original_images['0th_layer']
np.save('activations_res/original.npy',dic_original_images)
In [6]:
# calcualates the corelations between classes
for i in range(13):
# arr_hm_average = np.average(arr_hm[i],axis=0)
# print arr_hm_average
arr_hm_new = np.sum(arr_hm[i], axis=0)/num_clas[i]
cor_arr_hm = np.corrcoef(arr_hm_new[:-2])
# print cor_arr_hm
print np.average(cor_arr_hm[cor_arr_hm<1])
In [8]:
# calcualates the corelations between classes
for i in range(13):
# arr_hm_average = np.average(arr_hm[i],axis=0)
# print arr_hm_average
arr_hm_new = np.sum(arr_hm[i], axis=0)/num_clas[i]
cor_arr_hm = np.corrcoef(arr_hm_new[:-2])
# print cor_arr_hm
print np.average(cor_arr_hm[cor_arr_hm<1])
In [6]:
print arr_hm[0].shape
In [9]:
# read weights from the ckpt file
file_name = '../output/vgg16/voc_2007_trainval/default/vgg16_faster_rcnn_iter_70000.ckpt'
reader = pywrap_tensorflow.NewCheckpointReader(file_name)
var_to_shape_map = reader.get_variable_to_shape_map()
dic = {}
weights = []
for key in sorted(var_to_shape_map):
if 'conv' in key and key.endswith('weights'):
dic[key] = np.sum(reader.get_tensor(key),axis=(0,1,2))
weights.append(dic[key])
In [10]:
# correlation between weights and activations
for i in range(13):
arr_hm_new = np.sum(arr_hm[i], axis=(0,1))/num_clas[i]
# print arr_hm_new.shape
corr_weights_acts = np.corrcoef([weights[i],arr_hm_new])
print corr_weights_acts[0][1]
In [11]:
# correlation with AP for each class
APs = [0.689,0.784,0.675,0.534,0.586,0.786,0.841,0.839,0.478,0.814,0.669,0.807,0.841,0.756,0.771,0.462,0.707,0.670,0.742,0.731]
for i in range(13):
arr_hm_new = np.sum(arr_hm[i], axis=(0,2))/num_clas[i]
corr_weights_acts = np.corrcoef([APs,arr_hm_new[:-1][::-1]])
print corr_weights_acts[0][1]
In [12]:
# correlation with data amount for each class
for i in range(13):
arr_hm_new = np.sum(arr_hm[i], axis=(0,2))/num_clas[i]
corr_weights_acts = np.corrcoef([num_clas,arr_hm_new[::-1]])
print corr_weights_acts[0][1]
In [ ]: