Esempio n. 1
0
 def progress(self, i, l, filename):
     """Prints the progress of processing the files."""
     printProgressBar(i,
                      l,
                      prefix=f"Working on the file: {filename}. Progress:",
                      suffix='Complete',
                      length=50)
Esempio n. 2
0
def updateDelegationTable(bRelationList, table2Update):
    # Create DB connection
    cnx = dbConnection.connect()
    # Create a cursor
    cursor = cnx.cursor()
    # Progressbar length and iteration value
    l = len(bRelationList)
    i = 0
    # Create query to update table
    for item in bRelationList:
        updateQuery = createUpdateQuery(item, table2Update)
        #print('{}'.format(updateQuery), end='\n')
        try:
            # Execute cursor
            cursor.execute(updateQuery)
        except Exception as err:
            print(
                'Can not update primary table with bsuiness relation info. ERROR - {}'
                .format(err))
        # Increase progressbar iterator and show progress bar
        i += 1
        progressBar.printProgressBar(i,
                                     l,
                                     prefix='Progress:',
                                     suffix='Complete')
    # Commit data to DATABASE
    cnx.commit()
    # Close Cursor
    cursor.close()
Esempio n. 3
0
def inferenceVolume(net, img_batch, batch_size, epoch, deepSupervision):
    total = len(img_batch)

    Dice1 = torch.zeros(total, 2)
    Dice2 = torch.zeros(total, 2)
    Dice3 = torch.zeros(total, 2)

    net.eval()
    success = 0
    totalImages = 0
    img_names_ALL = []

    imagesAll = []
    dice = computeDiceOneHot().cuda()
    softMax = nn.Softmax().cuda()
    for i, data in enumerate(img_batch):
        printProgressBar(i,
                         total,
                         prefix="[Inference] Getting segmentations...",
                         length=30)
        image, labels, img_names = data
        img_names_ALL.append(img_names[0].split('/')[-1].split('.')[0])

        MRI = to_var(image)
        Segmentation = to_var(labels)

        if deepSupervision == False:
            segmentation_prediction = net(MRI)
        else:
            segmentation_prediction, seg3, seg2, seg1 = net(MRI)

        pred_y = softMax(segmentation_prediction)

        predDiscrete = predToSegmentation(pred_y)

        pdb.set_trace()
        Segmentation_planes = getOneHotSegmentation(Segmentation)

        DicesN, Dices1, Dices2, Dices3 = dice(pred_y, Segmentation_planes)

        Dice1[i] = Dices1.data
        Dice2[i] = Dices2.data
        Dice3[i] = Dices3.data

        # Save images
        # directory = 'resultBladder'
        # if not os.path.exists(directory):
        #     os.makedirs(directory)
        # filenameImg = os.path.join(directory, "original_image_{}_{}.png".format(epoch, i))
        # filenameMask = os.path.join(directory, "groundTruth_image_{}_{}.png".format(epoch, i))
        # filenamePred = os.path.join(directory, "Prediction_{}_{}.png".format(epoch, i))
    printProgressBar(total, total, done="[Inference] Segmentation Done !")

    ValDice1 = DicesToDice(Dice1)
    ValDice2 = DicesToDice(Dice2)
    ValDice3 = DicesToDice(Dice3)

    return [ValDice1, ValDice2, ValDice3]
def inferencee(network, x_train, y_train, imageNames, epoch, folder_save,
               number_modalities):
    a = 64
    b = 64
    '''root_dir = './Data/MRBrainS/DataNii/'
    model_dir = 'model'

    moda_1 = root_dir + 'Training/T1'
    moda_2 = root_dir + 'Training/T1_IR'
    moda_3 = root_dir + 'Training/T2_FLAIR'
    moda_g = root_dir + 'Training/GT'''
    network.eval()
    softMax = nn.Sigmoid()
    numClasses = 1
    if torch.cuda.is_available():
        softMax.cuda()
        network.cuda()

    patchSize = a
    patchSize_gt = b

    pred_numpy = np.zeros((0, patchSize_gt, patchSize_gt, patchSize_gt))
    pred_numpy = np.vstack(
        (pred_numpy,
         np.zeros(
             (x_train.shape[0], patchSize_gt, patchSize_gt, patchSize_gt))))
    # pred = network(numpy_to_var(x[0,:,:,:,:]).view(1,number_modalities,patchSize,patchSize,patchSize))
    for i_p in range(x_train.shape[0]):
        pred = network(
            numpy_to_var(x_train[i_p, :, :, :, :].reshape(
                1, number_modalities, patchSize, patchSize, patchSize)))
        pred_y = softMax(pred.reshape(patchSize_gt, patchSize_gt,
                                      patchSize_gt))
        pred_numpy[i_p, :, :, :] = pred_y.cpu().data.numpy()

        printProgressBar(i_p + 1,
                         x_train.shape[0],
                         prefix="[Training_eval] ",
                         length=15)

    # To reconstruct the predicted volume
    extraction_step_value = b
    pred_classes = np.round(pred_numpy)

    pred_classes = pred_classes.reshape(
        (x_train.shape[0], patchSize_gt, patchSize_gt, patchSize_gt))
    # bin_seg = reconstruct_volume(pred_classes, (img_shape[1], img_shape[2], img_shape[3]))

    # bin_seg = bin_seg[:,:,extraction_step_value:img_shape[3]-extraction_step_value]

    dsc = dc(y_train, pred_classes)
    acc = accuracy_score(y_train.flatten(), pred_classes.flatten())

    return dsc, acc
Esempio n. 5
0
def saveImages(net,
               img_batch,
               batch_size,
               epoch,
               modelName,
               deepSupervision=0):
    # print(" Saving images.....")
    #path = 'Results/ENet-Original'
    path = '../Results/Images_PNG/' + modelName + '_' + str(epoch)
    if not os.path.exists(path):
        os.makedirs(path)
    total = len(img_batch)
    net.eval()
    softMax = nn.Softmax()
    times = []
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="Saving images.....", length=30)
        image, labels, img_names = data

        MRI = to_var(image)
        Segmentation = to_var(labels)

        if deepSupervision == False:
            # No deep supervision
            tic = time.clock()
            segmentation_prediction = net(MRI)
            toc = time.clock()
            times.append(toc - tic)
        else:
            # Deep supervision
            segmentation_prediction, seg_3, seg_2, seg_1 = net(MRI)

        pred_y = softMax(segmentation_prediction)
        segmentation = getSingleImage(pred_y)

        #segmentation = getSingleImageBin(segmentation_prediction)

        out = torch.cat((MRI, segmentation, Segmentation))

        torchvision.utils.save_image(
            out.data,
            os.path.join(path,
                         str(i) + '_Ep_' + str(epoch) + '.png'),
            nrow=batch_size,
            padding=2,
            normalize=False,
            range=None,
            scale_each=False,
            pad_value=0)

    printProgressBar(total, total, done="Images saved !")
Esempio n. 6
0
def inference(net, img_batch, batch_size, epoch):
    total = len(img_batch)

    Dice1 = torch.zeros(total, 2)
    net.eval()
    
    dice = computeDiceOneHotBinary().cuda()
    softMax = nn.Softmax().cuda()

    img_names_ALL = []
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="[Inference] Getting segmentations...", length=30)
        image_f,image_i,image_o,image_w, labels, img_names = data

        # Be sure here your image is betwen [0,1]
        image_f=image_f.type(torch.FloatTensor)/65535
        image_i=image_i.type(torch.FloatTensor)/65535
        image_o=image_o.type(torch.FloatTensor)/65535
        image_w=image_w.type(torch.FloatTensor)/65535

        images = torch.cat((image_f,image_i,image_o,image_w),dim=1)
        img_names_ALL.append(img_names[0].split('/')[-1].split('.')[0])

        MRI = to_var(images)

        labels = labels.numpy()
        idx=np.where(labels>0.0)
        labels[idx]=1.0
        labels = torch.from_numpy(labels)
        labels = labels.type(torch.FloatTensor)
  
        Segmentation = to_var(labels)
        segmentation_prediction = net(MRI)

        pred_y = softMax(segmentation_prediction)
        
        Segmentation_planes = getOneHotSegmentation(Segmentation)
        segmentation_prediction_ones = predToSegmentation(pred_y)
        
        DicesN, Dices1 = dice(segmentation_prediction_ones, Segmentation_planes)

        Dice1[i] = Dices1.data
        

    printProgressBar(total, total, done="[Inference] Segmentation Done !")
    
    ValDice1 = DicesToDice(Dice1)
   
    return [ValDice1]
Esempio n. 7
0
File: run.py Progetto: Blesfia/ai
 def run(self):
     for i in range(0, self.attempts):
         printProgressBar(
             i, self.attempts, 'Progress: ',
             'Complete. (' + str(self.wins) + ' Win, ' + str(self.draws) +
             ' Draw, ' + str(self.loses) + ' Lose)')
         score = self.game.play(self.random)
         self.addScore(score)
         if self.train and i % 10 == 0:
             self.game.train()
     print()
     print(
         "Finish:", ((self.wins + self.draws) * 100) / self.attempts,
         "% (" + str(self.wins) + ' Wins, ' + str(self.draws) + ' Draws, ' +
         str(self.loses) + ' Loses)')
Esempio n. 8
0
def inference(net, img_batch):
    total = len(img_batch)

    Dice1 = torch.zeros(total, 2)
    Dice2 = torch.zeros(total, 2)
    Dice3 = torch.zeros(total, 2)
    Dice4 = torch.zeros(total, 2)

    net.eval()
    img_names_ALL = []

    dice = computeDiceOneHot().cuda()
    softMax = nn.Softmax().cuda()
    for i, data in enumerate(img_batch):

        printProgressBar(i,
                         total,
                         prefix="[Inference] Getting segmentations...",
                         length=30)
        image, labels, img_names = data
        img_names_ALL.append(img_names[0].split('/')[-1].split('.')[0])

        MRI = to_var(image)
        Segmentation = to_var(labels)

        segmentation_prediction = net(MRI)

        pred_y = softMax(segmentation_prediction)
        Segmentation_planes = getOneHotSegmentation(Segmentation)

        segmentation_prediction_ones = predToSegmentation(pred_y)
        DicesN, Dices1, Dices2, Dices3, Dices4 = dice(
            segmentation_prediction_ones, Segmentation_planes)

        Dice1[i] = Dices1.data
        Dice2[i] = Dices2.data
        Dice3[i] = Dices3.data
        Dice4[i] = Dices4.data

    printProgressBar(total, total, done="[Inference] Segmentation Done !")

    ValDice1 = DicesToDice(Dice1)
    ValDice2 = DicesToDice(Dice2)
    ValDice3 = DicesToDice(Dice3)
    ValDice4 = DicesToDice(Dice4)

    return [ValDice1, ValDice2, ValDice3, ValDice4]
Esempio n. 9
0
def getBusinessRel(delegationList):
    # Create DB connection
    cnx = dbConnection.connect()
    # Create a cursor
    cursor = cnx.cursor()
    # Create a list with business relation data
    bRelationList = []
    # Progressbar length/iteration value
    l = len(delegationList)
    i = 0
    #progressBar.printProgressBar(i, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
    # Create a query for business raltionship
    for item in delegationList:
        tmpList = []
        bRelQuery = createBRelQuery(item[0], item[1])
        try:
            # execute the query
            cursor.execute(bRelQuery)
            rows = cursor.fetchmany(size=1)
            if rows:
                for row in rows:
                    as_rel_type = row[0]
                    tmpList.append(item[0])
                    tmpList.append(item[1])
                    tmpList.append(as_rel_type)
            else:
                tmpList.append(item[0])
                tmpList.append(item[1])
                tmpList.append('undefined')
            bRelationList.append(tmpList)
        except Exception as err:
            print(
                'Cursor problem occured while retiving business ralation info. ERROR - {}'
                .format(err))
        # increase the progressbar iterator and show updated progressbar
        i += 1
        progressBar.printProgressBar(i,
                                     l,
                                     prefix='Progress:',
                                     suffix='Complete')
    return bRelationList
    # Commit data to DATABASE
    cnx.commit()
    # Close Cursor
    cursor.close()
Esempio n. 10
0
def saveImages(net, img_batch, batch_size, epoch, modelName):
    path = '../Results/Images_PNG/' + modelName + '_'+ str(epoch) 
    if not os.path.exists(path):
        os.makedirs(path)
        
    total = len(img_batch)
    net.eval()
    softMax = nn.Softmax()
    
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="Saving images.....", length=30)
        image_f,image_i,image_o,image_w, labels, img_names = data

        # Be sure here your image is betwen [0,1]
        image_f=image_f.type(torch.FloatTensor)
        image_i=image_i.type(torch.FloatTensor)
        image_o=image_o.type(torch.FloatTensor)
        image_w=image_w.type(torch.FloatTensor)

        images = torch.cat((image_f,image_i,image_o,image_w),dim=1)

        MRI = to_var(images)
        image_f_var = to_var(image_f)
        Segmentation = to_var(labels)
            
        segmentation_prediction = net(MRI)

        pred_y = softMax(segmentation_prediction)
        segmentation = getSingleImageBin(pred_y)
        imgname = img_names[0].split('/Fat/')
        imgname = imgname[1].split('_fat.png')
        
        out = torch.cat((image_f_var, segmentation, Segmentation*255))
        
        torchvision.utils.save_image(out.data, os.path.join(path,imgname[0] + '.png'),
                                     nrow=batch_size,
                                     padding=2,
                                     normalize=False,
                                     range=None,
                                     scale_each=False)
                                     
    printProgressBar(total, total, done="Images saved !")
def train(net, epochs, trainset_size=12500, batch_size=4, learning_rate=0.001):
    texts.print_blox("TREINO")
    # TRAINING:
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)
    trainloader, classes = dm.load_trainset_CIFAR10(batch_size)
    print("Training features:")
    print("Images: %i" % (trainset_size * batch_size))
    print("Mini-batch size: %i" % (batch_size))
    print("Learning rate: %.3f" % (learning_rate))
    print("Epochs: %i" % (epochs))
    print("")
    for epoch in range(epochs):  # loop over the dataset multiple times
        running_loss = 0.0
        acc = 0.0
        for i, data in enumerate(trainloader, 0):
            inputs, labels = data  # get the inputs
            optimizer.zero_grad()  #zero the parameter gradients
            # forward + backward + optimize
            outputs = net(inputs)
            # print("Labels:", labels)
            # print("outputs:", outputs)
            acc += (outputs.max(dim=1)[1] == labels).sum()
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            # print statistics
            running_loss += loss.item(
            )  # Returns the value of this tensor as a standard Python number. This only works for tensors with one element.
            progressBar.printProgressBar(i,
                                         trainset_size - 1,
                                         prefix='Epoch ' + str(epoch + 1) +
                                         ':',
                                         suffix='Complete',
                                         length=50)
            if i == trainset_size - 1:
                print("Report: ", end='')
                print('[Loss: %.3f; Hits: %i; Acc: %.1f%%]' %
                      (running_loss / trainset_size, acc, float(acc) /
                       (1.0 * trainset_size * batch_size) * 100.0))
                break
    print("Finished training!\n")
Esempio n. 12
0
def emptyReservoir(a):
    # takes 'a' time in seconds to run the air pump to drain the tank
    GPIO.output(9, GPIO.HIGH)
    b = 0
    #print ("Running the air pump for %i seconds" % (a))
    printProgressBar(0,
                     a,
                     prefix='Drain Progress:  ',
                     suffix='Complete',
                     length=100)
    while b < a:
        b = b + 1
        time.sleep(1)
        printProgressBar(b,
                         a,
                         prefix='Drain Progress:  ',
                         suffix='Complete',
                         length=100)
    printProgressBar(a,
                     a,
                     prefix='Drain Progress:  ',
                     suffix='Complete',
                     length=100)
    print()
    GPIO.output(9, GPIO.LOW)
Esempio n. 13
0
def saveImages(net, img_batch, batch_size, epoch, modelName, deepSupervision):
    # print(" Saving images.....")
    path = 'Results/' + modelName
    if not os.path.exists(path):
        os.makedirs(path)
    total = len(img_batch)
    net.eval()
    softMax = nn.Softmax()
    softMax.cuda()
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="Saving images.....", length=30)
        image, labels, weakly_labels, img_names = data

        MRI = to_var(image)
        Segmentation = to_var(labels)
            
        if deepSupervision == False:
            # No deep supervision
            segmentation_prediction = net(MRI)
        else:
            # Deep supervision
            segmentation_prediction, seg_3, seg_2, seg_1 = net(MRI)

        temperature = 0.1
        pred_y = softMax(segmentation_prediction/temperature)
            
        segmentation = getSingleImageBin(pred_y)
        
        out = torch.cat((MRI, segmentation, Segmentation))

        torchvision.utils.save_image(segmentation.data, os.path.join(path, str(i) + '_Ep_' + str(epoch) + '.png'), normalize=False, range=None, scale_each=False)
        
        '''torchvision.utils.save_image(out.data, os.path.join(path, str(i) + '_Ep_' + str(epoch) + '.png'),
                                     nrow=batch_size,
                                     padding=2,
                                     normalize=False,
                                     range=None,
                                     scale_each=False,
                                     pad_value=0)'''
    printProgressBar(total, total, done="Images saved !")
Esempio n. 14
0
def saveImages_for3D(net,
                     img_batch,
                     batch_size,
                     epoch,
                     modelName,
                     deepSupervision=False,
                     isBest=False):
    # print(" Saving images.....")
    path = 'Results/Images/' + modelName + '/' + str(epoch)
    if not os.path.exists(path):
        os.makedirs(path)

    total = len(img_batch)
    net.eval()
    softMax = nn.Softmax()
    for i, data in enumerate(img_batch):
        image, labels, img_names = data

        MRI = to_var(image)
        Segmentation = to_var(labels)

        segmentation_prediction = net(MRI)

        pred_y = softMax(segmentation_prediction)

        segmentation = getSingleImage(pred_y)

        out = torch.cat((MRI, segmentation, Segmentation))

        str_1 = img_names[0].split('/Img/')
        str_subj = str_1[1].split('slice')

        path_Subj = path + '/' + str_subj[0]
        if not os.path.exists(path_Subj):
            os.makedirs(path_Subj)

        str_subj = str_subj[1].split('_')
        torchvision.utils.save_image(segmentation.data,
                                     os.path.join(path_Subj, str_subj[1]))
    printProgressBar(total, total, done="Images saved !")
Esempio n. 15
0
def saveImagesAsMatlab(net, img_batch, batch_size, epoch):
    print(" Saving images.....")
    path = 'ResultsMatlab-ENet-ACDC-York'
    if not os.path.exists(path):
        os.makedirs(path)
    total = len(img_batch)
    net.eval()
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="Saving images.....", length=30)
        image, labels, img_names = data

        MRI = to_var(image)
        Segmentation = to_var(labels)

        segmentation_prediction = net(MRI)

        segmentation = getSingleImage(segmentation_prediction)
        nameT = img_names[0].split('Img/')
        nameT = nameT[1].split('.png')
        pred =  segmentation.data.cpu().numpy().reshape(256,256)
        sio.savemat(os.path.join(path, nameT[0] + '.mat'), {'pred':pred})
      
    printProgressBar(total, total, done="Images saved !")
Esempio n. 16
0
def saveImagesAsMatlab(net,
                       img_batch,
                       batch_size,
                       epoch,
                       modelName,
                       deepSupervision=False):
    print(" Saving images.....")
    path = '../Results/Images_MATLAB/' + modelName
    #path = 'ResultsMatlab/ENet-Original'
    if not os.path.exists(path):
        os.makedirs(path)

    total = len(img_batch)
    net.eval()
    softMax = nn.Softmax().cuda()
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="Saving images.....", length=30)
        image, labels, img_names = data

        MRI = to_var(image)
        Segmentation = to_var(labels)

        if deepSupervision == False:
            # No deep supervision
            segmentation_prediction = net(MRI)
        else:
            # Deep supervision
            segmentation_prediction, seg_3, seg_2, seg_1 = net(MRI)

        pred_y = softMax(segmentation_prediction)
        segmentation = getSingleImage(pred_y)
        nameT = img_names[0].split('Img/')
        nameT = nameT[1].split('.png')
        pred = segmentation.data.cpu().numpy().reshape(320, 320)
        sio.savemat(os.path.join(path, nameT[0] + '.mat'), {'pred': pred})

    printProgressBar(total, total, done="Images saved !")
Esempio n. 17
0
def saveImagesSegmentation(net, img_batch, batch_size, epoch, modelName, deepSupervision):
    # print(" Saving images.....")
    path = 'Results/' + modelName
    if not os.path.exists(path):
        os.makedirs(path)
    total = len(img_batch)
    net.eval()
    softMax = nn.Softmax()
    softMax.cuda()
    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="Saving images.....", length=30)
        image, labels, weakly_labels, img_names = data
        
        MRI = to_var(image)
        Segmentation = to_var(labels)
            
        if deepSupervision == False:
            # No deep supervision
            segmentation_prediction = net(MRI)
        else:
            # Deep supervision
            segmentation_prediction, seg_3, seg_2, seg_1 = net(MRI)

        pred_y = softMax(segmentation_prediction)
        #segmentation = getSingleImage(segmentation_prediction)
        #segmentation = getSingleImageBin(segmentation_prediction)
        segmentation = getSingleImageBin(pred_y)
        
        
        out = torch.cat((MRI, segmentation, Segmentation))
        
        name2save = img_names[0].split('./ACDC-2D-All/val/Img/')
        
        torchvision.utils.save_image(segmentation.data, os.path.join(path, name2save[1]))
        
    printProgressBar(total, total, done="Images saved !")
Esempio n. 18
0
def fillReservoir(a):
    # fill the reservoir to specified level of 'low', 'med, 'high' and return the total flow count
    if a == 'low':
        lvl = 26
        flowMax = 21000
    elif a == 'med':
        lvl = 19
        flowMax = 28000
    elif a == 'high':
        lvl = 13
        flowMax = 32000
    else:
        print(
            "the function fillReservoir now requires a level as 'low', 'med', 'high'"
        )
        return None
    num = 0
    b = 0
    writeCommand(1)
    #print ('flow counter cleared')
    for x in [10, 27, 17]:
        GPIO.output(x, GPIO.HIGH)
    printProgressBar(0,
                     flowMax,
                     prefix='Fill Progress:   ',
                     suffix='Complete',
                     length=100)
    while GPIO.input(lvl):
        time.sleep(1)
        b = flowCount()
        flowSafe = (b if b < flowMax else flowMax)
        #print ('Secs:%d, Target level: %d, Low:%d, Med:%d, High:%d, current flow count:%d ' % (num, lvl, GPIO.input(26), GPIO.input(19), GPIO.input(13), flowCount()))
        printProgressBar(flowSafe,
                         flowMax,
                         prefix='Fill Progress:   ',
                         suffix='Complete',
                         length=100)
        num = num + 1
    printProgressBar(flowMax,
                     flowMax,
                     prefix='Fill Progress:   ',
                     suffix='Complete',
                     length=100)
    print()
    for x in [10, 27, 17]:
        GPIO.output(x, GPIO.LOW)
    with open("fillcount-%s.txt" % (a), "a+") as fl:
        fl.write(str(b) + "\n")
    return b
def inference(network, moda_n, moda_g, imageNames, epoch, folder_save,
              number_modalities):
    a = 64
    b = 64
    '''root_dir = './Data/MRBrainS/DataNii/'
    model_dir = 'model'

    moda_1 = root_dir + 'Training/T1'
    moda_2 = root_dir + 'Training/T1_IR'
    moda_3 = root_dir + 'Training/T2_FLAIR'
    moda_g = root_dir + 'Training/GT'''
    network.eval()
    softMax = nn.Sigmoid()
    numClasses = 1
    if torch.cuda.is_available():
        softMax.cuda()
        network.cuda()

    dscAll = []
    accall = []
    for i_s in range(len(imageNames)):
        if number_modalities == 2:
            patch_1, patch_2, patch_g, img_shape = load_data_test(
                moda_n, moda_g, imageNames[i_s], number_modalities
            )  # hardcoded to read the first file. Loop this to get all files
        if number_modalities == 3:
            patch_1, patch_2, patch_3, patch_g, img_shape = load_data_test(
                [moda_n], moda_g, imageNames[i_s], number_modalities
            )  # hardcoded to read the first file. Loop this to get all files
    # Normalization

        patchSize = a
        patchSize_gt = b

        x = np.zeros((0, number_modalities, patchSize, patchSize, patchSize))
        x = np.vstack((x,
                       np.zeros((patch_1.shape[0], number_modalities,
                                 patchSize, patchSize, patchSize))))
        x[:, 0, :, :, :] = patch_1
        x[:, 1, :, :, :] = patch_2
        if (number_modalities == 3):
            x[:, 2, :, :, :] = patch_3

        pred_numpy = np.zeros((0, patchSize_gt, patchSize_gt, patchSize_gt))
        pred_numpy = np.vstack((pred_numpy,
                                np.zeros((patch_1.shape[0], patchSize_gt,
                                          patchSize_gt, patchSize_gt))))
        totalOp = len(imageNames) * patch_1.shape[0]
        #pred = network(numpy_to_var(x[0,:,:,:,:]).view(1,number_modalities,patchSize,patchSize,patchSize))
        for i_p in range(patch_1.shape[0]):
            pred = network(
                numpy_to_var(x[i_p, :, :, :, :].reshape(
                    1, number_modalities, patchSize, patchSize, patchSize)))
            pred_y = softMax(
                pred.reshape(patchSize_gt, patchSize_gt, patchSize_gt))
            pred_numpy[i_p, :, :, :] = pred_y.cpu().data.numpy()

            printProgressBar(i_s * ((totalOp + 0.0) / len(imageNames)) + i_p +
                             1,
                             totalOp,
                             prefix="[Validation] ",
                             length=15)

        # To reconstruct the predicted volume
        extraction_step_value = b
        pred_classes = np.round(pred_numpy)

        pred_classes = pred_classes.reshape(
            (patch_1.shape[0], patchSize_gt, patchSize_gt, patchSize_gt))
        #bin_seg = reconstruct_volume(pred_classes, (img_shape[1], img_shape[2], img_shape[3]))

        bin_seg = my_reconstruct_volume(
            pred_classes, (img_shape[1], img_shape[2], img_shape[3]),
            patch_shape=(a, a, a),
            extraction_step=(b, b, b))

        #bin_seg = bin_seg[:,:,extraction_step_value:img_shape[3]-extraction_step_value]
        #label_selector = [slice(None)] + [slice(9, 117) for i in range(3)]
        gt = nib.load(moda_g + '/' + imageNames[i_s]).get_fdata()
        gt_patches = extract_patches(gt, (a, a, a), (b, b, b))
        #gt_patches = gt_patches[label_selector]
        img_pred = nib.Nifti1Image(bin_seg, np.eye(4))
        img_gt = nib.Nifti1Image(gt, np.eye(4))

        img_name = imageNames[i_s].split('.nii')
        name = 'Pred_' + img_name[0] + '_Epoch_' + str(epoch) + '.nii.gz'

        namegt = 'GT_' + img_name[0] + '_Epoch_' + str(epoch) + '.nii.gz'

        if not os.path.exists(folder_save + 'Segmentations/'):
            os.makedirs(folder_save + 'Segmentations/')

        if not os.path.exists(folder_save + 'GT/'):
            os.makedirs(folder_save + 'GT/')

        nib.save(img_pred, folder_save + 'Segmentations/' + name)
        nib.save(img_gt, folder_save + 'GT/' + namegt)

        dsc = dc(gt_patches, pred_classes)
        acc = accuracy_score(gt_patches.flatten(), pred_classes.flatten())
        dscAll.append(dsc)
        accall.append(acc)
    return dscAll, accall
Esempio n. 20
0
# Get k for number of voters
k = int(input("k = "))

# Get row name for classification attribute
classCol = input("Name of column to be the class attribute: ")

# Clean data
trainingSet = cleanData(dataFile, nameFile)
testSet = cleanData(testFile, nameFile2)

# Preprocess datasets
preprocessedTrainingSet = preprocess(trainingSet)
preprocessedTestSet = preprocess(testSet)

# Find nearest neighbor for each test row and compute error rate
testCount = 0
errors = 0
totalCount = len(preprocessedTestSet)
printProgressBar(0, totalCount, prefix = 'Progress:', suffix = 'Complete', length = 50)
for row in preprocessedTestSet.iterrows():
    errors += nearestNeighbor(preprocessedTrainingSet, row, classCol, k)
    testCount += 1
    printProgressBar(testCount, totalCount, prefix = 'Progress:', suffix = 'Complete', length = 50)
    

print("Error rate: ", errors/(testCount))

dataFile.close()
if(not hasHeader):
    nameFile.close()
    nameFile2.close()
def runTraining(opts):
    print('' * 41)
    print('~' * 50)
    print('~~~~~~~~~~~~~~~~~  PARAMETERS ~~~~~~~~~~~~~~~~')
    print('~' * 50)
    print('  - Number of image modalities: {}'.format(opts.numModal))
    print('  - Number of classes: {}'.format(opts.numClasses))
    print('  - Directory to load images: {}'.format(opts.root_dir))
    for i in range(len(opts.modality_dirs)):
        print('  - Modality {}: {}'.format(i + 1, opts.modality_dirs[i]))
    print('  - Directory to save results: {}'.format(opts.save_dir))
    print('  - To model will be saved as : {}'.format(opts.modelName))
    print('-' * 41)
    print('  - Number of epochs: {}'.format(opts.numClasses))
    print('  - Batch size: {}'.format(opts.batchSize))
    print('  - Number of samples per epoch: {}'.format(opts.numSamplesEpoch))
    print('  - Learning rate: {}'.format(opts.l_rate))
    print('' * 41)

    print('-' * 41)
    print('~~~~~~~~  Starting the training... ~~~~~~')
    print('-' * 41)
    print('' * 40)
    a = 64
    b = 64
    samplesPerEpoch = opts.numSamplesEpoch
    batch_size = opts.batchSize

    lr = opts.l_rate
    epoch = opts.numEpochs

    root_dir = opts.root_dir
    model_name = opts.modelName

    if not (len(opts.modality_dirs) == opts.numModal): raise AssertionError

    moda_1 = root_dir + 'Training/' + opts.modality_dirs[0]
    moda_2 = root_dir + 'Training/' + opts.modality_dirs[1]

    if (opts.numModal == 3):
        moda_3 = root_dir + 'Training/' + opts.modality_dirs[2]

    moda_g = root_dir + 'Training/GT'

    print(' --- Getting image names.....')
    print(' - Training Set: -')
    if os.path.exists(moda_1):
        imageNames_tr = [
            f for f in os.listdir(moda_1) if isfile(join(moda_1, f))
        ]
        np.random.seed(1)
        np.random.shuffle(imageNames_tr)
        imageNames_val = imageNames_tr[0:40]
        imageNames_train = list(set(imageNames_tr) - set(imageNames_val))
        print(' ------- Images found ------')
        for i in range(len(imageNames_train)):
            print(' - {}'.format(imageNames_train[i]))
    else:
        raise Exception(' - {} does not exist'.format(moda_1))
    moda_1_val = root_dir + 'Training/' + opts.modality_dirs[0]
    moda_2_val = root_dir + 'Training/' + opts.modality_dirs[1]

    if (opts.numModal == 3):
        moda_3_val = root_dir + 'Training/' + opts.modality_dirs[2]
    moda_g_val = root_dir + 'Training/GT'

    print(' --------------------')
    print(' - Validation Set: -')
    if os.path.exists(moda_1):
        # imageNames_val = [f for f in os.listdir(moda_1_val) if isfile(join(moda_1_val, f))]
        # imageNames_val.sort()
        print(' ------- Images found ------')
        for i in range(len(imageNames_val)):
            print(' - {}'.format(imageNames_val[i]))
    else:
        raise Exception(' - {} does not exist'.format(moda_1_val))

    print("~~~~~~~~~~~ Creating the model ~~~~~~~~~~")
    num_classes = opts.numClasses

    # Define VNet_FED_2mod
    # To-Do. Get as input the config settings to create different networks
    if (opts.numModal == 2):
        hdNet = VNet_FED()

    #
    '''try:
        hdNet = torch.load(os.path.join(model_name, "Best_" + model_name + ".pkl"))
        print("--------model restored--------")
    except:
        print("--------model not restored--------")
        pass'''

    #softMax = nn.Softmax()
    softMax = nn.Sigmoid()
    CE_loss = DicexCELoss()

    if torch.cuda.is_available():
        hdNet.cuda()
        softMax.cuda()
        CE_loss.cuda()

    # To-DO: Check that optimizer is the same (and same values) as the Theano implementation
    optimizer = torch.optim.Adam(hdNet.parameters(), lr=lr, betas=(0.9, 0.999))
    #optimizer = torch.optim.SGD(hdNet.parameters(), lr=lr, momentum = 0.9)
    print(" ~~~~~~~~~~~ Starting the training ~~~~~~~~~~")

    dscAll = []
    accall = []
    train_eval = []
    dsc_eval = []
    acc_eval = []

    d1 = 0

    if (opts.numModal == 2):
        imgPaths = [moda_1, moda_2]

    if (opts.numModal == 3):
        imgPaths = [moda_1, moda_2, moda_3]
    val_epochs = [0, 21, 41, 61, 81, 101, 121, 141]
    x_train, y_train, img_shape = load_data_trainG(imgPaths, moda_g,
                                                   imageNames_train,
                                                   samplesPerEpoch,
                                                   opts.numModal)
    #x_train = np.moveaxis(x_train, 1, -1)
    print(x_train.shape)
    numBatches = int(x_train.shape[0] / batch_size)
    idx = np.arange(x_train.shape[0])
    for e_i in range(epoch):
        hdNet.train()
        lossEpoch = []
        np.random.shuffle(idx)
        x_train = x_train[idx]
        y_train = y_train[idx]
        for b_i in range(numBatches):
            optimizer.zero_grad()
            hdNet.zero_grad()
            MRIs = numpy_to_var(x_train[b_i * batch_size:b_i * batch_size +
                                        batch_size, :, :, :, :])
            Segmentation = numpy_to_var(
                y_train[b_i * batch_size:b_i * batch_size +
                        batch_size, :, :, :])

            segmentation_prediction = hdNet(MRIs)
            #print("segmentation_prediction : ", segmentation_prediction.shape)
            #predClass_y = softMax(segmentation_prediction)
            segmentation_prediction = softMax(segmentation_prediction)
            # To adapt CE to 3D
            # LOGITS:
            if e_i == 0 and b_i == 0:
                print("MRIS : ", MRIs.shape)
                print("Segmentation : ", Segmentation.shape)
                print("segmentation_prediction : ",
                      segmentation_prediction.shape)
            #segmentation_prediction = segmentation_prediction.reshape((MRIs.shape[0], b, b, b))
            #segmentation_prediction = segmentation_prediction.permute(0,2,3,4,1).contiguous()
            segmentation_prediction = segmentation_prediction.reshape(-1)
            CE_loss_batch = CE_loss(
                segmentation_prediction,
                Segmentation.reshape(-1).type(torch.cuda.FloatTensor))
            loss = CE_loss_batch
            loss.backward()

            optimizer.step()
            lossEpoch.append(CE_loss_batch.cpu().data.numpy())

            printProgressBar(b_i + 1,
                             numBatches,
                             prefix="[Training] Epoch: {} ".format(e_i),
                             length=15)

            del MRIs
            del Segmentation
            del segmentation_prediction
            # del predClass_y

        if not os.path.exists(model_name):
            os.makedirs(model_name)

        np.save(os.path.join(model_name, model_name + '_loss.npy'), dscAll)

        print(' Epoch: {}, loss: {}'.format(e_i, np.mean(lossEpoch)))

        if (e_i % 10) == 0:

            if (opts.numModal == 2):
                moda_n = [moda_1_val, moda_2_val]
            if (opts.numModal == 3):
                moda_n = [moda_1_val, moda_2_val, moda_3_val]

            dsct, acct = inferencee(hdNet, x_train, y_train, imageNames_train,
                                    e_i, opts.save_dir, opts.numModal)
            dsc_eval.append(dsct)
            acc_eval.append(acct)
            print(' Metrics: The mean of train Accuracy is : {} '.format(acct))
            print(' Metrics: The mean of train DSC is : {} '.format(dsct))
            np.save(os.path.join(model_name, model_name + 'train_DSCs.npy'),
                    dsc_eval)
            np.save(os.path.join(model_name, model_name + 'train_ACC.npy'),
                    acc_eval)

            dsc, acc = inference(hdNet, moda_n, moda_g_val, imageNames_val,
                                 e_i, opts.save_dir, opts.numModal)

            dscAll.append(dsc)
            accall.append(acc)

            print(' Metrics: The mean of Accuracy is : {} '.format(
                np.mean(acc)))
            print(' Metrics: The mean of DSC is : {} '.format(np.mean(dsc)))
            if not os.path.exists(model_name):
                os.makedirs(model_name)

            np.save(os.path.join(model_name, model_name + '_DSCs.npy'), dscAll)
            np.save(os.path.join(model_name, model_name + '_ACC.npy'), accall)

            if np.mean(dsc) > 0.60:
                if not os.path.exists(model_name):
                    os.makedirs(model_name)
                torch.save(
                    hdNet,
                    os.path.join(model_name,
                                 "Best2_" + model_name + str(e_i) + ".pkl"))
    """
Esempio n. 22
0
def coffeeHeat(level):
    # this controls the heat cycle when brewing
    n = os.fork()
    if n == 0:
        #child process fills and then dies. requires level as low med high
        fillWhileHeat(level)
    h = 5  # this is how many seconds the temp must hold otherwise heat again
    getTemp()
    a = getTemp()
    #GPIO.output(22, GPIO.HIGH) #heater
    while (a > heatPointLow
           ):  # we need to let the sensor normalize to cold water being added
        a = getTemp()
        #print (a)
        time.sleep(0.1)
    a = getTemp()
    maxa = 0
    offset = a + h
    offsetTotal = heatPointHigh - a
    offsetHighA = heatPointHigh - offset
    printProgressBar(0,
                     offsetTotal,
                     prefix='Heating progress:',
                     suffix='Complete',
                     length=100)
    b = 0
    while (b <= h):
        while (a < heatPointHigh):
            b = 0
            GPIO.output(22, GPIO.HIGH)
            a = getTemp()
            offsetA = a - offset
            maxa = (offsetA if offsetA > maxa else maxa)
            maxa = (offsetHighA if offsetA > offsetHighA else maxa)
            time.sleep(0.1)
            printProgressBar((maxa),
                             offsetTotal,
                             prefix='Heating progress:',
                             suffix='Complete',
                             length=100)
        GPIO.output(22, GPIO.LOW)
        b = b + 1
        time.sleep(1)
        a = getTemp()
        offsetA = a - offset
        maxa = (offsetA if offsetA > maxa else offsetA)  # this is pointless
        maxa = (offsetHighA if offsetA > offsetHighA else maxa)
        time.sleep(0.1)
        c = maxa + b
        printProgressBar((c),
                         offsetTotal,
                         prefix='Heating progress:',
                         suffix='Complete',
                         length=100)
    printProgressBar(offsetTotal,
                     offsetTotal,
                     prefix='Heating progress:',
                     suffix='Complete',
                     length=100)
    print()
    for x in [10, 22]:
        GPIO.output(x, GPIO.LOW)
Esempio n. 23
0
def runTraining(args):
    print('-' * 40)
    print('~~~~~~~~  Starting the training... ~~~~~~')
    print('-' * 40)

    batch_size = args.batch_size
    batch_size_val = 1
    batch_size_val_save = 1
    lr = args.lr

    epoch = args.epochs
    root_dir = './DataSet_Challenge/Val_1'
    model_dir = 'model'
    
    print(' Dataset: {} '.format(root_dir))

    transform = transforms.Compose([
        transforms.ToTensor()
    ])

    mask_transform = transforms.Compose([
        transforms.ToTensor()
    ])

    train_set = medicalDataLoader.MedicalImageDataset('train',
                                                      root_dir,
                                                      transform=transform,
                                                      mask_transform=mask_transform,
                                                      augment=True,
                                                      equalize=False)

    train_loader = DataLoader(train_set,
                              batch_size=batch_size,
                              num_workers=5,
                              shuffle=True)

    val_set = medicalDataLoader.MedicalImageDataset('val',
                                                    root_dir,
                                                    transform=transform,
                                                    mask_transform=mask_transform,
                                                    equalize=False)

    val_loader = DataLoader(val_set,
                            batch_size=batch_size_val,
                            num_workers=5,
                            shuffle=False)
                                                   
    val_loader_save_images = DataLoader(val_set,
                                        batch_size=batch_size_val_save,
                                        num_workers=4,
                                        shuffle=False)

                                                                    
    # Initialize
    print("~~~~~~~~~~~ Creating the DAF Stacked model ~~~~~~~~~~")
    net = DAF_stack()
    print(" Model Name: {}".format(args.modelName))
    print(" Model ot create: DAF_Stacked")

    net.apply(weights_init)

    softMax = nn.Softmax()
    CE_loss = nn.CrossEntropyLoss()
    Dice_loss = computeDiceOneHot()
    mseLoss = nn.MSELoss()

    if torch.cuda.is_available():
        net.cuda()
        softMax.cuda()
        CE_loss.cuda()
        Dice_loss.cuda()

    optimizer = Adam(net.parameters(), lr=lr, betas=(0.9, 0.99), amsgrad=False)

    BestDice, BestEpoch = 0, 0
    BestDice3D = [0,0,0,0]

    d1Val = []
    d2Val = []
    d3Val = []
    d4Val = []

    d1Val_3D = []
    d2Val_3D = []
    d3Val_3D = []
    d4Val_3D = []

    d1Val_3D_std = []
    d2Val_3D_std = []
    d3Val_3D_std = []
    d4Val_3D_std = []

    Losses = []

    print("~~~~~~~~~~~ Starting the training ~~~~~~~~~~")
    for i in range(epoch):
        net.train()
        lossVal = []

        totalImages = len(train_loader)
       
        for j, data in enumerate(train_loader):
            image, labels, img_names = data

            # prevent batchnorm error for batch of size 1
            if image.size(0) != batch_size:
                continue

            optimizer.zero_grad()
            MRI = to_var(image)
            Segmentation = to_var(labels)

            ################### Train ###################
            net.zero_grad()

            # Network outputs
            semVector_1_1, \
            semVector_2_1, \
            semVector_1_2, \
            semVector_2_2, \
            semVector_1_3, \
            semVector_2_3, \
            semVector_1_4, \
            semVector_2_4, \
            inp_enc0, \
            inp_enc1, \
            inp_enc2, \
            inp_enc3, \
            inp_enc4, \
            inp_enc5, \
            inp_enc6, \
            inp_enc7, \
            out_enc0, \
            out_enc1, \
            out_enc2, \
            out_enc3, \
            out_enc4, \
            out_enc5, \
            out_enc6, \
            out_enc7, \
            outputs0, \
            outputs1, \
            outputs2, \
            outputs3, \
            outputs0_2, \
            outputs1_2, \
            outputs2_2, \
            outputs3_2 = net(MRI)

            segmentation_prediction = (outputs0 + outputs1 + outputs2 + outputs3 + outputs0_2 + outputs1_2 + outputs2_2 + outputs3_2) / 8
            predClass_y = softMax(segmentation_prediction)

            Segmentation_planes = getOneHotSegmentation(Segmentation)

            segmentation_prediction_ones = predToSegmentation(predClass_y)

            # It needs the logits, not the softmax
            Segmentation_class = getTargetSegmentation(Segmentation)

            # Cross-entropy loss
            loss0 = CE_loss(outputs0, Segmentation_class)
            loss1 = CE_loss(outputs1, Segmentation_class)
            loss2 = CE_loss(outputs2, Segmentation_class)
            loss3 = CE_loss(outputs3, Segmentation_class)
            loss0_2 = CE_loss(outputs0_2, Segmentation_class)
            loss1_2 = CE_loss(outputs1_2, Segmentation_class)
            loss2_2 = CE_loss(outputs2_2, Segmentation_class)
            loss3_2 = CE_loss(outputs3_2, Segmentation_class)

            lossSemantic1 = mseLoss(semVector_1_1, semVector_2_1)
            lossSemantic2 = mseLoss(semVector_1_2, semVector_2_2)
            lossSemantic3 = mseLoss(semVector_1_3, semVector_2_3)
            lossSemantic4 = mseLoss(semVector_1_4, semVector_2_4)

            lossRec0 = mseLoss(inp_enc0, out_enc0)
            lossRec1 = mseLoss(inp_enc1, out_enc1)
            lossRec2 = mseLoss(inp_enc2, out_enc2)
            lossRec3 = mseLoss(inp_enc3, out_enc3)
            lossRec4 = mseLoss(inp_enc4, out_enc4)
            lossRec5 = mseLoss(inp_enc5, out_enc5)
            lossRec6 = mseLoss(inp_enc6, out_enc6)
            lossRec7 = mseLoss(inp_enc7, out_enc7)

            lossG = loss0 + loss1 + loss2 + loss3 + loss0_2 + loss1_2 + loss2_2 + loss3_2 + 0.25 * (
            lossSemantic1 + lossSemantic2 + lossSemantic3 + lossSemantic4) \
            + 0.1 * (lossRec0 + lossRec1 + lossRec2 + lossRec3 + lossRec4 + lossRec5 + lossRec6 + lossRec7)  # CE_lossG

            # Compute the DSC
            DicesN, DicesB, DicesW, DicesT, DicesZ = Dice_loss(segmentation_prediction_ones, Segmentation_planes)

            DiceB = DicesToDice(DicesB)
            DiceW = DicesToDice(DicesW)
            DiceT = DicesToDice(DicesT)
            DiceZ = DicesToDice(DicesZ)

            Dice_score = (DiceB + DiceW + DiceT+ DiceZ) / 4

            lossG.backward()
            optimizer.step()
            
            lossVal.append(lossG.cpu().data.numpy())

            printProgressBar(j + 1, totalImages,
                             prefix="[Training] Epoch: {} ".format(i),
                             length=15,
                             suffix=" Mean Dice: {:.4f}, Dice1: {:.4f} , Dice2: {:.4f}, , Dice3: {:.4f}, Dice4: {:.4f} ".format(
                                 Dice_score.cpu().data.numpy(),
                                 DiceB.data.cpu().data.numpy(),
                                 DiceW.data.cpu().data.numpy(),
                                 DiceT.data.cpu().data.numpy(),
                                 DiceZ.data.cpu().data.numpy(),))

      
        printProgressBar(totalImages, totalImages,
                             done="[Training] Epoch: {}, LossG: {:.4f}".format(i,np.mean(lossVal)))
       
        # Save statistics
        modelName = args.modelName
        directory = 'Results/Statistics/' + modelName
        
        Losses.append(np.mean(lossVal))
        
        d1,d2,d3,d4 = inference(net, val_loader)
        
        d1Val.append(d1)
        d2Val.append(d2)
        d3Val.append(d3)
        d4Val.append(d4)

        if not os.path.exists(directory):
            os.makedirs(directory)

        np.save(os.path.join(directory, 'Losses.npy'), Losses)
        np.save(os.path.join(directory, 'd1Val.npy'), d1Val)
        np.save(os.path.join(directory, 'd2Val.npy'), d2Val)
        np.save(os.path.join(directory, 'd3Val.npy'), d3Val)

        currentDice = (d1+d2+d3+d4)/4

        print("[val] DSC: (1): {:.4f} (2): {:.4f}  (3): {:.4f} (4): {:.4f}".format(d1,d2,d3,d4)) # MRI

        currentDice = currentDice.data.numpy()

        # Evaluate on 3D
        saveImages_for3D(net, val_loader_save_images, batch_size_val_save, 1000, modelName, False, False)
        reconstruct3D(modelName, 1000, isBest=False)
        DSC_3D = evaluate3D(modelName)

        mean_DSC3D = np.mean(DSC_3D, 0)
        std_DSC3D = np.std(DSC_3D,0)

        d1Val_3D.append(mean_DSC3D[0])
        d2Val_3D.append(mean_DSC3D[1])
        d3Val_3D.append(mean_DSC3D[2])
        d4Val_3D.append(mean_DSC3D[3])
        d1Val_3D_std.append(std_DSC3D[0])
        d2Val_3D_std.append(std_DSC3D[1])
        d3Val_3D_std.append(std_DSC3D[2])
        d4Val_3D_std.append(std_DSC3D[3])

        np.save(os.path.join(directory, 'd0Val_3D.npy'), d1Val_3D)
        np.save(os.path.join(directory, 'd1Val_3D.npy'), d2Val_3D)
        np.save(os.path.join(directory, 'd2Val_3D.npy'), d3Val_3D)
        np.save(os.path.join(directory, 'd3Val_3D.npy'), d4Val_3D)
        
        np.save(os.path.join(directory, 'd0Val_3D_std.npy'), d1Val_3D_std)
        np.save(os.path.join(directory, 'd1Val_3D_std.npy'), d2Val_3D_std)
        np.save(os.path.join(directory, 'd2Val_3D_std.npy'), d3Val_3D_std)
        np.save(os.path.join(directory, 'd3Val_3D_std.npy'), d4Val_3D_std)


        if currentDice > BestDice:
            BestDice = currentDice

            BestEpoch = i
            
            if currentDice > 0.40:

                if np.mean(mean_DSC3D)>np.mean(BestDice3D):
                    BestDice3D = mean_DSC3D

                print("###    In 3D -----> MEAN: {}, Dice(1): {:.4f} Dice(2): {:.4f} Dice(3): {:.4f} Dice(4): {:.4f}   ###".format(np.mean(mean_DSC3D),mean_DSC3D[0], mean_DSC3D[1], mean_DSC3D[2], mean_DSC3D[3]))

                print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Saving best model..... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
                if not os.path.exists(model_dir):
                    os.makedirs(model_dir)
                torch.save(net.state_dict(), os.path.join(model_dir, "Best_" + modelName + ".pth"),pickle_module=dill)
                reconstruct3D(modelName, 1000, isBest=True)

        print("###                                                       ###")
        print("###    Best Dice: {:.4f} at epoch {} with Dice(1): {:.4f} Dice(2): {:.4f} Dice(3): {:.4f} Dice(4): {:.4f}   ###".format(BestDice, BestEpoch, d1,d2,d3,d4))
        print("###    Best Dice in 3D: {:.4f} with Dice(1): {:.4f} Dice(2): {:.4f} Dice(3): {:.4f} Dice(4): {:.4f} ###".format(np.mean(BestDice3D),BestDice3D[0], BestDice3D[1], BestDice3D[2], BestDice3D[3] ))
        print("###                                                       ###")

        if i % (BestEpoch + 50) == 0:
            for param_group in optimizer.param_groups:
                lr = lr*0.5
                param_group['lr'] = lr
                print(' ----------  New learning Rate: {}'.format(lr))
Esempio n. 24
0
        # no need to compute D_real as it does not affect G
        g_loss = -G_fakem

        # Optimize
        g_loss.backward()
        optimizerG.step()

        lossEpochG.append(g_loss.item())

        # update Gs with exponential moving average
        exp_mov_avg(Gs, G, alpha=0.999, global_step=global_step)

        printProgressBar(i + 1,
                         total + 1,
                         length=20,
                         prefix=f'Epoch {epoch} ',
                         suffix=f', d_loss: {d_loss.item():.3f}'
                         f', d_loss_W: {d_loss_W.item():.3f}'
                         f', GP: {gradient_penalty.item():.3f}'
                         f', progress: {P.p:.2f}')

    printProgressBar(
        total,
        total,
        done=f'Epoch [{epoch:>3d}]  d_loss: {np.mean(lossEpochD):.4f}'
        f', d_loss_W: {np.mean(lossEpochD_W):.3f}'
        f', progress: {P.p:.2f}, time: {time() - t0:.2f}s')

    d_losses = np.append(d_losses, lossEpochD)
    d_losses_W = np.append(d_losses_W, lossEpochD_W)
    g_losses = np.append(g_losses, lossEpochG)
Esempio n. 25
0
def runTraining(opts):
    print('' * 41)
    print('~' * 50)
    print('~~~~~~~~~~~~~~~~~  PARAMETERS ~~~~~~~~~~~~~~~~')
    print('~' * 50)
    print('  - Number of classes: {}'.format(opts.numClasses))
    print('  - Directory to load images: {}'.format(opts.root_dir))
    print('  - Directory to save results: {}'.format(opts.save_dir))
    print('  - To model will be saved as : {}'.format(opts.modelName))
    print('-' * 41)
    print('  - Number of epochs: {}'.format(opts.numClasses))
    print('  - Batch size: {}'.format(opts.batchSize))
    print('  - Number of samples per epoch: {}'.format(opts.numSamplesEpoch))
    print('  - Learning rate: {}'.format(opts.l_rate))
    print('  - Perform validation each {} epochs'.format(opts.freq_inference))
    print('' * 41)

    print('-' * 41)
    print('~~~~~~~~  Starting the training... ~~~~~~')
    print('-' * 41)
    print('' * 40)

    samplesPerEpoch = opts.numSamplesEpoch
    batch_size = opts.batchSize

    lr = 0.0002
    epoch = opts.numEpochs

    root_dir = opts.root_dir
    model_name = opts.modelName

    moda_1 = root_dir + 'Training/T1'
    moda_g = root_dir + 'Training/GT'

    print(' --- Getting image names.....')
    print(' - Training Set: -')
    if os.path.exists(moda_1):
        imageNames_train = [
            f for f in os.listdir(moda_1) if isfile(join(moda_1, f))
        ]
        imageNames_train.sort()
        print(' ------- Images found ------')
        for i in range(len(imageNames_train)):
            print(' - {}'.format(imageNames_train[i]))
    else:
        raise Exception(' - {} does not exist'.format(moda_1))

    moda_1_val = root_dir + 'Validation/T1'
    moda_g_val = root_dir + 'Validation/GT'

    print(' --------------------')
    print(' - Validation Set: -')
    if os.path.exists(moda_1):
        imageNames_val = [
            f for f in os.listdir(moda_1_val) if isfile(join(moda_1_val, f))
        ]
        imageNames_val.sort()
        print(' ------- Images found ------')
        for i in range(len(imageNames_val)):
            print(' - {}'.format(imageNames_val[i]))
    else:
        raise Exception(' - {} does not exist'.format(moda_1_val))

    print("~~~~~~~~~~~ Creating the model ~~~~~~~~~~")
    num_classes = opts.numClasses

    # Define HyperDenseNet
    # To-Do. Get as input the config settings to create different networks
    if (opts.network == 'liviaNet'):
        print('.... Building LiviaNET architecture....')
        liviaNet = LiviaNet(num_classes)
    else:
        print('.... Building SemiDenseNet architecture....')
        liviaNet = LiviaSemiDenseNet(num_classes)
    '''try:
        hdNet = torch.load(os.path.join(model_name, "Best_" + model_name + ".pkl"))
        print("--------model restored--------")
    except:
        print("--------model not restored--------")
        pass'''

    softMax = nn.Softmax()
    CE_loss = nn.CrossEntropyLoss()

    if torch.cuda.is_available():
        liviaNet.cuda()
        softMax.cuda()
        CE_loss.cuda()

    # To-DO: Check that optimizer is the same (and same values) as the Theano implementation
    optimizer = torch.optim.Adam(liviaNet.parameters(),
                                 lr=lr,
                                 betas=(0.9, 0.999))

    print(" ~~~~~~~~~~~ Starting the training ~~~~~~~~~~")
    print(' --------- Params: ---------')

    numBatches = int(samplesPerEpoch / batch_size)

    print(' - Number of batches: {} ----'.format(numBatches))

    dscAll = []
    for e_i in range(epoch):
        liviaNet.train()

        lossEpoch = []

        x_train, y_train, img_shape = load_data_train(
            moda_1, moda_g, imageNames_train, samplesPerEpoch
        )  # hardcoded to read the first file. Loop this to get all files. Karthik

        for b_i in range(numBatches):
            optimizer.zero_grad()
            liviaNet.zero_grad()

            MRIs = numpy_to_var(x_train[b_i * batch_size:b_i * batch_size +
                                        batch_size, :, :, :, :])
            Segmentation = numpy_to_var(
                y_train[b_i * batch_size:b_i * batch_size +
                        batch_size, :, :, :])

            segmentation_prediction = liviaNet(MRIs)

            predClass_y = softMax(segmentation_prediction)

            # To adapt CE to 3D
            # LOGITS:
            segmentation_prediction = segmentation_prediction.permute(
                0, 2, 3, 4, 1).contiguous()
            segmentation_prediction = segmentation_prediction.view(
                segmentation_prediction.numel() // num_classes, num_classes)

            CE_loss_batch = CE_loss(
                segmentation_prediction,
                Segmentation.view(-1).type(torch.cuda.LongTensor))

            loss = CE_loss_batch
            loss.backward()

            optimizer.step()
            lossEpoch.append(CE_loss_batch.cpu().data.numpy())

            printProgressBar(b_i + 1,
                             numBatches,
                             prefix="[Training] Epoch: {} ".format(e_i),
                             length=15)

            del MRIs
            del Segmentation
            del segmentation_prediction
            del predClass_y

        if not os.path.exists(model_name):
            os.makedirs(model_name)

        np.save(os.path.join(model_name, model_name + '_loss.npy'), dscAll)

        print(' Epoch: {}, loss: {}'.format(e_i, np.mean(lossEpoch)))

        if (e_i % opts.freq_inference) == 0:
            dsc = inference(liviaNet, moda_1_val, moda_g_val, imageNames_val,
                            e_i, opts.save_dir)
            dscAll.append(dsc)
            print(
                ' Metrics: DSC(mean): {} per class: 1({}) 2({}) 3({})'.format(
                    np.mean(dsc), np.mean(dsc[:, 0]), np.mean(dsc[:, 1]),
                    np.mean(dsc[:, 2])))
            if not os.path.exists(model_name):
                os.makedirs(model_name)

            np.save(os.path.join(model_name, model_name + '_DSCs.npy'), dscAll)

        d1 = np.mean(dsc)
        if (d1 > 0.60):
            if not os.path.exists(model_name):
                os.makedirs(model_name)

            torch.save(liviaNet,
                       os.path.join(model_name, "Best_" + model_name + ".pkl"))

        if (100 + e_i % 20) == 0:
            lr = lr / 2
            print(' Learning rate decreased to : {}'.format(lr))
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr
Esempio n. 26
0
def inference(net, temperature, img_batch, batch_size, epoch, deepSupervision, modelName, minSize, maxSize):

    directory = 'Results/ImagesViolationConstraint/NIPS/' + modelName +'/Epoch_' + str(epoch)
    if not os.path.exists(directory):
        os.makedirs(directory)
            
    total = len(img_batch)

    Dice1 = torch.zeros(total, 2)
    
    net.eval()
    
    img_names_ALL = []

    softMax = nn.Softmax()
    softMax.cuda()
    
    dice = computeDiceOneHotBinary().cuda()

    targetSizeArrBatches_val = []
    predSizeArrBatches_val = []
    violatedCases = []
    numImages = len(img_batch)

    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="[Inference] Getting segmentations...", length=30)
        image, labels, labels_weak, img_names = data
        img_names_ALL.append(img_names[0].split('/')[-1].split('.')[0])

        MRI = to_var(image)
        Segmentation = to_var(labels)

        
        if deepSupervision == False:
            segmentation_prediction = net(MRI)
        else:
            segmentation_prediction, seg3,seg2,seg1 = net(MRI)

        Segmentation_planes = getOneHotSegmentation_Bin(Segmentation)
        
        pred_y = softMax(segmentation_prediction/temperature) 

        # ----- To compute the predicted and target size ------
        predSize = torch.sum((pred_y[:,1,:,:] > 0.5).type(torch.FloatTensor))
        predSizeNumpy = predSize.cpu().data.numpy()[0]

        LV_target = (labels == 1).type(torch.FloatTensor)
        targetSize = torch.sum(LV_target)
        targetSizeNumpy = targetSize # targetSize.cpu().data.numpy()[0]

        predSizeArrBatches_val.append(predSizeNumpy)
        targetSizeArrBatches_val.append(targetSizeNumpy)
            
        '''softmax_y = pred_y.cpu().data.numpy()
        softB = softmax_y[:,1,:,:]

        # Hard-Size
        pixelsClassB = np.where( softB > 0.5 )
        
        predLabTemp = np.zeros(softB.shape)
        predLabTemp[pixelsClassB] = 1.0
        sizePredNumpy = predLabTemp.sum()
        #minSize = 97.9
        #maxSize = 1722.6
        
        sizeCase = []
        idx = np.where(labels.numpy()==1.0)
        sizeLV_GT = len(idx[0])
        sizesGT.append(sizeLV_GT)
        sizesPred.append(sizePredNumpy)'''

        '''if sizeLV_GT > 0:
            if sizePredNumpy < minSize:
                #violated +=1
                out = torch.cat((MRI, pred_y[:,1,:,:].view(1,1,256,256), Segmentation))
                name2save = img_names[0].split('./ACDC-2D-All/val/Img/')
                name2save = name2save[1].split('.png')
                torchvision.utils.save_image(out.data, os.path.join(directory, name2save[0]+'_Lower_'+str(minSize-sizePredNumpy)+'.png'),
                                     nrow=batch_size,
                                     padding=2,
                                     normalize=False,
                                     range=None,
                                     scale_each=False,
                                     pad_value=0)
            
            if sizePredNumpy > maxSize:
                #violated +=1
                
                out = torch.cat((MRI, pred_y[:,1,:,:].view(1,1,256,256), Segmentation))
                name2save = img_names[0].split('./ACDC-2D-All/val/Img/')
                name2save = name2save[1].split('.png')
                torchvision.utils.save_image(out.data, os.path.join(directory, name2save[0]+'_Upper_'+str(sizePredNumpy-maxSize)+'.png'),
                                     nrow=batch_size,
                                     padding=2,
                                     normalize=False,
                                     range=None,
                                     scale_each=False,
                                     pad_value=0)

        else:
            if sizePredNumpy > 0:
                a = 0
                #segmentation = getSingleImageBin(pred_y)
                out = torch.cat((MRI, pred_y[:,1,:,:].view(1,1,256,256), Segmentation))
                name2save = img_names[0].split('./ACDC-2D-All/val/Img/')

                name2save = name2save[1].split('.png')
                torchvision.utils.save_image(out.data, os.path.join(directory, name2save[0]+'_'+str(sizePredNumpy)+'.png'),
                                     nrow=batch_size,
                                     padding=2,
                                     normalize=False,
                                     range=None,
                                     scale_each=False,
                                     pad_value=0)'''

               
        #DicesN, Dices1 = dice(segmentation_prediction, Segmentation_planes)
        DicesN, Dices1 = dice(pred_y, Segmentation_planes)

        Dice1[i] = Dices1.data

        # Save images
        # directory = 'resultBladder'
        # if not os.path.exists(directory):
        #     os.makedirs(directory)
        # filenameImg = os.path.join(directory, "original_image_{}_{}.png".format(epoch, i))
        # filenameMask = os.path.join(directory, "groundTruth_image_{}_{}.png".format(epoch, i))
        # filenamePred = os.path.join(directory, "Prediction_{}_{}.png".format(epoch, i))
    printProgressBar(total, total, done="[Inference] Segmentation Done !")

    ValDice1 = DicesToDice(Dice1)
   
    #percViol = (violated/numImages)*100
    
    #print(' [VAL] Constrained violated in {} images: {:.4f} %'.format(violated,percViol))

    return [ValDice1,targetSizeArrBatches_val, predSizeArrBatches_val]
Esempio n. 27
0
def inference_multiTask(net, img_batch, batch_size, epoch, deepSupervision):
    total = len(img_batch)

    Dice1 = torch.zeros(total, 2)
    Dice2 = torch.zeros(total, 2)
    Dice3 = torch.zeros(total, 2)
    
    net.eval()

    img_names_ALL = []

    dice = computeDiceOneHot().cuda()
    voldiff = []
    xDiff = []
    yDiff = []

    for i, data in enumerate(img_batch):
        printProgressBar(i, total, prefix="[Inference] Getting segmentations...", length=30)
        image, labels, img_names = data
        img_names_ALL.append(img_names[0].split('/')[-1].split('.')[0])

        MRI = to_var(image)
        Segmentation = to_var(labels)

        
        if deepSupervision == False:
            segmentation_prediction, reg_output = net(MRI)
        else:
            segmentation_prediction, seg3,seg2,seg1 = net(MRI)

        Segmentation_planes = getOneHotSegmentation(Segmentation)
        
        # Regression
        feats = getValuesRegression(labels)
        feats_t = torch.from_numpy(feats).float()
        featsVar = to_var(feats_t)
        
        diff =  reg_output - featsVar 
        diff_np = diff.cpu().data.numpy()
        
        voldiff.append(diff_np[0][0])
        xDiff.append(diff_np[0][1])
        yDiff.append(diff_np[0][2])

                    
        DicesN, Dices1, Dices2, Dices3= dice(segmentation_prediction, Segmentation_planes)

        Dice1[i] = Dices1.data
        Dice2[i] = Dices2.data
        Dice3[i] = Dices3.data
       

        # Save images
        # directory = 'resultBladder'
        # if not os.path.exists(directory):
        #     os.makedirs(directory)
        # filenameImg = os.path.join(directory, "original_image_{}_{}.png".format(epoch, i))
        # filenameMask = os.path.join(directory, "groundTruth_image_{}_{}.png".format(epoch, i))
        # filenamePred = os.path.join(directory, "Prediction_{}_{}.png".format(epoch, i))
    printProgressBar(total, total, done="[Inference] Segmentation Done !")

    ValDice1 = DicesToDice(Dice1)
    ValDice2 = DicesToDice(Dice2)
    ValDice3 = DicesToDice(Dice3)
   
    return [ValDice1,ValDice2,ValDice3, voldiff, xDiff, yDiff]
print("Please Wait...")
while (success):
    name = './' + output_folder + '/' + str('%06d' % currentFrame) + '.jpg'

    if debugging:
        if currentFrame >= start and currentFrame <= end and currentFrame % fps == 0:
            cv2.imwrite(name, frame)
    else:
        if currentFrame % fps == 0:
            cv2.imwrite(name, frame)

    if debugging:
        if currentFrame >= end:
            break

    # Printing progress bar
    printProgressBar(currentFrame,
                     totalFrames,
                     prefix='Progress:',
                     suffix='Progress',
                     length=50)
    # for next iteration
    currentFrame += 1
    success, frame = cap.read()

# release the memory with cap variable
cap.release()
cv2.destroyAllWindows()

print("\nComplete")
Esempio n. 29
0
def inference(network, moda_1, moda_g, imageNames, epoch, folder_save):
    '''root_dir = './Data/MRBrainS/DataNii/'
    model_dir = 'model'

    moda_1 = root_dir + 'Training/T1'
    moda_g = root_dir + 'Training/GT'''
    network.eval()
    softMax = nn.Softmax()
    numClasses = 4  # Move this out
    if torch.cuda.is_available():
        softMax.cuda()
        network.cuda()

    dscAll = np.zeros(
        (len(imageNames), numClasses - 1))  # 1 class is the background!!
    for i_s in range(len(imageNames)):
        patch_1, patch_g, img_shape = load_data_test(
            moda_1, moda_g, imageNames[i_s]
        )  # hardcoded to read the first file. Loop this to get all files
        patchSize = 27
        patchSize_gt = 9
        x = np.zeros((0, 3, patchSize, patchSize, patchSize))
        x = np.vstack(
            (x, np.zeros(
                (patch_1.shape[0], 3, patchSize, patchSize, patchSize))))
        x[:, 0, :, :, :] = patch_1

        pred_numpy = np.zeros(
            (0, numClasses, patchSize_gt, patchSize_gt, patchSize_gt))
        pred_numpy = np.vstack(
            (pred_numpy,
             np.zeros((patch_1.shape[0], numClasses, patchSize_gt,
                       patchSize_gt, patchSize_gt))))
        totalOp = len(imageNames) * patch_1.shape[0]
        #pred = network(numpy_to_var(x[0,:,:,:,:]).view(1,3,patchSize,patchSize,patchSize))
        for i_p in range(patch_1.shape[0]):
            pred = network(
                numpy_to_var(x[i_p, :, :, :, :].reshape(
                    1, 3, patchSize, patchSize, patchSize)))
            pred_y = softMax(pred)
            pred_numpy[i_p, :, :, :, :] = pred_y.cpu().data.numpy()

            printProgressBar(i_s * ((totalOp + 0.0) / len(imageNames)) + i_p +
                             1,
                             totalOp,
                             prefix="[Validation] ",
                             length=15)

        # To reconstruct the predicted volume
        extraction_step_value = 9
        pred_classes = np.argmax(pred_numpy, axis=1)

        pred_classes = pred_classes.reshape(
            (len(pred_classes), patchSize_gt, patchSize_gt, patchSize_gt))

        bin_seg = my_reconstruct_volume(
            pred_classes, (img_shape[1], img_shape[2], img_shape[3]),
            patch_shape=(27, 27, 27),
            extraction_step=(extraction_step_value, extraction_step_value,
                             extraction_step_value))

        bin_seg = bin_seg[:, :, extraction_step_value:img_shape[3] -
                          extraction_step_value]
        gt = nib.load(moda_g + '/' + imageNames[i_s]).get_data()

        img_pred = nib.Nifti1Image(bin_seg, np.eye(4))
        img_gt = nib.Nifti1Image(gt, np.eye(4))

        img_name = imageNames[i_s].split('.nii')
        name = 'Pred_' + img_name[0] + '_Epoch_' + str(epoch) + '.nii.gz'

        namegt = 'GT_' + img_name[0] + '_Epoch_' + str(epoch) + '.nii.gz'

        if not os.path.exists(folder_save + 'Segmentations/'):
            os.makedirs(folder_save + 'Segmentations/')

        if not os.path.exists(folder_save + 'GT/'):
            os.makedirs(folder_save + 'GT/')

        nib.save(img_pred, folder_save + 'Segmentations/' + name)
        nib.save(img_gt, folder_save + 'GT/' + namegt)

        dsc = evaluateSegmentation(gt, bin_seg)

        dscAll[i_s, :] = dsc

    return dscAll
Esempio n. 30
0
def bound_update(unary,
                 X,
                 kernel,
                 bound_lambda,
                 bound_iteration=20,
                 batch=False,
                 manual_parallel=False):
    """
    Here in this code, Q refers to Z in our paper.
    """
    start_time = timeit.default_timer()
    print("Inside Bound Update . . .")
    N, K = unary.shape
    oldE = float('inf')

    # Initialize the unary and Normalize
    if manual_parallel == False:
        # print 'Parallel is FALSE'
        Q = normalize(-unary)
        for i in range(bound_iteration):
            printProgressBar(i + 1, bound_iteration, length=12)
            additive = -unary
            mul_kernel = kernel.dot(Q)
            Q = -bound_lambda * mul_kernel
            additive = additive - Q
            Q = normalize(additive)
            E = entropy_energy(Q, unary, kernel, bound_lambda, batch)
            #            print('entropy_energy is ' +repr(E) + ' at iteration ',i)
            report_E = E
            if (i > 1 and (abs(E - oldE) <= 1e-5 * abs(oldE))):
                print('Converged')
                break

            else:
                oldE = E.copy()
                oldQ = Q.copy()
                report_E = E
    else:
        print('Manual Parallel is TRUE')
        Q = normalize(-unary)

        init(kernel_s_data=n2m(kernel.data))
        init(kernel_s_indices=n2m(kernel.indices))
        init(kernel_s_indptr=n2m(kernel.indptr))
        init(kernel_s_shape=n2m(kernel.shape))

        irange = range(N)
        krange = range(K)

        for i in range(bound_iteration):
            printProgressBar(i + 1, bound_iteration, length=15)
            additive = -unary
            init(Q_s=n2m(Q))
            pool = multiprocessing.Pool(processes=5,
                                        initializer=init,
                                        initargs=list(SHARED_VARS.items()))
            pool.map(mpassing, itertools.product(irange, krange))
            _, Q = get_shared_arrays('kernel_s_indptr', 'Q_s')
            Q = -bound_lambda * Q
            #            assert (Q.all()==SHARED_array['Q_s'].all())
            additive -= Q
            Q = normalize(additive)
            pool.close()
            pool.join()
            pool.terminate()
            E = entropy_energy(Q, unary, kernel, bound_lambda, batch)
            #            print ('entropy_energy is ' +repr(E) + ' at iteration ',i)
            if (i > 1 and (abs(E - oldE) <= 1e-4 * abs(oldE))):
                print('Converged')
                break
            else:
                oldE = E.copy()
                oldQ = Q.copy()
                report_E = E

    elapsed = timeit.default_timer() - start_time
    print('\n Elapsed Time in bound_update', elapsed)
    l = np.argmax(Q, axis=1)
    ind = np.argmax(Q, axis=0)
    C = X[ind, :]
    return l, C, ind, Q, report_E