def predict(self,address,dp): img = scipy.ndimage.imread(address) print img.shape size_1 = int(img.shape[1]*1.0/img.shape[0]*224.0) offset = int((size_1-224.0)/2) # print size_1,offset img = scipy.misc.imresize(img, (224,size_1), interp='bilinear', mode=None)[:,offset:offset+224,:] print img.shape img = img.reshape(224**2,3).T.reshape(1,3,224,224) img = img.astype("double") nn.show_images(img,(1,1),unit = True) assert nn.backend == nn.GnumpyBackend img = nn.garray(img - dp.data_mean.reshape(1,3,256,256)[:,:,:224,:224]) if dp.mode == "vgg": img = img[:,::-1,:,:] self.feedforward(img) H_cpu = self.H[-1].as_numpy_array() mask = nn.NumpyBackend.k_sparsity_mask(H_cpu,5,1) # H_cpu *= mask index = np.nonzero(mask) # print index[1] # print H_cpu[:100] for i in xrange(len(index[1])): print index[1][i],dp.words(index[1][i]),H_cpu[index][i]
def predict(self, address, dp): img = scipy.ndimage.imread(address) print img.shape size_1 = int(img.shape[1] * 1.0 / img.shape[0] * 224.0) offset = int((size_1 - 224.0) / 2) # print size_1,offset img = scipy.misc.imresize(img, (224, size_1), interp='bilinear', mode=None)[:, offset:offset + 224, :] print img.shape img = img.reshape(224**2, 3).T.reshape(1, 3, 224, 224) img = img.astype("double") nn.show_images(img, (1, 1), unit=True) assert nn.backend == nn.GnumpyBackend img = nn.garray(img - dp.data_mean.reshape(1, 3, 256, 256)[:, :, :224, :224]) if dp.mode == "vgg": img = img[:, ::-1, :, :] self.feedforward(img) H_cpu = self.H[-1].as_numpy_array() mask = nn.NumpyBackend.k_sparsity_mask(H_cpu, 5, 1) # H_cpu *= mask index = np.nonzero(mask) # print index[1] # print H_cpu[:100] for i in xrange(len(index[1])): print index[1][i], dp.words(index[1][i]), H_cpu[index][i]
def show_filters(self,*size): w1,b1=self.weights[1] w2,b2=self.weights[2] if self.size - 1 in self.cfg.index_convolution: # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') nn.show_images(np.swapaxes(w2.as_numpy_array(),0,3)[:,:,:,:size[0]*size[1]],(size[0],size[1]),unit=True) return if 1 in self.cfg.index_dense: plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') if type(self.cfg[0].shape)==int: nn.show_images(w1[:,:size[0]*size[1]].T,size) #MNIST dense elif self.cfg[0].shape[0]==1: nn.show_images(w1[:,:size[0]*size[1]].T,size) #MNIST dense elif self.cfg[0].shape[0]==3: nn.show_images(w1[:,:size[0]*size[1]].reshape(3,32,32,size[0]*size[1]),size[::-1]) #CIFAR10 dense
def visualize_conv(self,X): w1,b1 = self.weights[1] w2,b2 = self.weights[2] x=X[:,:,:,:1] self.feedforward(x) plt.figure(num=None, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') plt.subplot(131) nn.show_images(self.H[2][:,:,:,:1],(1,1)) plt.subplot(132) nn.show_images(w1[:,:,:,:16],(4,4)) plt.subplot(133) nn.show_images(np.swapaxes(w2.as_numpy_array(),0,3)[:,:,:,:16],(4,4),unit=True) plt.show() plt.figure(num=None, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') plt.subplot(131) self.plot_train(a=2) plt.subplot(132) nn.show_images(np.swapaxes(self.H[1][:16,:,:,:1].as_numpy_array(),0,3),(4,4),bg="white") plt.subplot(133) # if self.dataset == "cifar": nn.show_images(self.H1[:,:,:,:1].sum(0).reshape(1,32,32,1),(1,1)) # elif self.dataset == "mnist": nn.show_images(self.H1[:,:,:,:1].sum(0).reshape(1,28,28,1),(1,1)) plt.show()
def visualize(self,X,u=None,s=None): if self.H[-1].ndim ==4: self.visualize_conv(X) return x = self.data_provider(X,0,1) w1,b1=self.weights[1] plt.figure(num=None, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') plt.subplot(131); self.plot_train(a=2) plt.subplot(132); self.plot_test(a=2) plt.subplot(133) if 1 in self.cfg.index_dense: #dense if self.cfg[0].shape==[1, 28, 28] or self.cfg[0].shape==784: if w1.shape[1]>25: nn.show_images(w1[:,:25].T,(5,5)) #MNIST dense else: nn.show_images(w1[:,:].T,(5,w1.shape[1]/5)) #MNIST softmax elif self.cfg[0].shape==[3, 32, 32]: if w1.shape[1]>25: nn.show_images(w1[:,:25].reshape(3,32,32,25),(5,5)) #CIFAR10 dense else: nn.show_images(w1[:,:].reshape(3,32,32,10),(5,2)) #CIFAR10 softmax elif self.cfg[0].shape==[3, 8, 8]: #CIFAR10 dense patches if u==None: nn.show_images(w1[:,:25].reshape(3,8,8,25),(5,5)) else: nn.show_images(whiten_undo(w1[:,:25].T.as_numpy_array(),u,s).T.reshape(3,8,8,25),(5,5),unit=True) elif self.cfg[0].shape==[1, 8, 8]: #MNIST dense patches nn.show_images(w1[:,:25].T.as_numpy_array().T.reshape(1,8,8,16),(4,4),unit=True) else: if self.cfg[0].shape[0]<4: nn.show_images(w1[:,:,:,:16],(4,4)) #convnet plt.show() if self.cfg[-1].activation==nn.linear: #autoencoder if self.cfg[0].shape==784: #MNIST dense plt.subplot(121); nn.show_images(self.H[0][:1,:],(1,1)) plt.subplot(122); nn.show_images(self.H[-1][:1,:],(1,1)); if self.cfg[0].shape==[1, 28, 28]: #MNIST dense plt.subplot(121); nn.show_images(self.H[0][:,:,:,0].reshape(1, 28, 28,1)) plt.subplot(122); nn.show_images(self.H[-1][0].T.reshape(1,28,28,1),(1,1)); elif self.cfg[0].shape==[3, 32, 32]: #CIFAR10 plt.subplot(121); nn.show_images(self.H[0][:,:,:,0].reshape(3, 32, 32,1),(1,1)) plt.subplot(122); nn.show_images(self.H[-1][0].T.reshape(3,32,32,1),(1,1)) elif self.cfg[0].shape==[1, 8, 8]: #MNIST patches plt.subplot(121); nn.show_images(self.H[0][:,:,:,8:9],(1,1)) plt.subplot(122); nn.show_images(self.H[-1][8].T.reshape(1,8,8,1),(1,1)) elif self.cfg[0].shape==[3, 8, 8]: #CIFAR10 patches if u==None: plt.subplot(121); nn.show_images(self.H[0][:,:,:,0].reshape(3, 8, 8,1),(1,1)) plt.subplot(122); nn.show_images(self.H[-1][0].T.reshape(3,8,8,1),(1,1)) else: plt.subplot(121); nn.show_images(whiten_undo(X.reshape(192,1000000).T[0].as_numpy_array(),u,s).T.reshape(3,8,8,1),(1,1),unit=True) self.feedforward(x.reshape(3,8,8,1)) plt.subplot(122); nn.show_images(whiten_undo(self.H[-1][0].as_numpy_array(),u,s).T.reshape(3,8,8,1),(1,1),unit=True) plt.show() if not(1 in self.cfg.index_dense): self.feedforward(x) plt.figure(num=None, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') plt.subplot(121); nn.show_images(np.swapaxes(self.H[1][:16,:,:,:1].as_numpy_array(),0,3),(4,4),bg="white") plt.subplot(122); nn.show_images(np.swapaxes(self.H[2][:16,:,:,:1].as_numpy_array(),0,3),(4,4),bg="white") plt.show()
def show_filters(self,size=(10,20),scale = 1,unit = 1): w1,b1=self.weights[1] w2,b2=self.weights[-1] if self.cfg.arch == "dense" and self.cfg.learning == "auto": # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') if self.cfg.dataset == "mnist": # print w2.shape nn.show_images(w2[:size[0]*size[1]],(size[0],size[1]),scale=scale,unit=unit) elif self.cfg.dataset == "cifar10": nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],3,32,32),(size[0],size[1]),scale=scale,unit=unit) elif self.cfg.dataset == "frey": nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],3,20,20),(size[0],size[1]),scale=scale,unit=unit) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size_ = int(np.sqrt(self.cfg[0].shape/3)) nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],3,size_,size_),(size[0],size[1]),scale=scale,unit=unit) elif self.cfg.dataset == "natural": #CIFAR10 dense patches size = int(np.sqrt(self.cfg[0].shape)) nn.show_images(w2[:256].reshape(256,1,size,size),(16,16),unit=unit,scale=scale) if self.cfg.learning == "auto" and self.cfg.arch == "conv": # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') # if self.cfg.dataset == "mnist": # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:size[0]*size[1],:,:,:],(size[0],size[1]),unit=unit) # print w2.shape if self.cfg[-1].type=="convolution": num_filters = w2.shape[1] if size==None: nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:num_filters,:,:,:],(4,num_filters/4),scale=scale) else: print 'ali',np.swapaxes(w2.as_numpy_array(),0,1).shape nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:size[0]*size[1],:,:,:],(size[0],size[1]),scale=scale) if self.cfg[-1].type=="deconvolution": # print 'ali',size num_filters = w2.shape[0] nn.show_images(w2.as_numpy_array()[:num_filters,:,:,:],(4,num_filters/4),unit=1,scale=scale) # elif: # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:num_filters,:,:,:],(4,num_filters/4)) if self.cfg.learning == "disc" and self.cfg.arch == "dense": if self.cfg[1].type=="dense": plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') if type(self.cfg[0].shape)==int: nn.show_images(w1[:,:size[0]*size[1]].T,size) #MNIST dense elif self.cfg[0].shape[0]==1: nn.show_images(w1[:,:size[0]*size[1]].T,size) #MNIST dense elif self.cfg[0].shape[0]==3: nn.show_images(w1[:,:size[0]*size[1]].reshape(3,32,32,size[0]*size[1]),size[::-1]) #CIFAR10 dense if self.cfg.learning == "disc" and self.cfg.arch == "conv": nn.show_images(w1[:size[0]*size[1],:,:,:],size,scale=scale,unit=unit) plt.show()
def visualize(self,dp,visual_params): w1,b1 = self.weights[1] w2,b2 = self.weights[-1] if visual_params['save']: if self.cfg.learning == "disc": num_filters = w1.shape[0] nn.show_images(w1,(4,num_filters/4)) if self.cfg.arch == "dense" and self.cfg.learning == "auto": # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') size=(10,20) if self.cfg.dataset == "mnist": # print size, w2.shape nn.show_images(w2[:size[0]*size[1]],(size[0],size[1]),unit=1,scale=2) elif self.cfg.dataset in ("cifar10","svhn-ram"): nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],3,32,32),(size[0],size[1]),unit=1) elif self.cfg.dataset == "faces": nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],1,32,32),(size[0],size[1]),unit=1) elif self.cfg.dataset == "faces48": nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],1,48,48),(size[0],size[1]),unit=1) elif self.cfg.dataset == "frey": nn.show_images(w2[:size[0]*size[1]].reshape(size[0]*size[1],1,20,20),(size[0],size[1]),unit=1) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.cfg[0].shape/3)) nn.show_images(w2[:256].reshape(256,3,size,size),(16,16),unit=1,scale=2) else: #CIFAR10 dense patches size = int(np.sqrt(self.cfg[0].shape)) # print size nn.show_images(w2[:200].reshape(200,1,size,size),(10,20),unit=1,scale=2) # nn.show_images(w2[:64].reshape(64,1,size,size),(8,8),unit=1,scale=2) if self.cfg.learning == "auto" and self.cfg.arch == "conv": # print w2.as_numpy_array()[:num_filters,:,:,:].shape num_filters = w2.shape[0] # print w2.shape nn.show_images(w2.as_numpy_array()[:num_filters,:,:,:],(4,num_filters/4),unit=1,scale=2) # plt.subplot(212) # num_filters = w1.shape[0] # nn.show_images(w1.as_numpy_array()[:num_filters,:,:,:],(4,num_filters/4),unit=1) # print w1.shape # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:num_filters,:,:,:],(4,num_filters/4),unit=1) # plt.show() plt.savefig(self.cfg.directory+self.cfg.name+".png",format="png") plt.close() else: if not nn.is_interactive(): if self.cfg.learning == "auto" and not(self.cfg.dataset in ("cifar10-second","svhn-second","mnist-second")): plt.figure(num=1, figsize=(15,10), dpi=80, facecolor='w', edgecolor='k') else: plt.figure(num=1, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') # X = dp.X_id(0) # x = nn.data_convertor(X,0,1) w1,b1=self.weights[1] w2,b2=self.weights[-1] if self.cfg.arch == "dense" and self.cfg.learning == "disc": #dense if nn.is_interactive(): plt.figure(num=1, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot(131); self.plot_train() plt.subplot(132); self.plot_test() plt.subplot(133) if self.cfg.dataset == "mnist": if w1.shape[1]>25: nn.show_images(w1[:,:25].T,(5,5)) #MNIST dense else: nn.show_images(w1[:,:].T,(5,w1.shape[1]/5)) #MNIST softmax elif self.cfg.dataset in ("cifar10","svhn-ram"): # print w1.shape if w1.shape[1]>25: nn.show_images(w1.T[:25,:].reshape(25,3,32,32),(5,5)) #CIFAR10 dense else: nn.show_images(w1[:,:].reshape(3,32,32,10),(5,2)) #CIFAR10 softmax elif self.cfg.dataset in ("svhn-torch"): # print w1.shape if w1.shape[1]>25: nn.show_images(w1.T[:25,:].reshape(25,3,32,32),(5,5),yuv=True) #CIFAR10 dense else: nn.show_images(w1[:,:].reshape(3,32,32,10),(5,2),yuv=True) #CIFAR10 softmax elif self.cfg.dataset == "cifar10-patches": #CIFAR10 dense patches if u==None: nn.show_images(w1[:,:25].reshape(3,8,8,25),(5,5)) else: nn.show_images(whiten_undo(w1[:,:25].T.as_numpy_array(),u,s).T.reshape(3,8,8,25),(5,5),unit=True) elif self.cfg.dataset == "mnist-patches": #MNIST dense patches nn.show_images(w1[:,:25].T.as_numpy_array().T.reshape(1,8,8,16),(4,4),unit=True) else: channel = self.H[0].shape[1] size = self.H[0].shape[2] if w1.shape[1]>25: nn.show_images(w1.T[:25,:].reshape(25,channel,size,size),(5,5)) else: nn.show_images(w1[:,:].reshape(10,channel,size,size),(5,2)) if self.cfg.arch == "dense" and self.cfg.learning == "auto" and not self.cfg.dataset_extra: #dense if nn.is_interactive(): plt.figure(num=1, figsize=(15,10), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot2grid((2,3), (0,0), colspan=1); self.plot_train() plt.subplot2grid((2,3), (0,1), colspan=2) if self.cfg.dataset == "mnist": nn.show_images(w2[:50],(5,10)) # nn.show_images(w2[:25],(5,5)) elif self.cfg.dataset in ("cifar10","svhn-ram"): # print w2.shape nn.show_images(w2[:50].reshape(50,3,32,32),(5,10)) elif self.cfg.dataset == "svhn-torch": #CIFAR10 dense patches nn.show_images(w2[:50].reshape(50,3,32,32),(5,10),yuv=True) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1]/3)) # print size nn.show_images(w2[:50].reshape(50,3,size,size),(5,10)) # if u==None: nn.show_images(w1[:,:25].reshape(3,8,8,25),(5,5)) # else: nn.show_images(whiten_undo(w1[:,:25].T.as_numpy_array(),u,s).T.reshape(3,8,8,25),(5,5),unit=True) elif self.cfg.dataset == "mnist-patches": #MNIST dense patches nn.show_images(w1[:,:25].T.as_numpy_array().T.reshape(1,8,8,16),(4,4),unit=True) else: #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1])) # print w2[:50].shape,size nn.show_images(w2[:50].reshape(50,1,size,size),(5,10)) plt.subplot2grid((2,3), (1,0), colspan=1); if self.cfg.dataset in ("natural","mnist"): nn.show_images(w1[:,:25].T,(5,5)) # w1,b1 = self.weights[1] # w2,b2 = self.weights[2] # print w1[:5,:5] # print w2[:5,:5].T # print "------" plt.subplot2grid((2,3), (1,1), colspan=1); if self.cfg.dataset == "mnist": nn.show_images(self.H[0][0].reshape(1,1,28,28),(1,1)) elif self.cfg.dataset in ("cifar10","svhn-ram"): nn.show_images(self.H[0][0].reshape(1,3,32,32),(1,1)) elif self.cfg.dataset == "svhn-torch": nn.show_images(self.H[0][0].reshape(1,3,32,32),(1,1),yuv=True) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1]/3)) nn.show_images(self.H[0][0].reshape(1,3,size,size),(1,1)) else: #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1])) nn.show_images(self.H[0][0].reshape(1,1,size,size),(1,1)) plt.subplot2grid((2,3), (1,2), colspan=1); if self.cfg.dataset == "mnist": nn.show_images(self.H[-1][0].reshape(1,1,28,28),(1,1)) elif self.cfg.dataset in ("cifar10","svhn-ram"): nn.show_images(self.H[-1][0].reshape(1,3,32,32),(1,1)) elif self.cfg.dataset == "svhn-torch": nn.show_images(self.H[-1][0].reshape(1,3,32,32),(1,1),yuv=True) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1]/3)) nn.show_images(self.H[-1][0].reshape(1,3,size,size),(1,1)) else: #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1])) nn.show_images(self.H[-1][0].reshape(1,1,size,size),(1,1)) if self.cfg.arch == "conv" and self.cfg.learning == "disc": if nn.is_interactive(): plt.figure(num=1, figsize=(15,5), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot(131); self.plot_train() plt.subplot(132); self.plot_test() plt.subplot(133) nn.show_images(w1[:16,:,:,:],(4,4)) if self.cfg.arch == "conv" and self.cfg.learning == "auto": if nn.is_interactive(): plt.figure(num=1, figsize=(15,10), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) # w2,b2 = self.weights[-1] # x=X[:,:,:,:1] # self.feedforward(x) if self.cfg.dataset in ("cifar10-second","svhn-second","mnist-second"): #CIFAR10 plt.subplot(131); # print self.H[0].shape,self.H[-1].shape,self.H[-1].max() nn.show_images(np.swapaxes(self.H[0][:1,:16,:,:].as_numpy_array(),0,1),(4,4),bg="white") plt.subplot(132); nn.show_images(np.swapaxes(self.H[-1][:1,:16,:,:].as_numpy_array(),0,1),(4,4),bg="white") plt.subplot(133); nn.show_images(np.swapaxes(self.H[-2][:1,:16,:,:].as_numpy_array(),0,1),(4,4),bg="white") # print self.H[-1] else: plt.subplot(231); nn.show_images(self.H[0][0,:,:,:].reshape(1,self.H[0].shape[1],self.H[0].shape[2],self.H[0].shape[3]),(1,1)); plt.subplot(232); nn.show_images(self.H[-1][0,:,:,:].reshape(1,self.H[-1].shape[1],self.H[-1].shape[2],self.H[-1].shape[3]),(1,1)); plt.subplot(233); self.plot_train() plt.subplot(234) # if self.H[1].shape[1]>=16: # H1 = self.H[1].as_numpy_array() # H1 = H1.reshape(16*100,28*28) # print np.nonzero(H1)[0].shape nn.show_images(np.swapaxes(self.H[-2][:1,:16,:,:].as_numpy_array(),0,1),(4,4),bg="white") # else: # nn.show_images(np.swapaxes(self.H[1][:1,:8,:,:].as_numpy_array(),0,1),(2,4),bg="white") plt.subplot(235) # if w1.shape[0]>16: nn.show_images(w1[:16,:,:,:],(4,4)) # else: # nn.show_images(w1[:8,:,:,:],(2,4)) plt.subplot(236) # if w2.shape[0]>=16: # print w2.shape # if self.cfg.dataset == "svhn-torch": # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:16,:,:,:],(4,4),unit=1,yuv=1) if self.cfg[-1].type=="convolution": nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:16,:,:,:],(4,4),unit=1) if self.cfg[-1].type=="deconvolution": nn.show_images(w2[:16,:,:,:],(4,4),unit=1) # else: # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:,:8,:,:],(2,4),unit=True) if nn.is_interactive(): plt.show() else: plt.draw(); plt.pause(.01) if self.cfg.dataset_extra=="generate": #dense for k in self.cfg.index_dense: if self.cfg[k].l2_activity!=None: index = k if nn.is_interactive(): plt.figure(num=1, figsize=(15,10), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot2grid((2,3), (0,0), colspan=1); self.plot_train() plt.subplot2grid((2,3), (0,1), colspan=2) if self.cfg.dataset == "mnist": nn.show_images(w2[:50],(5,10)) plt.subplot2grid((2,3), (1,1), colspan=1); # if self.cfg.dataset == "mnist": # print self.H[index].shape x = self.H[index][:,0].as_numpy_array() y = self.H[index][:,1].as_numpy_array() plt.plot(x,y,'bo') # plt.grid() x = self.T_sort[:,0].as_numpy_array() y = self.T_sort[:,1].as_numpy_array() plt.plot(x,y,'ro') # plt.grid() # x = self.test_rand[:,0].as_numpy_array() # y = self.test_rand[:,1].as_numpy_array() # plt.plot(x,y,'go') plt.grid() # nn.show_images(self.H[0][0].reshape(1,1,28,28),(1,1)) plt.subplot2grid((2,3), (1,2), colspan=1); if self.cfg.dataset == "mnist": nn.show_images(self.H[-1][0].reshape(1,1,28,28),(1,1)) if self.cfg.dataset == "mnist" and self.cfg.dataset_extra in ("vae","generate"): plt.figure(num=5, figsize=(15,10), dpi=80, facecolor='w', edgecolor='k') temp = nn.randn((64,784)) self.test_mode = True self.feedforward(temp) self.test_mode = False nn.show_images(self.H[-1].reshape(64,1,28,28),(8,8)) if visual_params['save']: plt.savefig(self.cfg.directory+self.cfg.name+"_samples.png",format="png") else: plt.draw(); plt.pause(.01)
def show_filters(self, size=(10, 20), scale=1, unit=1): w1, b1 = self.weights[1] w2, b2 = self.weights[-1] if self.cfg.arch == "dense" and self.cfg.learning == "auto": # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') if self.cfg.dataset == "mnist": # print w2.shape nn.show_images(w2[:size[0] * size[1]], (size[0], size[1]), scale=scale, unit=unit) elif self.cfg.dataset == "cifar10": nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 3, 32, 32), (size[0], size[1]), scale=scale, unit=unit) elif self.cfg.dataset == "frey": nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 3, 20, 20), (size[0], size[1]), scale=scale, unit=unit) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size_ = int(np.sqrt(self.cfg[0].shape / 3)) nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 3, size_, size_), (size[0], size[1]), scale=scale, unit=unit) elif self.cfg.dataset == "natural": #CIFAR10 dense patches size = int(np.sqrt(self.cfg[0].shape)) nn.show_images(w2[:256].reshape(256, 1, size, size), (16, 16), unit=unit, scale=scale) if self.cfg.learning == "auto" and self.cfg.arch == "conv": # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') # if self.cfg.dataset == "mnist": # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:size[0]*size[1],:,:,:],(size[0],size[1]),unit=unit) # print w2.shape if self.cfg[-1].type == "convolution": num_filters = w2.shape[1] if size == None: nn.show_images(np.swapaxes(w2.as_numpy_array(), 0, 1)[:num_filters, :, :, :], (4, num_filters / 4), scale=scale) else: print 'ali', np.swapaxes(w2.as_numpy_array(), 0, 1).shape nn.show_images(np.swapaxes(w2.as_numpy_array(), 0, 1)[:size[0] * size[1], :, :, :], (size[0], size[1]), scale=scale) if self.cfg[-1].type == "deconvolution": # print 'ali',size num_filters = w2.shape[0] nn.show_images(w2.as_numpy_array()[:num_filters, :, :, :], (4, num_filters / 4), unit=1, scale=scale) # elif: # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:num_filters,:,:,:],(4,num_filters/4)) if self.cfg.learning == "disc" and self.cfg.arch == "dense": if self.cfg[1].type == "dense": plt.figure(num=None, figsize=(30, 90), dpi=80, facecolor='w', edgecolor='k') if type(self.cfg[0].shape) == int: nn.show_images(w1[:, :size[0] * size[1]].T, size) #MNIST dense elif self.cfg[0].shape[0] == 1: nn.show_images(w1[:, :size[0] * size[1]].T, size) #MNIST dense elif self.cfg[0].shape[0] == 3: nn.show_images(w1[:, :size[0] * size[1]].reshape( 3, 32, 32, size[0] * size[1]), size[::-1]) #CIFAR10 dense if self.cfg.learning == "disc" and self.cfg.arch == "conv": nn.show_images(w1[:size[0] * size[1], :, :, :], size, scale=scale, unit=unit) plt.show()
def visualize(self, dp, visual_params): w1, b1 = self.weights[1] w2, b2 = self.weights[-1] if visual_params['save']: if self.cfg.learning == "disc": num_filters = w1.shape[0] nn.show_images(w1, (4, num_filters / 4)) if self.cfg.arch == "dense" and self.cfg.learning == "auto": # plt.figure(num=None, figsize=(30,90), dpi=80, facecolor='w', edgecolor='k') size = (10, 20) if self.cfg.dataset == "mnist": # print size, w2.shape nn.show_images(w2[:size[0] * size[1]], (size[0], size[1]), unit=1, scale=2) elif self.cfg.dataset in ("cifar10", "svhn-ram"): nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 3, 32, 32), (size[0], size[1]), unit=1) elif self.cfg.dataset == "faces": nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 1, 32, 32), (size[0], size[1]), unit=1) elif self.cfg.dataset == "faces48": nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 1, 48, 48), (size[0], size[1]), unit=1) elif self.cfg.dataset == "frey": nn.show_images(w2[:size[0] * size[1]].reshape( size[0] * size[1], 1, 20, 20), (size[0], size[1]), unit=1) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.cfg[0].shape / 3)) nn.show_images(w2[:256].reshape(256, 3, size, size), (16, 16), unit=1, scale=2) else: #CIFAR10 dense patches size = int(np.sqrt(self.cfg[0].shape)) # print size nn.show_images(w2[:200].reshape(200, 1, size, size), (10, 20), unit=1, scale=2) # nn.show_images(w2[:64].reshape(64,1,size,size),(8,8),unit=1,scale=2) if self.cfg.learning == "auto" and self.cfg.arch == "conv": # print w2.as_numpy_array()[:num_filters,:,:,:].shape num_filters = w2.shape[0] # print w2.shape nn.show_images(w2.as_numpy_array()[:num_filters, :, :, :], (4, num_filters / 4), unit=1, scale=2) # plt.subplot(212) # num_filters = w1.shape[0] # nn.show_images(w1.as_numpy_array()[:num_filters,:,:,:],(4,num_filters/4),unit=1) # print w1.shape # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:num_filters,:,:,:],(4,num_filters/4),unit=1) # plt.show() plt.savefig(self.cfg.directory + self.cfg.name + ".png", format="png") plt.close() else: if not nn.is_interactive(): if self.cfg.learning == "auto" and not (self.cfg.dataset in ( "cifar10-second", "svhn-second", "mnist-second")): plt.figure(num=1, figsize=(15, 10), dpi=80, facecolor='w', edgecolor='k') else: plt.figure(num=1, figsize=(15, 5), dpi=80, facecolor='w', edgecolor='k') # X = dp.X_id(0) # x = nn.data_convertor(X,0,1) w1, b1 = self.weights[1] w2, b2 = self.weights[-1] if self.cfg.arch == "dense" and self.cfg.learning == "disc": #dense if nn.is_interactive(): plt.figure(num=1, figsize=(15, 5), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot(131) self.plot_train() plt.subplot(132) self.plot_test() plt.subplot(133) if self.cfg.dataset == "mnist": if w1.shape[1] > 25: nn.show_images(w1[:, :25].T, (5, 5)) #MNIST dense else: nn.show_images(w1[:, :].T, (5, w1.shape[1] / 5)) #MNIST softmax elif self.cfg.dataset in ("cifar10", "svhn-ram"): # print w1.shape if w1.shape[1] > 25: nn.show_images(w1.T[:25, :].reshape(25, 3, 32, 32), (5, 5)) #CIFAR10 dense else: nn.show_images(w1[:, :].reshape(3, 32, 32, 10), (5, 2)) #CIFAR10 softmax elif self.cfg.dataset in ("svhn-torch"): # print w1.shape if w1.shape[1] > 25: nn.show_images(w1.T[:25, :].reshape(25, 3, 32, 32), (5, 5), yuv=True) #CIFAR10 dense else: nn.show_images(w1[:, :].reshape(3, 32, 32, 10), (5, 2), yuv=True) #CIFAR10 softmax elif self.cfg.dataset == "cifar10-patches": #CIFAR10 dense patches if u == None: nn.show_images(w1[:, :25].reshape(3, 8, 8, 25), (5, 5)) else: nn.show_images(whiten_undo( w1[:, :25].T.as_numpy_array(), u, s).T.reshape(3, 8, 8, 25), (5, 5), unit=True) elif self.cfg.dataset == "mnist-patches": #MNIST dense patches nn.show_images(w1[:, :25].T.as_numpy_array().T.reshape( 1, 8, 8, 16), (4, 4), unit=True) else: channel = self.H[0].shape[1] size = self.H[0].shape[2] if w1.shape[1] > 25: nn.show_images( w1.T[:25, :].reshape(25, channel, size, size), (5, 5)) else: nn.show_images( w1[:, :].reshape(10, channel, size, size), (5, 2)) if self.cfg.arch == "dense" and self.cfg.learning == "auto" and not self.cfg.dataset_extra: #dense if nn.is_interactive(): plt.figure(num=1, figsize=(15, 10), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot2grid((2, 3), (0, 0), colspan=1) self.plot_train() plt.subplot2grid((2, 3), (0, 1), colspan=2) if self.cfg.dataset == "mnist": nn.show_images(w2[:50], (5, 10)) # nn.show_images(w2[:25],(5,5)) elif self.cfg.dataset in ("cifar10", "svhn-ram"): # print w2.shape nn.show_images(w2[:50].reshape(50, 3, 32, 32), (5, 10)) elif self.cfg.dataset == "svhn-torch": #CIFAR10 dense patches nn.show_images(w2[:50].reshape(50, 3, 32, 32), (5, 10), yuv=True) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1] / 3)) # print size nn.show_images(w2[:50].reshape(50, 3, size, size), (5, 10)) # if u==None: nn.show_images(w1[:,:25].reshape(3,8,8,25),(5,5)) # else: nn.show_images(whiten_undo(w1[:,:25].T.as_numpy_array(),u,s).T.reshape(3,8,8,25),(5,5),unit=True) elif self.cfg.dataset == "mnist-patches": #MNIST dense patches nn.show_images(w1[:, :25].T.as_numpy_array().T.reshape( 1, 8, 8, 16), (4, 4), unit=True) else: #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1])) # print w2[:50].shape,size nn.show_images(w2[:50].reshape(50, 1, size, size), (5, 10)) plt.subplot2grid((2, 3), (1, 0), colspan=1) if self.cfg.dataset in ("natural", "mnist"): nn.show_images(w1[:, :25].T, (5, 5)) # w1,b1 = self.weights[1] # w2,b2 = self.weights[2] # print w1[:5,:5] # print w2[:5,:5].T # print "------" plt.subplot2grid((2, 3), (1, 1), colspan=1) if self.cfg.dataset == "mnist": nn.show_images(self.H[0][0].reshape(1, 1, 28, 28), (1, 1)) elif self.cfg.dataset in ("cifar10", "svhn-ram"): nn.show_images(self.H[0][0].reshape(1, 3, 32, 32), (1, 1)) elif self.cfg.dataset == "svhn-torch": nn.show_images(self.H[0][0].reshape(1, 3, 32, 32), (1, 1), yuv=True) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1] / 3)) nn.show_images(self.H[0][0].reshape(1, 3, size, size), (1, 1)) else: #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1])) nn.show_images(self.H[0][0].reshape(1, 1, size, size), (1, 1)) plt.subplot2grid((2, 3), (1, 2), colspan=1) if self.cfg.dataset == "mnist": nn.show_images(self.H[-1][0].reshape(1, 1, 28, 28), (1, 1)) elif self.cfg.dataset in ("cifar10", "svhn-ram"): nn.show_images(self.H[-1][0].reshape(1, 3, 32, 32), (1, 1)) elif self.cfg.dataset == "svhn-torch": nn.show_images(self.H[-1][0].reshape(1, 3, 32, 32), (1, 1), yuv=True) elif self.cfg.dataset == "cifar10-patch": #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1] / 3)) nn.show_images(self.H[-1][0].reshape(1, 3, size, size), (1, 1)) else: #CIFAR10 dense patches size = int(np.sqrt(self.H[0].shape[1])) nn.show_images(self.H[-1][0].reshape(1, 1, size, size), (1, 1)) if self.cfg.arch == "conv" and self.cfg.learning == "disc": if nn.is_interactive(): plt.figure(num=1, figsize=(15, 5), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot(131) self.plot_train() plt.subplot(132) self.plot_test() plt.subplot(133) nn.show_images(w1[:16, :, :, :], (4, 4)) if self.cfg.arch == "conv" and self.cfg.learning == "auto": if nn.is_interactive(): plt.figure(num=1, figsize=(15, 10), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) # w2,b2 = self.weights[-1] # x=X[:,:,:,:1] # self.feedforward(x) if self.cfg.dataset in ("cifar10-second", "svhn-second", "mnist-second"): #CIFAR10 plt.subplot(131) # print self.H[0].shape,self.H[-1].shape,self.H[-1].max() nn.show_images(np.swapaxes( self.H[0][:1, :16, :, :].as_numpy_array(), 0, 1), (4, 4), bg="white") plt.subplot(132) nn.show_images(np.swapaxes( self.H[-1][:1, :16, :, :].as_numpy_array(), 0, 1), (4, 4), bg="white") plt.subplot(133) nn.show_images(np.swapaxes( self.H[-2][:1, :16, :, :].as_numpy_array(), 0, 1), (4, 4), bg="white") # print self.H[-1] else: plt.subplot(231) nn.show_images( self.H[0][0, :, :, :].reshape(1, self.H[0].shape[1], self.H[0].shape[2], self.H[0].shape[3]), (1, 1)) plt.subplot(232) nn.show_images( self.H[-1][0, :, :, :].reshape(1, self.H[-1].shape[1], self.H[-1].shape[2], self.H[-1].shape[3]), (1, 1)) plt.subplot(233) self.plot_train() plt.subplot(234) # if self.H[1].shape[1]>=16: # H1 = self.H[1].as_numpy_array() # H1 = H1.reshape(16*100,28*28) # print np.nonzero(H1)[0].shape nn.show_images(np.swapaxes( self.H[-2][:1, :16, :, :].as_numpy_array(), 0, 1), (4, 4), bg="white") # else: # nn.show_images(np.swapaxes(self.H[1][:1,:8,:,:].as_numpy_array(),0,1),(2,4),bg="white") plt.subplot(235) # if w1.shape[0]>16: nn.show_images(w1[:16, :, :, :], (4, 4)) # else: # nn.show_images(w1[:8,:,:,:],(2,4)) plt.subplot(236) # if w2.shape[0]>=16: # print w2.shape # if self.cfg.dataset == "svhn-torch": # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:16,:,:,:],(4,4),unit=1,yuv=1) if self.cfg[-1].type == "convolution": nn.show_images(np.swapaxes(w2.as_numpy_array(), 0, 1)[:16, :, :, :], (4, 4), unit=1) if self.cfg[-1].type == "deconvolution": nn.show_images(w2[:16, :, :, :], (4, 4), unit=1) # else: # nn.show_images(np.swapaxes(w2.as_numpy_array(),0,1)[:,:8,:,:],(2,4),unit=True) if nn.is_interactive(): plt.show() else: plt.draw() plt.pause(.01) if self.cfg.dataset_extra == "generate": #dense for k in self.cfg.index_dense: if self.cfg[k].l2_activity != None: index = k if nn.is_interactive(): plt.figure(num=1, figsize=(15, 10), dpi=80, facecolor='w', edgecolor='k') plt.figure(1) plt.subplot2grid((2, 3), (0, 0), colspan=1) self.plot_train() plt.subplot2grid((2, 3), (0, 1), colspan=2) if self.cfg.dataset == "mnist": nn.show_images(w2[:50], (5, 10)) plt.subplot2grid((2, 3), (1, 1), colspan=1) # if self.cfg.dataset == "mnist": # print self.H[index].shape x = self.H[index][:, 0].as_numpy_array() y = self.H[index][:, 1].as_numpy_array() plt.plot(x, y, 'bo') # plt.grid() x = self.T_sort[:, 0].as_numpy_array() y = self.T_sort[:, 1].as_numpy_array() plt.plot(x, y, 'ro') # plt.grid() # x = self.test_rand[:,0].as_numpy_array() # y = self.test_rand[:,1].as_numpy_array() # plt.plot(x,y,'go') plt.grid() # nn.show_images(self.H[0][0].reshape(1,1,28,28),(1,1)) plt.subplot2grid((2, 3), (1, 2), colspan=1) if self.cfg.dataset == "mnist": nn.show_images(self.H[-1][0].reshape(1, 1, 28, 28), (1, 1)) if self.cfg.dataset == "mnist" and self.cfg.dataset_extra in ( "vae", "generate"): plt.figure(num=5, figsize=(15, 10), dpi=80, facecolor='w', edgecolor='k') temp = nn.randn((64, 784)) self.test_mode = True self.feedforward(temp) self.test_mode = False nn.show_images(self.H[-1].reshape(64, 1, 28, 28), (8, 8)) if visual_params['save']: plt.savefig(self.cfg.directory + self.cfg.name + "_samples.png", format="png") else: plt.draw() plt.pause(.01)