Exemple #1
0
def test_save_load():
    np.random.seed(123)
    a_np = np.random.randn(3, 2).astype(np.float32)
    a = PyTorch.asFloatTensor(a_np)
    print('a', a)

    filename = '/tmp/foo.t7'  # TODO: should use tempfile to get this
    PyTorchAug.save(filename, a)

    b = PyTorchAug.load(filename)
    print('type(b)', type(b))
    print('b', b)

    assert np.abs(a_np - b.asNumpyTensor()).max() < 1e-4
Exemple #2
0
 def load(self, path):
     # dont remove this line. This is important to init the PyTorch nn
     self.init_modules()
     print('loading from: ' + path)
     model = PyTorchAug.load(path)
     print(model)
     return model
Exemple #3
0
        def __init__(self, *args):
            _fromLua = False
            if len(args) >= 1:
                if args[0] == '__FROMLUA__':
                   _fromLua = True
                   args = args[1:]
#            print('LuaWrapper.__init__', lua_classname, 'fromLua', _fromLua, 'args', args)
            self.luaclass = lua_classname
            if not _fromLua:
                PyTorchAug.LuaClass.__init__(self, splitName, *args)
            else:
                self.__dict__['__objectId'] = PyTorchAug.getNextObjectId()
Exemple #4
0
        def __init__(self, *args):
            _fromLua = False
            if len(args) >= 1:
                if args[0] == '__FROMLUA__':
                   _fromLua = True
                   args = args[1:]
#            print('LuaWrapper.__init__', lua_classname, 'fromLua', _fromLua, 'args', args)
            self.luaclass = lua_classname
            if not _fromLua:
                PyTorchAug.LuaClass.__init__(self, splitName, *args)
            else:
                self.__dict__['__objectId'] = PyTorchAug.getNextObjectId()
Exemple #5
0
lua.call(1, 0)

lua = PyTorchAug.lua
lua.getGlobal("require")
lua.pushString('modules/SelectiveGradientFilter')
lua.call(1, 0)

dim_hidden = 200
feature_maps = 96

filter_size = 5
colorchaPyTorchAugels = 1

# ipdb.set_trace()

encoder = PyTorchAug.Sequential()

encoder.add(
    PyTorchAug.SpatialConvolution(colorchaPyTorchAugels, feature_maps,
                                  filter_size, filter_size))

encoder.add(PyTorchAug.SpatialMaxPooling(2, 2, 2, 2))
encoder.add(PyTorchAug.Threshold(0, 1e-6))

encoder.add(
    PyTorchAug.SpatialConvolution(feature_maps, feature_maps / 2, filter_size,
                                  filter_size))
encoder.add(PyTorchAug.SpatialMaxPooling(2, 2, 2, 2))
encoder.add(PyTorchAug.Threshold(0, 1e-6))

encoder.add(
          net.params['rpn_cls_score'][0].data.reshape(18, 512))
transferB(tnet["rpn_cls_score"].bias, net.params['rpn_cls_score'][1].data)

tnet["rpn_bbox_pred"] = nn.SpatialConvolutionMM(512, 36, 1, 1, 1, 1, 0, 0)
transferW(tnet["rpn_bbox_pred"].weight,
          net.params['rpn_bbox_pred'][0].data.reshape(36, 512))
transferB(tnet["rpn_bbox_pred"].bias, net.params['rpn_bbox_pred'][1].data)

tnet["fc6"] = nn.Linear(25088, 4096)
transferW(tnet["fc6"].weight, net.params['fc6'][0].data)
transferB(tnet["fc6"].bias, net.params['fc6'][1].data)
tnet["relu6"] = nn.ReLU()
tnet["drop6"] = nn.Dropout(0.5)

tnet["fc7"] = nn.Linear(4096, 4096)
transferW(tnet["fc7"].weight, net.params['fc7'][0].data)
transferB(tnet["fc7"].bias, net.params['fc7'][1].data)
tnet["relu7"] = nn.ReLU()
tnet["drop7"] = nn.Dropout(0.5)

tnet["cls_score"] = nn.Linear(4096, 21)
transferW(tnet["cls_score"].weight, net.params['cls_score'][0].data)
transferB(tnet["cls_score"].bias, net.params['cls_score'][1].data)

tnet["bbox_pred"] = nn.Linear(4096, 84)
transferW(tnet["bbox_pred"].weight, net.params['bbox_pred'][0].data)
transferB(tnet["bbox_pred"].bias, net.params['bbox_pred'][1].data)

print net.params
PyTorchAug.save("faster_rcnn_2.t7", tnet)
#    print('res', res)
    return res

def cuda_from_double(self):
#    print('cl')
    res = PyCudaTorch.DoubleTensorToCudaTensor(self)
#    print('res', res)
    return res

FloatTensor.cuda = cuda_from_float
DoubleTensor.cuda = cuda_from_double

#PyTorchAug.

#def Linear_cl(self):
#    print('Linear_cl')
#    print('self', self)
#    self.cl()
#    return self

## import PyTorch
#Linear.cl = Linear_cl

print('dir(PyTorchAug)', dir(PyTorchAug))
print('dir(PyCudaTorch)', dir(PyCudaTorch))
PyTorchAug.cythonClasses['torch.CudaTensor'] = {'popFunction': PyCudaTorch.cyPopCudaTensor}
PyTorchAug.populateLuaClassesReverse()

PyTorchAug.pushFunctionByPythonClass[PyCudaTorch.CudaTensor] = PyCudaTorch.cyPushCudaTensor

Exemple #8
0
def cl(self):
    #    print('cl')
    res = PyClTorch.FloatTensorToClTensor(self)
    #    print('res', res)
    return res


FloatTensor.cl = cl

#PyTorchAug.

#def Linear_cl(self):
#    print('Linear_cl')
#    print('self', self)
#    self.cl()
#    return self

## import PyTorch
#Linear.cl = Linear_cl

print('dir(PyTorchAug)', dir(PyTorchAug))
print('dir(PyClTorch)', dir(PyClTorch))
PyTorchAug.cythonClasses['torch.ClTensor'] = {
    'popFunction': PyClTorch.cyPopClTensor
}
PyTorchAug.populateLuaClassesReverse()

PyTorchAug.pushFunctionByPythonClass[
    PyClTorch.ClTensor] = PyClTorch.cyPushClTensor
Exemple #9
0
transferW(tnet["conv5_3"].weight,
          net.params['conv5_3'][0].data.reshape(512, 4608))
transferB(tnet["conv5_3"].bias, net.params['conv5_3'][1].data)
tnet["relu5_3"] = nn.ReLU()

tnet["rpn_conv/3x3"] = nn.SpatialConvolutionMM(512, 512, 3, 3, 1, 1, 1, 1)
tnet["rpn_relu/3x3"] = nn.ReLU()

tnet["rpn_cls_score"] = nn.SpatialConvolutionMM(512, 18, 1, 1, 1, 1, 0, 0)

tnet["rpn_bbox_pred"] = nn.SpatialConvolutionMM(512, 36, 1, 1, 1, 1, 0, 0)

tnet["fc6"] = nn.Linear(25088, 4096)
transferW(tnet["fc6"].weight, net.params['fc6'][0].data)
transferB(tnet["fc6"].bias, net.params['fc6'][1].data)
tnet["relu6"] = nn.ReLU()
tnet["drop6"] = nn.Dropout(0.5)

tnet["fc7"] = nn.Linear(4096, 4096)
transferW(tnet["fc7"].weight, net.params['fc7'][0].data)
transferB(tnet["fc7"].bias, net.params['fc7'][1].data)
tnet["relu7"] = nn.ReLU()
tnet["drop7"] = nn.Dropout(0.5)

tnet["cls_score"] = nn.Linear(4096, 21)

tnet["bbox_pred"] = nn.Linear(4096, 84)

print net.params
PyTorchAug.save("vgg16.t7", tnet)
	    transferB(tnet["rpn_bbox_pred"].bias, net.params['rpn_bbox_pred'][1].data)

	tnet["fc6"] = nn.Linear(18432,4096) #512*6*6
        print "fc6"
	transferW(tnet["fc6"].weight, net.params['fc6'][0].data)
	transferB(tnet["fc6"].bias, net.params['fc6'][1].data)
	tnet["relu6"] = nn.ReLU()
	tnet["drop6"] = nn.Dropout(0.5)

	tnet["fc7"] = nn.Linear(4096,1024)
        print "fc7"
	transferW(tnet["fc7"].weight, net.params['fc7'][0].data)
	transferB(tnet["fc7"].bias, net.params['fc7'][1].data)
	tnet["relu7"] = nn.ReLU()
	tnet["drop7"] = nn.Dropout(0.5)

	tnet["cls_score"] = nn.Linear(1024,21)
        if args.rpn:
            print "cls_score"
	    transferW(tnet["cls_score"].weight, net.params['cls_score'][0].data)
	    transferB(tnet["cls_score"].bias, net.params['cls_score'][1].data)

	tnet["bbox_pred"] = nn.Linear(1024,84)
        if args.rpn:
            print "bbox_pred"
	    transferW(tnet["bbox_pred"].weight, net.params['bbox_pred'][0].data)
	    transferB(tnet["bbox_pred"].bias, net.params['bbox_pred'][1].data)

	print net.params
	PyTorchAug.save(args.out,tnet)
Exemple #11
0
 def save(self, model, path):
     PyTorchAug.save(path, model)