def analyzePixelRelevance(): "simplified implementation of paper: Zintgraf et al - Visualizing Deep Neural Network Decisions: Prediction Difference Analysis" # setup model model = Model(open(Constants.fnCharList).read(), DecoderType.BestPath, mustRestore=True) # read image and specify ground-truth text img = cv2.imread(Constants.fnAnalyze, cv2.IMREAD_GRAYSCALE) (w, h) = img.shape assert Model.imgSize[1] == w # compute probability of gt text in original image batch = Batch([Constants.gtText], [preprocess(img, Model.imgSize)]) (_, probs) = model.inferBatch(batch, calcProbability=True, probabilityOfGT=True) origProb = probs[0] grayValues = [0, 63, 127, 191, 255] if Constants.distribution == 'histogram': bins = [0, 31, 95, 159, 223, 255] (hist, _) = np.histogram(img, bins=bins) pixelProb = hist / sum(hist) elif Constants.distribution == 'uniform': pixelProb = [1.0 / len(grayValues) for _ in grayValues] else: raise Exception('unknown value for Constants.distribution') # iterate over all pixels in image pixelRelevance = np.zeros(img.shape, np.float32) for x in range(w): for y in range(h): # try a subset of possible grayvalues of pixel (x,y) imgsMarginalized = [] for g in grayValues: imgChanged = copy.deepcopy(img) imgChanged[x, y] = g imgsMarginalized.append(preprocess(imgChanged, Model.imgSize)) # put them all into one batch batch = Batch([Constants.gtText] * len(imgsMarginalized), imgsMarginalized) # compute probabilities (_, probs) = model.inferBatch(batch, calcProbability=True, probabilityOfGT=True) # marginalize over pixel value (assume uniform distribution) margProb = sum([probs[i] * pixelProb[i] for i in range(len(grayValues))]) pixelRelevance[x, y] = weightOfEvidence(origProb, margProb) print(x, y, pixelRelevance[x, y], origProb, margProb) np.save(Constants.fnPixelRelevance, pixelRelevance)
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') print(f'Probability: {probability[0]}')
def analyzeTranslationInvariance(): # setup model model = Model(open(Constants.fnCharList).read(), DecoderType.BestPath, mustRestore=True) # read image and specify ground-truth text img = cv2.imread(Constants.fnAnalyze, cv2.IMREAD_GRAYSCALE) (w, h) = img.shape assert Model.imgSize[1] == w imgList = [] for dy in range(Model.imgSize[0] - h + 1): targetImg = np.ones((Model.imgSize[1], Model.imgSize[0])) * 255 targetImg[:, dy:h + dy] = img imgList.append(preprocess(targetImg, Model.imgSize)) # put images and gt texts into batch batch = Batch([Constants.gtText] * len(imgList), imgList) # compute probabilities (texts, probs) = model.inferBatch(batch, calcProbability=True, probabilityOfGT=True) # save results to file f = open(Constants.fnTranslationInvarianceTexts, 'wb') pickle.dump(texts, f) f.close() np.save(Constants.fnTranslationInvariance, probs)
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') print(f'Probability: {probability[0]}') apex = open("D:/SimpleHTR/data/output.txt", "a") apex.write(recognized[0] + " ") apex.close()
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) print(img.shape) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') print(f'Confidence: {probability[0]}') recognizedWords = recognized[0].split('-') word = "" for i in recognizedWords: corrected = correct_sentence(i) word = word + corrected print(f'Word Recognized after correction: "{word}"') print("\n\nWriting Output\n\n") f = open("output.txt", "w") f.write(f'{word}\n') f.close()
def infer(model, fnImg): global prob global now global xaxis global yaxis "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') xaxis.append(recognized[0]) print(f'Probability: {probability[0]}') yaxis.append(probability[0] * 100) prob = prob + probability[0] now = now + 1 apex = open("D:/SimpleHTR/data/output.txt", "a") apex.write(recognized[0] + " ") apex.close()
def infer(model, Img): img = preprocess(cv2.cvtColor(Img, cv2.COLOR_BGR2GRAY), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) return recognized, probability