def AtrophyPrediction(self, roi): img = self.ModAtrophy.predict(roi) atrophyRate = met.atrophyRate(img) w, h, c = moil.getWidthHeightChannels(self.currentImg) img = cv2.resize(img, (round(160 * w / 600), round(160 * (w * 0.75) / 450))) img = moil.getBinaryThreshold(img) return atrophyRate, img
def ExitPrediction(self, roi, xExitShift, yExitShift, xRef, yRef): img = self.ModExit.predict(roi) img = cv2.resize(img, (xExitShift * 2, yExitShift * 2)) x, y = met.getCenter(img, xExitShift * 2, yExitShift * 2) x = int(x) y = int(y) return x + xRef - xExitShift, y + yRef - yExitShift
def OpticDiscPrediction(self): img = self.Mod.predict(self.currentImg) # img = moil.stackImageChannels(img) # resizing prediction w, h, c = moil.getWidthHeightChannels(self.currentImg) img = cv2.resize(img, (w, h)) # getting coords x, y = met.getCenter(img, w, h) x = int(x) y = int(y) return x, y, img
def check_performance(self, validate_generator, times=1, metrics=['distance', 'youden', 'jaccard', 'dice']): for i in range(times): pic = validate_generator.next() true = pic[1][0] pred = self.model.predict(pic[0][0].reshape( 1, self.rowDim, self.colDim, self.channels)) pred = moil.convertImageNetOutput(pred) true = moil.convertImageNetOutput(true) met.customMetric(pred, true, metrics=metrics) x = [] x.append(pic[0][0].reshape( (self.rowDim, self.colDim, self.channels))) x.append(true) x.append(pred) if self.show_function != None: self.show_function(x)
def make_prediction(self): x, y, pred = self.OpticDiscPrediction() self.x = x self.y = y copy = self.currentImg.copy() drawCopy = self.currentImg.copy() drawCopy = moil.stackImageChannels(drawCopy) w, h, c = moil.getWidthHeightChannels(copy) xShift = int(80 * w / 600) yShift = int(80 * (w * 0.75) / 450) xExitShift = int(40 * w / 600) yExitShift = int(40 * (w * 0.75) / 450) roi = moil.getRegionOfInterest(copy, x, y, xShift, yShift) roiExit = moil.getRegionOfInterest(copy, x, y, xExitShift, yExitShift) atrophyRate, atrophyMap = self.AtrophyPrediction(roi) self.atrophyRate = atrophyRate self.label.configure( text="Stopień zaniku (tylko faza tętniczo-żylna): " + str(atrophyRate)) xExit, yExit = self.ExitPrediction(roiExit, xExitShift, yExitShift, x, y) self.xOut = xExit self.yOut = yExit dist = np.linalg.norm( np.asarray([xExit / w * 600, yExit / (w * 0.75) * 450]) - np.asarray([x / w * 600, y / (w * 0.75) * 450])) if dist > 16: self.labelExit.configure( text='Przesunięcie naczyń (faza tętniczo-żylna lub późna) : ' + str(dist) + ', ZNACZNE!') else: self.labelExit.configure( text='Przesunięcie naczyń (faza tętniczo-żylna lub późna) : ' + str(dist)) wA, hA, cA = moil.getWidthHeightChannels(atrophyMap) mask = np.zeros((h, w), drawCopy.dtype) mask = moil.addToRegionOfInterest(mask, x, y, round(wA / 2 + 0.00001), round(hA / 2 + 0.00001), atrophyMap) # mask[y-round(hA/2+0.00001):y+round(hA/2+0.00001), x-round(wA/2+0.00001):x+round(wA/2+0.00001)] = atrophyMap redImg = np.zeros(drawCopy.shape, drawCopy.dtype) redImg[:, :] = (255, 0, 0) redMask = cv2.bitwise_and(redImg, redImg, mask=mask) drawCopy = cv2.addWeighted(redMask, 1, drawCopy, 1, 0) # moil.show(atrophyMap) # drawCopy[mask] = (255, 0, 0) cv2.rectangle(drawCopy, (x - xShift, y - yShift), (x + xShift, y + yShift), (127, 0, 127), int(5 / 1387 * w)) cv2.circle(drawCopy, (x, y), int(12 / 1387 * w), (127, 0, 127), thickness=int(5 / 1387 * w)) met.draw(pred, drawCopy, thickness=int(4 / 1387 * w)) cv2.circle(drawCopy, (xExit, yExit), int(12 / 1387 * w), (0, 127, 0), thickness=int(5 / 1387 * w)) self.updateGuiImage(drawCopy) self.predicted = True
def centerDiffMetric(pred, x, y): return met.centerDiff(pred, x, y)
def validate(self, pathForce=None, validateMode=0, preprocessFunc=lambda x: x, draw=True, onlyWithMetric=False, onlyWithoutMetric=False, sumTimes=None, metrics=['distance', 'youden', 'jaccard', 'dice'], validTimes=1, weightsTimesValids=None, validName=''): avgs, globals = (0, 0) for i in range(validTimes): if weightsTimesValids is not None: self.constantVar = i * weightsTimesValids self.load_weights() sum = [0] * len(metrics) confusion_matrix = [0] * 4 globalCount = False for metr in metrics: if 'global' in metr: globalCount = True times = 0 visited_path = {} while True: if pathForce is None: path = self.validate_path_provider_func( self.validate_start_path, visited_path) visited_path[path] = times else: path = pathForce if path is None: break if not os.path.exists(path): continue images = os.listdir(path) for imp in images: # len(os.listdir(path)) - 2): true_path = path + 'mask/' if not os.path.exists(os.path.join(path, imp)): continue if onlyWithMetric and not os.path.exists( os.path.join(true_path, imp)): continue else: if onlyWithoutMetric and os.path.exists( os.path.join(true_path, imp)): continue im = self.read_func(name=imp, extension='', path=path, target_size=(self.colDim, self.rowDim), mode=0) imgX, img = self.prepareImage(im, retboth=True) pred = self.model.predict(imgX) pred = moil.convertImageNetOutput(pred) toDraw = im if draw else None x = [im, pred, img] if os.path.exists(os.path.join(true_path, imp)): true = self.read_func(name=imp, extension='', path=true_path, target_size=(self.colDim, self.rowDim)) true = true.reshape( (self.rowDim, self.colDim, self.out_channels)) x.append(true) results = met.customMetric(pred, true, toDraw=toDraw, metrics=metrics, globalCount=globalCount) sum = list(map(add, sum, results[0])) confusion_matrix = list( map(add, confusion_matrix, results[1])) times += 1 if sumTimes is not None and times >= sumTimes: break else: met.draw(pred, toDraw) if sumTimes is None: self.show_function(x) avgs = [x / times for x in sum] strgSum = '' strgAvgs = '' for val in sum: strgSum += str(val) + ', ' for val in avgs: strgAvgs += str(val) + ', ' globals = [] if globalCount: globals = met.globals(confusion_matrix) print("Global Jaccard: " + str(globals[0]) + ", Global Dice: " + str(globals[1])) print("Times: " + str(times) + ", sums: " + strgSum + "Average metrics: " + strgAvgs) self.validate_to_csv(metrics, avgs + globals, validName) return avgs + globals