def cbconvFG_test1(gpu=True): filtSize = (3, 3) threshold = 0.00 input = torch.zeros(1, 2, 9, 9).float().contiguous() if gpu: input = input.cuda() prevInput = input.clone().contiguous() input[0, 0, 1, 1] = 1.0 input[0, 0, 1, 2] = 3.2 input[0, 0, 0, 5] = 1.5 input[0, 1, 5:, 7:].random_(0, 100).add_(-50) prevInput[0, 1, 7, 5] = 4.0 weight = torch.randn(3, input.size(-3), filtSize[1], filtSize[0]).contiguous() if gpu: weight = weight.cuda() # bias missing!!! weight.fill_(3.0) #weight[0,0,0,0] = 0.125 #generate stimuli and init outputRef = F.conv2d(F.Variable(input), F.Variable(weight), padding=tuple(s // 2 for s in filtSize)).data prevOutput = F.conv2d(F.Variable(prevInput), F.Variable(weight), padding=tuple(s // 2 for s in filtSize)).data output = prevOutput.clone().contiguous() cbconvFG(input, prevInput, output, weight, threshold) error = (output - outputRef).abs().max() #visualization for error analysis ic = 1 oc = 0 plt.subplot(3, 2, 1) plt.imshow((input)[0, ic]) plt.title('input') plt.subplot(3, 2, 2) plt.imshow((prevInput)[0, ic]) plt.title('prevInput') plt.subplot(3, 2, 3) plt.imshow((output)[0, oc]) plt.title('output') plt.subplot(3, 2, 4) plt.imshow((prevOutput)[0, oc]) plt.title('prevOutput') plt.subplot(3, 2, 5) plt.imshow((outputRef)[0, oc]) plt.title('outputRef') plt.subplot(3, 2, 6) plt.imshow((output - outputRef)[0, oc]) plt.title('outputDiff') passed = error < 1e-6 # print(error) return passed
def changeDetection_python(input, prevInput, filtSize, threshold): assert (input.size() == prevInput.size() and input.dim() == 4) tt = (input - prevInput).abs().ge(threshold).sum(1).gt(0) tt = tt.unsqueeze_(0).float() filt = input.new(1, 1, filtSize[0], filtSize[1]).fill_(1) ss = F.conv2d(F.Variable(tt), F.Variable(filt), padding=tuple([(sz - 1) // 2 for sz in filtSize])) res = (ss.data > 0).char() assert (res.dim() == 4 and res.size(0) == 1 and res.size(1) == 1) return res
def forward(self, input_x): """ :param input_x: a list size having the number of batch_size elements with the same length :return: batch_size X num_aspects tensor """ # Embedding x = self.embed( input_x) # dim: (batch_size, max_seq_len, embedding_size) if self.args.static: x = F.Variable(input_x) # Conv & max pool x = x.unsqueeze(1) # dim: (batch_size, 1, max_seq_len, embedding_size) # turns to be a list: [ti : i \in kernel_sizes] where ti: tensor of dim([batch, num_kernels, max_seq_len-i+1]) x = [F.relu(conv(x)).squeeze(3) for conv in self.conv] # dim: [(batch_size, num_kernels), ...]*len(kernel_sizes) x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] x = torch.cat(x, 1) # Dropout & output x = self.dropout(x) # (batch_size,len(kernel_sizes)*num_kernels) logit = F.log_softmax(self.fc(x)) # (batch_size, num_aspects) return logit
def matrixMult_python(Xmatrix, weights, bias, activFun=None): if Xmatrix.numel() == 0: return Xmatrix.clone() Ymatrix = Xmatrix.matmul( weights.view(weights.size(0), -1).transpose(0, 1)).add_(bias) if activFun is not None: Ymatrix = activFun(F.Variable(Ymatrix), inplace=True).data return Ymatrix
def forward_fg(self, inp): input = inp.data if self.prevInput.size() != input.size(): #init prevOutput self.prevOutput = F.conv2d( F.Variable(input), self.weight, padding=tuple(s // 2 for s in self.weight.size()[2:]), bias=self.bias).data else: po = self.prevOutput.clone() self.prevOutput = cbconvFG(input, self.prevInput, po, self.weight, self.threshold) outp = F.Variable(self.prevOutput) if self.withReLU: outp = F.relu(outp) self.prevInput = input return outp
def getEdge(batch): edgeslist=[] for kk in range(batch.size(0)): x=batch[kk] # print(x.size()) x=x.cpu().data.numpy() if len(x.shape)>2: x=np.transpose(x,(1,2,0)) x=rgb2gray(x) edges = filters.sobel(x) edgeslist.append(edges) edgeslist=np.array(edgeslist) edgeslist=torch.Tensor(edgeslist).cuda() edgeslist=F.Variable(edgeslist) return edgeslist
def forward(self, inp): assert (type(inp) == tuple and inp[0] == 'changeIndexes') input = inp[1].data.contiguous() changeIndexes = inp[2].data.contiguous() if len(changeIndexes) != 0: assert (changeIndexes.dim() == 1) nc, h, w = input.size(-3), input.size(-2), input.size(-1) if self.ceil_mode: oh, ow = (h - 1) // 2 + 1, (w - 1) // 2 + 1 else: oh, ow = h // 2, w // 2 if list(self.outputState.size()[-3:]) != [nc, oh, ow]: self.outputState.resize_(1, nc, oh, ow).fill_(1e1000) assert (self.outputState.is_cuda) maxPool2d(input, self.outputState, changeIndexes, self.kernel_size, self.stride, useHalf=(type(input) == torch.cuda.HalfTensor)) # output = torch.nn.functional.max_pool2d(torch.nn.functional.Variable(input), # self.kernel_size, stride=self.stride, # ceil_mode=self.ceil_mode).data # self.outputState = output output = self.outputState.clone() if self.propChangeIndexes: return 'changeIndexes', F.Variable(output), F.Variable( changeIndexes) else: return F.Variable(output)
def forward_normal(self, inp): #input parsing and checks if type(inp) == tuple: if inp[0] == 'changeIndexes': input = inp[1].data.contiguous() changeIndexes = inp[2].data.contiguous() assert (changeIndexes.dim() == 1) else: assert (False) else: input = inp.data.contiguous() assert (input.size(-3) == self.in_channels) assert (input.dim() == 4) if self.prevInput.size() != input.size(): # self.prevInput = input.new(input.size()).fill_(1e1000) self.prevInput.resize_as_(input).fill_(1e1000) outpSize = list(input.size()) outpSize[-3] = self.out_channels outpSize = torch.Size(outpSize) if self.prevOutput.size() != outpSize: self.prevOutput.resize_(outpSize).fill_(1e1000) if self.gatherComputationStats: #gather statistics based on fine-grained evaluation per input feature map #additional opt. would be to act on input pixel instead of affected output pixels changeTensor = (input - self.prevInput).abs().gt(self.threshold) proped = torch.nn.functional.conv2d( torch.nn.functional.Variable(changeTensor.float()), torch.nn.functional.Variable( changeTensor.new(changeTensor.size(-3), 1, self.weight.size(2), self.weight.size(3)).fill_(1).float()), groups=changeTensor.size(-3)).data.gt(0) opsPerValue = self.weight.size(0) * self.weight.size( 2) * self.weight.size(3) * 2 compStats = dict( numInputChangesPerFeatureMap=changeTensor.sum() * opsPerValue, # no. of Ops with FG FM&SP inference numInputChanges=changeTensor.sum(-3).gt(0).sum() * changeTensor.size(-3) * opsPerValue, # no. of Ops w/ FG SP numInputPropedChangesPerFeatureMap=proped.sum() * opsPerValue, # no. of Ops w/ FG FM numInputPropedChanges=proped.sum(-3).gt(0).sum() * changeTensor.size(-3) * opsPerValue, # no. of Ops w/ CG totalInputValues=changeTensor.size(-1) * changeTensor.size(-2) * changeTensor.size(-3) * opsPerValue # no. of Ops cuDNN ) self.compStats = compStats if 'changeIndexes' not in locals(): if input.is_cuda: changeMap = changeDetection( input, self.prevInput, self.kernel_size, self.threshold, updateInputState=self.feedbackLoop, useHalf=(type(input) == torch.cuda.HalfTensor)) else: assert (self.feedbackLoop == False) changeMap = changeDetection_python(input, self.prevInput, self.kernel_size, self.threshold) if self.saveChangeMap: self.changeMap = changeMap changeIndexes = changeIndexesExtr_python(changeMap) if not (self.feedbackLoop): if self.copyInput: self.prevInput.copy_(input) else: self.prevInput = input if changeIndexes.numel() != 0: if self.prevInput.is_cuda: Xmatrix = genXMatrix( self.prevInput, changeIndexes, self.kernel_size, useHalf=(type(self.prevInput) == torch.cuda.HalfTensor)) else: Xmatrix = genXMatrix_python(self.prevInput, changeIndexes, self.kernel_size) Ymatrix = matrixMult_python(Xmatrix, self.weight.data, self.bias.data) Ymatrix = Ymatrix.transpose(0, 1) if self.prevInput.is_cuda: output = updateOutput( Ymatrix, changeIndexes, self.prevOutput, withReLU=self.withReLU, useHalf=(type(input) == torch.cuda.HalfTensor)) else: output = updateOutput_python(Ymatrix, changeIndexes, self.prevOutput, withReLU=self.withReLU) self.prevOutput = output # not needed, since internally prevOutput is updated and output = self.prevOutput if self.propChangeIndexes: return 'changeIndexes', F.Variable( self.prevOutput), F.Variable(changeIndexes) else: return F.Variable(self.prevOutput)