Beispiel #1
0
def main(args):
    eddl.download_cifar10()

    num_classes = 10

    in_ = eddl.Input([3, 32, 32])

    layer = in_
    layer = eddl.MaxPool(eddl.ReLu(Normalization(
        eddl.Conv(layer, 32, [3, 3], [1, 1])
    )), [2, 2])
    layer = eddl.MaxPool(eddl.ReLu(Normalization(
        eddl.Conv(layer, 64, [3, 3], [1, 1])
    )), [2, 2])
    layer = eddl.MaxPool(eddl.ReLu(Normalization(
        eddl.Conv(layer, 128, [3, 3], [1, 1])
    )), [2, 2])
    layer = eddl.MaxPool(eddl.ReLu(Normalization(
        eddl.Conv(layer, 256, [3, 3], [1, 1])
    )), [2, 2])
    layer = eddl.GlobalMaxPool(layer)
    layer = eddl.Flatten(layer)
    layer = eddl.Activation(eddl.Dense(layer, 128), "relu")

    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    eddl.build(
        net,
        eddl.adam(0.001),
        ["soft_cross_entropy"],
        ["categorical_accuracy"],
        eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem)
    )

    eddl.summary(net)
    eddl.plot(net, "model.pdf")

    x_train = Tensor.load("cifar_trX.bin")
    y_train = Tensor.load("cifar_trY.bin")
    x_train.div_(255.0)

    x_test = Tensor.load("cifar_tsX.bin")
    y_test = Tensor.load("cifar_tsY.bin")
    x_test.div_(255.0)

    if args.small:
        x_train = x_train.select([":5000"])
        y_train = y_train.select([":5000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    for i in range(args.epochs):
        eddl.fit(net, [x_train], [y_train], args.batch_size, 1)
        eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
epochs = 50 if gpu else 1
batch_size = 50
num_classes = 10

bn = int(sys.argv[1]) == 1
initializer = eddl.GlorotUniform if bn else eddl.HeUniform

inp = eddl.Input([3, 32, 32])
l = inp
l = defblock(l, bn, 64, 2, initializer)
l = defblock(l, bn, 128, 2, initializer)
l = defblock(l, bn, 256, 4, initializer)
l = defblock(l, bn, 512, 4, initializer)
l = defblock(l, bn, 512, 4, initializer)
l = eddl.Flatten(l)
for i in range(2):
    l = initializer(eddl.Dense(l, 4096))
    if (bn):
        l = eddl.BatchNormalization(l, 0.99, 0.001, True, "")
    l = eddl.ReLu(l)

out = eddl.Softmax(initializer(eddl.Dense(l, num_classes)))

net = eddl.Model([inp], [out])
eddl.plot(net, "model.pdf")

eddl.build(net, eddl.adam(0.00001), ["soft_cross_entropy"],
           ["categorical_accuracy"],
           eddl.CS_GPU() if gpu else eddl.CS_CPU())