示例#1
0
def main(args):
    if not os.path.isfile(args.input):
        raise RuntimeError("input file '%s' not found" % args.input)

    eddl.download_mnist()

    print("importing net from", args.input)
    net = eddl.import_net_from_onnx_file(args.input)
    print("input.shape:", net.layers[0].input.shape)
    print("output size =", len(net.lout))

    eddl.build(
        net,
        eddl.rmsprop(0.01),
        ["soft_cross_entropy"],
        ["categorical_accuracy"],
        eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem),
        False  # do not initialize weights to random values
    )

    net.resize(args.batch_size)  # resize manually since we don't use "fit"
    eddl.summary(net)

    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")

    x_test.div_(255.0)

    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
示例#2
0
def main(args):
    eddl.download_mnist()

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.Activation(eddl.Dense(layer, 256), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 128), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 64), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 128), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 256), "relu")
    out = eddl.Dense(layer, 784)

    net = eddl.Model([in_], [out])
    mse_loss = MSELoss()
    mse_metric = MSEMetric()
    net.build(
        eddl.sgd(0.001, 0.9), [mse_loss], [mse_metric],
        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("mnist_trX.bin")
    x_train.div_(255.0)
    eddl.fit(net, [x_train], [x_train], args.batch_size, args.epochs)
    print("All done")
示例#3
0
def main(args):
    eddl.download_mnist()

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.Activation(eddl.Dense(layer, 256), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 128), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 64), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 128), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 256), "relu")
    out = eddl.Dense(layer, 784)

    net = eddl.Model([in_], [out])
    eddl.build(
        net, eddl.sgd(0.001, 0.9), ["mean_squared_error"],
        ["mean_squared_error"],
        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("mnist_trX.bin")
    if args.small:
        x_train = x_train.select([":6000"])
    x_train.div_(255.0)
    eddl.fit(net, [x_train], [x_train], args.batch_size, args.epochs)
    tout = eddl.predict(net, [x_train])
    tout[0].info()
    print("All done")
示例#4
0
def main(args):
    if not os.path.isfile(args.input):
        raise RuntimeError("input file '%s' not found" % args.input)

    eddl.download_mnist()

    net = eddl.import_net_from_onnx_file(args.input)
    eddl.build(
        net,
        eddl.rmsprop(0.01),
        ["soft_cross_entropy"],
        ["categorical_accuracy"],
        eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem),
        False  # do not initialize weights to random values
    )

    net.resize(args.batch_size)  # resize manually since we don't use "fit"
    eddl.summary(net)

    x_test = Tensor.load("mnist_tsX.bin")
    x_test.div_(255.0)

    sys.stderr.write("forward...\n")
    eddl.forward(net, [x_test])
    sys.stderr.write("forward done\n")

    sys.stderr.write("lout: %r\n" % (net.lout, ))
    out = eddl.getOut(net)
    sys.stderr.write("getOut done\n")
    sys.stderr.write("out: %r\n" % (out, ))
    print("All done")
示例#5
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.Reshape(layer, [1, 28, 28])
    layer = eddl.RandomCropScale(layer, [0.9, 1.0])
    layer = eddl.Reshape(layer, [-1])
    layer = eddl.ReLu(
        eddl.GaussianNoise(
            eddl.BatchNormalization(eddl.Dense(layer, 1024), True), 0.3))
    layer = eddl.ReLu(
        eddl.GaussianNoise(
            eddl.BatchNormalization(eddl.Dense(layer, 1024), True), 0.3))
    layer = eddl.ReLu(
        eddl.GaussianNoise(
            eddl.BatchNormalization(eddl.Dense(layer, 1024), True), 0.3))
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    eddl.build(
        net, eddl.sgd(0.01, 0.9), ["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("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    x_train.div_(255.0)
    x_test.div_(255.0)

    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)

    # LR annealing
    if args.epochs < 4:
        return
    eddl.setlr(net, [0.005, 0.9])
    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs // 2)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    eddl.setlr(net, [0.001, 0.9])
    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs // 2)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    eddl.setlr(net, [0.0001, 0.9])
    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs // 4)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
示例#6
0
def main(args):
    eddl.download_mnist()

    in_ = eddl.Input([784])
    target = eddl.Reshape(in_, [1, 28, 28])
    layer = in_
    layer = eddl.Reshape(layer, [1, 28, 28])
    layer = eddl.ReLu(eddl.Conv(layer, 8, [3, 3]))
    layer = eddl.ReLu(eddl.Conv(layer, 16, [3, 3]))
    layer = eddl.ReLu(eddl.Conv(layer, 8, [3, 3]))
    out = eddl.Sigmoid(eddl.Conv(layer, 1, [3, 3]))
    net = eddl.Model([in_], [])

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

    x_train = Tensor.load("mnist_trX.bin")
    if args.small:
        x_train = x_train.select([":6000"])
    x_train.div_(255.0)

    mse = eddl.newloss(mse_loss, [out, target], "mse_loss")
    dicei = eddl.newloss(dice_loss_img, [out, target], "dice_loss_img")
    dicep = eddl.newloss(dice_loss_pixel, [out, target], "dice_loss_pixel")

    batch = Tensor([args.batch_size, 784])
    num_batches = x_train.shape[0] // args.batch_size
    for i in range(args.epochs):
        print("Epoch %d/%d (%d batches)" % (i + 1, args.epochs, num_batches))
        diceploss = 0.0
        diceiloss = 0.0
        mseloss = 0
        for j in range(num_batches):
            print("Batch %d " % j, end="", flush=True)
            eddl.next_batch([x_train], [batch])
            eddl.zeroGrads(net)
            eddl.forward(net, [batch])
            diceploss += eddl.compute_loss(dicep) / args.batch_size
            print("diceploss = %.6f " % (diceploss / (j + 1)), end="")
            diceiloss += eddl.compute_loss(dicei) / args.batch_size
            print("diceiloss = %.6f " % (diceiloss / (j + 1)), end="")
            mseloss += eddl.compute_loss(mse) / args.batch_size
            print("mseloss = %.6f\r" % (mseloss / (j + 1)), end="")
            eddl.optimize(dicep)
            eddl.update(net)
        print()
    print("All done")
示例#7
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])
    layer = in_
    layer = eddl.Reshape(layer, [1, 784])  # image as a 1D signal with depth 1
    layer = eddl.MaxPool1D(eddl.ReLu(eddl.Conv1D(layer, 16, [3], [1])), [4],
                           [4])
    layer = eddl.MaxPool1D(
        eddl.ReLu(eddl.Conv1D(layer, 32, [3], [1])),
        [4],
        [4],
    )
    layer = eddl.MaxPool1D(
        eddl.ReLu(eddl.Conv1D(layer, 64, [3], [1])),
        [4],
        [4],
    )
    layer = eddl.MaxPool1D(
        eddl.ReLu(eddl.Conv1D(layer, 64, [3], [1])),
        [4],
        [4],
    )
    layer = eddl.Reshape(layer, [-1])
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

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

    eddl.summary(net)

    x_train = Tensor.load("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])
    x_train.div_(255.0)
    x_test.div_(255.0)

    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
示例#8
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])
    layer = in_
    layer = eddl.Activation(eddl.Dense(layer, 1024), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 1024), "relu")
    layer = eddl.Activation(eddl.Dense(layer, 1024), "relu")
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    acc = CategoricalAccuracy()
    net.build(
        eddl.sgd(0.01, 0.9),
        [eddl.getLoss("soft_cross_entropy")],
        [acc],
        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("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    # y_test = Tensor.load("mnist_tsY.bin")

    x_train.div_(255.0)
    x_test.div_(255.0)

    num_samples = x_train.shape[0]
    num_batches = num_samples // args.batch_size
    test_samples = x_test.shape[0]
    test_batches = test_samples // args.batch_size

    eddl.set_mode(net, TRMODE)

    for i in range(args.epochs):
        for j in range(num_batches):
            print("Epoch %d/%d (batch %d/%d)" %
                  (i + 1, args.epochs, j + 1, num_batches))
            indices = np.random.randint(0, num_samples, args.batch_size)
            eddl.train_batch(net, [x_train], [y_train], indices)
        for j in range(test_batches):
            print("Epoch %d/%d (batch %d/%d)" %
                  (i + 1, args.epochs, j + 1, test_batches))
            indices = np.random.randint(0, num_samples, args.batch_size)
            eddl.eval_batch(net, [x_train], [y_train], indices)
    print("All done")
示例#9
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.Reshape(layer, [-1])
    layer = eddl.ReLu(eddl.Dense(layer, 1024))
    layer = eddl.BatchNormalization(layer, True)
    layer = eddl.ReLu(eddl.Dense(layer, 1024))
    layer = eddl.BatchNormalization(layer, True)
    layer = eddl.ReLu(eddl.Dense(layer, 1024))
    layer = eddl.BatchNormalization(layer, True)
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    eddl.build(
        net,
        eddl.rmsprop(0.01),
        ["soft_cross_entropy"],
        ["categorical_accuracy"],
        eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem),
        True  # initialize weights to random values
    )

    eddl.summary(net)

    x_train = Tensor.load("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    x_train.div_(255.0)
    x_test.div_(255.0)

    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)

    eddl.save_net_to_onnx_file(net, args.output)
    print("saved net to", args.output)
    print("All done")
示例#10
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([28])

    layer = in_
    layer = eddl.LeakyReLu(eddl.Dense(layer, 32))
    layer = eddl.L2(eddl.LSTM(layer, 128), 0.001)
    ls = layer
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    eddl.build(
        net, eddl.rmsprop(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("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    x_train.reshape_([x_train.shape[0], 28, 28])
    x_test.reshape_([x_test.shape[0], 28, 28])
    y_train.reshape_([y_train.shape[0], 1, 10])
    y_test.reshape_([y_test.shape[0], 1, 10])

    x_train.div_(255.0)
    x_test.div_(255.0)

    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)
        ls_in = eddl.getInput(ls)
        ls_in.info()
        ls_out = eddl.getOutput(ls)
        ls_out.info()
    print("All done")
示例#11
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.BatchNormalization(
        eddl.Activation(eddl.L2(eddl.Dense(layer, 1024), 0.0001), "relu"), True
    )
    layer = eddl.BatchNormalization(
        eddl.Activation(eddl.L2(eddl.Dense(layer, 1024), 0.0001), "relu"), True
    )
    layer = eddl.BatchNormalization(
        eddl.Activation(eddl.L2(eddl.Dense(layer, 1024), 0.0001), "relu"), True
    )
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    acc = CategoricalAccuracy()
    net.build(
        eddl.sgd(0.01, 0.9),
        [eddl.getLoss("soft_cross_entropy")],
        [acc],
        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("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")

    x_train.div_(255.0)
    x_test.div_(255.0)

    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs)

    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
示例#12
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.ReLu(eddl.L2(eddl.Dense(layer, 1024), 0.0001))
    layer = eddl.ReLu(eddl.L1(eddl.Dense(layer, 1024), 0.0001))
    layer = eddl.ReLu(eddl.L1L2(eddl.Dense(layer, 1024), 0.00001, 0.0001))
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    eddl.build(
        net,
        eddl.sgd(0.01, 0.9),
        ["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("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    x_train.div_(255.0)
    x_test.div_(255.0)

    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs)
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
示例#13
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

    in_ = eddl.Input([784])

    layer = in_
    layer = eddl.Reshape(layer, [-1])
    layer = eddl.ReLu(eddl.Dense(layer, 1024))
    layer = eddl.ReLu(eddl.Dense(layer, 1024))
    layer = eddl.ReLu(eddl.Dense(layer, 1024))
    out = eddl.Softmax(eddl.Dense(layer, num_classes))
    net = eddl.Model([in_], [out])

    eddl.build(
        net,
        eddl.rmsprop(0.01),
        ["soft_cross_entropy"],
        ["categorical_accuracy"],
        eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem),
        True  # initialize weights to random values
    )

    serialized_net = eddl.serialize_net_to_onnx_string(net, False)
    eddl.summary(net)

    x_train = Tensor.load("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    x_train.div_(255.0)
    x_test.div_(255.0)

    eddl.fit(net, [x_train], [y_train], args.batch_size, args.epochs)
    print("evaluating before import")
    eddl.evaluate(net, [x_test], [y_test], bs=args.batch_size)

    imported_net = eddl.import_net_from_onnx_string(serialized_net)

    eddl.build(
        imported_net,
        eddl.rmsprop(0.01),
        ["soft_cross_entropy"],
        ["categorical_accuracy"],
        eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem),
        False  # do not initialize weights to random values
    )

    eddl.summary(imported_net)
    print("net layers:", len(net.layers))
    print("imported_net layers:", len(imported_net.layers))

    print("evaluating imported net")
    eddl.evaluate(imported_net, [x_test], [y_test], bs=args.batch_size)
    print("All done")
示例#14
0
def main(args):
    eddl.download_mnist()

    num_classes = 10

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

    eddl.build(
        net, eddl.sgd(0.001, 0.9), ["softmax_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")
    eddl.setlogfile(net, "mnist")

    x_train = Tensor.load("mnist_trX.bin")
    y_train = Tensor.load("mnist_trY.bin")
    x_test = Tensor.load("mnist_tsX.bin")
    y_test = Tensor.load("mnist_tsY.bin")
    if args.small:
        x_train = x_train.select([":6000"])
        y_train = y_train.select([":6000"])
        x_test = x_test.select([":1000"])
        y_test = y_test.select([":1000"])

    x_train.div_(255.0)
    x_test.div_(255.0)

    s = x_train.shape
    num_batches = s[0] // args.batch_size
    for i in range(args.epochs):
        eddl.reset_loss(net)
        print("Epoch %d/%d (%d batches)" % (i + 1, args.epochs, num_batches))
        for j in range(num_batches):
            indices = np.random.randint(0, s[0], args.batch_size)
            eddl.train_batch(net, [x_train], [y_train], indices)

    losses1 = eddl.get_losses(net)
    metrics1 = eddl.get_metrics(net)
    for l, m in zip(losses1, metrics1):
        print("Loss: %.6f\tMetric: %.6f" % (l, m))

    s = x_test.shape
    num_batches = s[0] // args.batch_size
    for j in range(num_batches):
        indices = np.arange(j * args.batch_size,
                            j * args.batch_size + args.batch_size)
        eddl.eval_batch(net, [x_test], [y_test], indices)

    losses2 = eddl.get_losses(net)
    metrics2 = eddl.get_metrics(net)
    for l, m in zip(losses2, metrics2):
        print("Loss: %.6f\tMetric: %.6f" % (l, m))

    last_batch_size = s[0] % args.batch_size
    if last_batch_size:
        indices = np.arange(j * args.batch_size,
                            j * args.batch_size + args.batch_size)
        eddl.eval_batch(net, [x_test], [y_test], indices)

    losses3 = eddl.get_losses(net)
    metrics3 = eddl.get_metrics(net)
    for l, m in zip(losses3, metrics3):
        print("Loss: %.6f\tMetric: %.6f" % (l, m))

    print("All done")