Θεωρία Πληροφορίας - Κώδικες Γιαννακόπουλος Θεόδωρος 1
Θεωρία Πληροφορίας - Κώδικες Εργαστήριο 4 Παραδείγματα Python για Θεωρία Πληροφορίας (3 και 4) 2
Εντροπία - Μέγιστη Εν. - Οριακές πιθανότητες (1) ITlib.py (part) import numpy eps = 0.000000000001 Υπολογισμός εντροπίας από array πιθανοτήτων συμβόλων def computeentropy(probs): if numpy.abs(probs.sum()-1.0)<eps: # if sum of probabilities is almost equal to 1 ProbsNonZero = Probs[Probs>eps] # remove zero probabilities return -numpy.sum(probsnonzero * numpy.log2(probsnonzero)) # compute entropy else: raise ValueError("Probabilities must sum to unity!") # raise an error if probabilities do not sum to 1 def computemaxentropy(n): return numpy.math.log(n, 2) Υπολογισμός μέγιστης εντροπίας def computepriorsfromjointp(jointp): if numpy.abs(jointp.sum()-1.0) < eps: Υπολογισμός marginal Px = jointp.sum(axis = 1) πιθανοτήτων P(x), P(y) από τις Py = jointp.sum(axis = 0) από κοινού πιθανότητες P(x,y) return Px, Py else: raise ValueError("Probabilities must sum to unity!") # raise an error if probabilities do not sum to 1 3
example3.py: προσομοίωση πηγής - πηγή εκπέμπει σύμβολα a, b και c με πιθανότητες 0.1, 0.2 και 0.7 - θεωρούμε ότι οι πιθανότητες είναι άγνωστες - άρα πρέπει να τις εκτιμήσουμε από τις τιμές των συμβόλων - δηλαδή προσομοιώνουμε μία εκπομπή για την οποία δεν γνωρίζουμε από πριν τις πιθανότητες των συμβόλων - για διαφορετικά πλήθη συμβόλων - να γίνει προσομοίωση της εκπομπής συμβόλων με την συγκεκριμένη κατανομή - να εκτιμηθούν οι πιθανότητες των συμβόλων - να βρεθεί η αντίστοιχη εντροπία - Παράδειγμα random generator accccbccc P(a) = 1/9 P(b) = 1/9 P(c) = 7/9 H = 0.98 4
example3.py import ITlib, numpy, os import matplotlib.pyplot as plt def generatesymbols(nsymbols): return [numpy.random.choice(["a","b","c"], p=[0.1,0.2,0.7]) for i in range(nsymbols)] Γεννήτρια τυχαίων μηνυμάτων μήκους nsymbols που ακολουθούν την κατανομή P(a) = 0.1, P(b) = 0.2, P(c) = 0.7 def computeprobsfromsymbols(symbols): Pa = float(symbols.count("a")) / len(symbols) Pb = float(symbols.count("b")) / len(symbols) Pc = float(symbols.count("c")) / len(symbols) return (Pa, Pb, Pc) Εκτίμηση των πιθανοτήτων μέσω counts Πιθανές τιμές του πλήθους δειγμάτων nsymbols = range(10,20,2)+range(25,105,5)+range(200,2000,100)+range(5000,100000,5000) Hs = [] realentropy = ITlib.computeEntropy(numpy.array([0.1,0.2,0.7])) for ns in nsymbols: print "Estimating entropy for %d symbols per message " % ns, symbols = generatesymbols(ns) (Pa, Pb, Pc) = computeprobsfromsymbols(symbols) Hs.append(ITlib.computeEntropy(numpy.array([Pa, Pb, Pc]))) print "P(a)=%.3f P(b)=%.3f P(c)=%.3f --> H=%.3f" % (Pa, Pb, Pc, Hs[-1]) Υπολογισμός συνολικής εντροπίας plt.plot(numpy.log10(nsymbols), Hs); plt.plot(numpy.log10(numpy.array([nsymbols[0],nsymbols[-1]])), [realentropy, realentropy],"--") plt.xlabel("log(nsymbols)"); plt.ylabel("entropy"); plt.show() Για κάθε πλήθος δειγμάτων ns: α) παράγουμε ns δείγματα με την (β) εκτιμούμε τις πιθανότητες (γ) υπολογίζουμε την εντροπία 5
example3.py Estimating entropy for 10 symbols per message P(a)=0.300 P(b)=0.100 P(c)=0.600 --> H=1.295 Estimating entropy for 12 symbols per message P(a)=0.083 P(b)=0.000 P(c)=0.917 --> H=0.414 Estimating entropy for 14 symbols per message P(a)=0.000 P(b)=0.214 P(c)=0.786 --> H=0.750 Estimating entropy for 16 symbols per message P(a)=0.125 P(b)=0.312 P(c)=0.562 --> H=1.366 Estimating entropy for 18 symbols per message P(a)=0.111 P(b)=0.056 P(c)=0.833 --> H=0.803... Estimating entropy for 85 symbols per message P(a)=0.106 P(b)=0.176 P(c)=0.718 --> H=1.128 Estimating entropy for 90 symbols per message P(a)=0.067 P(b)=0.200 P(c)=0.733 --> H=1.053 Estimating entropy for 95 symbols per message P(a)=0.116 P(b)=0.179 P(c)=0.705 --> H=1.160 Estimating entropy for 100 symbols per message P(a)=0.090 P(b)=0.180 P(c)=0.730 --> H=1.089 Estimating entropy for 200 symbols per message P(a)=0.130 P(b)=0.210 P(c)=0.660 --> H=1.251... Estimating entropy for 900 symbols per message P(a)=0.100 P(b)=0.211 P(c)=0.689 --> H=1.176 Estimating entropy for 1000 symbols per message P(a)=0.093 P(b)=0.199 P(c)=0.708 --> H=1.135 Estimating entropy for 1100 symbols per message P(a)=0.104 P(b)=0.227 P(c)=0.669 --> H=1.213 Estimating entropy for 1200 symbols per message P(a)=0.098 P(b)=0.197 P(c)=0.706 --> H=1.144 Estimating entropy for 1300 symbols per message P(a)=0.107 P(b)=0.195 P(c)=0.698 --> H=1.166... Estimating entropy for 1900 symbols per message P(a)=0.092 P(b)=0.198 P(c)=0.709 --> H=1.131 Estimating entropy for 5000 symbols per message P(a)=0.105 P(b)=0.195 P(c)=0.701 --> H=1.160 Estimating entropy for 10000 symbols per message P(a)=0.103 P(b)=0.199 P(c)=0.698 --> H=1.163 Estimating entropy for 15000 symbols per message P(a)=0.101 P(b)=0.200 P(c)=0.699 --> H=1.159 Estimating entropy for 20000 symbols per message P(a)=0.102 P(b)=0.201 P(c)=0.697 --> H=1.164 Estimating entropy for 25000 symbols per message P(a)=0.099 P(b)=0.200 P(c)=0.701 --> H=1.155... Estimating entropy for 75000 symbols per message P(a)=0.100 P(b)=0.200 P(c)=0.699 --> H=1.158 Estimating entropy for 80000 symbols per message P(a)=0.100 P(b)=0.199 P(c)=0.701 --> H=1.156 Estimating entropy for 85000 symbols per message P(a)=0.100 P(b)=0.198 P(c)=0.702 --> H=1.154 Estimating entropy for 90000 symbols per message P(a)=0.099 P(b)=0.198 P(c)=0.702 --> H=1.152 Estimating entropy for 95000 symbols per message P(a)=0.102 P(b)=0.200 P(c)=0.699 --> H=1.161 6
example4.py: εντροπία εικόνας - εντροπία εικόνας = ένδειξη ποικιλομορφίας - grayscale εικόνα: - 1 byte ανά pixel - πιθανές τιμές: 0..255 - κάθε πιθανή τιμή ενός pixel θεωρείται ένα διαφορετικό σύμβολο - πρέπει να εκτιμήσουμε τις πιθανότητες P(pixel=0), P(pixel=1),, P(pixel=255) 7
example4.py: εντροπία εικόνας 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 0 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 0 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 0 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 0 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 0 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 0 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 0 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 Entropy = 6.0879 bits/pixel 8
example4.py import ITlib, numpy, os, cv2 import matplotlib.pyplot as plt def computeimageentropy(path): RGB = cv2.imread(path, cv2.cv_load_image_color) # read an image from a jpg file Grayscale = cv2.cvtcolor(rgb, cv2.cv.cv_rgb2gray) # convert to grayscale (2D) countvalues = numpy.zeros((256,)) # inialize histogram (counter for each 0..255 pixel value) for i in range(grayscale.shape[0]): # for each row i for j in range(grayscale.shape[1]): # for each collumn j countvalues[grayscale[i,j]] += 1 # increase the respective grayscale value countvalues /= countvalues.sum() # normalize by sum() H = ITlib.computeEntropy(countValues) # compute entropy return H, Grayscale # return entropy and grayscale H1, I1 = computeimageentropy("data" + os.sep + "dot.jpg") H2, I2 = computeimageentropy("data" + os.sep + "seagray.jpg") H3, I3 = computeimageentropy("data" + os.sep + "citygray.jpg") H4, I4 = computeimageentropy("data" + os.sep + "noisegray.jpg") Hmax = ITlib.computeMaxEntropy(256) plt.subplot(2,2,1); plt.imshow(i1, cmap='greys_r'); plt.title('h = %.2f, P = %.1f%%' % (H1, 100*(1.0-H1/Hmax))) plt.subplot(2,2,2); plt.imshow(i2, cmap='greys_r'); plt.title('h = %.2f, P = %.1f%%' % (H2, 100*(1.0-H2/Hmax))) plt.subplot(2,2,3); plt.imshow(i3, cmap='greys_r'); plt.title('h = %.2f, P = %.1f%%' % (H3, 100*(1.0-H3/Hmax))) plt.subplot(2,2,4); plt.imshow(i4, cmap='greys_r'); plt.title('h = %.2f, P = %.1f%%' % (H4, 100*(1.0-H4/Hmax))) plt.show() 9
example4.py 10