def main(args): in_channels = 3 in_height = 224 in_width = 224 print("Importing ONNX model") net = eddl.import_net_from_onnx_file(args.model_fn, [in_channels, in_height, in_width]) # Add a softmax layer to get probabilities directly from the model input_ = net.lin[0] # getLayer(net,"input_layer_name") output = net.lout[0] # getLayer(net,"output_layer_name") new_output = eddl.Softmax(output) net = eddl.Model([input_], [new_output]) eddl.build( net, eddl.adam(0.001), # not used for prediction ["softmax_cross_entropy"], # not used for prediction ["categorical_accuracy"], # not used for prediction eddl.CS_GPU() if args.gpu else eddl.CS_CPU(), False # Disable model initialization, we want to use the ONNX weights ) eddl.summary(net) image = Tensor.load(args.img_fn) image_preprocessed = preprocess_input_resnet34(image, [in_height, in_width]) outputs = eddl.predict(net, [image_preprocessed]) print("Reading class names...") with open(args.classes_fn, "rt") as f: class_names = [_.strip() for _ in f] print("Top 5 predictions:") print(eddl.get_topk_predictions(outputs[0], class_names, 5))
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")
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")
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")
def main(args): eddl.download_cifar10() num_classes = 10 in_ = eddl.Input([3, 32, 32]) layer = in_ layer = eddl.RandomCropScale(layer, [0.8, 1.0]) layer = eddl.RandomHorizontalFlip(layer) layer = eddl.ReLu(BG(eddl.Conv(layer, 64, [3, 3], [1, 1], "same", False))) layer = eddl.Pad(layer, [0, 1, 1, 0]) for i in range(3): layer = ResBlock(layer, 64, 0, i == 0) for i in range(4): layer = ResBlock(layer, 128, i == 0) for i in range(6): layer = ResBlock(layer, 256, i == 0) for i in range(3): layer = ResBlock(layer, 512, i == 0) layer = eddl.MaxPool(layer, [4, 4]) layer = eddl.Reshape(layer, [-1]) out = eddl.Softmax(eddl.Dense(layer, num_classes)) net = eddl.Model([in_], [out]) eddl.build( net, eddl.sgd(0.001, 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", "TB") 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: # this is slow, make it really small x_train = x_train.select([":500"]) y_train = y_train.select([":500"]) x_test = x_test.select([":100"]) y_test = y_test.select([":100"]) lr = 0.01 for j in range(3): lr /= 10.0 eddl.setlr(net, [lr, 0.9]) 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")
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")
def main(args): eddl.download_cifar10() num_classes = 10 in_ = eddl.Input([3, 32, 32]) layer = in_ layer = eddl.RandomCropScale(layer, [0.8, 1.0]) layer = eddl.RandomFlip(layer, 1) layer = eddl.ReLu(BG(eddl.Conv(layer, 64, [3, 3], [1, 1]))) layer = eddl.Pad(layer, [0, 1, 1, 0]) layer = ResBlock(layer, 64, 2, True) layer = ResBlock(layer, 64, 2, False) layer = ResBlock(layer, 128, 2, True) layer = ResBlock(layer, 128, 2, False) layer = ResBlock(layer, 256, 2, True) layer = ResBlock(layer, 256, 2, False) layer = ResBlock(layer, 256, 2, True) layer = ResBlock(layer, 256, 2, False) layer = eddl.Reshape(layer, [-1]) layer = eddl.ReLu(BG(eddl.Dense(layer, 512))) 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", "TB") 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")
def main(args): eddl.download_cifar10() num_classes = 10 in_ = eddl.Input([3, 32, 32]) layer = in_ layer = eddl.RandomCropScale(layer, [0.8, 1.0]) layer = eddl.RandomFlip(layer, 1) layer = eddl.RandomCutout(layer, [0.1, 0.3], [0.1, 0.3]) layer = eddl.MaxPool(Block3_2(layer, 64)) layer = eddl.MaxPool(Block3_2(layer, 128)) layer = eddl.MaxPool(Block1(Block3_2(layer, 256), 256)) layer = eddl.MaxPool(Block1(Block3_2(layer, 512), 512)) layer = eddl.MaxPool(Block1(Block3_2(layer, 512), 512)) layer = eddl.Reshape(layer, [-1]) layer = eddl.Activation(eddl.Dense(layer, 512), "relu") out = eddl.Softmax(eddl.Dense(layer, num_classes)) net = eddl.Model([in_], [out]) eddl.build( net, eddl.sgd(0.001, 0.9), ["soft_cross_entropy"], ["categorical_accuracy"], eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem) ) eddl.setlogfile(net, "vgg16") 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")
def tissue_detector_DNN(): in_ = eddl.Input([3]) layer = in_ layer = eddl.ReLu(eddl.Dense(layer, 50)) layer = eddl.ReLu(eddl.Dense(layer, 50)) layer = eddl.ReLu(eddl.Dense(layer, 50)) out = eddl.Softmax(eddl.Dense(layer, 2)) net = eddl.Model([in_], [out]) return net
def test_build_net(opt_cls): num_classes = 10 in_ = eddl.Input([784]) layer = in_ layer = eddl.LeakyReLu(eddl.Dense(layer, 1024)) layer = eddl.LeakyReLu(eddl.Dense(layer, 1024)) layer = eddl.LeakyReLu(eddl.Dense(layer, 1024)) out = eddl.Softmax(eddl.Dense(layer, num_classes), -1) net = eddl.Model([in_], [out]) eddl.build(net, opt_cls(0.01), ["soft_cross_entropy"], ["categorical_accuracy"], eddl.CS_CPU(mem="low_mem"))
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")
def get_net(net_name='vgg16', in_size=[256, 256], num_classes=2, lr=1e-5, augs=False, gpus=[1], lsb=1, init=eddl.HeNormal, dropout=None, l2_reg=None): ## Network definition in_ = eddl.Input([3, in_size[0], in_size[1]]) if net_name == 'vgg16': out = models.VGG16_promort(in_, num_classes, init=init, l2_reg=l2_reg, dropout=dropout) else: print('model %s not available' % net_name) sys.exit(-1) net = eddl.Model([in_], [out]) eddl.build( net, eddl.rmsprop(lr), ["soft_cross_entropy"], ["categorical_accuracy"], #eddl.CS_GPU([1,1], mem="low_mem") if gpu else eddl.CS_CPU() eddl.CS_GPU(gpus, mem="low_mem", lsb=lsb) if gpus else eddl.CS_CPU() #eddl.CS_GPU(gpus, mem="low_mem") if gpus else eddl.CS_CPU() ) eddl.summary(net) eddl.setlogfile(net, "promort_VGG16_classification") if augs: ## Set augmentations training_augs = ecvl.SequentialAugmentationContainer( [ecvl.AugMirror(.5), ecvl.AugFlip(.5), ecvl.AugRotate([-10, 10])]) validation_augs = ecvl.SequentialAugmentationContainer([]) dataset_augs = [training_augs, validation_augs, None] else: dataset_augs = [None, None, None] return net, dataset_augs
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")
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")
def get_net(in_size=[256, 256], num_classes=2, lr=1e-5, gpu=True): ## Network definition in_ = eddl.Input([3, in_size[0], in_size[1]]) out = models.VGG16_promort(in_, num_classes) net = eddl.Model([in_], [out]) eddl.build( net, eddl.rmsprop(lr), ["soft_cross_entropy"], ["categorical_accuracy"], #eddl.CS_GPU([1,1], mem="low_mem") if gpu else eddl.CS_CPU() eddl.CS_GPU([1], mem="low_mem") if gpu else eddl.CS_CPU()) return net
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")
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")
def main(args): eddl.download_imdb_2000() epochs = 2 if args.small else 10 length = 250 embdim = 33 vocsize = 2000 in_ = eddl.Input([1]) # 1 word layer = in_ layer = eddl.RandomUniform(eddl.Embedding(layer, vocsize, 1, embdim), -0.05, 0.05) layer = eddl.GRU(layer, 37) layer = eddl.ReLu(eddl.Dense(layer, 256)) out = eddl.Sigmoid(eddl.Dense(layer, 1)) net = eddl.Model([in_], [out]) eddl.build( net, eddl.adam(0.001), ["cross_entropy"], ["binary_accuracy"], eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem)) eddl.summary(net) x_train = Tensor.load("imdb_2000_trX.bin") y_train = Tensor.load("imdb_2000_trY.bin") x_test = Tensor.load("imdb_2000_tsX.bin") y_test = Tensor.load("imdb_2000_tsY.bin") # batch x timesteps x input_dim x_train.reshape_([x_train.shape[0], length, 1]) x_test.reshape_([x_test.shape[0], length, 1]) y_train.reshape_([y_train.shape[0], 1, 1]) y_test.reshape_([y_test.shape[0], 1, 1]) if args.small: x_train = x_train.select([":64", ":", ":"]) y_train = y_train.select([":64", ":", ":"]) x_test = x_test.select([":64", ":", ":"]) y_test = y_test.select([":64", ":", ":"]) for i in range(epochs): eddl.fit(net, [x_train], [y_train], args.batch_size, 1) eddl.evaluate(net, [x_test], [y_test]) print("All done")
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")
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")
def main(args): size = 256 // 2 # Conv3D expects (B, C, dim1, dim2, dim3) in_ = eddl.Input([3, 10, size, size]) layer = in_ layer = eddl.MaxPool3D(eddl.ReLu(eddl.Conv3D( layer, 4, [1, 3, 3], [1, 1, 1], "same" )), [1, 2, 2], [1, 2, 2], "same") layer = eddl.MaxPool3D(eddl.ReLu(eddl.Conv3D( layer, 8, [1, 3, 3], [1, 1, 1], "same" )), [1, 2, 2], [1, 2, 2], "same") layer = eddl.MaxPool3D(eddl.ReLu(eddl.Conv3D( layer, 16, [1, 3, 3], [1, 1, 1], "same" )), [1, 2, 2], [1, 2, 2], "same") layer = eddl.GlobalMaxPool3D(layer) layer = eddl.Reshape(layer, [-1]) layer = eddl.LSTM(layer, 128) layer = eddl.Dense(layer, 100) layer = eddl.ReLu(layer) layer = eddl.Dense(layer, 2) out = eddl.ReLu(layer) net = eddl.Model([in_], [out]) eddl.build( net, eddl.adam(), ["mse"], ["mse"], eddl.CS_GPU(mem=args.mem) if args.gpu else eddl.CS_CPU(mem=args.mem) ) eddl.summary(net) seqImages = Tensor.randu([32, 10, 3, 10, size, size]) seqLabels = Tensor.randu([32, 7, 2]) eddl.fit(net, [seqImages], [seqLabels], 4, 2 if args.small else 10)
def main(args): num_classes = 1 size = [512, 512] # size of images thresh = 0.5 best_dice = -1 if args.out_dir: os.makedirs(args.out_dir, exist_ok=True) in_ = eddl.Input([1, size[0], size[1]]) out = SegNetBN(in_, num_classes) out_sigm = eddl.Sigmoid(out) net = eddl.Model([in_], [out_sigm]) eddl.build(net, eddl.adam(0.0001), ["cross_entropy"], ["mean_squared_error"], eddl.CS_GPU([1], mem='low_mem') if args.gpu else eddl.CS_CPU()) eddl.summary(net) eddl.setlogfile(net, "pneumothorax_segmentation_training") if args.ckpts and os.path.exists(args.ckpts): print("Loading checkpoints '{}'".format(args.ckpts)) eddl.load(net, args.ckpts, 'bin') training_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ecvl.AugMirror(0.5), ecvl.AugRotate([-10, 10]), ecvl.AugBrightness([0, 30]), ecvl.AugGammaContrast([0, 3]), ]) validation_augs = ecvl.SequentialAugmentationContainer( [ecvl.AugResizeDim(size)]) dataset_augs = ecvl.DatasetAugmentations( [training_augs, validation_augs, None]) print("Reading dataset") d = ecvl.DLDataset(args.in_ds, args.batch_size, dataset_augs, ecvl.ColorType.GRAY) # Prepare tensors which store batch x = Tensor([args.batch_size, d.n_channels_, size[0], size[1]]) y = Tensor([args.batch_size, d.n_channels_gt_, size[0], size[1]]) # Retrieve indices of images with a black ground truth # which are not include in a split train_split = d.GetSplit(ecvl.SplitType.training) val_split = d.GetSplit(ecvl.SplitType.validation) test_split = d.GetSplit(ecvl.SplitType.test) all_split = set(train_split + val_split + test_split) images_list = set(range(len(d.samples_))) # Obtain images with black ground truth black_images = images_list - all_split # Add a 25% of training samples with black ground truth. num_samples_train = math.floor(len(train_split) * 1.25) num_batches_train = num_samples_train // args.batch_size # Add a 25% of validation samples with black ground truth. num_samples_validation = math.floor(len(val_split) * 1.25) num_batches_validation = num_samples_validation // args.batch_size black_images = list(black_images) black_training = black_images[0:-(num_samples_validation - len(val_split))] black_validation = black_images[-(num_samples_validation - len(val_split)):] indices = list(range(args.batch_size)) evaluator = utils.Evaluator() print("Starting training") for e in range(args.epochs): print("Epoch {:d}/{:d} - Training".format(e + 1, args.epochs), flush=True) d.SetSplit(ecvl.SplitType.training) eddl.reset_loss(net) s = d.GetSplit() random.shuffle(s) d.split_.training_ = s random.shuffle(black_training) d.ResetAllBatches() # Indices to track mask and black vector in PneumothoraxLoadBatch m_i = 0 b_i = 0 for i, b in enumerate(range(num_batches_train)): d, images, labels, _, m_i, b_i = PneumothoraxLoadBatch( d, black_training, m_i, b_i) x, y = fill_tensors(images, labels, x, y) x.div_(255.0) y.div_(255.0) eddl.train_batch(net, [x], [y], indices) if i % args.log_interval == 0: print("Epoch {:d}/{:d} (batch {:d}/{:d}) - ".format( e + 1, args.epochs, b + 1, num_batches_train), end="", flush=True) eddl.print_loss(net, b) print() d.SetSplit(ecvl.SplitType.validation) evaluator.ResetEval() print("Epoch %d/%d - Evaluation" % (e + 1, args.epochs), flush=True) m_i = 0 b_i = 0 for b in range(num_batches_validation): n = 0 print("Epoch {:d}/{:d} (batch {:d}/{:d}) ".format( e + 1, args.epochs, b + 1, num_batches_validation), end="", flush=True) d, images, labels, names, m_i, b_i = PneumothoraxLoadBatch( d, black_validation, m_i, b_i) x, y = fill_tensors(images, labels, x, y) x.div_(255.0) y.div_(255.0) eddl.forward(net, [x]) output = eddl.getOutput(out_sigm) # Compute Dice metric and optionally save the output images for k in range(args.batch_size): pred = output.select([str(k)]) gt = y.select([str(k)]) pred_np = np.array(pred, copy=False) gt_np = np.array(gt, copy=False) # DiceCoefficient modifies image as a side effect dice = evaluator.DiceCoefficient(pred_np, gt_np, thresh=thresh) print("- Dice: {:.6f} ".format(dice), end="", flush=True) if args.out_dir: # Save original image fused together with prediction and # ground truth pred_np *= 255 pred_ecvl = ecvl.TensorToImage(pred) pred_ecvl.colortype_ = ecvl.ColorType.GRAY pred_ecvl.channels_ = "xyc" ecvl.ResizeDim(pred_ecvl, pred_ecvl, (1024, 1024), ecvl.InterpolationType.nearest) filename_gt = names[n + 1] gt_ecvl = ecvl.ImRead(filename_gt, ecvl.ImReadMode.GRAYSCALE) filename = names[n] # Image as BGR img_ecvl = ecvl.ImRead(filename) ecvl.Stack([img_ecvl, img_ecvl, img_ecvl], img_ecvl) img_ecvl.channels_ = "xyc" img_ecvl.colortype_ = ecvl.ColorType.BGR image_np = np.array(img_ecvl, copy=False) pred_np = np.array(pred_ecvl, copy=False) gt_np = np.array(gt_ecvl, copy=False) pred_np = pred_np.squeeze() gt_np = gt_np.squeeze() # Prediction summed in R channel image_np[:, :, -1] = np.where(pred_np == 255, pred_np, image_np[:, :, -1]) # Ground truth summed in G channel image_np[:, :, 1] = np.where(gt_np == 255, gt_np, image_np[:, :, 1]) n += 2 head, tail = os.path.splitext(os.path.basename(filename)) bname = "{}.png".format(head) filepath = os.path.join(args.out_dir, bname) ecvl.ImWrite(filepath, img_ecvl) print() mean_dice = evaluator.MeanMetric() if mean_dice > best_dice: print("Saving weights") eddl.save( net, "pneumothorax_segnetBN_adam_lr_0.0001_" "loss_ce_size_512_{}.bin".format(e + 1), "bin") best_dice = mean_dice print("Mean Dice Coefficient: {:.6g}".format(mean_dice))
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")
def main(args): freeze_epochs = 2 unfreeze_epochs = 5 num_classes = 10 # 10 labels in cifar10 eddl.download_cifar10() eddl.download_model("resnet18.onnx", "re7jodd12srksd7") net = eddl.import_net_from_onnx_file("resnet18.onnx", [3, 32, 32], DEV_CPU) names = [_.name for _ in net.layers] # Remove dense output layer eddl.removeLayer(net, "resnetv15_dense0_fwd") # Get last layer to connect the new dense layer = eddl.getLayer(net, "flatten_170") out = eddl.Softmax(eddl.Dense(layer, num_classes, True, "new_dense")) # Get input layer in_ = eddl.getLayer(net, "data") # Create a new model net = eddl.Model([in_], [out]) eddl.build( net, eddl.adam(0.0001), ["softmax_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(net) # Force initialization of new layers eddl.initializeLayer(net, "new_dense") x_train = Tensor.load("cifar_trX.bin") y_train = Tensor.load("cifar_trY.bin") x_test = Tensor.load("cifar_tsX.bin") y_test = Tensor.load("cifar_tsY.bin") if args.small: sel = [f":{2 * args.batch_size}"] x_train = x_train.select(sel) y_train = y_train.select(sel) x_test = x_test.select(sel) y_test = y_test.select(sel) x_train.div_(255.0) x_test.div_(255.0) # Freeze pretrained weights for n in names: eddl.setTrainable(net, n, False) # Train new layers eddl.fit(net, [x_train], [y_train], args.batch_size, freeze_epochs) # Unfreeze weights for n in names: eddl.setTrainable(net, n, True) # Train all layers eddl.fit(net, [x_train], [y_train], args.batch_size, unfreeze_epochs) # Evaluate eddl.evaluate(net, [x_test], [y_test], args.batch_size) print("All done")
def main(args): num_classes = 1 size = [192, 192] # size of images thresh = 0.5 if args.out_dir: os.makedirs(args.out_dir, exist_ok=True) in_ = eddl.Input([3, size[0], size[1]]) out = SegNet(in_, num_classes) out_sigm = eddl.Sigmoid(out) net = eddl.Model([in_], [out_sigm]) eddl.build(net, eddl.adam(0.0001), ["cross_entropy"], ["mean_squared_error"], eddl.CS_GPU([1]) if args.gpu else eddl.CS_CPU()) eddl.summary(net) eddl.setlogfile(net, "skin_lesion_segmentation_inference") if not os.path.exists(args.ckpts): raise RuntimeError('Checkpoint "{}" not found'.format(args.ckpts)) eddl.load(net, args.ckpts, "bin") training_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ]) test_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ]) dataset_augs = ecvl.DatasetAugmentations([training_augs, None, test_augs]) print("Reading dataset") d = ecvl.DLDataset(args.in_ds, args.batch_size, dataset_augs) x = Tensor([args.batch_size, d.n_channels_, size[0], size[1]]) y = Tensor([args.batch_size, d.n_channels_gt_, size[0], size[1]]) print("Testing") d.SetSplit(ecvl.SplitType.test) num_samples_test = len(d.GetSplit()) num_batches_test = num_samples_test // args.batch_size evaluator = utils.Evaluator() evaluator.ResetEval() for b in range(num_batches_test): n = 0 print("Batch {:d}/{:d} ".format(b + 1, num_batches_test), end="", flush=True) d.LoadBatch(x, y) x.div_(255.0) y.div_(255.0) eddl.forward(net, [x]) output = eddl.getOutput(out_sigm) for k in range(args.batch_size): img = output.select([str(k)]) gt = y.select([str(k)]) img_np, gt_np = np.array(img, copy=False), np.array(gt, copy=False) iou = evaluator.BinaryIoU(img_np, gt_np, thresh=thresh) print("- IoU: %.6g " % iou, end="", flush=True) if args.out_dir: # C++ BinaryIoU modifies image as a side effect img_np[img_np >= thresh] = 1 img_np[img_np < thresh] = 0 img_t = ecvl.TensorToView(img) img_t.colortype_ = ecvl.ColorType.GRAY img_t.channels_ = "xyc" img.mult_(255.) # orig_img orig_img = x.select([str(k)]) orig_img.mult_(255.) orig_img_t = ecvl.TensorToImage(orig_img) orig_img_t.colortype_ = ecvl.ColorType.BGR orig_img_t.channels_ = "xyc" tmp, labels = ecvl.Image.empty(), ecvl.Image.empty() ecvl.CopyImage(img_t, tmp, ecvl.DataType.uint8) ecvl.ConnectedComponentsLabeling(tmp, labels) ecvl.CopyImage(labels, tmp, ecvl.DataType.uint8) contours = ecvl.FindContours(tmp) ecvl.CopyImage(orig_img_t, tmp, ecvl.DataType.uint8) tmp_np = np.array(tmp, copy=False) for cseq in contours: for c in cseq: tmp_np[c[0], c[1], 0] = 0 tmp_np[c[0], c[1], 1] = 0 tmp_np[c[0], c[1], 2] = 255 filename = d.samples_[d.GetSplit()[n]].location_[0] head, tail = os.path.splitext(os.path.basename(filename)) bname = "%s.png" % head output_fn = os.path.join(args.out_dir, bname) ecvl.ImWrite(output_fn, tmp) gt_t = ecvl.TensorToView(gt) gt_t.colortype_ = ecvl.ColorType.GRAY gt_t.channels_ = "xyc" gt.mult_(255.) gt_filename = d.samples_[d.GetSplit()[n]].label_path_ gt_fn = os.path.join(args.out_dir, os.path.basename(gt_filename)) ecvl.ImWrite(gt_fn, gt_t) n += 1 print() print("MIoU: %.6g" % evaluator.MeanMetric())
def classificate(args): args = dotdict(args) ckpts_dir = opjoin(settings.TRAINING_DIR, 'ckpts') outputfile = None inference = None train = True if args.mode == 'training' else False batch_size = args.batch_size if args.mode == 'training' else args.test_batch_size weight_id = args.weight_id weight = dj_models.ModelWeights.objects.get(id=weight_id) if train: pretrained = None if weight.pretrained_on: pretrained = weight.pretrained_on.location else: inference_id = args.inference_id inference = dj_models.Inference.objects.get(id=inference_id) pretrained = weight.location save_stdout = sys.stdout size = [args.input_h, args.input_w] # Height, width try: model = bindings.models_binding[args.model_id] except KeyError: raise Exception( f'Model with id: {args.model_id} not found in bindings.py') try: dataset_path = str( dj_models.Dataset.objects.get(id=args.dataset_id).path) except KeyError: raise Exception( f'Dataset with id: {args.dataset_id} not found in bindings.py') dataset = bindings.dataset_binding.get(args.dataset_id) if dataset is None and not train: # Binding does not exist. it's a single image dataset # Use as dataset "stub" the dataset on which model has been trained dataset = bindings.dataset_binding.get(weight.dataset_id.id) elif dataset is None and train: raise Exception( f'Dataset with id: {args.dataset_id} not found in bindings.py') basic_augs = ecvl.SequentialAugmentationContainer( [ecvl.AugResizeDim(size)]) train_augs = basic_augs val_augs = basic_augs test_augs = basic_augs if args.train_augs: train_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ecvl.AugmentationFactory.create(args.train_augs) ]) if args.val_augs: val_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ecvl.AugmentationFactory.create(args.val_augs) ]) if args.test_augs: test_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ecvl.AugmentationFactory.create(args.test_augs) ]) logging.info('Reading dataset') print('Reading dataset', flush=True) dataset = dataset( dataset_path, batch_size, ecvl.DatasetAugmentations([train_augs, val_augs, test_augs])) d = dataset.d num_classes = dataset.num_classes in_ = eddl.Input([d.n_channels_, size[0], size[1]]) out = model(in_, num_classes) # out is already softmaxed in classific models net = eddl.Model([in_], [out]) if train: logfile = open(Path(weight.logfile), 'w') else: logfile = open(inference.logfile, 'w') outputfile = open(inference.outputfile, 'w') with redirect_stdout(logfile): # Save args to file print('args: ' + json.dumps(args, indent=2, sort_keys=True), flush=True) logging.info('args: ' + json.dumps(args, indent=2, sort_keys=True)) eddl.build( net, eddl.sgd(args.lr, 0.9), [bindings.losses_binding.get(args.loss)], [bindings.metrics_binding.get(args.metric)], eddl.CS_GPU([1], mem='low_mem') if args.gpu else eddl.CS_CPU()) eddl.summary(net) if pretrained and os.path.exists(pretrained): eddl.load(net, pretrained) logging.info('Weights loaded') # Create tensor for images and labels images = eddlT.create([batch_size, d.n_channels_, size[0], size[1]]) labels = eddlT.create([batch_size, num_classes]) logging.info(f'Starting {args.mode}') print(f'Starting {args.mode}', flush=True) if train: num_samples_train = len(d.GetSplit(ecvl.SplitType.training)) num_batches_train = num_samples_train // batch_size num_samples_val = len(d.GetSplit(ecvl.SplitType.validation)) num_batches_val = num_samples_val // batch_size indices = list(range(batch_size)) for e in range(args.epochs): eddl.reset_loss(net) d.SetSplit(ecvl.SplitType.training) s = d.GetSplit() random.shuffle(s) d.split_.training_ = s d.ResetCurrentBatch() # total_loss = 0. # total_metric = 0. for i in range(num_batches_train): d.LoadBatch(images, labels) images.div_(255.0) eddl.train_batch(net, [images], [labels], indices) total_loss = net.fiterr[0] total_metric = net.fiterr[1] print( f'Train Epoch: {e + 1}/{args.epochs} [{i + 1}/{num_batches_train}] {net.lout[0].name}' f'({net.losses[0].name}={total_loss / net.inferenced_samples:1.3f},' f'{net.metrics[0].name}={total_metric / net.inferenced_samples:1.3f})', flush=True) logging.info( f'Train Epoch: {e + 1}/{args.epochs} [{i + 1}/{num_batches_train}] {net.lout[0].name}' f'({net.losses[0].name}={total_loss / net.inferenced_samples:1.3f},' f'{net.metrics[0].name}={total_metric / net.inferenced_samples:1.3f})' ) eddl.save(net, opjoin(ckpts_dir, f'{weight_id}.bin')) logging.info('Weights saved') print('Weights saved', flush=True) if len(d.split_.validation_) > 0: logging.info(f'Validation {e}/{args.epochs}') print(f'Validation {e}/{args.epochs}', flush=True) d.SetSplit(ecvl.SplitType.validation) d.ResetCurrentBatch() for i in range(num_batches_val): d.LoadBatch(images, labels) images.div_(255.0) eddl.eval_batch(net, [images], [labels], indices) # eddl.evaluate(net, [images], [labels]) total_loss = net.fiterr[0] total_metric = net.fiterr[1] print( f'Val Epoch: {e + 1}/{args.epochs} [{i + 1}/{num_batches_val}] {net.lout[0].name}' f'({net.losses[0].name}={total_loss / net.inferenced_samples:1.3f},' f'{net.metrics[0].name}={total_metric / net.inferenced_samples:1.3f})', flush=True) logging.info( f'Val Epoch: {e + 1}/{args.epochs} [{i + 1}/{num_batches_val}] {net.lout[0].name}' f'({net.losses[0].name}={total_loss / net.inferenced_samples:1.3f},' f'{net.metrics[0].name}={total_metric / net.inferenced_samples:1.3f})' ) else: d.SetSplit(ecvl.SplitType.test) num_samples_test = len(d.GetSplit()) num_batches_test = num_samples_test // batch_size preds = np.empty((0, num_classes), np.float64) for b in range(num_batches_test): d.LoadBatch(images) images.div_(255.0) eddl.forward(net, [images]) print(f'Infer Batch {b + 1}/{num_batches_test}', flush=True) logging.info(f'Infer Batch {b + 1}/{num_batches_test}') # print( # f'Evaluation {b + 1}/{num_batches} {net.lout[0].name}({net.losses[0].name}={total_loss / net.inferenced_samples:1.3f},' # f'{net.metrics[0].name}={total_metric / net.inferenced_samples:1.3f})') # logging.info( # f'Evaluation {b + 1}/{num_batches} {net.lout[0].name}({net.losses[0].name}={total_loss / net.inferenced_samples:1.3f},' # f'{net.metrics[0].name}={total_metric / net.inferenced_samples:1.3f})') # Save network predictions for i in range(batch_size): pred = np.array(eddlT.select(eddl.getTensor(out), i), copy=False) # gt = np.argmax(np.array(labels)[indices]) # pred = np.append(pred, gt).reshape((1, num_classes + 1)) preds = np.append(preds, pred, axis=0) pred_name = d.samples_[d.GetSplit()[b * batch_size + i]].location_ # print(f'{pred_name};{pred}') outputfile.write(f'{pred_name};{pred.tolist()}\n') outputfile.close() print('<done>') logfile.close() del net del out del in_ return
def main(args): num_classes = 1 size = [192, 192] # size of images thresh = 0.5 if args.out_dir: os.makedirs(args.out_dir, exist_ok=True) in_ = eddl.Input([3, size[0], size[1]]) out = SegNet(in_, num_classes) out_sigm = eddl.Sigmoid(out) net = eddl.Model([in_], [out_sigm]) eddl.build(net, eddl.adam(0.0001), ["cross_entropy"], ["mean_squared_error"], eddl.CS_GPU([1]) if args.gpu else eddl.CS_CPU()) eddl.summary(net) eddl.setlogfile(net, "skin_lesion_segmentation") training_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim(size), ecvl.AugMirror(0.5), ecvl.AugFlip(0.5), ecvl.AugRotate([-180, 180]), ecvl.AugAdditivePoissonNoise([0, 10]), ecvl.AugGammaContrast([0.5, 1.5]), ecvl.AugGaussianBlur([0, 0.8]), ecvl.AugCoarseDropout([0, 0.3], [0.02, 0.05], 0.5) ]) validation_augs = ecvl.SequentialAugmentationContainer( [ecvl.AugResizeDim(size)]) dataset_augs = ecvl.DatasetAugmentations( [training_augs, validation_augs, None]) print("Reading dataset") d = ecvl.DLDataset(args.in_ds, args.batch_size, dataset_augs) x = Tensor([args.batch_size, d.n_channels_, size[0], size[1]]) y = Tensor([args.batch_size, d.n_channels_gt_, size[0], size[1]]) num_samples_train = len(d.GetSplit()) num_batches_train = num_samples_train // args.batch_size d.SetSplit(ecvl.SplitType.validation) num_samples_validation = len(d.GetSplit()) num_batches_validation = num_samples_validation // args.batch_size indices = list(range(args.batch_size)) evaluator = utils.Evaluator() print("Starting training") for e in range(args.epochs): print("Epoch {:d}/{:d} - Training".format(e + 1, args.epochs), flush=True) d.SetSplit(ecvl.SplitType.training) eddl.reset_loss(net) s = d.GetSplit() random.shuffle(s) d.split_.training_ = s d.ResetAllBatches() for b in range(num_batches_train): print("Epoch {:d}/{:d} (batch {:d}/{:d}) - ".format( e + 1, args.epochs, b + 1, num_batches_train), end="", flush=True) d.LoadBatch(x, y) x.div_(255.0) y.div_(255.0) tx, ty = [x], [y] eddl.train_batch(net, tx, ty, indices) eddl.print_loss(net, b) print() print("Saving weights") eddl.save(net, "isic_segmentation_checkpoint_epoch_%s.bin" % e, "bin") d.SetSplit(ecvl.SplitType.validation) evaluator.ResetEval() print("Epoch %d/%d - Evaluation" % (e + 1, args.epochs), flush=True) for b in range(num_batches_validation): n = 0 print("Epoch {:d}/{:d} (batch {:d}/{:d}) ".format( e + 1, args.epochs, b + 1, num_batches_validation), end="", flush=True) d.LoadBatch(x, y) x.div_(255.0) y.div_(255.0) eddl.forward(net, [x]) output = eddl.getOutput(out_sigm) for k in range(args.batch_size): img = output.select([str(k)]) gt = y.select([str(k)]) img_np = np.array(img, copy=False) gt_np = np.array(gt, copy=False) iou = evaluator.BinaryIoU(img_np, gt_np, thresh=thresh) print("- IoU: %.6g " % iou, end="", flush=True) if args.out_dir: # C++ BinaryIoU modifies image as a side effect img_np[img_np >= thresh] = 1 img_np[img_np < thresh] = 0 img_t = ecvl.TensorToView(img) img_t.colortype_ = ecvl.ColorType.GRAY img_t.channels_ = "xyc" img.mult_(255.) # orig_img orig_img = x.select([str(k)]) orig_img.mult_(255.) orig_img_t = ecvl.TensorToImage(orig_img) orig_img_t.colortype_ = ecvl.ColorType.BGR orig_img_t.channels_ = "xyc" tmp, labels = ecvl.Image.empty(), ecvl.Image.empty() ecvl.CopyImage(img_t, tmp, ecvl.DataType.uint8) ecvl.ConnectedComponentsLabeling(tmp, labels) ecvl.CopyImage(labels, tmp, ecvl.DataType.uint8) contours = ecvl.FindContours(tmp) ecvl.CopyImage(orig_img_t, tmp, ecvl.DataType.uint8) tmp_np = np.array(tmp, copy=False) for cseq in contours: for c in cseq: tmp_np[c[0], c[1], 0] = 0 tmp_np[c[0], c[1], 1] = 0 tmp_np[c[0], c[1], 2] = 255 filename = d.samples_[d.GetSplit()[n]].location_[0] head, tail = os.path.splitext(os.path.basename(filename)) bname = "%s.png" % head output_fn = os.path.join(args.out_dir, bname) ecvl.ImWrite(output_fn, tmp) if e == 0: gt_t = ecvl.TensorToView(gt) gt_t.colortype_ = ecvl.ColorType.GRAY gt_t.channels_ = "xyc" gt.mult_(255.) gt_filename = d.samples_[d.GetSplit()[n]].label_path_ gt_fn = os.path.join(args.out_dir, os.path.basename(gt_filename)) ecvl.ImWrite(gt_fn, gt_t) n += 1 print() print("MIoU: %.6g" % evaluator.MeanMetric())
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()) eddl.summary(net) x_train = Tensor.load("cifar_trX.bin") y_train = Tensor.load("cifar_trY.bin") x_train.div_(255) x_test = Tensor.load("cifar_tsX.bin") y_test = Tensor.load("cifar_tsY.bin") x_test.div_(255)
def main(args): eddl.download_cifar10() num_classes = 10 in_ = eddl.Input([3, 32, 32]) layer = in_ layer = eddl.RandomHorizontalFlip(layer) layer = eddl.RandomCropScale(layer, [0.8, 1.0]) layer = eddl.RandomCutout(layer, [0.1, 0.5], [0.1, 0.5]) layer = eddl.MaxPool(eddl.ReLu(eddl.BatchNormalization( eddl.HeUniform(eddl.Conv(layer, 32, [3, 3], [1, 1], "same", False)), True)), [2, 2]) layer = eddl.MaxPool(eddl.ReLu(eddl.BatchNormalization( eddl.HeUniform(eddl.Conv(layer, 64, [3, 3], [1, 1], "same", False)), True)), [2, 2]) layer = eddl.MaxPool(eddl.ReLu(eddl.BatchNormalization( eddl.HeUniform(eddl.Conv(layer, 128, [3, 3], [1, 1], "same", False)), True)), [2, 2]) layer = eddl.MaxPool(eddl.ReLu(eddl.BatchNormalization( eddl.HeUniform(eddl.Conv(layer, 256, [3, 3], [1, 1], "same", False)), True)), [2, 2]) layer = eddl.Reshape(layer, [-1]) layer = eddl.Activation(eddl.BatchNormalization( eddl.Dense(layer, 128), True ), "relu") out = eddl.Softmax(eddl.BatchNormalization( eddl.Dense(layer, num_classes), True )) net = eddl.Model([in_], [out]) eddl.build( net, eddl.adam(0.001), ["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") 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) eddl.setlr(net, [0.0001]) 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")
########## # CONFIG # ########## epochs = 50 batch_size = 10 num_classes = 2 shape = (256, 256) data_yml_path = "../data/padchest.yml" ################## # MODEL CREATION # ################## in_ = eddl.Input((1, ) + shape) out = VGG16(in_, num_classes) net = eddl.Model([in_], [out]) eddl.build( net, eddl.adam(0.0001), # Optimizer ["cross_entropy"], # Losses ["categorical_accuracy"], # Metrics eddl.CS_GPU([1]) # Computing service ) # Show model architecture eddl.summary(net) ################ # ECVL DATASET # ################