Пример #1
0
    def run(self, rect, cur_frame, next_frame):
        x, y, w, h = rect
        cur_roi = PostProcessing.get_roi_from_images(rect, cur_frame)
        center_of_window = (x + (w / 2), y + (h / 2))

        # compute centroid of current frame
        cur_moment = cv2.moments(cur_roi)
        cx = x + int(cur_moment['m10'] / cur_moment['m00'])
        cy = y + int(cur_moment['m01'] / cur_moment['m00'])
        cur_frame_centroid = (cx, cy)

        # compute centroid of next frame with current windows
        cur_roi_next = PostProcessing.get_roi_from_images(rect, next_frame)
        cur_moment_next = cv2.moments(cur_roi_next)
        next_cx = x + int(cur_moment_next['m10'] / cur_moment_next['m00'])
        next_cy = y + int(cur_moment_next['m01'] / cur_moment_next['m00'])
        next_frame_centroid = (next_cx, next_cy)

        # calculate distance between current frame centroid and next frame centroid
        x0, y0 = cur_frame_centroid
        x1, y1 = next_frame_centroid
        xwin, ywin = center_of_window
        new_center_of_window = ((xwin + (x1 - x0)), (ywin + (y1 - y0)))
        new_rect = (new_center_of_window[0] - (w / 2), new_center_of_window[1] - (h / 2), w, h)
        print new_rect

        pass
Пример #2
0
def removeEffect(effect):
    ch = list(PostProcessing.chain())
    try:
        ch.remove(effect)
        PostProcessing.chain(ch)
    except ValueError:
        pass
Пример #3
0
    def disable(self):
        self.__curMode = None
        for effect in self.__curEffects:
            effect.disable()

        self.__curEffects = []
        PostProcessing.chain([])
    def apply(self, cur_image):
        """
        apply the algorithm for running average in color
        :param cur_image: numpy array; a color image (RGB)
        :return new_objects_box: array consists of new object squares
        :return new_fg: binary image consists of image (black and white)
        """
        cur_image_gray = cv2.cvtColor(cur_image, cv2.COLOR_BGR2GRAY)
        if self.prev_image is not None:
            threshold_array = np.multiply(np.ones_like(cur_image_gray, 'uint8'), self.threshold)
            diff = np.absolute(np.subtract(cur_image_gray, self.prev_frame))
            fg_raw = np.multiply(
                np.ones_like(cur_image_gray, 'uint8'),
                np.where(
                    np.less(diff, threshold_array),
                    0,
                    255
                )
            )
            raw_boxes, new_fg = PostProcessing.foreground_process(fg_raw)
            new_objects_box = PostProcessing.bounding_box_mask(raw_boxes, new_fg)
        else:
            new_fg = np.zeros_like(cur_image_gray)
            new_objects_box = []

        self.prev_image = np.copy(cur_image_gray)
        return new_objects_box, new_fg
Пример #5
0
def removeEffect(effect):
    ch = list(PostProcessing.chain())
    try:
        ch.remove(effect)
        PostProcessing.chain(ch)
    except ValueError:
        pass
Пример #6
0
    def load(self, pSection, prereqs = None):
        """
        This method loads a .ppchain file. You may also specify additional
        properties to set on the chain.
        """
        if PostProcessing.isSupported(pSection.asString):
            actor = PostProcessing.load(pSection.asString)
            if actor == None or len(actor) == 0:
                ERROR_MSG('Could not load PostProcessing chain %s' % (pSection.asString,))
                actor = []
        else:
            actor = []
        for name, section in pSection.items():
            if name == 'Property':
                varName = section.asString
                if section.has_key('Float'):
                    PostProcessing.setMaterialProperty(actor, varName, section.readFloat('Float'))
                elif section.has_key('Vector4'):
                    PostProcessing.setMaterialProperty(actor, varName, section.readVector4('Vector4'))
                elif section.has_key('Colour'):
                    col = section.readVector4('Colour')
                    col[0] /= 255.0
                    col[1] /= 255.0
                    col[2] /= 255.0
                    col[3] /= 255.0
                    PostProcessing.setMaterialProperty(actor, varName, col)
                else:
                    PostProcessing.setMaterialProperty(actor, varName, section.asString)

        return actor
Пример #7
0
    def load(self, pSection, prereqs=None):
        """
        This method loads a .ppchain file. You may also specify additional
        properties to set on the chain.
        """
        if PostProcessing.isSupported(pSection.asString):
            actor = PostProcessing.load(pSection.asString)
            if actor == None or len(actor) == 0:
                ERROR_MSG('Could not load PostProcessing chain %s' %
                          (pSection.asString, ))
                actor = []
        else:
            actor = []
        for name, section in pSection.items():
            if name == 'Property':
                varName = section.asString
                if section.has_key('Float'):
                    PostProcessing.setMaterialProperty(
                        actor, varName, section.readFloat('Float'))
                elif section.has_key('Vector4'):
                    PostProcessing.setMaterialProperty(
                        actor, varName, section.readVector4('Vector4'))
                elif section.has_key('Colour'):
                    col = section.readVector4('Colour')
                    col[0] /= 255.0
                    col[1] /= 255.0
                    col[2] /= 255.0
                    col[3] /= 255.0
                    PostProcessing.setMaterialProperty(actor, varName, col)
                else:
                    PostProcessing.setMaterialProperty(actor, varName,
                                                       section.asString)

        return actor
Пример #8
0
    def fini(self):
        self.__curEffects = []
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.destroy()

        PostProcessing.fini()
        self.__saveSettings()
Пример #9
0
    def disable(self):
        self.__curMode = None
        for effect in self.__curEffects:
            effect.disable()

        self.__curEffects = []
        PostProcessing.chain([])
        return
Пример #10
0
    def fini(self):
        self.__curEffects = []
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.destroy()

        PostProcessing.fini()
        self.__saveSettings()
Пример #11
0
    def detach(self, actor, source, target = None):
        ch = PostProcessing.chain()
        for effect in actor:
            try:
                ch.remove(effect)
            except ValueError:
                pass

        PostProcessing.chain(ch)
Пример #12
0
    def detach(self, actor, source, target = None):
        ch = PostProcessing.chain()
        for effect in actor:
            try:
                ch.remove(effect)
            except ValueError:
                pass

        PostProcessing.chain(ch)
Пример #13
0
def main():
    print("Welcome to Organogenesis")
    print("------------------")

    #Keep a list of organs which can be contoured
    OARs = ["Body", "Spinal Cord", "Oral Cavity", "Left Parotid", "Right Parotid"] #Later will add an "all" option

    #Need to get user input. Make a string to easily ask for a number corresponding to an OAR.
    ChooseOAR_string = "Please enter the number for the organ you wish to contour / train a model for \n>>"
    for i in range(len(OARs)):
        ChooseOAR_string += str(i + 1) + ": " + OARs[i] + "\n"
    while True:
        try:
            chosenOAR = int(input(ChooseOAR_string)) - 1
            if chosenOAR < len(OARs):
                break
        except KeyboardInterrupt:
            quit()    
        except: pass     

    #Now determine if the goal is to train or to find contours
    chooseTask_string = "Please enter the number for the desired task\n"
    chooseTask_string += "1. Train a UNet model for predicting " + str(OARs[chosenOAR])
    chooseTask_string += "\n2. Predict " + str(OARs[chosenOAR]) + " contours using an existing model"
    chooseTask_string += "\n3. Determine threshold accuracies for predictions of the " + str(OARs[chosenOAR])
    chooseTask_string += "\n4. Determine F-score for validation set of the " + str(OARs[chosenOAR])
    chooseTask_string += "\n5. Plot predicted masks for the  " + str(OARs[chosenOAR])
    chooseTask_string += "\n6. Export model to ONNX"
    chooseTask_string += "\n7. predict using ONNX model \n>>"
    while True:
        try:
            task = int(input(chooseTask_string))
            if (task in range(0,8)):
                break
        except KeyboardInterrupt:
            quit()
        except: pass   

    if (task == 1):
        Train.Train(OARs[chosenOAR], 12, 1e-3, processData=False, loadModel=False)
        Test.Best_Threshold(OARs[chosenOAR],400)

        Test.TestPlot(OARs[chosenOAR], threshold=0.1)  
    elif task == 2:    
        Predict.GetContours(OARs[chosenOAR],"P86", threshold = 0.09, withReal=True, tryLoad=False) 
    elif task == 3:
        Test.Best_Threshold(OARs[chosenOAR],  testSize=500, onlyMasks=False,onlyBackground=False)
    elif task == 4:
        F_Score, recall, precision, accuracy = Test.FScore(OARs[chosenOAR], threshold=0.2)    
        print([F_Score, recall, precision, accuracy])
    elif task == 5:
        Test.TestPlot(OARs[chosenOAR], threshold=0.2) 
    elif task == 6:
        PostProcessing.Export_To_ONNX(OARs[chosenOAR])    
    elif task == 7:
        PostProcessing.Infer_From_ONNX(OARs[chosenOAR], 'P7')        
Пример #14
0
 def init(self):
     self.__loadSettings()
     PostProcessing.g_graphicsSettingListeners.append(_FuncObj(self, 'onSelectQualityOption'))
     PostProcessing.init()
     PostProcessing.chain(None)
     section = ResMgr.openSection(WGPostProcessing.__CONFIG_FILE_NAME)
     self.__load(section)
     for mode in self.__modes.itervalues():
         for effect in mode:
             effect.create()
Пример #15
0
    def fini(self):
        from account_helpers.SettingsCore import g_settingsCore
        g_settingsCore.onSettingsChanged -= self.__onSettingsChanging
        self.__curEffects = []
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.destroy()

        PostProcessing.fini()
        self.__saveSettings()
Пример #16
0
    def fini(self):
        from account_helpers.SettingsCore import g_settingsCore
        g_settingsCore.onSettingsChanged -= self.__onSettingsChanging
        self.__curEffects = []
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.destroy()

        PostProcessing.fini()
        self.__saveSettings()
Пример #17
0
    def onBlurred(self, callbackFn):
        import PostProcessing
        c = list(PostProcessing.chain())
        ch = []
        for e in c:
            if e.name != 'Teleport Progress Bar':
                ch.append(e)

        PostProcessing.chain(ch)
        if callbackFn:
            callbackFn()
Пример #18
0
    def onBlurred(self, callbackFn):
        import PostProcessing
        c = list(PostProcessing.chain())
        ch = []
        for e in c:
            if e.name != 'Teleport Progress Bar':
                ch.append(e)

        PostProcessing.chain(ch)
        if callbackFn:
            callbackFn()
    def go(self, effect, actor, source, target, **kargs):
        duration = effect.totalDuration
        try:
            self.v4 = Math.Vector4Translation(source.root)
        except:
            try:
                self.v4 = Math.Vector4Translation(source.model.root)
            except:
                self.v4 = Math.Vector4Translation(source.source)

        PostProcessing.setMaterialProperty(actor, self.propName, self.v4)
        return duration
Пример #20
0
    def enable(self, mode):
        if self.__modes.get(mode, None) is None:
            LOG_WARNING('Effect mode with name %s was not found.' % mode)
            return
        self.__curMode = mode
        self.__gatherEffects('common')
        self.__gatherEffects(mode)
        chain = []
        for effect in self.__curEffects:
            chain += effect.enable(self.__settings)

        PostProcessing.chain(chain)
    def go(self, effect, actor, source, target, **kargs):
        duration = effect.totalDuration
        try:
            self.v4 = Math.Vector4Translation(source.root)
        except:
            try:
                self.v4 = Math.Vector4Translation(source.model.root)
            except:
                self.v4 = Math.Vector4Translation(source.source)

        PostProcessing.setMaterialProperty(actor, self.propName, self.v4)
        return duration
Пример #22
0
    def init(self):
        self.__loadSettings()
        PostProcessing.g_graphicsSettingListeners.append(_FuncObj(self, 'onSelectQualityOption'))
        PostProcessing.init()
        PostProcessing.chain(None)
        section = ResMgr.openSection(WGPostProcessing.__CONFIG_FILE_NAME)
        self.__load(section)
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.create()

        return
 def preTeleport(self, callbackFn):
     import PostProcessing
     effect = PostProcessing.blur()
     effect.name = 'Teleport Progress Bar'
     effect.phases[-1].renderTarget = BigWorld.RenderTarget('teleportGobo', -3, -3)
     effect.phases[-1].material.additionalAlpha = 1.0
     c = list(PostProcessing.chain())
     c.append(effect)
     PostProcessing.chain(c)
     self.component.secondaryTexture = effect.phases[-1].renderTarget.texture
     self.component.freeze = None
     self.component.fader.value = 1.0
     BigWorld.callback(self.component.fader.speed, lambda : self.onBlurred(callbackFn))
     return
Пример #24
0
    def init(self):
        self.__loadSettings()
        PostProcessing.g_graphicsSettingListeners.append(_FuncObj(self, 'onSelectQualityOption'))
        PostProcessing.init()
        PostProcessing.chain(None)
        section = ResMgr.openSection(WGPostProcessing.__CONFIG_FILE_NAME)
        self.__load(section)
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.create()

        from account_helpers.SettingsCore import g_settingsCore
        g_settingsCore.onSettingsChanged += self.__onSettingsChanging
        return
Пример #25
0
    def enable(self, mode):
        if self.__modes.get(mode, None) is None:
            LOG_WARNING('Effect mode with name %s was not found.' % mode)
            return
        else:
            self.__curMode = mode
            self.__gatherEffects('common')
            self.__gatherEffects(mode)
            chain = []
            for effect in self.__curEffects:
                chain += effect.enable(self.__settings)

            PostProcessing.chain(chain)
            return
Пример #26
0
 def preTeleport(self, callbackFn):
     import PostProcessing
     effect = PostProcessing.blur()
     effect.name = 'Teleport Progress Bar'
     effect.phases[-1].renderTarget = BigWorld.RenderTarget('teleportGobo', -3, -3)
     effect.phases[-1].material.additionalAlpha = 1.0
     c = list(PostProcessing.chain())
     c.append(effect)
     PostProcessing.chain(c)
     self.component.secondaryTexture = effect.phases[-1].renderTarget.texture
     self.component.freeze = None
     self.component.fader.value = 1.0
     BigWorld.callback(self.component.fader.speed, lambda : self.onBlurred(callbackFn))
     return
Пример #27
0
    def init(self):
        self.__loadSettings()
        PostProcessing.g_graphicsSettingListeners.append(
            _FuncObj(self, 'onSelectQualityOption'))
        PostProcessing.init()
        PostProcessing.chain(None)
        section = ResMgr.openSection(WGPostProcessing.__CONFIG_FILE_NAME)
        self.__load(section)
        for mode in self.__modes.itervalues():
            for effect in mode:
                effect.create()

        from account_helpers.SettingsCore import g_settingsCore
        g_settingsCore.onSettingsChanged += self.__onSettingsChanging
        return
Пример #28
0
 def add_object(self, obj, frame):
     # set up ROI
     roi = PostProcessing.get_roi_from_images(obj, frame)
     hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
     n_in_frame = 0
     n_not_moving = 0
     self.list_of_objects.append((obj, hsv_roi, n_in_frame, n_not_moving))
Пример #29
0
    def run(self, cur_frame, next_frame,):
        # Setup the termination criteria, either 10 iteration or move by at least 1 pt
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        new_list_of_objects = []

        for obj_tuple in self.list_of_objects:
            hsv_roi = None
            if len(obj_tuple) == 4:
                obj, hsv_roi, n_in_frame, n_not_moving = obj_tuple
            if (hsv_roi is not None) and (obj[2] > 0 or obj[3] > 0):
                mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
                roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
                cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

                # track in next frame
                # backprojection
                hsv = cv2.cvtColor(next_frame, cv2.COLOR_BGR2HSV)
                dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

                # apply meanshift to get the new location
                ret, obj_new = cv2.meanShift(dst, obj, term_crit)
                n_in_frame += 1
                if PostProcessing.distance_two_squares(obj, obj_new) < 1:
                    n_not_moving += 1
                else:
                    n_not_moving = 0

                x, y, w, h = obj_new
                if n_not_moving < 20:
                    new_list_of_objects.append((obj_new, hsv_roi, n_in_frame, n_not_moving))

                # draw
                cv2.rectangle(next_frame, (x, y), (x + w, y + h), 255, 2)
        self.list_of_objects = new_list_of_objects
        pass
def RunBurnin(ModelType,modelvals,modelPopNames,resultsName,PopulationParameters,DiseaseParameters,endTime,mprandomseed,stepLength=1,writefolder='',startDate=datetime(2020,2,1),fitdates=[],hospitalizations=[],deaths=[],cases=[],fitper=.3,FolderContainer='',saveRun=False,historyData={},SavedRegionFolder=ParameterSet.SavedRegionFolder,burnin=True,vaccinationdata={}):
    
    if saveRun:
        if not os.path.exists(os.path.join(SavedRegionFolder,FolderContainer)):
            os.makedirs(os.path.join(SavedRegionFolder,FolderContainer))
            
    cleanUp(modelPopNames)
        
    print("RunBurnin: TransProb_AH Len:",len(DiseaseParameters['TransProb_AH']))
    PopulationData, GlobalInteractionMatrix, HospitalTransitionRate, HospitalNames, GlobalLocations, LocationImportationRisk = modelSetup(ModelType,modelvals,PopulationParameters,DiseaseParameters)
    
    ParameterVals = PopulationParameters
    ParameterVals.update(DiseaseParameters)
    
    RegionalList, timeRange, fitinfo = ProcessManager.RunModel(GlobalLocations, GlobalInteractionMatrix, HospitalTransitionRate,LocationImportationRisk,PopulationParameters,DiseaseParameters,endTime,resultsName,mprandomseed,startDate=startDate,modelPopNames=modelPopNames,fitdates=fitdates,hospitalizations=hospitalizations,deaths=deaths,cases=cases,fitper=fitper,burnin=burnin,FolderContainer=FolderContainer,saveRun=saveRun,historyData=historyData,SavedRegionFolder=SavedRegionFolder,vaccinationdata=vaccinationdata)

    if saveRun and fitinfo['fitted']:
        PostProcessing.WriteFitvals(resultsName,ModelType,fitinfo['SLSH'], fitinfo['SLSD'], fitinfo['SLSC'], fitinfo['avgperdiffhosp'], fitinfo['avgperdiffdeaths'], fitinfo['avgperdiffcases'],writefolder)
        Utils.PickleFileWrite(os.path.join(SavedRegionFolder,FolderContainer,"PopulationParameters.pickle"), PopulationParameters)
        Utils.PickleFileWrite(os.path.join(SavedRegionFolder,FolderContainer,"DiseaseParameters.pickle"), DiseaseParameters)
    else:
        if os.path.exists(os.path.join(SavedRegionFolder,FolderContainer)):
            os.rmdir(os.path.join(SavedRegionFolder,FolderContainer))
    cleanUp(modelPopNames,len(RegionalList))
    if os.path.exists(os.path.join(ParameterSet.ResultsFolder,"Results_"+resultsName+".pickle")):    
        os.remove(os.path.join(ParameterSet.ResultsFolder,"Results_"+resultsName+".pickle"))

    return fitinfo
Пример #31
0
def FScore(organ, threshold):
    model = Model.UNet()
    model.load_state_dict(torch.load(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + ".pt")))  
    model = model.cuda()
    model = model.eval()
    dataPath = 'Processed_Data/' + organ + "_Val/"
    dataFolder = os.path.join(pathlib.Path(__file__).parent.absolute(), dataPath)
    dataFiles = sorted(os.listdir(dataFolder))
    d = 0
    print('Calculating Statistics')
    TP = 0
    FP = 0
    FN = 0

    xLen,yLen = [0,0]
    while d <  150:#len(dataFiles):
        numStack = min(3, len(dataFiles) - 1 - d) #loading 1400 images at a time (takes about 20GB RAM)
        p = 0
        concatList = []
        while p < numStack:
            imagePath = dataFiles[d]
            image = pickle.load(open(os.path.join(dataFolder, imagePath), 'rb'))
            image = image.reshape((2,1,image.shape[2], image.shape[2]))
            concatList.append(image)
            p+=1
            d+=1
        if len(concatList) == 0:
            break    
        data = np.concatenate(concatList, axis=1)  
        numSlices = data.shape[1]
        for sliceNum in range(numSlices):
            x = torch.from_numpy(data[0, sliceNum, :, :]).cuda()
            y = torch.from_numpy(data[1:2, sliceNum, :,:]).cuda()
            xLen, yLen = x.shape
            #need to reshape 
            x.requires_grad = True
            y.requires_grad = True
            x = torch.reshape(x, (1,1,xLen,yLen)).float()
            y = torch.reshape(y, (1,1,xLen,yLen)).float()
            predictionRaw = (model(x)).cpu().detach().numpy()
            prediction = PostProcessing.Process(predictionRaw, threshold)
            for x_idx in range(xLen):
                for y_idx in range(yLen):
                    if prediction[0,0,x_idx,y_idx] == 1:
                        if y[0,0,x_idx,y_idx] == 1:
                            TP += 1
                        else:
                            FP += 1
                    elif y[0,0,x_idx,y_idx] == 1:     
                        FN += 1  
            print("Finished a patient")            
        print("Finished " + str(d) + "patients...")                     
    totalPoints = d * xLen * yLen                    
    recall = TP / (TP + FN)
    precision = TP / (TP + FP)
    F_Score = 2 / (recall**(-1) + precision ** (-1))
    accuracy = (totalPoints - FP - FN) / totalPoints
    return F_Score, recall, precision, accuracy

        
Пример #32
0
def getShimmerEffect():
    import PostProcessing
    c = PostProcessing.chain()
    for e in c:
        if e.name == 'heatShimmer':
            return e

    return
 def apply(self, data):
     rects, fg = PostProcessing.foreground_detection(data, self.bg, False)
     new_bg = np.where(
         np.equal(fg, 0)
         , np.add(((1 - self.alpha) * self.bg), (self.alpha * data))
         , np.add(((1 - self.beta) * self.bg), (self.beta * data))
     )
     return cv2.convertScaleAbs(new_bg)
Пример #34
0
def getShimmerEffect():
    import PostProcessing
    c = PostProcessing.chain()
    for e in c:
        if e.name == 'heatShimmer':
            return e

    return
    def apply(self, cur_image, cur_objects):
        """
        apply the algorithm for running average in color
        :param cur_image: numpy array; a color image (RGB)
        :param cur_objects: array consists of object squares
        :return new_objects_box: array consists of new object squares
        :return new_fg: binary image consists of image (black and white)
        """

        cols, rows, depth = cur_image.shape

        if self.bg is None:
            self.bg = np.copy(cur_image)

        if self.prev_frame is None:
            self.prev_frame = np.copy(cur_image)
            self.prev_prev_frame = np.copy(cur_image)

        # get neighbor pixels
        neighbor_pixels = map(
            lambda x: cv2.warpAffine(cur_image, x, (cols, rows)),
            self.WARP_MATRIX
        )

        # update background
        new_bg = np.add(((1 - self.alpha) * self.bg), (self.alpha * cur_image))

        # compare neighbor pixel with current background
        # neighbor_pixels_diff = map(
        #     lambda x: np.absolute(np.subtract(new_bg, x)),
        #     neighbor_pixels
        # )

        # get difference at this pixel
        diff = np.absolute(np.subtract(new_bg, cur_image))
        fg_raw = cv2.inRange(cv2.cvtColor(diff.astype('uint8'), cv2.COLOR_BGR2GRAY), 25, 255)
        raw_boxes, new_fg = PostProcessing.foreground_process(fg_raw)
        new_objects_box = PostProcessing.bounding_box_mask(raw_boxes, new_fg)

        cv2.imshow('Background', new_bg.astype('uint8'))
        self.bg = np.copy(new_bg)

        self.prev_prev_frame = np.copy(self.prev_frame)
        self.prev_frame = np.copy(cur_image)
        return new_objects_box, new_fg
Пример #36
0
 def __create(self, fileName):
     self.__chain = PostProcessing.load(fileName)
     if self.__chain is None:
         self.__chain = []
         self.__ctrl = None
         LOG_WARNING('Post effect <%s> was not loaded.' % fileName)
     elif self.__ctrlName is not None:
         self.__ctrl = getattr(post_effect_controllers, self.__ctrlName)()
         self.__ctrl.create()
Пример #37
0
def loadStyle(ds, fadeSpeed=1.0):
    """
    This function loads a bloom style from the given data section.  It smoothly
    changes from the current bloom settings to the new settings over
    "fadeSpeed" seconds.
    """
    newBloom = None
    if ds != None:
        enable = ds.readBool('enable', True)
        filterMode = ds.readInt('filterMode', 1)
        bloomBlur = ds.readBool('bloomAndBlur', True)
        attenuation = ds.readVector4('attenuation', (1, 1, 1, 1))
        attenuation *= attenuation.w
        attenuation.w = 1.0
        numPasses = ds.readInt('numPasses', 2)
        power = ds.readFloat('power', 8)
        width = ds.readFloat('width', 1.0)
        if bloomBlur:
            newBloom = PostProcessing.Effects.Bloom.bloom(
                filterMode, attenuation, numPasses, width, power)
        else:
            newBloom = PostProcessing.Effects.Bloom.blur(
                filterMode, attenuation, numPasses, width)
    else:
        print 'Bloom.loadStyle : No DataSection was provided'
        return
    ch = list(PostProcessing.chain())
    oldBloomList = _getBloomEffects()
    for oldBloom in oldBloomList:
        v4mDn = Math.Vector4Morph((1, 1, 1, 1))
        v4mDn.duration = fadeSpeed
        v4mDn.target = (0, 0, 0, 0)
        oldBloom.phases[-1].material.alpha = v4mDn
        oldBloom.bypass = v4mDn
        BigWorld.callback(fadeSpeed, partial(removeEffect, oldBloom))

    ch.append(newBloom)
    v4mUp = Math.Vector4Morph((0, 0, 0, 0))
    v4mUp.duration = fadeSpeed
    v4mUp.target = (1, 1, 1, 1)
    newBloom.phases[-1].material.alpha = v4mUp
    newBloom.bypass = v4mUp
    PostProcessing.chain(ch)
    return
Пример #38
0
def _getBloomEffect():
    """
    This function finds the bloom effect in the post-processing chain
    """
    chain = PostProcessing.chain()
    for e in chain:
        if e.name in ('Bloom', 'bloom'):
            return e

    return None
Пример #39
0
def _getBloomEffect():
    """
    This function finds the bloom effect in the post-processing chain
    """
    chain = PostProcessing.chain()
    for e in chain:
        if e.name in ('Bloom', 'bloom'):
            return e

    return None
Пример #40
0
 def __create(self, fileName):
     self.__chain = PostProcessing.load(fileName)
     if self.__chain is None:
         self.__chain = []
         self.__ctrl = None
         LOG_WARNING('Post effect <%s> was not loaded.' % fileName)
     elif self.__ctrlName is not None:
         self.__ctrl = getattr(post_effect_controllers, self.__ctrlName)()
         self.__ctrl.create()
     return
Пример #41
0
    def apply_meanshift(self, obj, cur_frame, next_frame):
        new_obj = None
        if next_frame is not None and cur_frame is not None:
            cur_frame_gray = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY)
            next_frame_gray = cv2.cvtColor(next_frame, cv2.COLOR_BGR2GRAY)
            x, y, w, h = obj
            if w > 0 and h > 0:
                cur_roi = PostProcessing.get_roi_from_images(obj, cur_frame_gray)
                # center_of_window = (x + (w / 2), y + (h / 2))

                # compute centroid of current frame
                cur_moment = cv2.moments(cur_roi)
                cx0 = x + int(cur_moment['m10'] / cur_moment['m00'])
                cy0 = y + int(cur_moment['m01'] / cur_moment['m00'])

                num_of_iteration = 0
                delta = -1
                prev_obj = obj
                while (num_of_iteration < 15) and (delta > 1 or delta == -1):

                    x1, y1, w1, h1 = prev_obj
                    next_frame_roi = PostProcessing.get_roi_from_images(prev_obj, next_frame_gray)
                    next_w, next_h = next_frame_roi.shape
                    if next_w > 0 and next_h > 0:

                        # get moment
                        next_frame_moment = cv2.moments(next_frame_roi)
                        cx1 = x1 + int(next_frame_moment['m10'] / next_frame_moment['m00'])
                        cy1 = y1 + int(next_frame_moment['m01'] / next_frame_moment['m00'])

                        # compare with previous moment
                        deltacx = cx1 - cx0
                        deltacy = cy1 - cy0
                        new_obj = x1+deltacx, y1+deltacy, w1, h1

                        # initialization for next iteration
                        cx0, cy0 = cx1, cy1
                        prev_obj = new_obj
                        delta = math.sqrt((deltacx)**2 + (deltacy)**2)
                    num_of_iteration += 1

        return new_obj
Пример #42
0
def loadStyle(ds, fadeSpeed = 1.0):
    """
    This function loads a bloom style from the given data section.  It smoothly
    changes from the current bloom settings to the new settings over
    "fadeSpeed" seconds.
    """
    newBloom = None
    if ds != None:
        enable = ds.readBool('enable', True)
        filterMode = ds.readInt('filterMode', 1)
        bloomBlur = ds.readBool('bloomAndBlur', True)
        attenuation = ds.readVector4('attenuation', (1, 1, 1, 1))
        attenuation *= attenuation.w
        attenuation.w = 1.0
        numPasses = ds.readInt('numPasses', 2)
        power = ds.readFloat('power', 8)
        width = ds.readFloat('width', 1.0)
        if bloomBlur:
            newBloom = PostProcessing.Effects.Bloom.bloom(filterMode, attenuation, numPasses, width, power)
        else:
            newBloom = PostProcessing.Effects.Bloom.blur(filterMode, attenuation, numPasses, width)
    else:
        print 'Bloom.loadStyle : No DataSection was provided'
        return
    ch = list(PostProcessing.chain())
    oldBloomList = _getBloomEffects()
    for oldBloom in oldBloomList:
        v4mDn = Math.Vector4Morph((1, 1, 1, 1))
        v4mDn.duration = fadeSpeed
        v4mDn.target = (0, 0, 0, 0)
        oldBloom.phases[-1].material.alpha = v4mDn
        oldBloom.bypass = v4mDn
        BigWorld.callback(fadeSpeed, partial(removeEffect, oldBloom))

    ch.append(newBloom)
    v4mUp = Math.Vector4Morph((0, 0, 0, 0))
    v4mUp.duration = fadeSpeed
    v4mUp.target = (1, 1, 1, 1)
    newBloom.phases[-1].material.alpha = v4mUp
    newBloom.bypass = v4mUp
    PostProcessing.chain(ch)
    return
Пример #43
0
def _getBloomEffects():
    """
    This function finds all bloom effects in the post-processing chain
    """
    blooms = []
    chain = PostProcessing.chain()
    for e in chain:
        if e.name in ('Bloom', 'bloom'):
            blooms.append(e)

    return blooms
def RunDefaultModelType(ModelType,modelvals,modelPopNames,resultsName,PopulationParameters,DiseaseParameters,endTime,mprandomseed,stepLength=1,writefolder='',startDate=datetime(2020,2,1),fitdates=[],hospitalizations=[],deaths=[],cases=[],fitper=.3,StartInfected=-1,historyData={},vaccinationdata={}):
    
    cleanUp(modelPopNames)
    ParameterVals = PopulationParameters
    ParameterVals.update(DiseaseParameters)
    
    PopulationData, GlobalInteractionMatrix, HospitalTransitionRate, HospitalNames, GlobalLocations, LocationImportationRisk = modelSetup(ModelType,modelvals,PopulationParameters,DiseaseParameters)
    
    RegionalList, timeRange, fitinfo = ProcessManager.RunModel(GlobalLocations, GlobalInteractionMatrix, HospitalTransitionRate,LocationImportationRisk,PopulationParameters,DiseaseParameters,endTime,resultsName,mprandomseed,startDate=startDate,stepLength=1,numregions=-1,modelPopNames=modelPopNames,fitdates=fitdates,hospitalizations=hospitalizations,deaths=deaths,cases=cases,fitper=fitper,burnin=False,StartInfected=StartInfected,historyData=historyData,vaccinationdata=vaccinationdata)
    

    PostProcessing.WriteParameterVals(resultsName,ModelType,ParameterVals,writefolder)
    results = PostProcessing.CompileResults(resultsName,modelPopNames,RegionalList,timeRange)
    PostProcessing.WriteAggregatedResults(results,ModelType,resultsName,modelPopNames,RegionalList,HospitalNames,endTime,writefolder)    
    
    cleanUp(modelPopNames,len(RegionalList))
    if os.path.exists(os.path.join(ParameterSet.ResultsFolder,"Results_"+resultsName+".pickle")):    
        os.remove(os.path.join(ParameterSet.ResultsFolder,"Results_"+resultsName+".pickle"))

    return fitinfo
Пример #45
0
def _getBloomEffects():
    """
    This function finds all bloom effects in the post-processing chain
    """
    blooms = []
    chain = PostProcessing.chain()
    for e in chain:
        if e.name in ('Bloom', 'bloom'):
            blooms.append(e)

    return blooms
def RunSavedRegionModelType(ModelType,modelvals,modelPopNames,resultsName,PopulationParameters,DiseaseParameters,endTime,mprandomseed,stepLength=1,writefolder='',startDate=datetime(2020,2,1),SavedRegionFolder='',numregions=-1,FolderContainer='',vaccinationdata={}):
    cleanUp(modelPopNames)
    ParameterVals = PopulationParameters
    ParameterVals.update(DiseaseParameters)
    
    PopulationData, GlobalInteractionMatrix, HospitalTransitionRate, HospitalNames, GlobalLocations, LocationImportationRisk = modelSetup(ModelType,modelvals,PopulationParameters,DiseaseParameters)
    
    RegionalList, timeRange, fitinfo = ProcessManager.RunModel(GlobalLocations, GlobalInteractionMatrix, HospitalTransitionRate,LocationImportationRisk,PopulationParameters,DiseaseParameters,endTime,resultsName,mprandomseed,startDate=startDate,modelPopNames=modelPopNames,SavedRegionFolder=SavedRegionFolder,numregions=numregions,FolderContainer=FolderContainer,vaccinationdata=vaccinationdata)
    
    if fitinfo['fitted']:
        PostProcessing.WriteFitvals(resultsName,ModelType,fitinfo['SLSH'], fitinfo['SLSD'], fitinfo['SLSC'], fitinfo['avgperdiffhosp'], fitinfo['avgperdiffdeaths'], fitinfo['avgperdiffcases'],writefolder)
        PostProcessing.WriteParameterVals(resultsName,ModelType,ParameterVals,writefolder)
        results = PostProcessing.CompileResults(resultsName,modelPopNames,RegionalList,timeRange)
        PostProcessing.WriteAggregatedResults(results,ModelType,resultsName,modelPopNames,RegionalList,HospitalNames,endTime,writefolder)    
    
    cleanUp(modelPopNames,len(RegionalList))
    if os.path.exists(os.path.join(ParameterSet.ResultsFolder,"Results_"+resultsName+".pickle")):    
        os.remove(os.path.join(ParameterSet.ResultsFolder,"Results_"+resultsName+".pickle"))

    return fitinfo
Пример #47
0
 def __init__(self, maxlen, num_tags, word_index, embeddings, model_type,
              texts_to_eval_dir, dumpPath):
     self.num_tags = num_tags
     self.word_index = word_index
     self.texts_to_eval_dir = texts_to_eval_dir
     self.dumpPath = dumpPath
     self.model_maker = nm.NeuralModel(maxlen, num_tags, word_index,
                                       embeddings)
     num_measures = 1 + 3 * (num_tags - 2)
     self.evaluator = ev.Evaluator(num_tags, num_measures,
                                   self.model_maker.tags)
     self.postprocessing = pp.PostProcessing(num_tags,
                                             self.model_maker.tags)
Пример #48
0
    def compile(self, opt):
        self.contexts = ["(&_global_context)"]

        self.parser.package = self.filename
        self.parser.imports = self.parser.allImports[self.filename]
        PostProcessing.simplifyAst(self.parser, self.tree)

        includes = self.toCHelp()

        mainCode = ""


        mainCode += ("".join(self.main_parts))
        outerCode = "".join(self.out_parts)
        forward_ref = "".join(self.header_parts)

        (generatedTypes, mainC) = Types.getGeneratedDataTypes(self.filename)
        Types.compiledTypes = coll.OrderedDict()
        Types.dataTypes = []

        mainC = "".join(self.init_types) + "\n" + mainC

        headerCode = f"{generatedTypes}\n{forward_ref}"
        print_code = "printf(" + '"' + self.filename + '\\n");'
        cCode = f"{outerCode}\nvoid {self.filename}InitTypes() {{ \n {mainC} }}\nvoid {self.filename}Init() {{ \n{mainCode};\n}};"

        #print("To C took :", time() - t)

        f = open("lib/" + self.filename + ".c", mode="w")
        f.write(cCode)
        f.close()

        f = open("lib/" + self.filename + ".h", mode="w")
        f.write(headerCode)
        f.close()

        return includes
Пример #49
0
def TestPlot(organ, threshold):
    model = Model.UNet()
    model.load_state_dict(torch.load(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + ".pt")))    
    model = model.cuda()      
    model.eval()
    dataPath = 'Processed_Data/' + organ + "_Val/"
    dataFolder = os.path.join(pathlib.Path(__file__).parent.absolute(), dataPath)
    dataFiles = os.listdir(dataFolder)
    filesRange = list(range(len(dataFiles)))
    random.shuffle(filesRange)
    for d in filesRange:
        imagePath = dataFiles[d]
        #data has 4 dimensions, first is the type (image, contour, background), then slice, and then the pixels.
        data = pickle.load(open(os.path.join(dataFolder, imagePath), 'rb'))
        print("Validating with " + dataFiles[d])
        x = torch.from_numpy(data[0, :, :]).cuda()
        y = torch.from_numpy(data[1:2, :,:]).cuda()
        xLen, yLen = x.shape
        #need to reshape 
        x = torch.reshape(x, (1,1,xLen,yLen)).float()
        y = torch.reshape(y, (1,1,xLen,yLen)).float()   
        predictionRaw = (model(x)).cpu().detach().numpy()
        prediction = PostProcessing.Process(predictionRaw, threshold)
        x = x.cpu().numpy()
        y = y.cpu().numpy()
        maskOnImage = MaskOnImage(x[0,0,:,:], prediction[0,0,:,:])
        ROIOnImage = MaskOnImage(x[0,0,:,:], y[0,0,:,:])

        fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15, 15))
        
        #predictedContour = PostProcessing.MaskToContour(prediction[0,0,:,:])
        #contour = PostProcessing.MaskToContourImage(y[0,0,:,:])
        axs[0,0].imshow(ROIOnImage, cmap = "gray")
        axs[0,0].set_title("Original Image")
        #axs[0,1].hist(predictionRaw[0,0,:,:], bins=5)
        axs[0,1].imshow(maskOnImage, cmap = "gray")
        axs[0,1].set_title("Mask on Image")
        axs[1,0].imshow(y[0,0, :,:], cmap = "gray")
        axs[1,0].set_title("Original Mask")
        axs[1,1].imshow(prediction[0,0, :,:], cmap="gray")
        axs[1,1].set_title("Predicted Mask")

        
        # axs[2,0].imshow(y[0,1, :,:], cmap = "gray")
        # axs[2,0].set_title("Original Background")
        # axs[2,1].imshow(prediction[0,1, :,:], cmap = "gray")
        # axs[2,1].set_title("Predicted Background")
        plt.show()
Пример #50
0
def GetMasks(organ, patientName, path, threshold):
    #Returns a 3d array of predicted masks for a given patient name.
    if path == None:
        path = pathlib.Path(__file__).parent.absolute()
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = Model.UNet()
    model.load_state_dict(
        torch.load(
            os.path.join(path,
                         "Models/Model_" + organ.replace(" ", "") + ".pt")))
    model = model.to(device)
    model.eval()

    dataPath = 'Processed_Data/' + organ  #+ "_Val/" #Currently looking for patients in the test folder.
    dataFolder = os.path.join(path, dataPath)
    dataFiles = os.listdir(dataFolder)
    filesRange = list(range(len(dataFiles)))

    patientImages = []
    for d in filesRange:  #First get the files for the patientName given
        if patientName in dataFiles[d]:
            patientImages.append(dataFiles[d])
    patientImages.sort()

    predictions = [
    ]  #First put 2d masks into predictions list and then at the end stack into a 3d array
    originalMasks = []
    for image in patientImages:
        #data has 4 dimensions, first is the type (image, contour, background), then slice, and then the pixels.
        data = pickle.load(open(os.path.join(dataFolder, image), 'rb'))
        x = torch.from_numpy(data[0, :, :])
        y = data[1, :, :]
        x = x.to(device)
        xLen, yLen = x.shape
        #need to reshape
        x = torch.reshape(x, (1, 1, xLen, yLen)).float()
        predictionRaw = (model(x)).cpu().detach().numpy()
        #now post-process the image
        x = x.cpu()
        x = x.numpy()
        prediction = PostProcessing.Process(predictionRaw[0, 0, :, :],
                                            threshold)
        predictions.append(prediction)
        originalMasks.append(y)
    #Stack into 3d array
    predictionsArray = np.stack(predictions, axis=0)
    originalsArray = np.stack(originalMasks, axis=0)
    return predictionsArray, originalsArray
    def run(self):
        vid_src = cv2.VideoCapture(self.filename)
        _, frame = vid_src.read()
        prev_frame = None
        object_box = None

        # applying background detection
        while frame is not None:
            _, frame = vid_src.read()
            if frame is None:
                break

            new_object_box, fg = self.algorithm.apply(frame, object_box)

            if self.tracking.is_object_empty():
                if object_box is not None:
                    for box in object_box:
                        self.tracking.add_object(box, frame)
            else:
                # cek dalam new object box apakah ada yang baru # nanti
                if new_object_box is not None:
                    for new_box in new_object_box:
                        if PostProcessing.is_new_square(new_box, self.tracking.objects()):
                            self.tracking.add_object(new_box, frame)

            if object_box is not None:
                for box in object_box:
                    x, y, w, h = box
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)

            if prev_frame is not None:
                self.tracking.run(prev_frame, frame)

            object_box = new_object_box
            # showing
            cv2.imshow('img', frame)

            prev_frame = np.copy(frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cv2.destroyAllWindows()
        vid_src.release()
Пример #52
0
    def Retrieve( self ):
        # thing to retrieve:
        # image (complex)
        # support (real)
        # partially coherent intensity (real)
        # Gaussian partial coherence function (real)
        # 3D Gaussian partial coherence parameters
        
        self.finalImage = fftshift( self._cImage.eval( session=self.__sess__ ) )
        self.finalSupport = fftshift( np.absolute( self._support.eval( session=self.__sess__ ) ) )
        self.finalImage, self.finalSupport = post.centerObject( self.finalImage, self.finalSupport )
        
        # partial coherence function in Fourier space
        self.finalPCC = fftshift( self._blurKernel.eval( session=self.__sess__ ) )
        self.finalPCSignal = fftshift( self._imgBlurred.eval( session=self.__sess__ ) )
        self.finalGaussPCCParams = self.__sess__.run( self._var_list )

        self.__sess__.close()
        return
Пример #53
0
 def attach(self, actor, source, target = None):
     ch = PostProcessing.chain()
     ch += actor
     PostProcessing.chain(ch)
Пример #54
0
def Best_Threshold(organ, testSize=10e6, onlyMasks=False, onlyBackground=False):
    #return the model output threshold that maximizes accuracy. onlyMasks if true will calculate statistics
    #only for images that have a mask on the plane, and onlyBackground will do it for images without masks.
    print("Determining most accurate threshold...")
    model = Model.UNet()
    model.load_state_dict(torch.load(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + ".pt")))    
    model = model.cuda()      
    model.eval()
    dataPath = 'Processed_Data/' + organ + "_Val/"
    dataFolder = os.path.join(pathlib.Path(__file__).parent.absolute(), dataPath)
    dataFiles = os.listdir(dataFolder)

    #also get the bools to find if masks are on image plane
    boolPath = 'Processed_Data/' + organ + " bools"
    boolFolder = os.path.join(pathlib.Path(__file__).parent.absolute(), boolPath)
    boolFiles = os.listdir(boolFolder)

    #shuffle the order (boolFiles is in same order)
    filesRange = list(range(len(dataFiles)))
    random.shuffle(filesRange)

    
    accuracies = [] #make a list of average accuracies calculated using different thresholds
    falsePos = []
    falseNeg = []
    thresholds = np.linspace(0.05,0.7,15)
    
    for thres in thresholds:
        print("Checking Threshold: %0.3f"%(thres))
        d = 0
        #get the accuracy for the current threshold value
        thresAccuracy = []
        thresFP = []
        thresFN = [] #false pos, neg
        testSize = min(testSize, len(filesRange))
        while d < testSize:
            numStack = min(200, len(filesRange) - 1 - d)
            p = 0
            concatList = []
            while p < numStack:
                imagePath = dataFiles[d]
                if onlyMasks:
                    boolMask = pickle.load(open(os.path.join(boolFolder, boolFiles[d]), 'rb'))
                    if not boolMask:
                        p+=1
                        d+=1
                        continue
                elif onlyBackground:
                    boolMask = pickle.load(open(os.path.join(boolFolder, boolFiles[d]), 'rb'))
                    if boolMask:
                        p+=1
                        d+=1
                        continue    
                image = pickle.load(open(os.path.join(dataFolder, imagePath), 'rb'))
                image = image.reshape((2,1,image.shape[1], image.shape[2]))
                concatList.append(image)
                p+=1
                d+=1
            if len(concatList) == 0:
                break    
            data = np.concatenate(concatList, axis=1)    #data has 4 dimensions, first is the type (image, contour, background), then slice, and then the pixels.
            print('Loaded ' + str(data.shape[1]) + ' images. Proceeding...') 
            data = torch.from_numpy(data)
            numSlices = data.shape[1]
            slices = list(range(numSlices))
            random.shuffle(slices)


            for sliceNum in slices:
                x = data[0, sliceNum, :, :].cuda()
                y = data[1, sliceNum, :, :]
                xLen, yLen = x.shape
                #need to reshape 
                x = torch.reshape(x, (1,1,xLen,yLen)).float()
                y = torch.reshape(y, (xLen,yLen)).float()   
                predictionRaw = (model(x)).cpu().detach().numpy()
                prediction = PostProcessing.Process(predictionRaw,thres) 
                prediction = np.reshape(prediction, (xLen, yLen))
                #now judge accuracy: 
               
                thresPrediction = (prediction > thres)
                imageAccuracy = np.sum(thresPrediction == y.numpy())
                imageFP = np.sum(np.logical_and(thresPrediction != y.numpy(), thresPrediction == 1))
                imageFN = np.sum(np.logical_and(thresPrediction != y.numpy(), thresPrediction == 0))         
                imageAccuracy *= 100/float(prediction.size) 
                imageFN *= 100 / float(prediction.size)
                imageFP *= 100 / float(prediction.size)
                thresAccuracy.append(imageAccuracy)
                thresFP.append(imageFP)
                thresFN.append(imageFN) #false pos, neg

        accuracies.append(sum(thresAccuracy) / len(thresAccuracy))
        falseNeg.append(sum(thresFN)/len(thresFN))
        falsePos.append(sum(thresFP)/len(thresFP))
    #Now need to determine what the best threshold to use is. Also plot the accuracy, FP, FN:
    fig, axs = plt.subplots(nrows=3, ncols=1, figsize=(15, 15))

    axs[0].scatter(thresholds, accuracies)
    axs[0].plot(thresholds, accuracies)
    axs[0].set_title("Accuracy vs Threshold")
    axs[1].scatter(thresholds, falsePos)
    axs[1].plot(thresholds, falsePos)
    axs[1].set_title("False Positives vs Threshold")
    axs[2].scatter(thresholds, falseNeg)
    axs[2].plot(thresholds, falseNeg)
    axs[2].set_title("False Negatives vs Threshold")

    plt.show()    

    #Get maximimum accuracy 
    maxAccuracy = max(accuracies) 
    maxAccuracyIndices = [i for i, j in enumerate(accuracies) if j == maxAccuracy]
    bestThreshold = thresholds[maxAccuracyIndices[0]] 
    #save this threshold
    thresFile = open(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + "_Thres.txt"),'w')
    thresFile.write(str(bestThreshold))
    thresFile.close()
    with open(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + "_Accuracy.txt"),'wb') as fp:
        pickle.dump(accuracies, fp)      
    with open(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + "_FalseNeg.txt"),'wb') as fp:
        pickle.dump(falseNeg, fp)   
    with open(os.path.join(pathlib.Path(__file__).parent.absolute(), "Models/Model_" + organ.replace(" ", "") + "_FalsePos.txt"),'wb') as fp:
        pickle.dump(falsePos, fp)            
    return bestThreshold
Пример #55
0
def FScore(organ, path, threshold):
    if path == None:  #if no path supplied, assume that data folders are set up as default in the working directory.
        path = pathlib.Path(__file__).parent.absolute()
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print("Device being used for training: " + device.type)
    model = Model.UNet()
    model.load_state_dict(
        torch.load(
            os.path.join(path,
                         "Models/Model_" + organ.replace(" ", "") + ".pt")))
    model = model.to(device)
    model = model.eval()
    dataPath = 'Processed_Data/' + organ + "_Val/"
    dataFolder = os.path.join(path, dataPath)
    dataFiles = sorted(os.listdir(dataFolder))
    d = 0
    print('Calculating Fscore Statistics')
    TP = 0
    FP = 0
    FN = 0

    xLen, yLen = [0, 0]
    while d < len(dataFiles):
        numStack = min(
            3,
            len(dataFiles) - 1 -
            d)  #loading 1400 images at a time (takes about 20GB RAM)
        p = 0
        concatList = []
        while p < numStack:
            imagePath = dataFiles[d]
            image = pickle.load(open(os.path.join(dataFolder, imagePath),
                                     'rb'))
            image = image.reshape((2, 1, image.shape[2], image.shape[2]))
            concatList.append(image)
            p += 1
            d += 1
        if len(concatList) == 0:
            break
        data = np.concatenate(concatList, axis=1)
        numSlices = data.shape[1]
        for sliceNum in range(numSlices):
            x = torch.from_numpy(data[0, sliceNum, :, :])
            y = torch.from_numpy(data[1:2, sliceNum, :, :])
            x = x.to(device)
            y = y.to(device)
            xLen, yLen = x.shape
            #need to reshape
            x.requires_grad = True
            y.requires_grad = True
            x = torch.reshape(x, (1, 1, xLen, yLen)).float()
            y = y.cpu().detach().numpy()
            y = np.reshape(y, (xLen, yLen))

            predictionRaw = (model(x)).cpu().detach().numpy()
            predictionRaw = np.reshape(predictionRaw, (xLen, yLen))
            prediction = PostProcessing.Process(predictionRaw, threshold)
            for x_idx in range(xLen):
                for y_idx in range(yLen):
                    if prediction[x_idx, y_idx] == 1:
                        if y[x_idx, y_idx] == 1:
                            TP += 1
                        else:
                            FP += 1
                    elif y[x_idx, y_idx] == 1:
                        FN += 1
        print("Finished " + str(d) + " out of " + str(len(dataFiles)) +
              " contours...")
    totalPoints = d * xLen * yLen
    recall = TP / (TP + FN)
    precision = TP / (TP + FP)
    F_Score = 2 / (recall**(-1) + precision**(-1))
    accuracy = (totalPoints - FP - FN) / totalPoints

    return F_Score, recall, precision, accuracy
Пример #56
0
def TestPlot(organ, path, threshold):
    if path == None:  #if no path supplied, assume that data folders are set up as default in the working directory.
        path = pathlib.Path(__file__).parent.absolute()
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = Model.UNet()
    model.load_state_dict(
        torch.load(
            os.path.join(path,
                         "Models/Model_" + organ.replace(" ", "") + ".pt")))
    model = model.to(device)
    model.eval()
    dataPath = 'Processed_Data/' + organ + "_Val/"
    dataFolder = os.path.join(path, dataPath)
    dataFiles = os.listdir(dataFolder)
    filesRange = list(range(len(dataFiles)))
    random.shuffle(filesRange)
    for d in filesRange:
        imagePath = dataFiles[d]
        #data has 4 dimensions, first is the type (image, contour, background), then slice, and then the pixels.
        data = pickle.load(open(os.path.join(dataFolder, imagePath), 'rb'))
        x = torch.from_numpy(data[0, :, :])
        y = torch.from_numpy(data[1:2, :, :])
        x = x.to(device)
        y = y.to(device)
        xLen, yLen = x.shape
        #need to reshape
        x = torch.reshape(x, (1, 1, xLen, yLen)).float()
        y = torch.reshape(y, (1, 1, xLen, yLen)).float()
        predictionRaw = (model(x)).cpu().detach().numpy()
        #now post-process the image
        x = x.cpu()
        y = y.cpu()
        x = x.numpy()
        y = y.numpy()
        print(np.amax(y))
        print(np.amin(y))
        prediction = PostProcessing.Process(predictionRaw[0, 0, :, :],
                                            threshold)

        maskOnImage = MaskOnImage(
            x[0,
              0, :, :], prediction)  #this puts the mask ontop of the CT image
        ROIOnImage = MaskOnImage(x[0, 0, :, :], y[0, 0, :, :])

        fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15, 15))

        #predictedContour = PostProcessing.MaskToContour(prediction[0,0,:,:])
        #contour = PostProcessing.MaskToContourImage(y[0,0,:,:])
        axs[0, 0].imshow(ROIOnImage, cmap="gray")
        axs[0, 0].set_title("Original Image")
        #axs[0,1].hist(predictionRaw[0,0,:,:], bins=5)
        axs[0, 1].imshow(maskOnImage, cmap="gray")
        axs[0, 1].set_title("Mask on Image")
        axs[1, 0].imshow(y[0, 0, :, :], cmap="gray")
        axs[1, 0].set_title("Original Mask")
        axs[1, 1].imshow(prediction, cmap="gray")
        axs[1, 1].set_title("Predicted Mask")

        # axs[2,0].imshow(y[0,1, :,:], cmap = "gray")
        # axs[2,0].set_title("Original Background")
        # axs[2,1].imshow(prediction[0,1, :,:], cmap = "gray")
        # axs[2,1].set_title("Predicted Background")
        plt.show()
Пример #57
0
        def report(self):
                
                #Run is over, destroy display windows
                #Postprocessing
                cv2.destroyAllWindows()
                if self.remove_singles:
                        singles_removed=PostProcessing.remove_singletons(self.frame_results,self.single_distance*self.frame_rate,self.file_destination)
        
                #Print parameters
                #Batch or single file
                self.log_report.write("\nInput Parameters")        
                self.log_report.write("\nRun type: %s" % self.runtype)
                if self.runtype in ["file","pictures"]:
                        self.log_report.write("\nInput file path: %s" % self.inDEST)
                        
                else:
                        self.log_report.write("\nInput file path: %s" % self.batchpool)
                self.log_report.write("\nOutput dir: %s" % self.fileD)
                self.log_report.write("\nBackground Subtraction Method?: %s" % self.subMethod)
                self.log_report.write("\nAdapt settings: %s" % self.adapt)
                self.log_report.write("\nFrame Rate: %s" % self.frame_rate)
                
                if self.subMethod == "MOG":
                        self.log_report.write("\nLearning Rate: %s" % self.moglearning)
                        self.log_report.write("\nVariance Rate: %s" % self.mogvariance)
                
                if self.subMethod == "Acc":
                        self.log_report.write("\nAccumulated Averaging: %s" % self.accAvg) 
                        self.log_report.write("\nThreshold: %s" % self.threshT)                
        
                if self.adapt:
                        self.log_report.write("\nExpected hitrate: %s" % self.frameHIT)
                
                self.log_report.write("\nFrame crop: %s" % self.set_ROI)     
                if self.set_ROI:        
                        self.log_report.write("\nSet ROI: %s" % self.ROI_include)        
                self.log_report.write("\nMinimum size was drawn or entered?: %s" % self.drawSmall)
                self.log_report.write("\nMinimum area: %s percent of frame" % (self.minSIZE * 100))
                if self.burnin > 0:
                        self.log_report.write("\nBurnin: %s" % self.burnin)
                if self.scan > 0:
                        self.log_report.write("\nScan frames: %s" % self.scan)
                if self.frameSET:
                        self.log_report.write("\nManual framerate: %s" % self.frame_rate)
                if self.set_areacounter:
                        self.log_report.write("\nArea counter: %s" % self.set_areacounter)
                self.log_report.write("\nOutput type: %s\n\n" % self.makeVID)
        
                #Ending time
                end=time.time()
        
                #total_time()
                self.total_min=(end-self.start)/60
        
                #processed frames per second
                try:
                        pfps=float(self.frame_count)/(self.total_min*60)
                except:
                        #run failed before initialization
                        self.frame_count=1
                        pfps=0
                        self.total_min=0
                        self.total_count=0
                
                ##Write to log file
                self.log_report.write("Processing\n")        
                self.log_report.write("Total run time (min): %.2f \n" % self.total_min)
                self.log_report.write("Average frames per second: %.2f \n " % pfps)
        
                #End of program, report some statistic to screen and log
                #log
                self.log_report.write("\nResults\n")
                self.log_report.write("Candidate motion events: %.0f \n" % self.total_count )
                self.log_report.write("Frames skipped due to insufficient movement based on the threshold parameter: %.0f \n" % self.nocountr)
                self.log_report.write("Frames skipped due to minimum size of the contours: %.0f \n" % self.toosmall)
                if self.windy:
                        self.log_report.write("Frames deleted due to windy conditions: %.0f \n" % self.windy_count)
                if self.remove_singles:
                        self.log_report.write("Frames deleted due to singletons: %.0f \n" % singles_removed)
               
                self.log_report.write("Total frames in files: %.0f \n" % self.frame_count)
                
                try:
                        rate=float(self.total_count)/self.frame_count*100
                except:
                        rate=0
                self.log_report.write("Hitrate: %.2f %% \n" % rate)
        
                #print to screen
                print("\n\nThank you for using MotionMeerkat! \n")
                print("Total run time (min): %.2f \n " % self.total_min)
                print("Average frames processed per second: %.2f \n " % pfps)   
                print("Candidate motion events: %.0f \n " % self.total_count )
                print("Frames skipped due to insufficient movement based on the threshold parameter: %.0f \n " % self.nocountr)
                print("Frames skipped due to minimum size of the contours: %.0f \n " % self.toosmall)
                
                #if windy
                if self.windy:
                        print("Frames skipped due to windy conditions: %.0f \n " % self.windy_count)
                if self.remove_singles:
                        print("Frames deleted due to singletons: %.0f \n" % singles_removed)
                        
                print("Total frames in files: %.0f \n " % self.frame_count)
        
                print("Hitrate: %.2f %% \n" % rate)
                
                ####Generate plots        
                #Show box size by area

                #First frame is artifact of intilization
                try:
                        tarea=(self.width * self.height)
                        self.scale_size=[x/tarea for x in self.avg_area]                        
                        self.scale_size[0]=None
                except:
                        pass
                                
                
                #Write csv of time stamps and frame counts
                #file name
                time_stamp_report = self.file_destination + "/" + "Frames.csv"
        
                with open(time_stamp_report, 'wb') as f:
                        writer = csv.writer(f)
                        writer.writerows(self.stamp)
                if self.set_areacounter:
                        area_report = self.file_destination + "/" + "AreaCounter.csv"
                        with open(area_report, 'wb') as f:
                                writer = csv.writer(f)
                                writer.writerows(self.areaC)
Пример #58
0
 def prerequisites(self, pSection):
     return PostProcessing.prerequisites(pSection.asString)
Пример #59
0
 def go(self, effect, actor, source, target, **kargs):
     self.animation.time = 0.0
     self.animation.keyframes = self.fkeyframes
     PostProcessing.setMaterialProperty(actor, self.property, self.animation)
     return self.time
Пример #60
0
 def prerequisites(self):
     if self.__isMapDepended:
         return []
     return PostProcessing.prerequisites(self.__name + _Effect.__FILE_EXT)