def extract_patches_train(img_test_normalized, patch_size):
    # Extract training patches manual
    stride = patch_size

    height, width, channel = img_test_normalized.shape
    #print(height, width)

    num_patches_h = height // stride
    num_patches_w = width // stride
    #print(num_patches_h, num_patches_w)

    new_shape = (num_patches_h * num_patches_w, patch_size, patch_size,
                 channel)
    new_img = np.zeros(new_shape)
    print(new_img.shape)
    cont = 0
    # rows
    for h in range(num_patches_h):
        # columns
        for w in range(num_patches_w):
            new_img[cont] = img_test_normalized[h * stride:(h + 1) * stride,
                                                w * stride:(w + 1) * stride]
            cont += 1
    #print(cont)

    return new_img
def extract_patches_test(binary_img_test_ref, patch_size):
    # Extract training patches
    stride = patch_size

    height, width = binary_img_test_ref.shape
    #print(height, width)

    num_patches_h = int(height / stride)
    num_patches_w = int(width / stride)
    #print(num_patches_h, num_patches_w)

    new_shape = (num_patches_h * num_patches_w, patch_size, patch_size)
    new_img_ref = np.zeros(new_shape)
    print(new_img_ref.shape)
    cont = 0
    # rows
    for h in range(num_patches_h):
        #columns
        for w in range(num_patches_w):
            new_img_ref[cont] = binary_img_test_ref[h * stride:(h + 1) *
                                                    stride, w *
                                                    stride:(w + 1) * stride]
            cont += 1
    #print(cont)

    return new_img_ref
def pred_recostruction(patch_size,
                       pred_labels,
                       binary_img_test_ref,
                       img_type=1):
    # Patches Reconstruction
    if img_type == 1:
        stride = patch_size

        height, width = binary_img_test_ref.shape

        num_patches_h = height // stride
        num_patches_w = width // stride
        #print(num_patches_h, num_patches_w)

        new_shape = (height, width)
        img_reconstructed = np.zeros(new_shape)
        cont = 0
        # rows
        for h in range(num_patches_h):
            # columns
            for w in range(num_patches_w):
                img_reconstructed[h * stride:(h + 1) * stride, w *
                                  stride:(w + 1) * stride] = pred_labels[cont]
                cont += 1
        print('Reconstruction Done!')
    if img_type == 2:
        stride = patch_size

        height, width = binary_img_test_ref.shape

        num_patches_h = height // stride
        num_patches_w = width // stride

        new_shape = (height, width, 3)
        img_reconstructed = np.zeros(new_shape)
        cont = 0
        # rows
        for h in range(num_patches_h):
            # columns
            for w in range(num_patches_w):
                img_reconstructed[h * stride:(h + 1) * stride,
                                  w * stride:(w + 1) *
                                  stride, :] = pred_labels[cont]
                cont += 1
        print('Reconstruction Done!')
    return img_reconstructed
def pmus_passive(fs, rr):
    """
    Passive profile, i.e, no respiratory effort (no spontaneous breathing)
    :param fs: sample frequency
    :param rr: respiratory rate
    :return: pmus profile
    """
    pmus = np.zeros(1, int(np.floor(60.0 / rr * fs) + 1))

    return pmus
def pmus_trapezoidal(fs, rr, ttrap, inspeak, exppeak):
    """
    Trapezoidal profile
    :param fs: sample frequency
    :param rr: respiratory rate
    :param ttrap: time array defining trapezoidal profile
    :param inspeak: negative peak pressure
    :param exppeak: positive peak pressure
    :return: pmus profile
    """
    pmus = np.zeros(1, int(np.floor(60.0 / rr * fs) + 1))
    return pmus
def convert_preds2rgb(img_reconstructed, label_dict):
    reversed_label_dict = {value: key for (key, value) in label_dict.items()}
    print(reversed_label_dict)
    height, width = img_reconstructed.shape
    img_reconstructed_rgb = np.zeros((height, width, 3))
    for h in range(height):
        for w in range(width):
            pixel_class = img_reconstructed[h, w]
            img_reconstructed_rgb[h, w, :] = ast.literal_eval(
                reversed_label_dict[pixel_class])
    print('Conversion to RGB Done!')
    return img_reconstructed_rgb.astype(np.uint8)
示例#7
0
 def zero_grads_for_batch():
     gW3 = np.zeros(W3_shape)
     gW2 = np.zeros(W2_shape)
     gW1 = np.zeros(W1_shape)
     gb1 = np.zeros(b1_shape[0])
     gb2 = np.zeros(b2_shape[0])
     gb3 = np.zeros(b3_shape[0])
     return [gW3, gW2, gW1, gb1, gb2, gb3]
# Mask with tiles
tile_number = np.ones((340, 480))
mask_c_1 = np.concatenate((tile_number, 2 * tile_number, 3 * tile_number),
                          axis=1)
mask_c_2 = np.concatenate((4 * tile_number, 5 * tile_number, 6 * tile_number),
                          axis=1)
mask_c_3 = np.concatenate((7 * tile_number, 8 * tile_number, 9 * tile_number),
                          axis=1)
mask_c_4 = np.concatenate(
    (10 * tile_number, 11 * tile_number, 12 * tile_number), axis=1)
mask_c_5 = np.concatenate(
    (13 * tile_number, 14 * tile_number, 15 * tile_number), axis=1)
mask_tiles = np.concatenate((mask_c_1, mask_c_2, mask_c_3, mask_c_4, mask_c_5),
                            axis=0)

mask_tr_val = np.zeros((mask_tiles.shape))
tr1 = 1
tr2 = 6
tr3 = 7
tr4 = 13
val1 = 5
val2 = 12

mask_tr_val[mask_tiles == tr1] = 1
mask_tr_val[mask_tiles == tr2] = 1
mask_tr_val[mask_tiles == tr3] = 1
mask_tr_val[mask_tiles == tr4] = 1
mask_tr_val[mask_tiles == val1] = 2
mask_tr_val[mask_tiles == val2] = 2

total_no_def = 0
示例#9
0
def solve_model(header_params,params,header_features,features,debugmsg):
    #Extracts each parameter
    fs = params[header_params.index('Fs')]
    rvent = params[header_params.index('Rvent')]
    c = params[header_params.index('C')]
    rins = params[header_params.index('Rins')]
    rexp = rins  # params[4]
    peep = params[header_params.index('PEEP')]
    sp = params[header_params.index('SP')]
    trigger_type = features[header_features.index('Triggertype')]
    trigger_arg = params[header_params.index('Triggerarg')]
    rise_type = features[header_features.index('Risetype')]
    rise_time = params[header_params.index('Risetime')]
    cycle_off = params[header_params.index('Cycleoff')]
    rr = params[header_params.index('RR')]
    pmus_type = features[header_features.index('Pmustype')]
    pp = params[header_params.index('Pp')]
    tp = params[header_params.index('Tp')]
    tf = params[header_params.index('Tf')]
    noise = params[header_params.index('Noise')]
    e2 = params[header_params.index('E2')]
    model = features[header_features.index('Model')]

    expected_len = int(np.floor(180.0 / np.min(RR) * np.max(Fs)) + 1)
    
    #Assings pmus profile
    pmus = pmus_profile(fs, rr, pmus_type, pp, tp, tf)
    pmus = pmus + peep #adjusts PEEP
    pmus = np.concatenate((np.array([0]), pmus)) #sets the first value to zero

    
    #Unit conversion from cmH2O.s/L to cmH2O.s/mL
    rins = rins / 1000.0
    rexp = rexp / 1000.0
    rvent = rvent / 1000.0


    #Generates time, flow, volume, insex and paw waveforms
    time = np.arange(0, np.floor(60.0 / rr * fs) + 1, 1) / fs
    time = np.concatenate((np.array([0]), time))
    flow = np.zeros(len(time))
    volume = np.zeros(len(time))
    insex = np.zeros(len(time))
    paw = np.zeros(len(time)) + peep #adjusts PEEP
    len_time = len(time)

    #Peak flow detection
    peak_flow = flow[0]
    detect_peak_flow = False

    #Support detection
    detect_support = False
    time_support = -1

    #Expiration detection
    detect_exp = False
    time_exp = -1

    if trigger_type == 'flow':
        # units conversion from L/min to mL/s
        trigger_arg = trigger_arg / 60.0 * 1000.0

    for i in range(1, len(time)):
        # period until the respiratory effort beginning
        if (((trigger_type == 'flow' and flow[i] < trigger_arg) or
             (trigger_type == 'pressure' and paw[i] > trigger_arg + peep) or
             (trigger_type == 'delay' and time[i] < trigger_arg)) and
                (not detect_support) and (not detect_exp)):
            paw[i] = peep
            y0 = volume[i - 1]
            tspan = [time[i - 1], time[i]]
            args = (paw[i], pmus[i], model, c, e2, rins)
            sol = odeint(flow_model, y0, tspan, args=args)
            volume[i] = sol[-1]
            flow[i] = flow_model(volume[i], time[i], paw[i], pmus[i], model, c, e2, rins)
            if debugmsg:
                print('volume[i]= {:.2f}, flow[i]= {:.2f}, paw[i]= {:.2f}, waiting'.format(volume[i], flow[i], paw[i]))

            if (((trigger_type == 'flow' and flow[i] >= trigger_arg) or
                 (trigger_type == 'pressure' and paw[i] <= trigger_arg + peep) or
                 (trigger_type == 'delay' and time[i] >= trigger_arg))):
                detect_support = True
                time_support = time[i+1]
                continue

        # detection of inspiratory effort
        # ventilator starts to support the patient
        elif (detect_support and (not detect_exp)):
            if rise_type == 'step':
                paw[i] = sp + peep
            elif rise_type == 'exp':
                rise_type = rise_type if np.random.random() > 0.01 else 'linear'
                if paw[i] < sp + peep:
                    paw[i] = (1.0 - np.exp(-(time[i] - time_support) / rise_time )) * sp + peep
                if paw[i] >= sp + peep:
                    paw[i] = sp + peep
            elif rise_type == 'linear':
                rise_type = rise_type if np.random.random() > 0.01 else 'exp'
                if paw[i] < sp + peep:
                    paw[i] = (time[i] - time_support) / rise_time * sp + peep
                if paw[i] >= sp + peep:
                    paw[i] = sp + peep

            y0 = volume[i - 1]
            tspan = [time[i - 1], time[i]]
            args = (paw[i], pmus[i], model, c, e2, rins)
            sol = odeint(flow_model, y0, tspan, args=args)
            volume[i] = sol[-1]
            flow[i] = flow_model(volume[i], time[i], paw[i], pmus[i], model, c, e2, rins)
            if debugmsg:
                print('volume[i]= {:.2f}, flow[i]= {:.2f}, paw[i]= {:.2f}, supporting'.format(volume[i], flow[i], paw[i]))

            if flow[i] >= flow[i - 1]:
                peak_flow = flow[i]
                detect_peak_flow = False
            elif flow[i] < flow[i - 1]:
                detect_peak_flow = True

            if (flow[i] <= cycle_off * peak_flow) and detect_peak_flow and i<len_time:
                detect_exp = True
                time_exp = i+1    
                try:
                    paw[i + 1] = paw[i]
                except IndexError:
                    pass

        elif detect_exp:
            if rise_type == 'step':
                paw[i] = peep
            elif rise_type == 'exp':
                if paw[i - 1] > peep:
                    paw[i] = sp * (np.exp(-(time[i] - time[time_exp-1]) / rise_time )) + peep
                if paw[i - 1] <= peep:
                    paw[i] = peep
            elif rise_type == 'linear':
                rise_type = rise_type if np.random.random() > 0.01 else 'exp'
                if paw[i - 1] > peep:
                    paw[i] = sp * (1 - (time[i] - time[time_exp-1]) / rise_time) + peep
                if paw[i - 1] <= peep:
                    paw[i] = peep

            y0 = volume[i - 1]
            tspan = [time[i - 1], time[i]]
            args = (paw[i], pmus[i], model, c, e2, rexp + rvent)
            sol = odeint(flow_model, y0, tspan, args=args)
            volume[i] = sol[-1]
            flow[i] = flow_model(volume[i], time[i], paw[i], pmus[i], model, c, e2, rexp + rvent)
            if debugmsg:
                print('volume[i]= {:.2f}, flow[i]= {:.2f}, paw[i]= {:.2f}, exhaling'.format(volume[i], flow[i], paw[i]))

    #Generates InsEx trace
    if time_exp > -1:
        insex = np.concatenate((np.ones(time_exp), np.zeros(len(time) - time_exp)))

    #Drops the first element
    flow = flow[1:] / 1000.0 * 60.0  # converts back to L/min
    volume = volume[1:]
    paw = paw[1:]
    pmus = pmus[1:] - peep #reajust peep again
    insex = insex[1:]

    flow,volume,pmus,insex,paw = generate_cycle(expected_len,flow,volume,pmus,insex,paw,peep=peep)

    # paw = generate_cycle(expected_len,paw,peep=peep)[0]
    
    flow,volume,paw,pmus,insex = generate_noise(noise,flow,volume,paw,pmus,insex)

    # plt.plot(flow)
    # plt.plot(volume)
    # plt.plot(paw)
    # plt.plot(pmus)
    # plt.show()

    return flow, volume, paw, pmus, insex, rins,rexp, c
示例#10
0
def train_model(args,
                net,
                x_train_paths,
                y_train_paths,
                x_val_paths,
                y_val_paths,
                batch_size,
                epochs,
                x_shape_batch,
                y_shape_batch,
                patience=10,
                delta=0.001,
                metrics_names=None):
    # patches_train = x_train_paths
    print('Start training...')
    print('=' * 60)
    print(f'Training on {len(x_train_paths)} images')
    print(f'Validating on {len(x_val_paths)} images')
    print('=' * 60)
    print(f'Total Epochs: {epochs}')
    # Initialize tensorboard metrics
    train_summary_writer = tf.summary.create_file_writer(
        os.path.join(args.results_path, 'logs', 'train'))
    val_summary_writer = tf.summary.create_file_writer(
        os.path.join(args.results_path, 'logs', 'val'))
    # Initialize as maximum possible number
    min_loss = float('inf')
    cont = 0
    x_train_b = np.zeros(x_shape_batch, dtype=np.float32)
    y_train_h_b_seg = np.zeros(y_shape_batch, dtype=np.float32)
    x_val_b = np.zeros(x_shape_batch, dtype=np.float32)
    y_val_h_b_seg = np.zeros(y_shape_batch, dtype=np.float32)
    if args.multitasking:
        # Bounds
        y_train_h_b_bound = np.zeros(y_shape_batch, dtype=np.float32)
        y_val_h_b_bound = np.zeros(y_shape_batch, dtype=np.float32)
        # Dists
        y_train_h_b_dist = np.zeros(y_shape_batch, dtype=np.float32)
        y_val_h_b_dist = np.zeros(y_shape_batch, dtype=np.float32)
        # Colors
        y_train_h_b_color = np.zeros(
            (y_shape_batch[0], y_shape_batch[1], y_shape_batch[2], 3),
            dtype=np.float32)
        y_val_h_b_color = np.zeros(
            (y_shape_batch[0], y_shape_batch[1], y_shape_batch[2], 3),
            dtype=np.float32)

    # print(net.metrics_names)
    print(net.output_names)
    for epoch in range(epochs):
        # metrics_len = len(net.metrics_names)
        metrics_len = len(metrics_names)
        loss_tr = np.zeros((1, metrics_len))
        loss_val = np.zeros((1, metrics_len))
        # Computing the number of batchs on training
        n_batchs_tr = len(x_train_paths) // batch_size
        # Random shuffle the data
        if not args.multitasking:
            (x_train_paths_rand,
             y_train_paths_rand_seg) = shuffle(x_train_paths, y_train_paths[0])
        else:
            (x_train_paths_rand, y_train_paths_rand_seg,
             y_train_paths_rand_bound, y_train_paths_rand_dist,
             y_train_paths_rand_color) \
             = shuffle(x_train_paths, y_train_paths[0], y_train_paths[1],
                       y_train_paths[2], y_train_paths[3])

        # Training the network per batch
        for batch in tqdm(range(n_batchs_tr), desc="Train"):
            x_train_paths_b = x_train_paths_rand[batch *
                                                 batch_size:(batch + 1) *
                                                 batch_size]
            y_train_paths_b_seg = y_train_paths_rand_seg[batch *
                                                         batch_size:(batch +
                                                                     1) *
                                                         batch_size]
            # if args.multitasking:
            #     y_train_paths_b_bound = y_train_paths_rand_bound[batch * batch_size:(batch + 1) * batch_size]
            #     y_train_paths_b_dist = y_train_paths_rand_dist[batch * batch_size:(batch + 1) * batch_size]
            #     y_train_paths_b_color = y_train_paths_rand_color[batch * batch_size:(batch + 1) * batch_size]
            for b in range(batch_size):
                x_train_b[b] = np.load(x_train_paths_b[b])
                y_train_h_b_seg[b] = np.load(y_train_paths_b_seg[b]).astype(
                    np.float32)
                # if args.multitasking:
                #     y_train_h_b_bound[b] = np.load(y_train_paths_b_bound[b])
                #     y_train_h_b_dist[b] = np.load(y_train_paths_b_dist[b])
                #     y_train_h_b_color[b] = np.load(y_train_paths_b_color[b])

            if not args.multitasking:
                loss_tr = loss_tr + net.train_on_batch(x_train_b,
                                                       y_train_h_b_seg)
            else:
                # Get paths per batch on multitasking labels
                y_train_paths_b_bound = y_train_paths_rand_bound[batch *
                                                                 batch_size:
                                                                 (batch + 1) *
                                                                 batch_size]
                y_train_paths_b_dist = y_train_paths_rand_dist[batch *
                                                               batch_size:
                                                               (batch + 1) *
                                                               batch_size]
                y_train_paths_b_color = y_train_paths_rand_color[batch *
                                                                 batch_size:
                                                                 (batch + 1) *
                                                                 batch_size]
                # Load multitasking labels
                for b in range(batch_size):
                    y_train_h_b_bound[b] = np.load(
                        y_train_paths_b_bound[b]).astype(np.float32)
                    y_train_h_b_dist[b] = np.load(
                        y_train_paths_b_dist[b]).astype(np.float32)
                    y_train_h_b_color[b] = np.load(
                        y_train_paths_b_color[b]).astype(np.float32)

                y_train_b = {"seg": y_train_h_b_seg}
                y_train_b['bound'] = y_train_h_b_bound
                y_train_b['dist'] = y_train_h_b_dist
                y_train_b['color'] = y_train_h_b_color

                loss_tr = loss_tr + net.train_on_batch(
                    x=x_train_b, y=y_train_b, return_dict=False)

        # Training loss; Divide by the number of batches
        # print(loss_tr)
        loss_tr = loss_tr / n_batchs_tr

        # Computing the number of batchs on validation
        n_batchs_val = len(x_val_paths) // batch_size

        # Evaluating the model in the validation set
        for batch in tqdm(range(n_batchs_val), desc="Validation"):
            x_val_paths_b = x_val_paths[batch * batch_size:(batch + 1) *
                                        batch_size]
            y_val_paths_b_seg = y_val_paths[0][batch * batch_size:(batch + 1) *
                                               batch_size]
            for b in range(batch_size):
                x_val_b[b] = np.load(x_val_paths_b[b])
                y_val_h_b_seg[b] = np.load(y_val_paths_b_seg[b]).astype(
                    np.float32)

            if not args.multitasking:
                loss_val = loss_val + net.test_on_batch(x_val_b, y_val_h_b_seg)
            else:
                # Get paths per batch on multitasking labels
                y_val_paths_b_bound = y_val_paths[1][batch *
                                                     batch_size:(batch + 1) *
                                                     batch_size]
                y_val_paths_b_dist = y_val_paths[2][batch *
                                                    batch_size:(batch + 1) *
                                                    batch_size]
                y_val_paths_b_color = y_val_paths[3][batch *
                                                     batch_size:(batch + 1) *
                                                     batch_size]
                # Load multitasking labels
                for b in range(batch_size):
                    y_val_h_b_bound[b] = np.load(
                        y_val_paths_b_bound[b]).astype(np.float32)
                    y_val_h_b_dist[b] = np.load(y_val_paths_b_dist[b]).astype(
                        np.float32)
                    y_val_h_b_color[b] = np.load(
                        y_val_paths_b_color[b]).astype(np.float32)
                # Dict template: y_val_b = {"segmentation": y_val_h_b_seg,
                # "boundary": y_val_h_b_bound, "distance":  y_val_h_b_dist,
                # "color": y_val_h_b_color}
                y_val_b = {"seg": y_val_h_b_seg}
                y_val_b['bound'] = y_val_h_b_bound
                y_val_b['dist'] = y_val_h_b_dist
                y_val_b['color'] = y_val_h_b_color

                loss_val = loss_val + net.test_on_batch(x=x_val_b, y=y_val_b)
        loss_val = loss_val / n_batchs_val

        # train_metrics = dict(zip(net.metrics_names, loss_tr.tolist()[0]))
        # val_metrics = dict(zip(net.metrics_names, loss_val.tolist()[0]))
        train_metrics = dict(zip(metrics_names, loss_tr.tolist()[0]))
        val_metrics = dict(zip(metrics_names, loss_val.tolist()[0]))
        if not args.multitasking:
            # print(f'loss_val shape: {loss_val.shape}')
            train_loss = train_metrics['loss']
            train_acc = train_metrics['accuracy']
            val_loss = val_metrics['loss']
            val_acc = val_metrics['accuracy']

            mcc = compute_mcc(val_metrics['true_positives'],
                              val_metrics['true_negatives'],
                              val_metrics['false_positives'],
                              val_metrics['false_negatives'])

            print(f"Epoch: {epoch} " + f"Training loss: {train_loss :.5f} " +
                  f"Train acc.: {100*train_acc:.5f}% " +
                  f"Validation loss: {val_loss :.5f} " +
                  f"Validation acc.: {100*val_acc:.5f}%")

            add_tensorboard_scalars(train_summary_writer,
                                    val_summary_writer,
                                    epoch,
                                    'Total',
                                    train_loss,
                                    val_loss,
                                    train_acc,
                                    val_acc,
                                    val_mcc=mcc)
        else:
            mcc = compute_mcc(val_metrics['seg_true_positives'],
                              val_metrics['seg_true_negatives'],
                              val_metrics['seg_false_positives'],
                              val_metrics['seg_false_negatives'])

            metrics_table = PrettyTable()
            metrics_table.title = f'Epoch: {epoch}'
            metrics_table.field_names = [
                'Task', 'Loss', 'Val Loss', 'Acc %', 'Val Acc %'
            ]

            metrics_table.add_row([
                'Seg',
                round(train_metrics['seg_loss'], 5),
                round(val_metrics['seg_loss'], 5),
                round(100 * train_metrics['seg_accuracy'], 5),
                round(100 * val_metrics['seg_accuracy'], 5)
            ])
            add_tensorboard_scalars(train_summary_writer,
                                    val_summary_writer,
                                    epoch,
                                    'Segmentation',
                                    train_metrics['seg_loss'],
                                    val_metrics['seg_loss'],
                                    train_metrics['seg_accuracy'],
                                    val_metrics['seg_accuracy'],
                                    val_mcc=mcc)

            metrics_table.add_row([
                'Bound',
                round(train_metrics['bound_loss'], 5),
                round(val_metrics['bound_loss'], 5), 0, 0
            ])
            add_tensorboard_scalars(train_summary_writer, val_summary_writer,
                                    epoch, 'Boundary',
                                    train_metrics['bound_loss'],
                                    val_metrics['bound_loss'])

            metrics_table.add_row([
                'Dist',
                round(train_metrics['dist_loss'], 5),
                round(val_metrics['dist_loss'], 5), 0, 0
            ])
            add_tensorboard_scalars(train_summary_writer, val_summary_writer,
                                    epoch, 'Distance',
                                    train_metrics['dist_loss'],
                                    val_metrics['dist_loss'])

            metrics_table.add_row([
                'Color',
                round(train_metrics['color_loss'], 5),
                round(val_metrics['color_loss'], 5), 0, 0
            ])
            add_tensorboard_scalars(train_summary_writer, val_summary_writer,
                                    epoch, 'Color',
                                    train_metrics['color_loss'],
                                    val_metrics['color_loss'])

            metrics_table.add_row([
                'Total',
                round(train_metrics['loss'], 5),
                round(val_metrics['loss'], 5), 0, 0
            ])
            add_tensorboard_scalars(train_summary_writer, val_summary_writer,
                                    epoch, 'Total', train_metrics['loss'],
                                    val_metrics['loss'])
            val_loss = val_metrics['loss']
            print(metrics_table)
        # Early stop
        # Save the model when loss is minimum
        # Stop the training if the loss don't decreases after patience epochs
        if val_loss >= min_loss + delta:
            cont += 1
            print(f'EarlyStopping counter: {cont} out of {patience}')
            if cont >= patience:
                print("Early Stopping! \t Training Stopped")
                # print("Saving model...")
                # net.save(os.path.join(args.checkpoint, 'model_early_stopping.h5'))
                return net
        else:
            cont = 0
            min_loss = val_loss
            print("Saving best model...")
            net.save(os.path.join(args.results_path, 'best_model.h5'))
示例#11
0
header_features, feature = get_random_features(features, num_test_cases)

print(f'Number of respiratory cycles to be simulated: {num_test_cases}')

fs = max(Fs)
rr = min(RR)

print(f'Creating waveforms for fs={fs} / rr={rr}')

num_points = int(np.floor(180.0 / rr * fs) + 1)

print("Test cases:", num_test_cases)
print("Number points: ", num_points)

# Target waveforms
flow = np.zeros((num_points, num_test_cases))
volume = np.zeros((num_points, num_test_cases))
paw = np.zeros((num_points, num_test_cases))
pmus = np.zeros((num_points, num_test_cases))
ins = np.zeros((num_points, num_test_cases))
resistances_ins = np.zeros((1, num_test_cases))
resistances_exp = np.zeros((1, num_test_cases))
capacitances = np.zeros((1, num_test_cases))

t = time.time()

for i in range(num_test_cases):
    if i % 500 == 0:
        print('%d/%d' % (i, num_test_cases))
    (flow[:, i], volume[:, i], paw[:, i], pmus[:, i], ins[:, i], rins, rexp,
     c) = solve_model(header_params, param[i], header_features, feature[i], '')
示例#12
0
capacitances = capacitances.T

print("transposed")

num_examples = flow.shape[0]
num_samples = flow.shape[1]

(min_flow, max_flow, flow) = normalize_data(flow)
(min_volume, max_volume, volume) = normalize_data(volume)
(min_paw, max_paw, paw) = normalize_data(paw)
(min_resistance, max_resistance, resistances) = normalize_data(resistances)
(min_capacitance, max_capacitance, capacitances) = normalize_data(capacitances)

print("normalized data")

input_data = np.zeros((num_examples, num_samples, 3))
input_data[:, :, 0] = flow
input_data[:, :, 1] = volume
input_data[:, :, 2] = paw
output_data = np.concatenate((resistances, capacitances), axis=1)
indices = np.arange(num_examples)

print("input created")


input_train, input_test, output_train, output_test, indices_train, indices_test = \
    train_test_split(input_data, output_data, indices, test_size=0.3, shuffle=False)

input_validation, input_test, output_validation, output_test, indices_validation, indices_test = \
    train_test_split(input_test, output_test, indices_test, test_size=0.5, shuffle=False)
示例#13
0
# gc.collect()
# print(gc.get_count())

# Mask with tiles
# Divide tiles in 5 rows and 3 columns. Total = 15 tiles
tile_number = np.ones((1220,2200))
mask_c_1 = np.concatenate((tile_number, 2*tile_number, 3*tile_number), axis=1)
mask_c_2 = np.concatenate((4*tile_number, 5*tile_number, 6*tile_number), axis=1)
mask_c_3 = np.concatenate((7*tile_number, 8*tile_number, 9*tile_number), axis=1)
mask_c_4 = np.concatenate((10*tile_number, 11*tile_number, 12*tile_number), axis=1)
mask_c_5 = np.concatenate((13*tile_number, 14*tile_number, 15*tile_number), axis=1)
mask_tiles = np.concatenate((mask_c_1, mask_c_2, mask_c_3 , mask_c_4, mask_c_5), axis=0)

#%% Test model
# Creation of mask with test tiles
mask_ts_ = np.zeros((mask_tiles.shape))
ts1 = 1
ts2 = 2
ts3 = 3
ts4 = 4
ts5 = 6
ts6 = 9
ts7 = 12
ts8 = 14
ts9 = 15
mask_ts_[mask_tiles == ts1] = 1
mask_ts_[mask_tiles == ts2] = 1
mask_ts_[mask_tiles == ts3] = 1
mask_ts_[mask_tiles == ts4] = 1
mask_ts_[mask_tiles == ts5] = 1
mask_ts_[mask_tiles == ts6] = 1