示例#1
0
    n_images = np.min((seg_pred.shape[0], max_n_images))
    for i in range(n_images):
        seg_pred[i][0, :6] = np.array([0,1,2,3,4,5])
        seg[i,0,0,:6] = np.array([0,1,2,3,4,5])
        plt.subplot(n_images, 2, 2*i+1)
        plt.imshow(seg[i, 0])
        plt.subplot(n_images, 2, 2*i+2)
        plt.imshow(seg_pred[i])
    plt.savefig(os.path.join(results_dir, "some_segmentations_ep_%d.png"%epoch))


while epoch < 50:
    data_gen_train = SegmentationBatchGeneratorBraTS2014(all_patients, BATCH_SIZE, validation_patients, PATCH_SIZE=(180, 164), mode="train", ignore=[81], losses=losses, num_batches=1500, seed=None)
    data_gen_train = seg_channel_selection_generator(data_gen_train, [2])
    data_gen_train = rotation_generator(data_gen_train)
    data_gen_train = elastric_transform_generator(data_gen_train, 200., 14.)
    data_gen_train = pad_generator(data_gen_train, PATCH_SIZE)
    data_gen_train = center_crop_seg_generator(data_gen_train, (180, 164))
    data_gen_train = Multithreaded_Generator(data_gen_train, 8, 50)
    data_gen_train._start()
    print "epoch: ", epoch
    train_loss = 0
    train_acc_tmp = 0
    train_loss_tmp = 0
    batch_ctr = 0
    for data, seg, idx in data_gen_train:
        if batch_ctr != 0 and batch_ctr % int(np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) == 0:
            print "number of batches: ", batch_ctr, "/", n_batches_per_epoch
            print "training_loss since last update: ", train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch), " train accuracy: ", train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)
            all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
示例#2
0
    avg_loss = np.mean(losses[:, 1])
    loss_new = (loss_old + losses/avg_loss) / 2.
    return loss_new'''

def update_losses(losses, idx, loss):
    losses[idx] = (losses[idx] + loss*2.) / 3.
    return losses

n_epochs = 30
auc_scores=None
for epoch in range(14,n_epochs):
    data_gen_train = memmapGenerator_allInOne_segmentation_lossSampling(memmap_data, memmap_gt, BATCH_SIZE, validation_patients, mode="train", ignore=[40], losses=losses)
    data_gen_train = seg_channel_selection_generator(data_gen_train, [2])
    data_gen_train = rotation_generator(data_gen_train)
    data_gen_train = center_crop_generator(data_gen_train, (PATCH_SIZE, PATCH_SIZE))
    data_gen_train = elastric_transform_generator(data_gen_train, 550., 20.)
    data_gen_train = Multithreaded_Generator(data_gen_train, 12, 100)
    data_gen_train._start()
    print "epoch: ", epoch
    train_loss = 0
    train_acc_tmp = 0
    train_loss_tmp = 0
    batch_ctr = 0
    for data, seg, idx in data_gen_train:
        if batch_ctr != 0 and batch_ctr % int(np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) == 0:
            print "number of batches: ", batch_ctr, "/", n_batches_per_epoch
            print "training_loss since last update: ", train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch), " train accuracy: ", train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)
            all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            train_loss_tmp = 0
            train_acc_tmp = 0
    n_images = np.min((seg_pred.shape[0], max_n_images))
    for i in range(n_images):
        seg_pred[i][0, :6] = np.array([0,1,2,3,4,5])
        seg[i,0,0,:6] = np.array([0,1,2,3,4,5])
        plt.subplot(n_images, 2, 2*i+1)
        plt.imshow(seg[i, 0])
        plt.subplot(n_images, 2, 2*i+2)
        plt.imshow(seg_pred[i])
    plt.savefig(os.path.join(results_dir, "some_segmentations_ep_%d.png"%epoch))


while epoch < n_epochs:
    data_gen_train = SegmentationBatchGeneratorDavid(all_patients, BATCH_SIZE, validation_patients, PATCH_SIZE=OUTPUT_PATCH_SIZE, mode="train", ignore=[81], losses=losses, num_batches=None, seed=None)
    data_gen_train = seg_channel_selection_generator(data_gen_train, [2])
    data_gen_train = rotation_generator(data_gen_train)
    data_gen_train = elastric_transform_generator(data_gen_train, 200., 14.)
    data_gen_train = pad_generator(data_gen_train, INPUT_PATCH_SIZE)
    data_gen_train = center_crop_seg_generator(data_gen_train, OUTPUT_PATCH_SIZE)
    data_gen_train = Multithreaded_Generator(data_gen_train, 8, 16)
    data_gen_train._start()
    print "epoch: ", epoch
    train_loss = 0
    train_acc_tmp = 0
    train_loss_tmp = 0
    batch_ctr = 0
    for data, seg, idx in data_gen_train:
        if batch_ctr != 0 and batch_ctr % int(np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) == 0:
            print "number of batches: ", batch_ctr, "/", n_batches_per_epoch
            print "training_loss since last update: ", train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch), " train accuracy: ", train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)
            all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
    loss_new = (loss_old + losses/avg_loss) / 2.
    return loss_new'''

losses = np.ones(len(memmap_gt))
def update_losses(losses, idx, loss):
    losses[idx] = (losses[idx] + loss*2.) / 3.
    return losses

n_epochs = 40
auc_scores=None
for epoch in range(0,n_epochs):
    data_gen_train = memmapGenerator_allInOne_segmentation_lossSampling(memmap_data, memmap_gt, BATCH_SIZE, validation_patients, mode="train", ignore=[40], losses=losses)
    data_gen_train = seg_channel_selection_generator(data_gen_train, [2])
    data_gen_train = rotation_generator(data_gen_train)
    data_gen_train = center_crop_generator(data_gen_train, (PATCH_SIZE, PATCH_SIZE))
    data_gen_train = elastric_transform_generator(data_gen_train, 550., 20.)
    data_gen_train = Multithreaded_Generator(data_gen_train, 12, 100)
    data_gen_train._start()
    print "epoch: ", epoch
    train_loss = 0
    train_acc_tmp = 0
    train_loss_tmp = 0
    batch_ctr = 0
    for data, seg, idx in data_gen_train:
        if batch_ctr != 0 and batch_ctr % int(np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) == 0:
            print "number of batches: ", batch_ctr, "/", n_batches_per_epoch
            print "training_loss since last update: ", train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch), " train accuracy: ", train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)
            all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            train_loss_tmp = 0
            train_acc_tmp = 0
tmp = SegmentationBatchGeneratorFromRawData(all_patients, BATCH_SIZE, validation_patients, PATCH_SIZE=PATCH_SIZE, mode="train", ignore=[40], losses=None, num_batches=None, seed=None)

losses = np.ones(tmp.get_losses().shape[0])
def update_losses(losses, idx, loss):
    losses[idx] = (losses[idx] + loss*2.) / 3.
    return losses

n_epochs = 30
auc_scores=None
for epoch in range(0,n_epochs):
    data_gen_train = SegmentationBatchGeneratorFromRawData(all_patients, BATCH_SIZE, validation_patients, PATCH_SIZE=PATCH_SIZE, mode="train", ignore=[40], losses=losses, num_batches=1500, seed=None)
    data_gen_train = seg_channel_selection_generator(data_gen_train, [2])
    data_gen_train = rotation_generator(data_gen_train)
    # data_gen_train = center_crop_generator(data_gen_train, (PATCH_SIZE, PATCH_SIZE))
    data_gen_train = elastric_transform_generator(data_gen_train, 450., 16.)
    data_gen_train = Multithreaded_Generator(data_gen_train, 12, 100)
    data_gen_train._start()
    print "epoch: ", epoch
    train_loss = 0
    train_acc_tmp = 0
    train_loss_tmp = 0
    batch_ctr = 0
    for data, seg, idx in data_gen_train:
        if batch_ctr != 0 and batch_ctr % int(np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) == 0:
            print "number of batches: ", batch_ctr, "/", n_batches_per_epoch
            print "training_loss since last update: ", train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch), " train accuracy: ", train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)
            all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch))
            train_loss_tmp = 0
            train_acc_tmp = 0