示例#1
0
    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]
示例#2
0
 def load_npz(self, name):
     f = np.load(name)
     w = f['w']
     # print w.shape
     # print self.weights.mem.shape
     if nn.backend == nn.GnumpyBackend: self.weights.mem[:] = nn.garray(w)
     else: self.weights.mem[:] = w
示例#3
0
    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]
示例#4
0
 def load_text(self, name):
     if nn.backend == nn.GnumpyBackend:
         print "gnumpy"
         self.weights.mem = nn.garray(
             np.loadtxt("./out/" + name, delimiter=','))
     else:
         print "numpy"
         self.weights.mem = np.loadtxt("./out/" + name, delimiter=',')
示例#5
0
 def make_tied_copy(self, i, j):
     wk0, bk0 = self[i]
     wk1, bk1 = self[j]
     if wk0.ndim == 2:
         wk0 = wk1.T
         wk1[:] = wk0.T
     else:
         w = wk1.as_numpy_array()
         for ch in xrange(wk1.shape[0]):
             for f in range(wk1.shape[1]):
                 x = w[ch, f, :, :]
                 x_flip = np.flipud(np.fliplr(x))
                 wk0[f, ch, :, :] = nn.garray(x_flip)
示例#6
0
 def make_tied_copy(self,i,j):
     wk0,bk0 = self[i]
     wk1,bk1 = self[j]
     if wk0.ndim==2:
         wk0    = wk1.T
         wk1[:] = wk0.T
     else:
         w=wk1.as_numpy_array()
         for ch in xrange(wk1.shape[0]):
             for f in range(wk1.shape[1]):
                 x = w[ch,f,:,:]
                 x_flip = np.flipud(np.fliplr(x))
                 wk0[f,ch,:,:] = nn.garray(x_flip)
示例#7
0
 def load_npz(self,name):
     f=np.load(name); w = f['w']
     # print w.shape
     # print self.weights.mem.shape
     if nn.backend==nn.GnumpyBackend: self.weights.mem[:]=nn.garray(w)
     else:                            self.weights.mem[:]=w
示例#8
0
 def load_text(self,name):
     if nn.backend==nn.GnumpyBackend: print "gnumpy"; self.weights.mem=nn.garray(np.loadtxt("./out/"+name, delimiter=','))
     else:                            print "numpy" ; self.weights.mem=np.loadtxt("./out/"+name, delimiter=',')