Esempio n. 1
0
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)
Esempio n. 2
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)
Esempio n. 3
0
 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('Recognized:', '"' + recognized[0] + '"')
     print('Probability:', probability[0])