Пример #1
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
Пример #2
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