Beispiel #1
0
def trainSimpleTest(optCls, dtype, *args, **kwargs):
    from PuzzleLib.Containers.Sequential import Sequential

    from PuzzleLib.Modules.Linear import Linear
    from PuzzleLib.Modules.Activation import Activation, relu
    from PuzzleLib.Modules.Cast import Cast

    from PuzzleLib.Cost.MSE import MSE

    seq = Sequential()

    seq.append(Linear(128, 64, useBias=False))
    seq.append(Activation(relu))
    seq.append(Linear(64, 32, useBias=False))
    seq.append(Activation(relu))
    seq.append(Linear(32, 16))

    seq.calcMode(dtype)
    seq.append(Cast(intype=dtype, outtype=np.float32))

    optimizer = optCls(*args, **kwargs)
    optimizer.setupOn(seq, useGlobalState=True)

    mse = MSE()

    data = gpuarray.to_gpu(np.random.randn(16, 128).astype(dtype))
    target = gpuarray.to_gpu(np.random.randn(16, 16).astype(np.float32))

    for i in range(200):
        error, grad = mse(seq(data), target)

        optimizer.zeroGradParams()
        seq.backward(grad)
        optimizer.update()

        if (i + 1) % 5 == 0:
            print("Iteration #%d error: %s" % (i + 1, error))
Beispiel #2
0
def loadInceptionBN(modelpath,
                    actInplace=False,
                    bnInplace=False,
                    initscheme="none",
                    name="Inception-BN-0126"):
    net = Sequential(name=name)

    net.append(
        Conv2D(3,
               64,
               7,
               stride=2,
               pad=3,
               useBias=False,
               initscheme=initscheme,
               name="conv_1"))
    net.append(BatchNorm2D(64, inplace=bnInplace, name="bn_1"))
    net.append(Activation(relu, inplace=actInplace, name="relu_1"))

    net.append(MaxPool2D(3, 2, pad=1, name="pool_1"))

    net.append(
        Conv2D(64,
               64,
               1,
               useBias=False,
               initscheme=initscheme,
               name="conv_2_red"))
    net.append(BatchNorm2D(64, inplace=bnInplace, name="bn_2_red"))
    net.append(Activation(relu, inplace=actInplace, name="relu_2_red"))

    net.append(
        Conv2D(64,
               192,
               3,
               pad=1,
               useBias=False,
               initscheme=initscheme,
               name="conv_2"))
    net.append(BatchNorm2D(192, inplace=bnInplace, name="bn_2"))
    net.append(Activation(relu, inplace=actInplace, name="relu_2"))

    net.append(MaxPool2D(3, 2, pad=1, name="pool_2"))

    act, bn = actInplace, bnInplace
    net.extend(
        bnBlock(192, [64], [64, 64], [64, 96, 96], [32],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="3a"))
    net.extend(
        bnBlock(256, [64], [64, 96], [64, 96, 96], [64],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="3b"))
    net.extend(
        bnShrinkBlock(320, [128, 160], [64, 96, 96],
                      bn=bn,
                      act=act,
                      scheme=initscheme,
                      name="3c"))

    net.extend(
        bnBlock(576, [224], [64, 96], [96, 128, 128], [128],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="4a"))
    net.extend(
        bnBlock(576, [192], [96, 128], [96, 128, 128], [128],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="4b"))
    net.extend(
        bnBlock(576, [160], [128, 160], [128, 160, 160], [128],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="4c"))
    net.extend(
        bnBlock(608, [96], [128, 192], [160, 192, 192], [128],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="4d"))
    net.extend(
        bnShrinkBlock(608, [128, 192], [192, 256, 256],
                      act=act,
                      bn=bn,
                      scheme=initscheme,
                      name="4e"))

    net.extend(
        bnBlock(1056, [352], [192, 320], [160, 224, 224], [128],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="5a"))
    net.extend(
        bnBlock(1024, [352], [192, 320], [192, 224, 224], [128],
                act=act,
                bn=bn,
                scheme=initscheme,
                name="5b"))

    net.append(AvgPool2D(7, 1, name="global_pool"))
    net.append(Flatten(name="flatten"))
    net.append(Linear(1024, 1000, initscheme=initscheme, name="fc1"))
    net.append(SoftMax(name="softmax"))

    if modelpath is not None:
        net.load(modelpath, assumeUniqueNames=True)

    return net
Beispiel #3
0
def loadInceptionV3(modelpath,
                    actInplace=False,
                    bnInplace=False,
                    initscheme="none",
                    name="Inception-7-0001"):
    net = Sequential(name=name)

    net.append(
        Conv2D(3,
               32,
               3,
               stride=2,
               useBias=False,
               initscheme=initscheme,
               name="conv_conv2d"))
    net.append(BatchNorm2D(32, name="conv_batchnorm"))
    net.append(Activation(relu, inplace=actInplace, name="conv_relu"))

    net.append(
        Conv2D(32,
               32,
               3,
               useBias=False,
               initscheme=initscheme,
               name="conv_1_conv2d"))
    net.append(BatchNorm2D(32, name="conv_1_batchnorm"))
    net.append(Activation(relu, inplace=actInplace, name="conv_1_relu"))

    net.append(
        Conv2D(32,
               64,
               3,
               pad=1,
               useBias=False,
               initscheme=initscheme,
               name="conv_2_conv2d"))
    net.append(BatchNorm2D(64, name="conv_2_batchnorm"))
    net.append(Activation(relu, inplace=actInplace, name="conv_2_relu"))

    net.append(MaxPool2D(3, 2, name="pool"))

    net.append(
        Conv2D(64,
               80,
               1,
               useBias=False,
               initscheme=initscheme,
               name="conv_3_conv2d"))
    net.append(BatchNorm2D(80, name="conv_3_batchnorm"))
    net.append(Activation(relu, inplace=actInplace, name="conv_3_relu"))

    net.append(
        Conv2D(80,
               192,
               3,
               useBias=False,
               initscheme=initscheme,
               name="conv_4_conv2d"))
    net.append(BatchNorm2D(192, name="conv_4_batchnorm"))
    net.append(Activation(relu, inplace=actInplace, name="conv_4_relu"))

    net.append(MaxPool2D(3, 2, name="pool1"))

    act, bn = actInplace, bnInplace
    net.extend(
        bnBlock(192, [64], [48, 64], [64, 96, 96], [32], "mixed", act, bn,
                initscheme, 5, 2, "v3"))
    net.extend(
        bnBlock(256, [64], [48, 64], [64, 96, 96], [64], "mixed_1", act, bn,
                initscheme, 5, 2, "v3"))
    net.extend(
        bnBlock(288, [64], [48, 64], [64, 96, 96], [64], "mixed_2", act, bn,
                initscheme, 5, 2, "v3"))
    net.extend(
        bnShrinkBlock(288, [384], [64, 96, 96], "mixed_3", act, bn, initscheme,
                      False, 0, "v3"))

    net.extend(
        factorBlock(768, [192], [128, 128, 192], [128, 128, 128, 128, 192],
                    [192], "mixed_4", act, bn, initscheme))
    net.extend(
        factorBlock(768, [192], [160, 160, 192], [160, 160, 160, 160, 192],
                    [192], "mixed_5", act, bn, initscheme))
    net.extend(
        factorBlock(768, [192], [160, 160, 192], [160, 160, 160, 160, 192],
                    [192], "mixed_6", act, bn, initscheme))
    net.extend(
        factorBlock(768, [192], [192, 192, 192], [192, 192, 192, 192, 192],
                    [192], "mixed_7", act, bn, initscheme))
    net.extend(
        v3ShrinkBlock(768, [192, 320], [192, 192, 192, 192], "mixed_8", act,
                      bn, initscheme))

    net.extend(
        expandBlock(1280, [320], [384, 384, 384], [448, 384, 384, 384], [192],
                    "mixed_9",
                    act,
                    bn,
                    initscheme,
                    pool="avg"))

    net.extend(
        expandBlock(2048, [320], [384, 384, 384], [448, 384, 384, 384], [192],
                    "mixed_10",
                    act,
                    bn,
                    initscheme,
                    pool="max"))

    net.append(AvgPool2D(8, 1, name="global_pool"))
    net.append(Flatten(name="flatten"))
    net.append(Linear(2048, 1008, name="fc1"))
    net.append(SoftMax(name="softmax"))

    if modelpath is not None:
        net.load(modelpath, assumeUniqueNames=True)

    return net
Beispiel #4
0
def loadMiniYolo(modelpath, numOutput, actInplace=False, initscheme="none"):
    net = Sequential(name="YOLONet")

    block0 = block(idx=["1"],
                   inmaps=[3],
                   outmaps=[64],
                   sizeconv=[7],
                   strideconv=[2],
                   initscheme=initscheme,
                   actInPlace=actInplace)
    net.extend(block0)

    block1 = block(idx=["2"],
                   inmaps=[64],
                   outmaps=[192],
                   sizeconv=[3],
                   strideconv=[1],
                   initscheme=initscheme,
                   actInPlace=actInplace)
    net.extend(block1)

    block2 = block(idx=["3", "4", "5", "6"],
                   inmaps=[192, 128, 256, 256],
                   outmaps=[128, 256, 256, 512],
                   sizeconv=[1, 3, 1, 3],
                   strideconv=[1, 1, 1, 1],
                   initscheme=initscheme,
                   actInPlace=actInplace)
    net.extend(block2)

    block3 = block(
        idx=["7", "8", "9", "10", "11", "12", "13", "14", "15", "16"],
        inmaps=[512, 256, 512, 256, 512, 256, 512, 256, 512, 512],
        outmaps=[256, 512, 256, 512, 256, 512, 256, 512, 512, 1024],
        sizeconv=[1, 3, 1, 3, 1, 3, 1, 3, 1, 3],
        strideconv=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        initscheme=initscheme,
        actInPlace=actInplace)
    net.extend(block3)

    block4 = block(idx=["17", "18", "19", "20", "21", "22", "23", "24"],
                   inmaps=[1024, 512, 1024, 512, 1024, 1024, 1024, 1024],
                   outmaps=[512, 1024, 512, 1024, 1024, 1024, 1024, 1024],
                   sizeconv=[1, 3, 1, 3, 3, 3, 3, 3],
                   strideconv=[1, 1, 1, 1, 1, 2, 1, 1],
                   initscheme=initscheme,
                   actInPlace=actInplace,
                   addMaxpool=False)
    net.extend(block4)

    net.append(Flatten())
    insize = int(np.prod(net.dataShapeFrom((1, 3, 448, 448))))

    net.append(Linear(insize, 512, initscheme=initscheme, name="fc25"))
    net.append(Activation(relu, inplace=actInplace, name="fc_relu24"))

    net.append(Linear(512, 4096, initscheme=initscheme, name="fc26"))
    net.append(Activation(relu, inplace=actInplace, name="fc_relu25"))

    net.append(Linear(4096, numOutput, initscheme=initscheme, name="fc27"))
    net.append(SoftMax())

    if modelpath is not None:
        net.load(modelpath)

    return net
Beispiel #5
0
def loadResNet(modelpath,
               layers,
               actInplace=False,
               bnInplace=False,
               initscheme="none",
               name=None):
    if layers == "50":
        if name is None:
            name = "ResNet-50"

        level3names = ["3%s" % alpha for alpha in string.ascii_lowercase[1:4]]
        level4names = ["4%s" % alpha for alpha in string.ascii_lowercase[1:6]]

    elif layers == "101":
        if name is None:
            name = "ResNet-101"

        level3names = ["3b%s" % num for num in range(1, 4)]
        level4names = ["4b%s" % num for num in range(1, 23)]

    elif layers == "152":
        if name is None:
            name = "ResNet-152"

        level3names = ["3b%s" % num for num in range(1, 8)]
        level4names = ["4b%s" % num for num in range(1, 36)]

    else:
        raise ValueError("Unsupported ResNet layers mode")

    net = Sequential(name=name)

    net.append(
        Conv2D(3,
               64,
               7,
               stride=2,
               pad=3,
               name="conv1",
               initscheme=initscheme,
               useBias=False))
    net.append(BatchNorm2D(64, name="bn_conv1", inplace=bnInplace))
    net.append(Activation(relu, inplace=actInplace, name="conv1_relu"))
    net.append(MaxPool2D(3, 2, name="pool1"))

    net.extend(
        residBlock(64, 64, 1, "2a", True, actInplace, bnInplace, initscheme))
    net.extend(
        residBlock(256, 64, 1, "2b", False, actInplace, bnInplace, initscheme))
    net.extend(
        residBlock(256, 64, 1, "2c", False, actInplace, bnInplace, initscheme))

    net.extend(
        residBlock(256, 128, 2, "3a", True, actInplace, bnInplace, initscheme))

    for name in level3names:
        net.extend(
            residBlock(512, 128, 1, name, False, actInplace, bnInplace,
                       initscheme))

    net.extend(
        residBlock(512, 256, 2, "4a", True, actInplace, bnInplace, initscheme))

    for name in level4names:
        net.extend(
            residBlock(1024, 256, 1, name, False, actInplace, bnInplace,
                       initscheme))

    net.extend(
        residBlock(1024, 512, 2, "5a", True, actInplace, bnInplace,
                   initscheme))
    net.extend(
        residBlock(2048, 512, 1, "5b", False, actInplace, bnInplace,
                   initscheme))
    net.extend(
        residBlock(2048, 512, 1, "5c", False, actInplace, bnInplace,
                   initscheme))

    net.append(AvgPool2D(7, 1))
    net.append(Flatten())
    net.append(Linear(2048, 1000, initscheme=initscheme, name="fc1000"))
    net.append(SoftMax())

    if modelpath is not None:
        net.load(modelpath, assumeUniqueNames=True)

    return net
Beispiel #6
0
def loadVGG(modelpath, layers, poolmode="max", initscheme="none", withLinear=True, actInplace=False, name=None):
	if poolmode == "avg":
		pool = AvgPool2D
	elif poolmode == "max":
		pool = MaxPool2D
	else:
		raise ValueError("Unsupported pool mode")

	if layers not in {"11", "16", "19"}:
		raise ValueError("Unsupported VGG layers mode")

	if name is None and layers == "11":
		name = "VGG_ILSVRC_11_layers"
	elif name is None and layers == "16":
		name = "VGG_ILSVRC_16_layers"
	elif name is None and layers == "19":
		name = "VGG_ILSVRC_19_layers"

	layers = int(layers)

	net = Sequential(name=name)

	net.append(Conv2D(3, 64, 3, pad=1, initscheme=initscheme, name="conv1_1"))
	net.append(Activation(relu, inplace=actInplace, name="relu1_1"))

	if layers > 11:
		net.append(Conv2D(64, 64, 3, pad=1, initscheme=initscheme, name="conv1_2"))
		net.append(Activation(relu, inplace=actInplace, name="relu1_2"))

	net.append(pool(2, 2, name="pool1"))

	net.append(Conv2D(64, 128, 3, pad=1, initscheme=initscheme, name="conv2_1"))
	net.append(Activation(relu, inplace=actInplace, name="relu2_1"))

	if layers > 11:
		net.append(Conv2D(128, 128, 3, pad=1, initscheme=initscheme, name="conv2_2"))
		net.append(Activation(relu, inplace=actInplace, name="relu2_2"))

	net.append(pool(2, 2, name="pool2"))

	net.append(Conv2D(128, 256, 3, pad=1, initscheme=initscheme, name="conv3_1"))
	net.append(Activation(relu, inplace=actInplace, name="relu3_1"))
	net.append(Conv2D(256, 256, 3, pad=1, initscheme=initscheme, name="conv3_2"))
	net.append(Activation(relu, inplace=actInplace, name="relu3_2"))

	if layers > 11:
		net.append(Conv2D(256, 256, 3, pad=1, initscheme=initscheme, name="conv3_3"))
		net.append(Activation(relu, inplace=actInplace, name="relu3_3"))

	if layers > 16:
		net.append(Conv2D(256, 256, 3, pad=1, initscheme=initscheme, name="conv3_4"))
		net.append(Activation(relu, inplace=actInplace, name="relu3_4"))

	net.append(pool(2, 2, name="pool3"))

	net.append(Conv2D(256, 512, 3, pad=1, initscheme=initscheme, name="conv4_1"))
	net.append(Activation(relu, inplace=actInplace, name="relu4_1"))
	net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv4_2"))
	net.append(Activation(relu, inplace=actInplace, name="relu4_2"))

	if layers > 11:
		net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv4_3"))
		net.append(Activation(relu, inplace=actInplace, name="relu4_3"))

	if layers > 16:
		net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv4_4"))
		net.append(Activation(relu, inplace=actInplace, name="relu4_4"))

	net.append(pool(2, 2, name="pool4"))

	net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv5_1"))
	net.append(Activation(relu, inplace=actInplace, name="relu5_1"))
	net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv5_2"))
	net.append(Activation(relu, inplace=actInplace, name="relu5_2"))

	if layers > 11:
		net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv5_3"))
		net.append(Activation(relu, inplace=actInplace, name="relu5_3"))

	if layers > 16:
		net.append(Conv2D(512, 512, 3, pad=1, initscheme=initscheme, name="conv5_4"))
		net.append(Activation(relu, inplace=actInplace, name="relu5_4"))

	net.append(pool(2, 2, name="pool5"))

	if withLinear:
		net.append(Flatten())

		insize = int(np.prod(net.dataShapeFrom((1, 3, 224, 224))))

		net.append(Linear(insize, 4096, initscheme=initscheme, name="fc6"))
		net.append(Activation(relu, inplace=actInplace, name="relu6"))

		net.append(Linear(4096, 4096, initscheme=initscheme, name="fc7"))
		net.append(Activation(relu, inplace=actInplace, name="relu7"))

		net.append(Linear(4096, 1000, initscheme=initscheme, name="fc8"))
		net.append(SoftMax())

	if modelpath is not None:
		net.load(modelpath)

	return net
Beispiel #7
0
def buildNet(vocabulary,
             branches,
             w2v,
             sentlength,
             embsize,
             wscale,
             dim=2,
             branchMaps=100,
             name="sentinet"):
    def onVocabulary(W):
        W[0] = np.zeros((1, embsize), dtype=np.float32)

        arrayPOS = [
            "", "_S", "_A", "_V", "_UNKN", "_ADJ", "_ADV", "_INTJ", "_NOUN",
            "_PROPN", "_VERB", "_ADP", "_AUX", "_CCONJ", "_DET", "_NUM",
            "_PART", "_PRON", "_SCONJ", "_SUM", "_X"
        ]

        tmpPOS = []
        if not w2v:
            return

        for word in vocabulary:
            for pos in tmpPOS:
                if (word + pos) in w2v.vocab:
                    W[vocabulary[word]] = w2v[word + pos]
                    break

            for i, pos in enumerate(arrayPOS):
                if (word + pos) in w2v.vocab:
                    tmpPOS.append(pos)
                    W[vocabulary[word]] = w2v[word + pos]
                    del arrayPOS[i]
                    break

    net = Sequential(name)
    net.setAttr("timestamp", int(time.time()))

    net.append(
        Embedder(vocabulary,
                 sentlength,
                 embsize,
                 wscale=wscale,
                 onVocabulary=onVocabulary,
                 learnable=True,
                 name="embedder"))

    net.append(Reshape((-1, 1, sentlength, embsize)))

    branchNum = len(branches)
    net.append(Replicate(times=branchNum))

    par = Parallel()

    for branchFilterSize in branches:
        par.append(
            buildBranch(branchFilterSize, sentlength, branchMaps, embsize))

    net.append(par)
    net.append(Concat(axis=1))
    net.append(Activation(relu))
    net.append(Dropout(p=0.5))

    net.append(Linear(branchNum * branchMaps, dim))

    return net