00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import util as ut
00032 import dimreduce as dr
00033 import numpy as np
00034 import pickle as pk
00035 import math
00036 import exceptions
00037
00038 def load_pickle(filename):
00039 p = open(filename, 'r')
00040 picklelicious = pk.load(p)
00041 p.close()
00042 return picklelicious
00043
00044 def reconstruct_input(nparray):
00045
00046 npbig = nparray[0:675].A
00047 npsmall = nparray[675:].A
00048
00049
00050 npbig_rs = npbig.reshape((15,15,3))
00051 npsml_rs = npsmall.reshape((3,3,3))
00052
00053
00054 img = ut.np2cv(npbig_rs.astype(np.uint8))
00055 context = ut.np2cv(npsml_rs.astype(np.uint8))
00056 return img, context
00057
00058 def normalize_for_display(vec):
00059 return (255 * ((vec - np.min(vec)) / np.max(vec)))
00060
00061 def scale_image(img, scale):
00062 new_img = cv.CreateImage((img.width*scale, img.height*scale), img.depth, img.nChannels)
00063 cv.Resize(img, new_img, cv.CV_INTER_NN)
00064 return new_img
00065
00066 def tile_images(img_width, img_height, num_width, num_height, images, channels=3):
00067 w = img_width * num_width
00068 h = img_height * num_height
00069 image = cv.CreateImage((int(w), int(h)), 8, channels)
00070 cv.Set(image, cv.Scalar(255,255,255))
00071 while len(images) > 0:
00072 try:
00073 for y in range(int(num_height)):
00074 for x in range(int(num_width)):
00075 small_tile = images.pop()
00076 img_x = x * img_width
00077 img_y = y * img_height
00078 cropped = cv.GetSubRect(image, cv.Rectangle(img_x, img_y, img_width,img_height))
00079 cv.Copy(small_tile, cropped)
00080 except exceptions.IndexError, e:
00081 break
00082 return image
00083
00084 def display(vec, name):
00085 patch, context = reconstruct_input(vec)
00086 patch = scale_image(patch, 5)
00087 context = scale_image(context, 5)
00088 hg.cvSaveImage(name + '_patch.png', patch)
00089 hg.cvSaveImage(name + '_context.png', context)
00090 hg.cvShowImage('image', patch)
00091 hg.cvShowImage('context', context)
00092 hg.cvWaitKey()
00093
00094 def tile_nsave(vecs):
00095 patches = []
00096 for i in xrange(vecs.shape[1]):
00097 patch, context = reconstruct_input(vecs[:,i])
00098 patches.append(patch)
00099 tile_width = math.ceil(math.pow(vecs.shape[1], .5))
00100 large_image = tile_images(15, 15, tile_width, tile_width, patches, 3)
00101 return large_image
00102
00103 show_pca = False
00104 save_pca_bases = False
00105 process_inputs = False
00106 separate_negatives_positives = True
00107 dataset = load_pickle('PatchClassifier.dataset.pickle')
00108 hg.cvNamedWindow('image', 1)
00109 hg.cvNamedWindow('context', 1)
00110 pca_basis = normalize_for_display(dataset.projection_basis)
00111
00112 if show_pca:
00113 for i in range(pca_basis.shape[1]):
00114 print 'basis', i
00115 display(pca_basis[:,i], 'pca_basis'+str(i))
00116
00117 if save_pca_bases:
00118 large_image = tile_nsave(pca_basis)
00119 large_image = scale_image(large_image, 8)
00120 hg.cvSaveImage('pca_large_image.png', large_image)
00121
00122 if process_inputs:
00123 large_image = tile_nsave(dataset.inputs)
00124 hg.cvSaveImage('inputs.png', large_image)
00125
00126 if separate_negatives_positives:
00127 r, c = np.where(dataset.outputs == 0)
00128 negatives = tile_nsave(dataset.inputs[:,c.A[0]])
00129 r, c = np.where(dataset.outputs == 1)
00130 positives = tile_nsave(dataset.inputs[:,c.A[0]])
00131 hg.cvSaveImage('negatives.png', negatives)
00132 hg.cvSaveImage('positives.png', positives)
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329