Example #1
0
def train_unet():
    trainSet = 'data/train_0207_normalised.h5'  #Define dataset for training
    testSet = 'data/test_0207_normalised.h5'  #Define dataset for testing during training

    bSize = int(10)
    print("bSize = %d" % bSize)

    trainIter = 400000  # DeepDbar typically was 200,000
    checkPointInterval = 10000  # In which intervals should we save the network parameter
    useTensorboard = True  # Use Tensorboard for tracking (need to setup tensorboard and change path)
    filePath = 'D://neural_net/run_030720/'  # Where to save the network, can be absolute or relative

    lossFunc = 'l2_loss'  #or 'l1_loss'
    unetType = 'resUnet'  #or 'Unet'

    experimentName = 'EIT_Continuous_' + unetType + '_' + lossFunc + '_test'  #Name for this experiment

    dataSet = UNet.read_data_sets(trainSet, testSet, bSize)

    UNet.train(dataSet,
               trainSet,
               testSet,
               filePath=filePath,
               experimentName=experimentName,
               useTensorboard=useTensorboard,
               trainIter=trainIter,
               checkpointInterval=checkPointInterval)
    def __init__(self, model_file='put_your_model_file_name_here'):
        # You should
        #       1. create the model object
        #       2. load your state_dict
        #       3. call cuda()
        # self.model = ...
        #

        self.batch_size = 1

        # set device
        self.device = torch.device(
            "cuda:0" if torch.cuda.is_available() else "cpu")

        #### model 1: predict Binary RoadMap ####
        self.model1 = UNet(in_channel=1, out_channel=1).to(self.device)
        self.model1.load_state_dict(
            torch.load('best_val_loss_road_map_labeleddata.pt',
                       map_location=self.device))
        # TODO: self.model1.load_state_dict(torch.load('classification.pth', map_location=self.device))

        #### model 2: predict Bounding Boxes ####
        self.model2 = BoundingBox().to(self.device)
        # TODO: self.model2.load_state_dict(torch.load('classification.pth', map_location=self.device))
        pass
Example #3
0
def ValidRed2D(testloader, Bone, Network, path):
    dice_value = []

    if Network == 'UNet':
        net = UNet().to(device)
    elif Network == 'ResNet':
        net = ResNet(BasicBlock, [3, 4, 6]).to(device)
    net.load_state_dict(torch.load(path))
    for i, data in enumerate(testloader, 0):
        inputs, labels = data
        inputs = inputs.to(device)
        labels = labels.to(device)

        outputs = net(inputs)
        dice = 0.0
        pr = labels[0].cpu().detach().numpy()
        gt = outputs[0].cpu().detach().numpy()
        gt[0, :, :][gt[0, :, :] <= 0.5] = 0
        gt[0, :, :][gt[0, :, :] > 0.5] = 1
        dice = dice + np.abs(
            computeQualityMeasures(pr[0, :, :].flatten(),
                                   gt[0, :, :].flatten()))

        if (i == 1600):
            plt.figure()
            plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(),
                       cmap=plt.cm.gray,
                       interpolation="nearest",
                       vmin=-3,
                       vmax=2)
            plt.imshow(outputs[0, 0, :, :].cpu().detach().numpy(),
                       'OrRd',
                       interpolation='none',
                       alpha=0.4)
            plt.savefig(os.path.join(
                ResultsDirectory, Bone, 'Images',
                'patches_' + Network + '_' + Bone + str(i) + '.png'),
                        dpi=150)

            plt.figure()
            plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(),
                       cmap=plt.cm.gray,
                       interpolation="nearest",
                       vmin=-3,
                       vmax=2)
            plt.imshow(labels[0, 0, :, :].cpu().detach().numpy(),
                       'OrRd',
                       interpolation='none',
                       alpha=0.4)
            plt.savefig(os.path.join(
                ResultsDirectory, Bone, 'Images',
                'patches_Truth_' + Network + '_' + Bone + str(i) + '.png'),
                        dpi=150)
        dice_value.append(dice)

    np.savetxt(os.path.join(ResultsDirectory, Bone, 'DICE_test.txt'),
               dice_value)
    print('Standard Deviation:' + str(statistics.stdev(dice_value)))
    print('Mean:' + str(statistics.mean(dice_value)))
Example #4
0
 def load(size,modelName):
     """function to load the images
     size -- tuple for the image dimensions required by the model
     modelName -- name for the model
     """
     if not os.path.exist(savePath):
     model = UNet((size[0],size[1],1))
     model.load_weights(modelName)
     return model
def ValidRed3D(testloader, Bone, Network, path, sujet):
    volume3D_terrain = []
    volume3D_exp = []

    if Network == 'UNet':
        net = UNet().to(device)
    elif Network == 'ResNet':
        net = ResNet(BasicBlock, [3, 4, 6]).to(device)
    net.load_state_dict(torch.load(path))

    for i, data in enumerate(testloader, 0):
        inputs, labels = data
        inputs = inputs.to(device)
        labels = labels.to(device)

        outputs = net(inputs)
        gt = outputs[0].cpu().detach().numpy()
        gt[0, :, :][gt[0, :, :] <= 0.5] = 0
        gt[0, :, :][gt[0, :, :] > 0.5] = 1

        volume3D_terrain.append(labels[0, 0].cpu().detach().numpy())
        volume3D_exp.append(gt[0])

        if i == 30:
            plt.figure()
            plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(),
                       'gray',
                       interpolation='none')
            plt.imshow(outputs[0, 0, :, :].cpu().detach().numpy(),
                       'OrRd',
                       interpolation='none',
                       alpha=0.6)
            plt.savefig(os.path.join(
                ResultsDirectory, Bone, 'Images',
                '2DSlices_' + Network + '_' + Bone + sujet + str(i) + '.png'),
                        dpi=150)

            plt.figure()
            plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(),
                       'gray',
                       interpolation='none')
            plt.imshow(labels[0, 0, :, :].cpu().detach().numpy(),
                       'OrRd',
                       interpolation='none',
                       alpha=0.6)
            plt.savefig(os.path.join(
                ResultsDirectory, Bone, 'Images', '2DSlices_Truth_' + Network +
                '_' + Bone + sujet + str(i) + '.png'),
                        dpi=150)

    volume3D_terrain = np.array(volume3D_terrain)
    volume3D_exp = np.array(volume3D_exp)
    dice = np.abs(
        computeQualityMeasures(volume3D_terrain.flatten(),
                               volume3D_exp.flatten()))
    print('dice:' + str(dice))
Example #6
0
 def __init__(self, **kwargs):
     self.kwargs = kwargs
     self.data = Data(kwargs['h5'])
     self.net = UNet.UNet(3, kwargs["nc"])
     self.net = self.net.cuda()
     if kwargs['MD']:
         dic = torch.load(kwargs['MD'])
         self.net.load_state_dict(dic)
     self.loss = UNet.FocalSmoothLoss(kwargs['nc'], kwargs['sm'], kwargs['gm'], kwargs['wt'])
     self.opt = torch.optim.SGD(self.net.parameters(), lr = self.kwargs['lr'], momentum = 0.9, nesterov = True, weight_decay = kwargs['wd'])
     self.sch = torch.optim.lr_scheduler.ReduceLROnPlateau(self.opt, "min", patience = 3)
     self.losses = {"train": [], "val": []}
Example #7
0
def evaluate_unet(number=240000, index=None):
    testSet = './data/test_0207_normalised.h5'
    filePath = 'D://neural_net/run_030720/EIT_Continuous_resUnet_l2_loss_test_' + str(
        number) + '.h5'

    lossFunc = 'l2_loss'  #or 'l1_loss'
    unetType = 'resUnet'  #or 'Unet'

    experimentName = 'EIT_Continuous_' + unetType + '_' + lossFunc + '_test'  #Name for this experiment

    if index is None:
        h = UNet.h5py.File(testSet, 'r')
        reconstructions = h['train_data'][()]
        true = h['truth_data'][()]
        h.close()
        evaluation = UNet.test(testSet, filePath, experimentName)
        print('number =', number)
        print(evaluation)
    else:
        recs = UNet.get_rec_from_file(testSet, index)
        outputs = UNet.process_image(recs, filePath)
        h = UNet.h5py.File(testSet, 'r')
        reconstructions = h['train_data'][()]
        true = h['truth_data'][()]
        h.close()

        true = true.reshape(-1, 64, 64)
        reconstructions = reconstructions.reshape(-1, 64, 64)
        outputs = outputs.reshape(64, 64)
        plt.figure(1)
        im1 = plt.imshow(true[index],
                         cmap=plt.cm.viridis,
                         origin='lower',
                         extent=[-1, 1, -1, 1])
        plt.colorbar(im1)
        #plt.show()
        plt.figure(2)
        im2 = plt.imshow(reconstructions[index],
                         cmap=plt.cm.viridis,
                         origin='lower',
                         extent=[-1, 1, -1, 1])
        plt.colorbar(im2)

        plt.figure(3)
        im3 = plt.imshow(outputs,
                         cmap=plt.cm.viridis,
                         origin='lower',
                         extent=[-1, 1, -1, 1])
        plt.colorbar(im3)
        plt.show()
Example #8
0
    def InitializeNetwork(self):
        self.vgg = Network.vgg16_features(cuda_id=self.cuda_id[0])
        self.stn = Network.STN_Flow_relative(size=self.SIZE,
                                             cuda_id=self.cuda_id[0])
        self.stn_destroy = Network.STN_Flow_relative(size=self.SIZE,
                                                     cuda_id=self.cuda_id[0])

        self.models = {}
        self.models['seg_feat_src'] = Network.DRIU_novgg_siamese_feat(
            with_relu=False, cuda_id=self.cuda_id[0])
        self.models['seg_feat_tgt'] = Network.DRIU_novgg_siamese_feat(
            with_relu=False, cuda_id=self.cuda_id[0])
        self.models['seg_pred'] = Network.DRIU_novgg_siamese_seg(
            with_relu=True, with_sigmoid=True, cuda_id=self.cuda_id[0])
        self.models['flow'] = UNet.UNetFlow(
            down_scales=self.opt.flow_feat_scales,
            output_scale=self.opt.flow_scale_times,
            num_filters_base=self.opt.flow_base_filters,
            max_filters=self.opt.flow_max_filters,
            input_channels=128,
            output_channels=2,
            downsampling=self.opt.flow_downsample,
            cuda_id=self.cuda_id[1])

        for k in self.models.keys():
            print(self.models[k])
        for k in self.models.keys():
            num_params = 0
            for param in self.models[k].parameters():
                num_params += param.numel()
            print('[Network %s] Total number of parameters : %.3f M' %
                  (k, num_params / 1e6))
Example #9
0
def start(args):
    # listing out hyperparameters
    hyperparameters = {}
    hyperparameters['name'] = args[0]
    hyperparameters['device'] = torch.device('cuda:' + args[1])
    hyperparameters['epochs'] = int(args[2])
    hyperparameters['batch_size'] = int(args[3])
    hyperparameters['augument_factor'] = int(args[4])
    hyperparameters['model_depth'] = int(args[5])
    hyperparameters['nonlinear_activation'] = args[6]  # 'elu' or 'relu'
    hyperparameters['dropout_rate'] = float(args[7])
    hyperparameters['learning_rate'] = float(args[8])
    hyperparameters['optimizer'] = args[9]  # 'adam' or 'sgd'
    hyperparameters['aug_tricks'] = args[10]  #0/1/2 = none/standard/all

    masks = pickle.load(open("cache/masks.p", "rb"))
    oowl = pickle.load(open("cache/oowl.p", "rb"))

    train_keys = pickle.load(open("cache/train_keys.p", "rb"))
    val_keys = pickle.load(open("cache/val_keys.p", "rb"))
    test_keys = pickle.load(open("cache/test_keys.p", "rb"))

    unet = UNet(hyperparameters)
    trainer = Trainer(unet, hyperparameters, train_keys, val_keys, oowl, masks)
    trainer.train()
Example #10
0
def predict(image_path):
    model = UNet.UNet(input_channels=3)
    model.load_state_dict(torch.load(CONFIG.MODEL_PATH))
    model.eval()

    image = np.array(Image.open(image_path).convert('RGB'))
    mean = CONFIG.mean
    std = CONFIG.std

    transforms = alb.Compose([
        alb.Normalize(mean, std, always_apply=True),
        alb.Resize(512, 512, always_apply=True)
    ])

    image_ = transforms(image=image)['image']
    image_ = torch.tensor(image_).unsqueeze(0)
    image_ = image_.permute(0, 3, 1, 2)

    with torch.no_grad():
        prediction = model(image_)
    prediction = prediction.squeeze(0).squeeze(0)
    prediction = torch.sigmoid(prediction)
    prediction = (prediction > CONFIG.pred_threshold) * 1
    prediction = prediction.detach().cpu().numpy()

    print('-- SAVING IMAGE --\n')

    image = cv2.resize(image, (prediction.shape[-1], prediction.shape[-1]))
    image[:, :, 1] = image[:, :, 1] * (1 - prediction)

    cv2.imwrite('output/segmented.jpg', image)
Example #11
0
def main(args):
    ckpt_path = os.path.join(args.output_path, "Checkpoint")
    log_path = os.path.join(args.output_path, "Log")

    # this is just for my path, remember to change if you use your own dir
    check_dir("../output/")
    check_dir(args.output_path)
    check_dir(log_path)
    check_dir(ckpt_path)

    torch.cuda.set_device(args.gpu_id)
    train_list, test_list = create_list(args.data_path, ratio=args.train_ratio)

    # define the dataset and loader
    train_set = MySet(train_list)
    train_loader = DataLoader(train_set,
                              batch_size=args.batch_size,
                              shuffle=True)
    test_set = MySet(test_list)
    test_loader = DataLoader(test_set, batch_size=1, shuffle=False)

    # define the network and load the init weight
    net = UNet.UNet().cuda()
    if args.is_load:
        net.load_state_dict(torch.load(args.load_path))

    # define the optimizer of the training process
    optimizer = torch.optim.Adam(
        net.parameters(),
        lr=args.lr,
    )

    # define the loss function
    cost = torch.nn.BCELoss()
    best_dice = 0.
    for epoch in range(args.init_epoch, args.init_epoch + args.num_epoch):
        start_time = time.time()
        # train one epoch
        epoch_loss = train_epoch(net, train_loader, optimizer, cost)
        epoch_time = time.time() - start_time
        # eval in test data after one epoch training
        epoch_dice = test_epoch(net, test_loader)

        info_line = "Epoch {} || Loss: {:.4f} | Time: {:.2f} | Test Dice: {:.4f} ".format(
            str(epoch).zfill(3), epoch_loss, epoch_time, epoch_dice)
        print(info_line)
        open(os.path.join(log_path, 'train_log.txt'),
             'a').write(info_line + '\n')

        # save the checkpoint
        torch.save(net.state_dict(),
                   os.path.join(ckpt_path, "Network_{}.pth.gz".format(epoch)))
        if epoch_dice > best_dice:
            best_dice = epoch_dice
            torch.save(net.state_dict(),
                       os.path.join(ckpt_path, "Best_Dice.pth.gz"))
def predict_whole_dataset(patient_id):
    t1km_img, flair_img, adc_img, cbv_img, t1km_downsampled, seg_combined = load_patient_resampled(patient_id)
    import matplotlib.pyplot as plt
    assert t1km_img.shape == flair_img.shape
    assert t1km_img.shape == adc_img.shape
    assert t1km_img.shape == cbv_img.shape
    shape_0_new = t1km_img.shape[1] - t1km_img.shape[1]%16
    shape_1_new = t1km_img.shape[2] - t1km_img.shape[2]%16
    t1km_img = t1km_img[:, :shape_0_new, :shape_1_new]
    flair_img = flair_img[:, :shape_0_new, :shape_1_new]
    adc_img = adc_img[:, :shape_0_new, :shape_1_new]
    cbv_img = cbv_img[:, :shape_0_new, :shape_1_new]
    seg_combined = seg_combined[:, :shape_0_new, :shape_1_new]

    # uild_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), base_n_filters=64, do_dropout=False):
    net = UNet.build_UNet(20, 1, 5, input_dim=(t1km_img.shape[1], t1km_img.shape[2]), base_n_filters=16)
    output_layer = net["output_flattened"]

    with open("../../../results/segment_tumor_v0.1_Unet_Params_ep1.pkl", 'r') as f:
        params = cPickle.load(f)
        lasagne.layers.set_all_param_values(output_layer, params)

    import theano.tensor as T
    data_sym = T.tensor4()

    output = lasagne.layers.get_output(output_layer, data_sym, deterministic=False)
    pred_fn = theano.function([data_sym], output)

    cmap = ListedColormap([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (1,1,0)])

    z = 2
    data = np.zeros((1, 20, t1km_img.shape[1], t1km_img.shape[2])).astype(np.float32)
    res = np.zeros((t1km_img.shape[0], t1km_img.shape[1] - t1km_img.shape[1]%16, t1km_img.shape[2] - t1km_img.shape[2]%16))
    while z < t1km_img.shape[0]-2:
        data[0,0:5] = t1km_img[z-2:z+3]
        data[0,5:10] = flair_img[z-2:z+3]
        data[0,10:15] = adc_img[z-2:z+3]
        data[0,15:20] = cbv_img[z-2:z+3]
        pred = pred_fn(data).argmax(-1).reshape((t1km_img.shape[1] - t1km_img.shape[1]%16, t1km_img.shape[2] - t1km_img.shape[2]%16))
        res[z] = pred
        z += 1

    for i in xrange(t1km_img.shape[0]):
        res[i][0,0:5] = [0,1,2,3,4]
        seg_combined[i][0,0:5] = [0,1,2,3,4]
        plt.figure(figsize=(18,6))
        plt.subplot(1,3,1)
        plt.imshow(res[i], cmap=cmap)
        plt.subplot(1,3,2)
        plt.imshow(seg_combined[i], cmap=cmap)
        plt.subplot(1,3,3)
        plt.imshow(seg_combined[i] == res[i], cmap="gray")
        plt.savefig("../../../some_images/segWholeDataset_z%03.0f"%i)
        plt.close()
    return res
def main():
    pytorch_model = UNet.UNet(1, 1, start_filts=16, depth=2)
    saved_params = torch.load('../unet_2/t7/epoch_89.pth')
    pytorch_model.load_state_dict(saved_params['state_dict'])

    test_image = np.load('../pytorch_keras/test.npy').astype(np.float32)
    x = Variable(
        torch.from_numpy(test_image[np.newaxis, np.newaxis, 12:, :-14]))

    out = pytorch_model(x)
    pytorch2caffe(x, out, 'pillnet.prototxt', 'pillnet.caffemodel')
Example #14
0
    def __init__(self, output_folder, in_channels=1, out_channels=2, depth=5, epochs=350, batch_size=96,
                    lr=0.001, do_data_augmentation=0, cuda=False,
                    lr_decay=100, previous=False, number_filter=4, size=128, step=128):
        # Assign member variables
        self.output_folder = output_folder
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.depth = depth
        self.epochs = epochs
        self.batch_size = batch_size
        self.lr = lr
        self.data_aug = do_data_augmentation
        self.cuda = cuda
        self.lrDecay = lr_decay
        self.number_filter = number_filter
        self.size = size
        self.step = step

        # Creation of the output folder
        try :
            os.makedirs(self.output_folder, exist_ok=False)
            pickle.dump(vars(self), open(os.path.join(self.output_folder, "params_trainer.pkl"), "wb"))
        except OSError as err:
            print("The name of the folder already exist! Try changing the name of the folder.")
            print("OSError", err)
            exit()

        # Creation of the network
        self.network = UNet.UNet(in_channels=self.in_channels, out_channels=self.out_channels,
                            number_filter=self.number_filter, depth=self.depth, size=self.size)
        if self.cuda:
            self.network = self.network.cuda()

        # Creation of the optimizer
        self.optimizer = torch.optim.Adam(self.network.parameters(), lr=self.lr)

        # To keep track of the statistics
        self.stats = defaultdict(list)

        # Creation of the criterion
        self.criterion = torch.nn.CrossEntropyLoss()
        if self.cuda:
            self.criterion = self.criterion.cuda()

        # To keep track of the network generalizing the most
        self.min_valid_loss = numpy.inf
Example #15
0
def main(args):
    annotations = pd.read_csv(args.a)
    directory = args.d
    output = args.o

    k = int(args.k)
    if k > 600:
        k = 600

    patient_ids = list(set(annotations["seriesuid"]))
    size = 128

    random.shuffle(patient_ids)

    lungs, masks = load_images(patient_ids, directory, size=128, k=k)

    print(lungs.shape)
    print(masks.shape)
    print(output)

    lungs = np.array([lungs]).transpose((1, 2, 3, 0))
    masks = np.array([masks]).transpose((1, 2, 3, 0))

    train_split = len(patient_ids) * 8 // 10
    train_split *= k

    lungs_train, masks_train = lungs[:train_split], masks[:train_split]
    lungs_test, masks_test = lungs[train_split:], masks[train_split:]

    model = UNet.get_unet(size, size)
    model_checkpoint = ModelCheckpoint('{}/weights_checkpoint.hdf5'.format(output),
                                       monitor='val_loss',
                                       save_best_only=True)
    model.fit(lungs_train, masks_train,
              batch_size=32,
              epochs=20,
              verbose=1,
              shuffle=True,
              validation_data=(lungs_test, masks_test),
              callbacks=[model_checkpoint])
    model.save_weights("{}/learned_weights.hdf5".format(output))
Example #16
0
def initialNetGenerator(ks, fm, train_loader):
    good_net = False
    while not good_net:
        net = UNet(ks, fm, show=False).cuda(1)
        net.apply(weightInitialization)
        net.train()
        list_of_booleans = []
        for img, label in train_loader:
            img, label = tensor_format(img), tensor_format(label)
            output = net(img)
            output, label = crop(output, label)
            cell_prob_mean = getCellProb(output).mean()
            # cell_prob_mean = getSigmoidProb(output).mean()
            diff = abs(cell_prob_mean - 0.5)
            list_of_booleans.append(diff > 0.2)
        outlier = any(list_of_booleans)
        if outlier:
            pass
        else:
            # print("One Initial Cell Probability: ",cell_prob_mean)
            return net
Example #17
0
                     fill_mode='nearest')
myGene = DataAugmentation.trainGenerator(2,
                                         'Data',
                                         'image',
                                         'label',
                                         data_gen_args,
                                         save_to_dir=None)

filters = [[8, 16, 32, 64, 128], [16, 32, 64, 128, 256],
           [32, 64, 128, 256, 512]]

Test_X = Train_X[-3:]
for filter in filters:
    print(filter)

    model = UNet.UNet(filters=filter)
    for use_aug in [True, False]:
        file_name = str(filter)[1:-1].replace(', ', '-')
        if use_aug:
            history = model.fit_generator(myGene,
                                          epochs=2,
                                          steps_per_epoch=300)
            file_name = file_name + '_Aug'
        else:
            history = model.fit(Train_X,
                                Train_Y,
                                validation_split=0.2,
                                epochs=50,
                                batch_size=1)

        result = model.predict(Test_X)
Example #18
0
def main(args):
    model_id = str(time.time())
    if args.s:
        save_path = '/home/fast_seg/FastSeg/model_checkpoints/' + model_id
        os.mkdir(save_path)
    else:
        save_path = './'
    print("Saving all output to ", save_path)

    if args.stdout:
        print("Routing stdout to " + save_path + "/console.txt")
        sys.stdout = open(save_path + "/console.txt", "w")
    print(args)

    minibatch_size = 64
    num_classes = NUM_CLASSES

    seed = 1
    #class_weights = classes.getWeights()
    class_weights = args.w
    class_weights = torch.tensor(class_weights,
                                 dtype=torch.float,
                                 requires_grad=False,
                                 device=device)
    learning_rate = args.lr
    bn = 0
    dp = 0.1
    wd = 0

    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)

    dset_train = coco_custom_Dataset(train_dir, length=args.n)
    dset_val = coco_custom_Dataset(val_dir, length=args.nval)
    #dset_val = coco_custom_Dataset(train_dir, length=args.nval)

    train_loader = DataLoader(dset_train,
                              batch_size=minibatch_size,
                              shuffle=True,
                              num_workers=0)
    val_loader = DataLoader(dset_val,
                            batch_size=minibatch_size,
                            shuffle=False,
                            num_workers=0)

    if args.m == 0:
        model = UNet.UNet(in_channel=3,
                          num_classes=num_classes,
                          start_filters=args.f,
                          num_batchnorm_layers=bn,
                          dropout=dp)
    elif args.m == 1:
        model = U_Net_separable.U_net_separable(in_channel=3,
                                                num_classes=num_classes,
                                                start_filters=args.f,
                                                num_batchnorm_layers=bn,
                                                dropout=dp)
    elif args.m == 2:
        model = UNetSep2.UNetSep2(in_channel=3,
                                  num_classes=num_classes,
                                  start_filters=args.f,
                                  num_batchnorm_layers=bn,
                                  dropout=dp)
    elif args.m == 3:
        model = UNetSep3.UNetSep3(in_channel=3,
                                  num_classes=num_classes,
                                  start_filters=args.f,
                                  dropout=dp,
                                  nlayers=args.nl,
                                  conv_type=args.convtype)
    model.to(device)

    print("Model parameters (thousands): ", count_parameters(model) / 1000)

    optimizer = optim.Adam(model.parameters(),
                           lr=learning_rate,
                           weight_decay=wd)
    train_model(model,
                optimizer,
                train_loader,
                class_weights,
                val_loader,
                save_path,
                epochs=args.e,
                do_save=args.s)
Example #19
0
    assert t1km_img.shape == flair_img.shape
    assert t1km_img.shape == adc_img.shape
    assert t1km_img.shape == cbv_img.shape
    shape_0_new = t1km_img.shape[1] - t1km_img.shape[1] % 16
    shape_1_new = t1km_img.shape[2] - t1km_img.shape[2] % 16
    t1km_img = t1km_img[:, :shape_0_new, :shape_1_new]
    flair_img = flair_img[:, :shape_0_new, :shape_1_new]
    adc_img = adc_img[:, :shape_0_new, :shape_1_new]
    cbv_img = cbv_img[:, :shape_0_new, :shape_1_new]
    seg_combined = seg_combined[:, :shape_0_new, :shape_1_new]

    print "compiling theano functions"
    # uild_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), base_n_filters=64, do_dropout=False):
    net = UNet.build_UNet(20,
                          1,
                          5,
                          input_dim=(t1km_img.shape[1], t1km_img.shape[2]),
                          base_n_filters=16)
    output_layer = net["output_segmentation"]
    with open(
            os.path.join(
                results_folder,
                "%s_allLossesNAccur_ep%d.pkl" % (experiment_name, epoch)),
            'r') as f:
        tmp = cPickle.load(f)

    with open(
            os.path.join(results_folder,
                         "%s_Params_ep%d.pkl" % (experiment_name, epoch)),
            'r') as f:
        params = cPickle.load(f)
Example #20
0
# general network training
parser.add_argument('--device', type=int, default=0)

parser.add_argument('--imgNormIn', type=float, default=0.15)
parser.add_argument('--imgOffsetIn', type=float, default=-1)

parser.add_argument('--imgNormOut', type=float, default=0.025)
parser.add_argument('--imgOffsetOut', type=float, default=0)


# In[16]:


tf.reset_default_graph()
net = UNet.UNet()
parser = net.AddArgsToArgParser(parser)


# In[17]:


if sys.argv[0] != 'TestNetwork.py':
    from IPython import display
    import matplotlib.pyplot as plt
    get_ipython().run_line_magic('matplotlib', 'inline')
    args = parser.parse_args(['--device', '0',
                              '--nTest', '1',
                              '--checkPoint', '/home/dwu/trainData/Noise2Noise/train/ctp/simul/supervised_beta_25_N0_200000/99',
                              '--outFile', '/home/dwu/trainData/Noise2Noise/train/ctp/real/supervised/test'])
else:
Example #21
0
                      (512, 512))
    _, temp = cv2.threshold(temp, 127, 255, cv2.THRESH_BINARY)
    test_label.append(temp)
test_data = np.array(test_data)
test_label = np.array(test_label)


x_test = test_data.astype('float32') / 255.

y_test = test_label.astype('float32') / 255.
x_test = np.reshape(x_test, (len(x_test), 512, 512, 3))  # adapt this if using `channels_first` image data format
y_test = np.reshape(y_test, (len(y_test), 512, 512, 1))  # adapt this if using `channels_first` im


from  UNet import *
model=UNet(input_size=(512,512,3))
weight="Model/Luna/UNet.h5"

if os.path.isfile(weight): model.load_weights(weight)

model_checkpoint = ModelCheckpoint(weight, monitor='val_acc', verbose=1, save_best_only=True)

y_pred = model.predict(x_test)
y_pred_threshold = []
i=0
for y in y_pred:

    _, temp = cv2.threshold(y, 0.5, 1, cv2.THRESH_BINARY)
    y_pred_threshold.append(temp)
    y = y * 255
    cv2.imwrite('./Luna/test/result/%d.png' % i, y)
    nargs='+',
    default=[10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000])

# data augmentation
parser.add_argument('--aug', dest='aug', type=int, default=0)
parser.add_argument('--imgNorm', dest='imgNorm', type=float, default=0.019)
parser.add_argument('--imgOffset', dest='imgOffset', type=float, default=-1)

# window
parser.add_argument('--vmin', dest='vmin', type=float, default=0.84)
parser.add_argument('--vmax', dest='vmax', type=float, default=1.24)

# In[6]:

tf.reset_default_graph()
net = UNet.UNet()
parser = net.AddArgsToArgParser(parser)

# In[7]:

if sys.argv[0] != 'recon_alter_2d.py':
    from IPython import display
    import matplotlib.pyplot as plt
    get_ipython().run_line_magic('matplotlib', 'inline')

    showPlots = True
    args = parser.parse_args([
        '--device',
        '0',
        #                               '--slices', '0', '1',
        '--imgshape',
def load_patient_david(pat_id):
    folder = "/media/fabian/DeepLearningData/datasets/Hirntumor_David_raw_highRes_noAdapt/"
    all_patient_data = {}
    if os.path.isfile(folder + "%03.0d.npy" % pat_id):
        all_data = np.array(np.load(folder + "%03.0d.npy" % pat_id, mmap_mode="c"))
        # t1, t1km, flair, adc, cbv, seg
        return all_data[0], all_data[1], all_data[2], all_data[3], all_data[4], all_data[5]
    else:
        return [None] * 6




print "compiling theano functions"
# uild_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), base_n_filters=64, do_dropout=False):
net = UNet.build_UNet(25, 1, 5, input_dim=INPUT_PATCH_SIZE, base_n_filters=16, pad="valid")
output_layer = net["output_segmentation"]
with open(os.path.join(results_folder, "%s_allLossesNAccur_ep%d.pkl" % (experiment_name, epoch)), 'r') as f:
    tmp = cPickle.load(f)

with open(os.path.join(results_folder, "%s_Params_ep%d.pkl" % (experiment_name, epoch)), 'r') as f:
    params = cPickle.load(f)
    lasagne.layers.set_all_param_values(output_layer, params)

import theano.tensor as T
data_sym = T.tensor4()

output = softmax_helper(lasagne.layers.get_output(output_layer, data_sym, deterministic=False))
pred_fn = theano.function([data_sym], output)

print "predicting image"
Example #24
0
            output = net(img)
            output, label = crop(output, label)

            loss = criterion(output, label)
            loss_list.append(loss.data[0])
            lr_list.append(group['lr'])

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            scheduler.step()

    plt.subplot(1, 2, 1)
    plt.plot(loss_list)
    plt.title("Loss")
    plt.subplot(1, 2, 2)
    plt.plot(lr_list)
    plt.title("Learning Rate")
    plt.show()
    return loss_list, lr_list


if __name__ == '__main__':
    kernel_size = 6
    feature_maps = 16

    net = UNet(kernel_size, feature_maps).cuda(1)
    net.apply(weightInitialization)
    net.train()
    loss_list, lr_list = learningRateFinder(net, 1)
class ModelLoader():
    # Fill the information for your team
    team_name = 'DHL'
    round_number = 1
    team_member = ['Jiayi Du', 'Ziyu Lei', 'Meiyi He']
    contact_email = '@nyu.edu'

    def __init__(self, model_file='put_your_model_file_name_here'):
        # You should
        #       1. create the model object
        #       2. load your state_dict
        #       3. call cuda()
        # self.model = ...
        #

        self.batch_size = 1

        # set device
        self.device = torch.device(
            "cuda:0" if torch.cuda.is_available() else "cpu")

        #### model 1: predict Binary RoadMap ####
        self.model1 = UNet(in_channel=1, out_channel=1).to(self.device)
        self.model1.load_state_dict(
            torch.load('best_val_loss_road_map_labeleddata.pt',
                       map_location=self.device))
        # TODO: self.model1.load_state_dict(torch.load('classification.pth', map_location=self.device))

        #### model 2: predict Bounding Boxes ####
        self.model2 = BoundingBox().to(self.device)
        # TODO: self.model2.load_state_dict(torch.load('classification.pth', map_location=self.device))
        pass

    def get_bounding_boxes(self, samples):
        # samples is a cuda tensor with size [batch_size, 6, 3, 256, 306]
        # You need to return a tuple with size 'batch_size' and each element is a cuda tensor [N, 2, 4]
        # where N is the number of object
        #return torch.rand(1, 15, 2, 4) * 10
        #samples = torch.stack(samples)
        samples = samples.view(self.batch_size, -1, 256, 306)

        # TODO: transform BBOX in the model and return BEV coordinates
        pred_bbox = self.model2(samples.to(self.device))

        #return pred_bbox.view(self.batch_size, -1, 4)
        return torch.rand(1, 15, 2, 4) * 10

    def get_binary_road_map(self, samples):
        # samples is a cuda tensor with size [batch_size, 6, 3, 256, 306]
        # You need to return a cuda tensor with size [batch_size, 800, 800]
        # return torch.rand(1, 800, 800) > 0.5

        # transform samples
        samples = self.combine_images(samples)
        pred_map = self.model1(samples.unsqueeze(1))
        pred_map = (pred_map.squeeze(1) > 0.5).float()
        return pred_map

    def combine_images(self, batch):
        '''
        Given a single sample in size [batch_size, 6, 3, 256, 306], 
        combine them into 1 single image, keep only 1 channel, then resize to 800x800: [batch_size, 800, 800]
        
        Front_left | Front | Front_right
        -------------------------------- 
        Back_left  | Back  | Back_right
        
        and then rotate the entire image so that front is facing right
        (since ego car is always face right)
        
        '''
        imgs = []
        for sample in batch:
            # concatenating images
            front = torch.cat((torch.tensor(sample[0]), torch.tensor(
                sample[1]), torch.tensor(sample[2])), 2)
            back = torch.cat((torch.tensor(sample[3]), torch.tensor(
                sample[4]), torch.tensor(sample[5])), 2)

            # flip by 2 to make it face right
            curr_image = torch.cat((front, back), 1).transpose(2, 1).flip(2)

            # resize the image into 800x800 since binary road map is of size 800x800
            trans = torchvision.transforms.Compose([
                torchvision.transforms.ToPILImage(),
                torchvision.transforms.Resize((800, 800)),
                torchvision.transforms.ToTensor()
            ])

            # apply transformation
            combined = trans(curr_image)
            # append to the return list
            imgs.append(combined.squeeze(0))

            # apply transform function to make it grayscale
            transform = torchvision.transforms.Compose([
                torchvision.transforms.ToPILImage(),
                torchvision.transforms.Grayscale(num_output_channels=1),
                torchvision.transforms.ToTensor(),
                torchvision.transforms.Normalize(mean=[0.5], std=[0.5])
            ])

        return transform(imgs[0])
Example #26
0
def main(_):
    FLAGS = tf.flags.FLAGS
    # gpu config.
    config = tf.compat.v1.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    config.gpu_options.allow_growth = True

    # tf.compat.v1.disable_eager_execution()
    image = tf.compat.v1.placeholder(tf.float32,
                                     shape=[None, 1024, 1024, 1],
                                     name="image")
    mask = tf.compat.v1.placeholder(tf.int32,
                                    shape=[None, 1024, 1024, 1],
                                    name="mask")

    train_model = UNet.inference(image)
    loss = tf.reduce_mean(
        tf.keras.losses.binary_crossentropy(mask, train_model))

    trainable_var = tf.compat.v1.trainable_variables()
    train_op = train(loss, trainable_var)

    saver = tf.compat.v1.train.Saver(max_to_keep=FLAGS.epochs)

    if FLAGS.mode == "train":
        with tf.compat.v1.Session(config=config) as sess:
            ckpt = tf.train.get_checkpoint_state(
                os.path.join(FLAGS.weight_dir, str(FLAGS.threshold)))
            if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                sess.run(tf.compat.v1.global_variables_initializer())

            img_list = sorted(glob.glob(FLAGS.training_set + '/img/*.tif'))
            mask_list = sorted(glob.glob(FLAGS.training_set + '/mask/*.tif'))

            val_img_list = sorted(
                glob.glob(FLAGS.validation_set + '/img/*.tif'))
            val_mask_list = sorted(
                glob.glob(FLAGS.validation_set + '/mask/*.tif'))

            train_data_size = len(img_list)
            train_iterations = int(train_data_size / FLAGS.bs)
            val_data_size = len(val_img_list)

            for cur_epoch in range(FLAGS.epochs):
                # Shuffle
                tmp = [[x, y] for x, y in zip(img_list, mask_list)]
                random.shuffle(tmp)
                img_list = [n[0] for n in tmp]
                mask_list = [n[1] for n in tmp]

                now = datetime.datetime.now()
                print(now)
                print("threshold: ", FLAGS.threshold,
                      " Start training!, epoch: ", cur_epoch)
                for i in range(train_data_size):
                    # print('i : ',i)
                    input_img, input_mask, _, _ = load(img_list, mask_list, i)

                    feed_dict = {image: input_img, mask: input_mask}
                    sess.run(train_op, feed_dict=feed_dict)

                # validation
                if cur_epoch % 1 == 0:
                    avg_loss = 0
                    iou_score_list = []
                    for i in range(val_data_size):
                        input_img, input_mask, _, _ = load(
                            val_img_list, val_mask_list, i)

                        feed_dict = {image: input_img, mask: input_mask}
                        loss1, pred = sess.run([loss, train_model],
                                               feed_dict=feed_dict)
                        avg_loss += loss1

                        # save result
                        pred1 = np.where(pred < FLAGS.threshold, 0.0, 1.0)
                        ttt = Image.fromarray(pred1[0, :, :, 0])
                        img_save_path = os.path.join(FLAGS.result,
                                                     str(FLAGS.threshold))
                        if not os.path.exists(img_save_path):
                            os.makedirs(img_save_path)
                        file_name = img_save_path + '/epoch' + '%02d' % cur_epoch + '_' + val_mask_list[
                            i].split('/')[-1]
                        ttt.save(file_name)

                        # calculate IoU
                        if np.sum(input_mask) == 0:
                            input_mask = np.logical_not(input_mask)
                            pred1 = np.logical_not(pred1)
                        intersection = np.logical_and(input_mask, pred1)
                        union = np.logical_or(input_mask, pred1)
                        iou_score_list.append(
                            np.sum(intersection) / np.sum(union))

                avg_loss = avg_loss / val_data_size
                print('epoch: ', cur_epoch, 'average loss: ', avg_loss)
                print(iou_score_list)
                print('average iou: ',
                      sum(iou_score_list) / len(iou_score_list))

                weight_save_path = os.path.join(FLAGS.weight_dir,
                                                str(FLAGS.threshold),
                                                FLAGS.weight)
                if not os.path.exists(weight_save_path):
                    os.makedirs(weight_save_path)
                saver.save(sess, weight_save_path, global_step=cur_epoch)

    if FLAGS.mode == "val":
        val_img_list = sorted(glob.glob(FLAGS.validation_set + '/img/*.tif'))
        val_mask_list = sorted(glob.glob(FLAGS.validation_set + '/mask/*.tif'))

        val_data_size = len(val_img_list)

        with tf.Session(config=config) as sess:
            ckpt = tf.train.get_checkpoint_state(
                os.path.join(FLAGS.weight_dir, str(FLAGS.threshold)))
            if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                sys.exit("No weights!!")
            avg_loss = 0
            iou_score_list = []
            for i in range(val_data_size):
                input_img, input_mask, origin_img, origin_mask = load(
                    val_img_list, val_mask_list, i)
                feed_dict = {image: input_img, mask: input_mask}
                loss1, pred = sess.run([loss, train_model],
                                       feed_dict=feed_dict)
                pred1 = np.where(pred < FLAGS.threshold, 0, 1)
                avg_loss += loss1
                # display
                # image_show = Image.fromarray(np.uint8(origin_img)).convert('RGB')
                # image_show = image_show.resize((400,400))
                # image_show.show()
                fig = plt.figure(figsize=(16, 8))
                rows = 1
                cols = 2

                ax1 = fig.add_subplot(rows, cols, 1)
                ax1.imshow(input_mask[0, :, :, 0], cmap='gray')
                ax1.set_title('Ground truth')
                ax1.axis("off")

                ax2 = fig.add_subplot(rows, cols, 2)
                ax2.imshow(pred1[0, :, :, 0], cmap='gray')
                ax2.set_title('Predicted result')
                ax2.axis("off")

                plt.show(block=False)
                plt.pause(3)  # 3 seconds
                plt.close()

                # calculate IoU
                if np.sum(input_mask) == 0:
                    input_mask = np.logical_not(input_mask)
                    pred1 = np.logical_not(pred1)
                intersection = np.logical_and(input_mask, pred1)
                union = np.logical_or(input_mask, pred1)
                iou_score_list.append(np.sum(intersection) / np.sum(union))

            print(iou_score_list)
            print('average iou: ', sum(iou_score_list) / len(iou_score_list))
Example #27
0
def run():
    path = CONFIG.INPUT_PATH 
    x_ray_image_names = os.listdir(path + '/CXR_png/')
    image_names = []
    for name in x_ray_image_names:
        image_names.append(name.split('.')[0])
    
    dataset_image_names = []
    mask_image_names = os.listdir(path + '/masks/')
    for name in mask_image_names:
        name = name.split('.png')[0].split('_mask')[0]
        if name in image_names:
            dataset_image_names.append(name)


    image_transforms = alb.Compose([
        alb.Normalize(CONFIG.mean, CONFIG.std, always_apply=True),
        alb.Resize(512, 512, always_apply=True),
        alb.pytorch.ToTensor()
    ])

    mask_transforms = alb.Compose([
        alb.Normalize(0, 1, always_apply=True),
        alb.Resize(512, 512, always_apply=True),
        alb.pytorch.ToTensor()
    ])

    train_images_name, val_images_name = train_test_split(dataset_image_names)

    train_data = DataLoader.DataLoader(
        train_images_name,
        image_transforms,
        mask_transforms
    )

    val_data = DataLoader.DataLoader(
        val_images_name,
        image_transforms,
        mask_transforms
    )

    train_loader = torch.utils.data.DataLoader(
        train_data,
        num_workers=4,
        batch_size=CONFIG.Batch_size,
        pin_memory=True
    )

    val_loader = torch.utils.data.DataLoader(
        val_data,
        num_workers=4,
        batch_size=CONFIG.Batch_size,
        pin_memory=True
    )

    if torch.cuda.is_available():
        accelarator = 'cuda'
        torch.backends.cudnn.benchmark = True
    else:
        accelarator = 'cpu'
    
    device = torch.device(accelarator)

    model = UNet.UNet(input_channels=3)

    model = model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=CONFIG.LR)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
        optimizer,
        patience=CONFIG.patience,
        threshold=CONFIG.scheduler_thresh,
        mode="min",
        factor=CONFIG.decay_factor
    )

    best_loss = 1e4
    
    print('------ [INFO] STARTING TRAINING ------')
    for epoch in range(CONFIG.Epochs):
        train_loss = engine.train_fn(model, train_loader, optimizer, device)
        val_loss = engine.eval_fn(model, val_loader, device)
        print(f'EPOCH -> {epoch+1}/{CONFIG.Epochs} | TRAIN LOSS = {train_loss} | VAL LOSS = {val_loss} | LR = {optimizer.param_groups[0]["lr"]}\n')
        scheduler.step(val_loss)
        if best_loss > val_loss:
            best_loss = val_loss
            best_model = model.state_dict()
            torch.save(best_model, CONFIG.MODEL_PATH)
            predict.predict('input/CXR_png/CHNCXR_0001_0.png')
Example #28
0
import os
import numpy as np
import cv2
from keras.callbacks import TensorBoard, ModelCheckpoint
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
np.random.seed(42)
import scipy.misc as mc
data_location = ''
training_images_loc = data_location + 'Chase/train/image/'
training_label_loc = data_location + 'Chase/train/label/'
testing_images_loc = data_location + 'Chase/test/image/'
testing_label_loc = data_location + 'Chase/test/label/'
train_files = os.listdir(training_images_loc)
train_data = []
train_label = []
desired_size = 1024
for i in train_files:
    im = mc.imread(training_images_loc + i)
    label = mc.imread(training_label_loc + "Image_" +
                      i.split('_')[1].split(".")[0] + "_1stHO.png")
    old_size = im.shape[:2]  # old_size is in (height, width) format
    delta_w = desired_size - old_size[1]
    delta_h = desired_size - old_size[0]
    top, bottom = delta_h // 2, delta_h - (delta_h // 2)
    left, right = delta_w // 2, delta_w - (delta_w // 2)
    color = [0, 0, 0]
    color2 = [0]
    new_im = cv2.copyMakeBorder(im,
                                top,
Example #29
0
    return a[0], a[1], a[2], a[3], a[4]

def load_pat_orig(id):
    a = np.load("/media/fabian/DeepLearningData/datasets/BraTS_2014/HGG/original_value_range/%03.0d.npy"%id)
    return a[0], a[1], a[2], a[3], a[4]

validation_patients = [7,  97, 235,  12, 231, 200, 177, 104, 247,  41, 237, 24, 118, 198, 103,   6, 243,  35,   0,  18, 112, 180,  25, 157,  69]
experiment_name = "segmentPatches_BraTS_2014_UNet_lossSampling_gradClip_adam_TitanX_no_adapted_values"
results_folder = "/home/fabian/datasets/Hirntumor_von_David/experiments/results/%s/" % experiment_name
epoch = 37

input_shape = (368, 352)

print "compiling theano functions"
# uild_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), base_n_filters=64, do_dropout=False):
net = UNet.build_UNet(20, 1, 6, input_dim=input_shape, base_n_filters=16, pad="valid")
output_shape = net["output_segmentation"].output_shape[-2:]
output_layer = net["output_segmentation"]
with open(os.path.join(results_folder, "%s_allLossesNAccur_ep%d.pkl" % (experiment_name, epoch)), 'r') as f:
    tmp = cPickle.load(f)

with open(os.path.join(results_folder, "%s_Params_ep%d.pkl" % (experiment_name, epoch)), 'r') as f:
    params = cPickle.load(f)
    lasagne.layers.set_all_param_values(output_layer, params)

import theano.tensor as T
data_sym = T.tensor4()

output = softmax_helper(lasagne.layers.get_output(output_layer, data_sym, deterministic=False))
pred_fn = theano.function([data_sym], output)
def do_experiment(model_config):

    tf.reset_default_graph()
    experiment_id = ex.current_run._id
    print('Experiment ID: {eid}'.format(eid=experiment_id))

    # Prepare data
    print('Preparing dataset')
    train_data, val_data, test_data = Dataset.prepare_datasets(model_config)
    print('Dataset ready')

    # Start session
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    sess = tf.Session(config=tf_config)
    print('Session started')

    # Create iterators
    handle = tf.placeholder(tf.string, shape=[])
    iterator = tf.data.Iterator.from_string_handle(handle,
                                                   train_data.output_types,
                                                   train_data.output_shapes)
    mixed_spec, voice_spec, mixed_audio, voice_audio = iterator.get_next()

    training_iterator = train_data.make_initializable_iterator()
    validation_iterator = val_data.make_initializable_iterator()
    testing_iterator = test_data.make_initializable_iterator()

    training_handle = sess.run(training_iterator.string_handle())
    validation_handle = sess.run(validation_iterator.string_handle())
    testing_handle = sess.run(testing_iterator.string_handle())
    print('Iterators created')
    # Create variable placeholders
    is_training = tf.placeholder(shape=(), dtype=bool)
    mixed_mag = tf.expand_dims(mixed_spec[:, :, 1:, 0], 3)
    mixed_phase = tf.expand_dims(mixed_spec[:, :, 1:, 1], 3)
    voice_mag = tf.expand_dims(voice_spec[:, :, 1:, 0], 3)

    # Build U-Net model
    print('Creating model')
    model = UNet.UNetModel(mixed_mag,
                           voice_mag,
                           mixed_phase,
                           mixed_audio,
                           voice_audio,
                           'unet',
                           is_training,
                           name='U_Net_Model')

    if model_config['loading']:
        # TODO - Think this works now but needs proper testing
        print('Loading checkpoint')
        checkpoint = os.path.join(model_config['model_base_dir'],
                                  model_config['checkpoint_to_load'])
        restorer = tf.train.Saver()
        restorer.restore(sess, checkpoint)

    # Summaries
    model_folder = str(experiment_id)
    writer = tf.summary.FileWriter(os.path.join(model_config["log_dir"],
                                                model_folder),
                                   graph=sess.graph)

    # Get baseline metrics at initialisation
    sess.run(tf.global_variables_initializer())
    test_count = 0
    if model_config['INITIALISATION_TEST']:
        print('Running initialisation test')
        initial_test_loss, test_count = test(sess, model, model_config, handle,
                                             testing_iterator, testing_handle,
                                             writer, test_count)

    # Train the model
    model = train(sess, model, model_config, model_folder, handle,
                  training_iterator, training_handle, validation_iterator,
                  validation_handle, writer)

    # Test trained model
    mean_test_loss, test_count = test(sess, model, model_config, handle,
                                      testing_iterator, testing_handle, writer,
                                      test_count)
    print('{ts}:\n\tAll done!'.format(ts=datetime.datetime.now()))
    if model_config['INITIALISATION_TEST']:
        print('\tInitial test loss: {init}'.format(init=initial_test_loss))
    print('\tFinal test loss: {final}'.format(final=mean_test_loss))
Example #31
0
##########################################################
train_dataset = FakeDataset(train_images_path,
                            train_labels_path,
                            transform=transforms)
train_dataset.fit([center])
checkTrainSetMean(train_dataset)

train_loader = DataLoader(train_dataset, shuffle=True)
##########################################################
test_dataset = FakeDataset(test_images_path,
                           test_labels_path,
                           transform=transforms)
test_loader = DataLoader(test_dataset, shuffle=True)
##########################################################

net = UNet(6, 32).cuda(1)
net.apply(weightInitialization)
net.train()

learn_rate = 1e-2
momentum_rate = 0.8
cyclic_rate = 25
epochs = 50
alpha = 0.06
weight_map = getWeightMap(train_loader)
# weight_map = np.array([alpha,1-alpha])
training_parameters = "Learning Rate: {} \n Momentum: {} \n Cycle Length: {} \n Number of epochs: {}\n Weight Map: {}".format(
    learn_rate, momentum_rate, cyclic_rate, epochs, weight_map)

os.chdir("Fake")
w = SummaryWriter()