Example #1
0
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))
Example #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])
    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")
Example #3
0
def main(args):
    slide_fn = args.slide_fn
    level = args.level

    ## Load model
    net = models.tissue_detector_DNN()
    eddl.build(net, eddl.rmsprop(0.00001), ["soft_cross_entropy"],
               ["categorical_accuracy"],
               eddl.CS_GPU() if args.gpu else eddl.CS_CPU())

    eddl.load(net, args.weights_fn, "bin")
    eddl.summary(net)

    ## Load Slide
    slide_T, s = read_slide(slide_fn, level)

    ## Compute tissue mask
    #len_T = slide_T.getShape()[0]
    #bs = args.batch_size
    #nb = int(np.ceil((len_T / bs)))
    #print ("n. batches: %d" % nb)
    #output_l = []
    #for b in range(nb):
    #    start = bs*b
    #    stop = bs*(b+1) if bs*(b+1) < len_T else len_T
    #    b_T = slide_T.select(["%d:%d" % (start, stop)])

    output_l = eddl.predict(net, [slide_T])

    print(output_l)
    ## Binarize the output
    mask = get_mask(output_l, s, args.threshold)
    np.save("mask", mask)
Example #4
0
    def get_tissue_mask(self,
                        np_img,
                        channel_first=True,
                        BGR=False,
                        get_prob=False):
        """
        @np_img: numpy array of a PIL image (x,y,4) if alpha channel is present 
                 or (x, y, 3) if alpha channel not present
        @channel_first: boolean to specify if the image is channel first (3,x,y) 
                        or channel last (x,y,3) (3 is replaced by 4 if alpha is present)
        
        returns a (x,y) numpy array with binary values (0:bg, 1:tissue)
        """

        if channel_first:
            np_img = np_img.transpose((1, 2, 0))  # Convert to channel last

        if BGR:
            np_img = np_img[
                ..., ::
                -1]  # Convert from BGR to RGB assuming a channel_last image

        s = np_img.shape
        n_px = s[0] * s[1]
        np_img = np_img[:, :, :3].reshape(n_px, 3)

        if not self.model:
            print("Cannot make predictions without a model. Please, load one!")
            return None

        ## From numpy array to eddl tensor to predict
        t_eval = Tensor.fromarray(np_img)

        output_l = eddl.predict(self.model,
                                [t_eval])  # Prediction.. get probabilities

        if get_prob:
            output_np_l = [
                i.getdata()[:, 1] for i in output_l
            ]  ## Create a list  f numpy array from output tensors
        else:
            output_np_l = self.__get_binary_mask(
                output_l, self.th)  ## Get the actual mask (binarization)

        mask_values = np.vstack(output_np_l)
        mask = mask_values.reshape((s[0], s[1]))

        return mask
Example #5
0
def main(args):
    
    """
        Test a model of a patient in the detection of seizures.
    """

    # Arguments
    index_test = [args.index]
    patient_id = args.id
    model_id = args.model
    batch_size = args.batch_size
    gpus = args.gpus
    exp_dir = args.dir

    # Create Data Generator object for the test set
    print('Creating Test Data Generator...', file=sys.stderr)
    dg_test = RawRecurrentDataGenerator(index_filenames=index_test,
                          window_length = 1,
                          shift = 0.5, 
                          timesteps = 19,
                          sampling_rate = 256, # in Hz
                          batch_size=batch_size,
                          in_training_mode=False,
                          patient_id=patient_id)
    #

    model_dir = os.path.join(exp_dir, 'models')

    # Find best model in the models directory
    best_model = dict()  # {epoch: model_filename}
    for file in os.listdir(model_dir):
        if 'best' in file:
            w = file.split('_')
            for i in range(len(w)):
                if w[i] == 'epoch':
                    epoch = int(w[i + 1])
                    break
            #
            best_model[epoch] = file
    #
    # Get the highest epoch model filename - which is the best model -
    best_model_name = best_model[max(best_model.keys())]

    print(f'Evaluating best model with the test set -> {best_model_name}', file=sys.stderr)

    model_filename = os.path.join(model_dir, best_model_name)

    # Load the model in the eddl
    print('Loading the model...', file=sys.stderr)
    net = create_model(model_id=model_id,
                       input_shape=None, # Not needed if we are loading
                       num_classes=2,
                       filename=model_filename,
                       gpus=gpus)
    #


    # Get predictions for the test set with the best model
    print('Testing the model with the test signals...', file=sys.stderr)
    Y_true_single_channel = list()
    Y_pred_single_channel = list()
    Y_true = list()
    Y_pred = list()

    for j in tqdm(range(len(dg_test))):
        x, y = dg_test[j]
        
        channels_y_pred = list()
        for channel in range(x.shape[3]):
            x_channel = x[:, :, :, channel]

            channel_tensor_batch = Tensor.fromarray(x_channel)
            # Forward and backward of the channel through the net
            (y_pred, ) = eddl.predict(net, [channel_tensor_batch])

            y_pred = y_pred.getdata()
            
            channels_y_pred.append(y_pred)
            Y_pred_single_channel += y_pred.argmax(axis=1).tolist()
            Y_true_single_channel += y.tolist()
        
        channels_y_pred = numpy.array(channels_y_pred)
        # (23, batch_size, 2)
        channels_y_pred = numpy.sum(channels_y_pred, axis=0)
        # print(channels_y_pred.shape) -> (batch_size, 2)
        
        Y_true += y.tolist()
        Y_pred += channels_y_pred.argmax(axis=1).tolist()
    #

    y_true = numpy.array(Y_true) * 1.0
    y_pred = numpy.array(Y_pred) * 1.0
    y_true_single_channel = numpy.array(Y_true_single_channel) * 1.0
    y_pred_single_channel = numpy.array(Y_pred_single_channel) * 1.0

    
    # Calculate and print basic metrics

    test_accuracy_single_channel = sum(y_true_single_channel == y_pred_single_channel) / len(y_true_single_channel)
    cnf_matrix = confusion_matrix(y_true_single_channel, y_pred_single_channel)
    report = classification_report(y_true_single_channel, y_pred_single_channel)
    fscore_single_channel = f1_score(y_true_single_channel, y_pred_single_channel, labels=[0, 1], average='macro')
    
    print('***************************************************************\n', file=sys.stderr)
    print(f'Test results\n', file=sys.stderr)
    print(' -- Single channel results (no combination of channels) --\n', file=sys.stderr)
    print(f'Test accuracy : {test_accuracy_single_channel}', file=sys.stderr)
    print(f'Test macro f1-score : {fscore_single_channel}', file=sys.stderr)
    print('Confussion matrix:', file=sys.stderr)
    print(f'{cnf_matrix}\n', file=sys.stderr)
    print('Classification report:', file=sys.stderr)
    print(report, file=sys.stderr)

    print('\n--------------------------------------------------------------\n', file=sys.stderr)

    test_accuracy = sum(y_true == y_pred) / len(y_true)
    cnf_matrix = confusion_matrix(y_true, y_pred)
    report = classification_report(y_true, y_pred)
    fscore = f1_score(y_true, y_pred, labels=[0, 1], average='macro')

    print(' -- All channels involved (combined for each timestamp) --\n', file=sys.stderr)
    print(f'Test accuracy : {test_accuracy}', file=sys.stderr)
    print(f'Test macro f1-score : {fscore}', file=sys.stderr)
    print('Confussion matrix:', file=sys.stderr)
    print(f'{cnf_matrix}\n', file=sys.stderr)
    print('Classification report:', file=sys.stderr)
    print(report, file=sys.stderr)
    
    print('\n--------------------------------------------------------------\n', file=sys.stderr)

    # Calculate and print other metrics: 
    acc_window, latency, fp_h, recall = calculate_detection_metrics(
                                        y_true,
                                        y_pred,
                                        sample_shift=args.sample_shift,
                                        sliding_window_length=args.window_length,
                                        alpha_pos=args.alpha_pos,
                                        alpha_neg=args.alpha_neg,
                                        detection_threshold=args.detection_threshold
                                        )

    print('Global metrics after inference\n\n', file=sys.stderr)
    print(f'Accuracy of the sliding window: {acc_window * 100.0:.4f}', file=sys.stderr)
    print(f'Percentage of detected seizures: {recall * 100.0:.4f}', file=sys.stderr)
    print(f'Average latency: {latency} seconds', file=sys.stderr)
    print(f'False Alarms per Hour: {fp_h}', file=sys.stderr)

    print('***************************************************************\n\n', file=sys.stderr)
def main(args):
    """
    Train a model on epilepsy detection with recurrent neural networks.
    """

    index_training = [args.index]
    index_validation = [args.index_val]
    patient_id = args.id
    model_id = args.model
    epochs = args.epochs
    batch_size = args.batch_size
    resume_dir = args.resume
    starting_epoch = args.starting_epoch
    gpus = args.gpus

    model_checkpoint = None

    # Create dirs for the experiment
    if resume_dir is None:
        os.makedirs('experiments', exist_ok=True)
        exp_name = f'detection_recurrent_{patient_id}_LSTM'
        exp_dir = f'experiments/{exp_name}'
        os.makedirs(exp_dir, exist_ok=False)
        os.makedirs(exp_dir + '/models')
    else:
        exp_dir = resume_dir
        model_dir = exp_dir + '/models'
        for f in os.listdir(model_dir):
            if 'last' in f:
                model_checkpoint = os.path.join(model_dir, f)
        #
        if model_checkpoint is None:
            raise Exception(f'Last model not found in {model_dir}')
        #

    log_file = open(f'{exp_dir}/training_log.txt', 'w')
    log_file.write('epoch, train_loss, train_acc, val_acc_single_channel,' +
                   ' val_f1score_single_channel, val_acc, val_f1score\n')
    log_file.flush()

    # Data Generator Object for training
    print('\n\nCreating Training Data Generator...', file=sys.stderr)
    dg = RawRecurrentDataGenerator(
        index_filenames=index_training,
        window_length=args.window_length,  # in seconds
        shift=args.shift,  # in seconds
        timesteps=args.timesteps,  # in seconds
        sampling_rate=256,  # in Hz
        batch_size=batch_size,
        in_training_mode=True,
        balance_batches=True,
        patient_id=patient_id)
    #

    print('\n\nCreating Validation Data Generator...', file=sys.stderr)
    dg_val = RawRecurrentDataGenerator(
        index_filenames=index_validation,
        window_length=args.window_length,
        shift=args.shift,
        timesteps=args.timesteps,
        sampling_rate=256,  # in Hz
        batch_size=batch_size,
        in_training_mode=False,
        patient_id=patient_id)

    net = create_model(model_id,
                       dg.input_shape,
                       num_classes=2,
                       filename=model_checkpoint,
                       gpus=gpus)
    #

    best_val_acc = 0.0

    for epoch in range(starting_epoch, epochs):

        print(f'\nTraining epoch {epoch+1} of {epochs}...', file=sys.stderr)
        dg.shuffle_data()
        # TRAINING STAGE
        eddl.reset_loss(net)

        # Set a progress bar for the training loop
        pbar = tqdm(range(len(dg)))

        for i in pbar:
            # Load batch of data
            x, y = dg[i]

            _y_ = numpy.zeros([len(y), 2])
            for i in range(2):
                _y_[y == i, i] = 1
            _y_ = _y_.reshape((len(y), 1, 2))
            y = Tensor.fromarray(_y_)

            for channel in range(x.shape[3]):
                x_channel = x[:, :, :, channel]

                channel_tensor_batch = Tensor.fromarray(x_channel)
                # Forward and backward of the channel through the net
                eddl.train_batch(net, [channel_tensor_batch], [y])

                losses = eddl.get_losses(net)
                #metrics = eddl.get_metrics(net)

                pbar.set_description(
                    f'Training[loss={losses[0]:.5f}, acc=Not Available]')

        print()

        # VALIDATION
        print(f'\nValidation epoch {epoch+1}', file=sys.stderr)

        Y_true_single_channel = list()
        Y_pred_single_channel = list()
        Y_true = list()
        Y_pred = list()

        for j in tqdm(range(len(dg_val))):
            x, y = dg_val[j]

            channels_y_pred = list()
            for channel in range(x.shape[3]):
                x_channel = x[:, :, :, channel]

                channel_tensor_batch = Tensor.fromarray(x_channel)
                # Forward and backward of the channel through the net
                (y_pred, ) = eddl.predict(net, [channel_tensor_batch])

                y_pred = y_pred.getdata()

                channels_y_pred.append(y_pred)
                Y_pred_single_channel += y_pred.argmax(axis=1).tolist()
                Y_true_single_channel += y.tolist()

            channels_y_pred = numpy.array(channels_y_pred)
            # (23, batch_size, 2)
            channels_y_pred = numpy.sum(channels_y_pred, axis=0)
            channels_y_pred = channels_y_pred / 23.0
            # print(channels_y_pred.shape) -> (batch_size, 2)

            Y_true += y.tolist()
            Y_pred += channels_y_pred.argmax(axis=1).tolist()
        #

        y_true = numpy.array(Y_true) * 1.0
        y_pred = numpy.array(Y_pred) * 1.0
        y_true_single_channel = numpy.array(Y_true_single_channel) * 1.0
        y_pred_single_channel = numpy.array(Y_pred_single_channel) * 1.0

        val_accuracy_single_channel = sum(
            y_true_single_channel == y_pred_single_channel) / len(
                y_true_single_channel)
        cnf_matrix = confusion_matrix(y_true_single_channel,
                                      y_pred_single_channel)
        report = classification_report(y_true_single_channel,
                                       y_pred_single_channel)
        fscore_single_channel = f1_score(y_true_single_channel,
                                         y_pred_single_channel,
                                         labels=[0, 1],
                                         average='macro')

        print(
            '***************************************************************\n',
            file=sys.stderr)
        print(f'Epoch {epoch + 1}: Validation results\n', file=sys.stderr)
        print(' -- Single channel results (no combination of channels) --\n',
              file=sys.stderr)
        print(f'Validation acc : {val_accuracy_single_channel}',
              file=sys.stderr)
        print(f'Validation macro f1-score : {fscore_single_channel}',
              file=sys.stderr)
        print('Confussion matrix:', file=sys.stderr)
        print(f'{cnf_matrix}\n', file=sys.stderr)
        print('Classification report:', file=sys.stderr)
        print(report, file=sys.stderr)

        print(
            '\n--------------------------------------------------------------\n',
            file=sys.stderr)

        val_accuracy = sum(y_true == y_pred) / len(y_true)
        cnf_matrix = confusion_matrix(y_true, y_pred)
        report = classification_report(y_true, y_pred)
        fscore = f1_score(y_true, y_pred, labels=[0, 1], average='macro')

        print(' -- All channels involved (combined for each timestamp) --\n',
              file=sys.stderr)
        print(f'Validation acc : {val_accuracy}', file=sys.stderr)
        print(f'Validation macro f1-score : {fscore}', file=sys.stderr)
        print('Confussion matrix:', file=sys.stderr)
        print(f'{cnf_matrix}\n', file=sys.stderr)
        print('Classification report:', file=sys.stderr)
        print(report, file=sys.stderr)
        print(
            '***************************************************************\n\n',
            file=sys.stderr)

        log_file.write('%d,%d,%g,%g,%g,%g,%g\n' %
                       (epoch, -1, losses[0], val_accuracy_single_channel,
                        fscore_single_channel, val_accuracy, fscore))

        log_file.flush()

        # Save best model
        if (val_accuracy > best_val_acc):
            best_val_acc = val_accuracy
            eddl.save_net_to_onnx_file(
                net,
                f'{exp_dir}/models/best_model_epoch_{epoch:04d}_val_acc_{val_accuracy:.4f}.onnx'
            )

        eddl.save_net_to_onnx_file(net, f'{exp_dir}/models/last.onnx')

    #
    log_file.close()