def createResidualLayer(self, customLayer=None): core1 = nn.Sequential() if customLayer: core2 = customLayer else: core2 = nn.Identity() core = nn.ConcatTable() core.add(core1) core.add(core2) layer = nn.Sequential() layer.add(core) layer.add(nn.CAddTable()) layer.add(nn.ReLU()) return layer, core1, core2
def init_modules(self): n = nn.Sequential() n = nn.SpatialConvolution(1, 1, 1,1, 1,1, 1,1) n = nn.SpatialBatchNormalization(1) n = nn.ReLU() n = nn.Sigmoid() n = nn.SpatialMaxPooling(1,1, 1,1, 1,1) n = nn.SpatialAveragePooling(1,1, 1,1, 1,1) n = nn.Linear(1, 1) n = nn.Dropout(0.1) n = nn.SoftMax() n = nn.Identity() n = nn.Reshape(1) n = nn.BCECriterion() n = nn.MSECriterion()
tnet = {} net = caffe.Net( 'test_c.prototxt', '/work/cv3/sankaran/caffe-exp/faster-rcnn-exp/VGG16_e2e_pascal/models/ms_tv_vgg16_faster_rcnn_iter_100000.caffemodel', caffe.TEST) print net.params['fc6'][0].data.shape print net.params['fc6'][1].data.shape tnet["conv1_1"] = nn.SpatialConvolutionMM(3, 64, 3, 3, 1, 1, 1, 1) transferW(tnet["conv1_1"].weight, net.params['conv1_1'][0].data.reshape(64, 27)) transferB(tnet["conv1_1"].bias, net.params['conv1_1'][1].data) tnet["relu1_1"] = nn.ReLU() tnet["conv1_2"] = nn.SpatialConvolutionMM(64, 64, 3, 3, 1, 1, 1, 1) transferW(tnet["conv1_2"].weight, net.params['conv1_2'][0].data.reshape(64, 576)) transferB(tnet["conv1_2"].bias, net.params['conv1_2'][1].data) tnet["relu1_2"] = nn.ReLU() tnet["pool1"] = nn.SpatialMaxPooling(2, 2, 2, 2, 0, 0) tnet["conv2_1"] = nn.SpatialConvolutionMM(64, 128, 3, 3, 1, 1, 1, 1) transferW(tnet["conv2_1"].weight, net.params['conv2_1'][0].data.reshape(128, 576)) transferB(tnet["conv2_1"].bias, net.params['conv2_1'][1].data) tnet["relu2_1"] = nn.ReLU() tnet["conv2_2"] = nn.SpatialConvolutionMM(128, 128, 3, 3, 1, 1, 1, 1)
myeval('linear.output') # print('linearCl.output', linear.output) output = linear.forward(a) print('output.dims()', output.dims()) print('output.size()', output.size()) outputFloat = output.float() print('outputFloat', outputFloat) print('output', output) mlp = nn.Sequential() mlp.add(nn.SpatialConvolutionMM(1, 16, 5, 5, 1, 1, 2, 2)) mlp.add(nn.ReLU()) mlp.add(nn.SpatialMaxPooling(3, 3, 3, 3)) mlp.add(nn.SpatialConvolutionMM(16, 32, 5, 5, 1, 1, 2, 2)) mlp.add(nn.ReLU()) mlp.add(nn.SpatialMaxPooling(2, 2, 2, 2)) mlp.add(nn.Reshape(32 * 4 * 4)) mlp.add(nn.Linear(32 * 4 * 4, 150)) mlp.add(nn.Tanh()) mlp.add(nn.Linear(150, 10)) mlp.add(nn.LogSoftMax()) mlp.cl() print('mlp', mlp) myeval('mlp.output') input = PyTorch.FloatTensor(128, 1, 28, 28).uniform().cl()
def test_cltorch(): if 'ALLOW_NON_GPUS' in os.environ: PyClTorch.setAllowNonGpus(True) # a = PyTorch.foo(3,2) # print('a', a) # print(PyTorch.FloatTensor(3,2)) a = PyClTorch.ClTensor([3, 4, 9]) assert a[0] == 3 assert a[1] == 4 assert a[2] == 9 print('a', a) a = PyClTorch.ClTensor([[3, 5, 7], [9, 2, 4]]) print('a', a) print('a[0]', a[0]) print('a[0][0]', a[0][0]) assert a[0][0] == 3 assert a[1][0] == 9 assert a[1][2] == 4 PyTorch.manualSeed(123) a = PyTorch.FloatTensor(4, 3).uniform() print('a', a) a_cl = a.cl() print(type(a_cl)) assert str(type(a_cl)) == '<class \'PyClTorch.ClTensor\'>' print('a_cl[0]', a_cl[0]) print('a_cl[0][0]', a_cl[0][0]) assert a[0][0] == a_cl[0][0] assert a[0][1] == a_cl[0][1] assert a[1][1] == a_cl[1][1] print('a.dims()', a.dims()) print('a.size()', a.size()) print('a', a) assert a.dims() == 2 assert a.size()[0] == 4 assert a.size()[1] == 3 a_sum = a.sum() a_cl_sum = a_cl.sum() assert abs(a_sum - a_cl_sum) < 1e-4 a_cl2 = a_cl + 3.2 assert abs(a_cl2[1][0] - a[1][0] - 3.2) < 1e-4 b = PyClTorch.ClTensor() print('got b') myeval('b') assert b.dims() == -1 b.resizeAs(a) myeval('b') assert b.dims() == 2 assert b.size()[0] == 4 assert b.size()[1] == 3 print('run uniform') b.uniform() myeval('b') print('create new b') b = PyClTorch.ClTensor() print('b.dims()', b.dims()) print('b.size()', b.size()) print('b', b) c = PyTorch.FloatTensor().cl() print('c.dims()', c.dims()) print('c.size()', c.size()) print('c', c) assert b.dims() == -1 assert b.size() is None print('creating Linear...') linear = nn.Linear(3, 5).float() print('created linear') print('linear:', linear) myeval('linear.output') myeval('linear.output.dims()') myeval('linear.output.size()') myeval('linear.output.nElement()') linear_cl = linear.clone().cl() print('type(linear.output)', type(linear.output)) print('type(linear_cl.output)', type(linear_cl.output)) assert str(type(linear.output)) == '<class \'PyTorch._FloatTensor\'>' assert str(type(linear_cl.output)) == '<class \'PyClTorch.ClTensor\'>' # myeval('type(linear)') # myeval('type(linear.output)') myeval('linear_cl.output.dims()') myeval('linear_cl.output.size()') # myeval('linear.output') assert str(type(linear)) == '<class \'PyTorchAug.Linear\'>' assert str(type(linear_cl)) == '<class \'PyTorchAug.Linear\'>' # assert str(type(linear.output)) == '<class \'PyClTorch.ClTensor\'>' # assert linear.output.dims() == -1 # why is this 0? should be -1??? # assert linear.output.size() is None # again, should be None? a_cl = PyClTorch.ClTensor(4, 3).uniform() # print('a_cl', a_cl) output_cl = linear_cl.forward(a_cl) # print('output', output) assert str(type(output_cl)) == '<class \'PyClTorch.ClTensor\'>' assert output_cl.dims() == 2 assert output_cl.size()[0] == 4 assert output_cl.size()[1] == 5 a = a_cl.float() output = linear.forward(a) assert str(type(output)) == '<class \'PyTorch._FloatTensor\'>' assert output.dims() == 2 assert output.size()[0] == 4 assert output.size()[1] == 5 print('a.size()', a.size()) print('a_cl.size()', a_cl.size()) assert a[1][0] == a_cl[1][0] assert a[2][1] == a_cl[2][1] mlp = nn.Sequential() mlp.add(nn.SpatialConvolutionMM(1, 16, 5, 5, 1, 1, 2, 2)) mlp.add(nn.ReLU()) mlp.add(nn.SpatialMaxPooling(3, 3, 3, 3)) mlp.add(nn.SpatialConvolutionMM(16, 32, 5, 5, 1, 1, 2, 2)) mlp.add(nn.ReLU()) mlp.add(nn.SpatialMaxPooling(2, 2, 2, 2)) mlp.add(nn.Reshape(32 * 4 * 4)) mlp.add(nn.Linear(32 * 4 * 4, 150)) mlp.add(nn.Tanh()) mlp.add(nn.Linear(150, 10)) mlp.add(nn.LogSoftMax()) mlp.float() mlp_cl = mlp.clone().cl() print('mlp_cl', mlp_cl) # myeval('mlp.output') input = PyTorch.FloatTensor(128, 1, 28, 28).uniform() input_cl = PyClTorch.FloatTensorToClTensor( input.clone()) # This is a bit hacky... output = mlp.forward(input) # myeval('input[0]') output_cl = mlp_cl.forward(input_cl) # myeval('output[0]') assert (output_cl.float() - output).abs().max() < 1e-4
def test_pynn(): PyTorch.manualSeed(123) linear = nn.Linear(3, 5) linear print('linear', linear) print('linear.weight', linear.weight) print('linear.output', linear.output) print('linear.gradInput', linear.gradInput) input = PyTorch.DoubleTensor(4, 3).uniform() print('input', input) output = linear.updateOutput(input) print('output', output) gradInput = linear.updateGradInput(input, output) print('gradInput', gradInput) criterion = nn.ClassNLLCriterion() print('criterion', criterion) print('dir(linear)', dir(linear)) mlp = nn.Sequential() mlp.add(linear) output = mlp.forward(input) print('output', output) # import sys # sys.path.append('thirdparty/python-mnist') from mnist import MNIST import numpy import array numpy.random.seed(123) mlp = nn.Sequential() mlp.add(nn.SpatialConvolutionMM(1, 16, 5, 5, 1, 1, 2, 2)) res = mlp.add(nn.ReLU()) print('res', res) mlp.add(nn.SpatialMaxPooling(3, 3, 3, 3)) mlp.add(nn.SpatialConvolutionMM(16, 32, 3, 3, 1, 1, 1, 1)) mlp.add(nn.ReLU()) mlp.add(nn.SpatialMaxPooling(2, 2, 2, 2)) mlp.add(nn.Reshape(32 * 4 * 4)) mlp.add(nn.Linear(32 * 4 * 4, 150)) mlp.add(nn.Tanh()) mlp.add(nn.Linear(150, 10)) mlp.add(nn.LogSoftMax()) criterion = nn.ClassNLLCriterion() print('got criterion') learningRate = 0.02 mndata = MNIST('data/mnist') imagesList, labelsB = mndata.load_training() images = numpy.array(imagesList).astype(numpy.float64) # print('imagesArray', images.shape) # print(images[0].shape) labelsf = array.array('d', labelsB.tolist()) imagesTensor = PyTorch.asDoubleTensor(images) # imagesTensor = PyTorch.FloatTensor(100,784) # labels = numpy.array(20,).astype(numpy.int32) # labelsTensor = PyTorch.FloatTensor(100).fill(1) # print('labels', labels) # print(imagesTensor.size()) def printStorageAddr(name, tensor): print('printStorageAddr START') storage = tensor.storage() if storage is None: print(name, 'storage is None') else: print(name, 'storage is ', hex(storage.dataAddr())) print('printStorageAddr END') labelsTensor = PyTorch.asDoubleTensor(labelsf) labelsTensor += 1 # print('calling size on imagestensor...') # print(' (called size)') desiredN = 128 maxN = int(imagesTensor.size()[0]) desiredN = min(maxN, desiredN) imagesTensor = imagesTensor.narrow(0, 0, desiredN) labelsTensor = labelsTensor.narrow(0, 0, desiredN) print('imagesTensor.size()', imagesTensor.size()) print('labelsTensor.size()', labelsTensor.size()) N = int(imagesTensor.size()[0]) print('type(imagesTensor)', type(imagesTensor)) size = PyTorch.LongStorage(4) size[0] = N size[1] = 1 size[2] = 28 size[3] = 28 imagesTensor.resize(size) imagesTensor /= 255.0 imagesTensor -= 0.2 print('imagesTensor.size()', imagesTensor.size()) print('start training...') for epoch in range(4): numRight = 0 for n in range(N): # print('n', n) input = imagesTensor[n] label = labelsTensor[n] labelTensor = PyTorch.DoubleTensor(1) labelTensor[0] = label # print('label', label) output = mlp.forward(input) prediction = PyTorch.getDoublePrediction(output) # print('prediction', prediction) if prediction == label: numRight += 1 criterion.forward(output, labelTensor) mlp.zeroGradParameters() gradOutput = criterion.backward(output, labelTensor) mlp.backward(input, gradOutput) mlp.updateParameters(learningRate) # PyTorch.collectgarbage() # if n % 100 == 0: # print('n=', n) print('epoch ' + str(epoch) + ' accuracy: ' + str(numRight * 100.0 / N) + '%')
print "Transfering RPN layers" proto = 'frcnn_vgg1024.prototxt' tnet = {} #net = caffe.Net('test.prototxt','/work/cv3/sankaran/faster-rcnn/data/imagenet_models/VGG_CNN_M_1024.v2.caffemodel',caffe.TEST) #net = caffe.Net('test_c.prototxt','vgg_cnn_m_1024_faster_rcnn.caffemodel',caffe.TEST) net = caffe.Net(proto,args.model,caffe.TEST) print net.params['fc6'][0].data.shape print net.params['fc6'][1].data.shape tnet["conv1"] = nn.SpatialConvolutionMM(3,96,7,7,2,2,0,0) print "conv1" transferW(tnet["conv1"].weight, net.params['conv1'][0].data.reshape(96,147)) transferB(tnet["conv1"].bias, net.params['conv1'][1].data) tnet["relu1"] = nn.ReLU() tnet["norm1"] = nn.SpatialCrossMapLRN(5,0.0005,0.75,2) tnet["pool1"] = nn.SpatialMaxPooling(3,3,2,2,0,0) tnet["conv2"] = nn.SpatialConvolutionMM(96,256,5,5,2,2,1,1) print "conv2" transferW(tnet["conv2"].weight, net.params['conv2'][0].data.reshape(256,2400)) transferB(tnet["conv2"].bias, net.params['conv2'][1].data) tnet["relu2"] = nn.ReLU() tnet["norm2"] = nn.SpatialCrossMapLRN(5,0.0005,0.75,2) tnet["pool2"] = nn.SpatialMaxPooling(3,3,2,2,0,0) tnet["conv3"]= nn.SpatialConvolutionMM(256,512,3,3,1,1,1,1) print "conv3" transferW(tnet["conv3"].weight, net.params['conv3'][0].data.reshape(512,2304)) transferB(tnet["conv3"].bias, net.params['conv3'][1].data)
def exportModel(self, graph): order, expanded_order = graph.topologicalSort() net = nn.Sequential() for id in order: node = graph.nodes.get(id) name = node.type m = node.params l = node.learned_params if m.get('in_channels') : nInputPlane = m.get('in_channels') else: nInputPlane = 1 if m.get('out_channels') : nOutputPlane = m.get('out_channels') else: nOutputPlane = 1 if m.get('num_features') : nFeatures = m.get('num_features') else: nFeatures = 1 if m.get('in_features') : inputDimension = m.get('in_features') else: inputDimension = 1 if m.get('out_features') : outputDimension = m.get('out_features') else: outputDimension = 1 if m.get('p') : p = m.get('p') else: p = 0.1 if m.get('kernel_size') : kernel_size = m.get('kernel_size') if type(kernel_size) == type((3,3)): kW = kernel_size[0] kH = kernel_size[1] else: kW = kernel_size kH = kernel_size else: kW = 1 kH = 1 if m.get('stride') : stride = m.get('stride') if type(stride) == type((3,3)): dW = stride[0] dH = stride[1] else: dW = stride dH = stride else: dW = 1 dH = 1 if m.get('padding') : padding = m.get('padding') if type(padding) == type((3,3)): padW = padding[0] padH = padding[1] else: padW = padding padH = padding else: padW = 0 padH = 0 if m.get('subgraph'): subgraph = m.get('subgraph') # copy the network architecture if name == 'Conv2d': n = nn.SpatialConvolution(nInputPlane, nOutputPlane, kW, kH, dW, dH, padW, padH) elif name == 'BatchNorm2d': n = nn.SpatialBatchNormalization(nFeatures) elif name == 'ReLU': n = nn.ReLU() elif name == 'Sigmoid': n = nn.Sigmoid() elif name == 'MaxPool2d': n = nn.SpatialMaxPooling(kW, kH, dW, dH, padW, padH) elif name == 'AvgPool2d': n = nn.SpatialAveragePooling(kW, kH, dW, dH, padW, padH) elif name == 'Linear': n = nn.Linear(inputDimension, outputDimension) elif name == 'Dropout': n = nn.Dropout(p) elif name == 'Softmax': n = nn.SoftMax() elif name == 'Identity': n = nn.Identity() elif name == 'Reshape': # add params here n = nn.Reshape() elif name == 'BCELoss': n = nn.BCECriterion() elif name == 'MSELoss': n = nn.MSECriterion() elif name == 'Sequential': n = self.exportModel(subgraph) elif name == 'ResNet': n, core, _ = self.createResidualLayer() core.add(nn.SpatialConvolution(128,128, 3,3, 2,2, 1,1)) core.add(nn.SpatialBatchNormalization(128)) core.add(nn.ReLU()) core.add(nn.SpatialConvolution(128,128, 3,3, 1,1, 1,1)) core.add(nn.SpatialBatchNormalization(128)) else: # Group or Sequential nodes if node.group == True: n = self.exportModel(subgraph) else: print('Not Implement', name) # copy the network weights weight = l.get('weight') bias = l.get('bias') running_mean = l.get('running_mean') running_var = l.get('running_var') # copy over the learned params if they exist self.copyWeights(n.weight, weight) self.copyWeights(n.bias, bias) self.copyWeights(n.running_mean, running_mean) self.copyWeights(n.running_var, running_var) net.add(n) return net