In [3]:
from __future__ import division
import time
import click
import numpy as np
import nn
import data
import tta
import util
from lasagne.layers import get_output
import theano
from theano import tensor as T
In [39]:
# cnf = 'configs/c_512_5x5_32.py'
# weights_from = 'weights_VIP/c_512_5x5_32/best/0217_2017-02-28-17-51-48_0.302194565535.pkl'
cnf = 'configs_CAM/c_128_5x5_32.py'
weights_from = 'weights_CAM//c_128_5x5_32/best/0196_2017-03-16-00-55-26_0.595941841602.pkl'
# cnf = 'configs_CAM/c_256_5x5_32.py'
# weights_from = 'weights_CAM//c_256_5x5_32/best/0189_2017-03-16-10-42-45_0.538861095905.pkl'
n_iter = 1
skip = 0
test = False
train = True
test_dir = None
In [40]:
reload(nn)
config = util.load_module(cnf).config
config.cnf['batch_size_train'] = 128
config.cnf['batch_size_test'] = 128
runs = {}
if train:
runs['train'] = config.get('train_dir')
if test or test_dir:
runs['test'] = test_dir or config.get('test_dir')
net = nn.create_net(config)
if weights_from is None:
net.load_params_from(config.weights_file)
print("loaded weights from {}".format(config.weights_file))
else:
weights_from = str(weights_from)
net.load_params_from(weights_from)
print("loaded weights from {}".format(weights_from))
In [41]:
tfs, color_vecs = tta.build_quasirandom_transforms(
n_iter, skip=skip, color_sigma=0.0,
**data.no_augmentation_params)
ret_val_x = []
ret_val_y = []
for run, directory in sorted(runs.items(), reverse=True):
print("extracting features for files in {}".format(directory))
tic = time.time()
files = data.get_image_files(directory)
Xs, Xs2 = None, None
for i, (tf, color_vec) in enumerate(zip(tfs, color_vecs), start=1):
print("{} transform iter {}".format(run, i))
X = net.last_conv(files[:1000], transform=tf, color_vec=color_vec)
Y = net.predict(files[:1000], transform=tf, color_vec=color_vec)
ret_val_x.append(X)
ret_val_y.append(Y)
In [42]:
ret_val_x = np.stack(ret_val_x, axis=0).squeeze()
ret_val_y = np.stack(ret_val_y, axis=0).squeeze()
softmax = net.get_all_params_values()
softmax = softmax[softmax.keys()[-1]][0]
CAM = np.dot(ret_val_x.transpose(0,2,3,1), softmax).squeeze()
names = data.get_names(files)
labels = data.get_labels(names).astype(np.float32)
pred = ret_val_y
In [236]:
k=26
print 'pred:'+str(pred[k])+' true:'+str(labels[k])
print 'pred:'+str(pred256[k])+' true:'+str(labels[k])
In [237]:
%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
k =26#4:584 3:
im = Image.open('data/train_medium/'+names[k]+'.tiff')
# plt.figure();plt.subplot(1,2,1);plt.imshow(CAM[k]);plt.title(names[k]);plt.subplot(1,2,2);plt.imshow(im);plt.title('pred:'+str(pred[k]))
# plt.figure();plt.imshow(im);plt.title('true: '+str(labels[k]))
plt.figure(figsize=(20,16));plt.subplot(1,2,1);plt.imshow(CAM[k], interpolation='lanczos');plt.axis('off')#plt.title(names[k]);
plt.subplot(1,2,2);plt.imshow(im);plt.axis('off')#plt.title('pred:'+str(pred[k])+' true:'+str(labels[k]))
# plt.figure();plt.subplot(1,2,1);plt.imshow(CAM[k]);plt.title(names[k]);plt.subplot(1,2,2);plt.imshow(im);plt.title('pred:'+str(pred[k]))
# plt.figure();plt.imshow(im);plt.title('true: '+str(labels[k]))
plt.figure(figsize=(20,16));plt.subplot(1,2,1);plt.imshow(CAM256[k], interpolation='lanczos');plt.axis('off')#plt.title(names[k]);
plt.subplot(1,2,2);plt.imshow(im);plt.axis('off')#plt.title('pred:'+str(pred256[k])+' true:'+str(labels[k]))
#CAM_syn = pred256[k]/(pred256[k]+pred128[k])*(CAM256[k][1:55,1:55] - CAM256[k][1:55,1:55].min())/(CAM256[k][1:55,1:55].max() - CAM256[k][1:55,1:55].min()) + pred128[k]/(pred256[k]+pred128[k])*(CAM[k] - CAM[k].min())/(CAM[k].max() - CAM[k].min())
CAM_syn = (CAM256[k][1:55,1:55] - CAM256[k][1:55,1:55].min())/(CAM256[k][1:55,1:55].max() - CAM256[k][1:55,1:55].min()) + (CAM[k] - CAM[k].min())/(CAM[k].max() - CAM[k].min())
plt.figure(figsize=(20,16));plt.subplot(1,2,1);plt.imshow(CAM_syn, interpolation='lanczos');plt.axis('off')#plt.title(names[k]);
plt.subplot(1,2,2);plt.imshow(im);plt.axis('off')#plt.title('pred:'+str(0.5*(pred[k]+pred256[k]))+' true:'+str(labels[k]))
print 'pred:'+str(pred[k])+' true:'+str(labels[k])
print 'pred:'+str(pred256[k])+' true:'+str(labels[k])
In [46]:
#CAM256 = CAM.copy();pred256 = pred.copy()
CAM128 = CAM.copy();pred128 = pred.copy()
In [81]:
import nolearn
cnf_d = 'configs_GAP_full/c_256_5x5_32.py'
#cnf_d = 'configs/c_128_5x5_32.py'
config_d = util.load_module(cnf_d).config
net_d = nn.create_net(config_d)
net_d.initialize()
layer_info = nolearn.lasagne.PrintLayerInfo()
firstInfo = layer_info._get_greeting(net_d)
layer_info = layer_info._get_layer_info_plain(net_d)
print firstInfo
print layer_info
In [233]:
np.where(labels[:1000]==1)
Out[233]:
In [122]:
names[k]
Out[122]:
In [101]:
CAM256[k]
Out[101]:
In [ ]: