26
26
import timeit
27
27
28
28
import numpy
29
+ import cPickle
29
30
30
31
import theano
31
32
import theano .tensor as T
34
35
35
36
from logistic_sgd_test import LogisticRegression , load_data
36
37
from mlp import HiddenLayer
38
+ from fetex_image import FetexImage
37
39
38
40
39
41
class LeNetConvPoolLayer (object ):
@@ -117,9 +119,9 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
117
119
# def evaluate_lenet5(learning_rate=0.1, n_epochs=100,
118
120
# dataset='mnist.pkl.gz',
119
121
# nkerns=[(96) , (256)], batch_size=300):
120
- def evaluate_lenet5 (learning_rate = 0.1 , n_epochs = 100 ,
122
+ def evaluate_lenet5 (learning_rate = 0.1 , n_epochs = 2 ,
121
123
dataset = 'mnist.pkl.gz' ,
122
- nkerns = [(25 / 1 ) , (25 / 1 )], batch_size = 4 ):
124
+ nkerns = [(25 / 1 ) , (25 / 1 )], batch_size = 400 ):
123
125
124
126
""" Demonstrates lenet on MNIST dataset
125
127
@@ -224,14 +226,14 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=100,
224
226
rng ,
225
227
input = layer2_input ,
226
228
n_in = nkerns [1 ] * 13 * 13 ,
227
- n_out = 2 ,
229
+ n_out = 400 ,
228
230
activation = T .tanh
229
231
)
230
232
231
233
# classify the values of the fully-connected sigmoidal layer
232
234
#layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10)
233
235
#layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=2)
234
- layer4 = LogisticRegression (input = layer3 .output , n_in = 2 , n_out = 2 )
236
+ layer4 = LogisticRegression (input = layer3 .output , n_in = 400 , n_out = 21 )
235
237
236
238
# the cost we minimize during training is the NLL of the model
237
239
#cost = layer3.negative_log_likelihood(y)
@@ -352,9 +354,33 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=100,
352
354
(epoch , minibatch_index + 1 , n_train_batches ,
353
355
test_score * 100. ))
354
356
357
+ output = open ('../data/layer0.pkl' , 'wb' )
358
+ cPickle .dump (layer0 , output ,protocol = - 1 )
359
+ output .close ()
360
+
361
+ output = open ('../data/layer1.pkl' , 'wb' )
362
+ cPickle .dump (layer1 , output ,protocol = - 1 )
363
+ output .close ()
364
+
365
+ # output = open('../data/layer2.pkl', 'wb')
366
+ # cPickle.dump(layer2, output,protocol=-1)
367
+ # output.close()
368
+
369
+ output = open ('../data/layer3.pkl' , 'wb' )
370
+ cPickle .dump (layer3 , output ,protocol = - 1 )
371
+ output .close ()
372
+
373
+ output = open ('../data/layer4.pkl' , 'wb' )
374
+ cPickle .dump (layer4 , output ,protocol = - 1 )
375
+ output .close ()
376
+
355
377
# save the best model
356
378
# with open('best_model.pkl', 'w') as f:
357
- # cPickle.dump(classifier, f)
379
+ # cPickle.dump(layer0, f)
380
+ # cPickle.dump(layer1, f)
381
+ # cPickle.dump(layer2, f)
382
+ # cPickle.dump(layer3, f)
383
+ # cPickle.dump(layer4, f)
358
384
359
385
if patience <= iter :
360
386
done_looping = True
@@ -369,32 +395,155 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=100,
369
395
os .path .split (__file__ )[1 ] +
370
396
' ran for %.2fm' % ((end_time - start_time ) / 60. ))
371
397
372
- def predict ():
373
- """
374
- An example of how to load a trained model and use it
375
- to predict labels.
376
- """
398
+ # def predict(self, data):
399
+ # """
400
+ # the CNN expects inputs with Nsamples = self.batch_size.
401
+ # In order to run 'predict' on an arbitrary number of samples we
402
+ # pad as necessary.
403
+ # """
404
+ # if isinstance(data, list):
405
+ # data = np.array(data)
406
+ # if data.ndim == 1:
407
+ # data = np.array([data])
408
+
409
+ # nsamples = data.shape[0]
410
+ # n_batches = nsamples//self.batch_size
411
+ # n_rem = nsamples%self.batch_size
412
+ # if n_batches > 0:
413
+ # preds = [list(self.predict_wrap(data[i*self.batch_size:(i+1)*self.batch_size]))\
414
+ # for i in range(n_batches)]
415
+ # else:
416
+ # preds = []
417
+ # if n_rem > 0:
418
+ # z = np.zeros((self.batch_size, self.n_in * self.n_in))
419
+ # z[0:n_rem] = data[n_batches*self.batch_size:n_batches*self.batch_size+n_rem]
420
+ # preds.append(self.predict_wrap(z)[0:n_rem])
421
+
422
+ # return np.hstack(preds).flatten()
423
+
424
+ def cosine_distance (a , b ):
425
+ import numpy as np
426
+ from numpy import linalg as LA
427
+ dot_product = np .dot (a ,b .T )
428
+ cosine_distance = dot_product / (LA .norm (a ) * LA .norm (b ))
429
+ return cosine_distance
430
+
431
+ def predict ():
432
+
433
+ from sktheano_cnn import MetaCNN as CNN
434
+ cnn = CNN ()
435
+
436
+ pkl_file = open ( '../data/train_set.pkl' , 'rb' )
437
+ train_set = cPickle .load (pkl_file )
438
+
439
+ pkl_file = open ( '../data/valid_set.pkl' , 'rb' )
440
+ valid_set = cPickle .load (pkl_file )
441
+
442
+ pkl_file = open ( '../data/test_set.pkl' , 'rb' )
443
+ test_set = cPickle .load (pkl_file )
444
+
445
+ """An example of how to load a trained model and use it
446
+ to predict labels.
447
+ """
448
+
449
+ fe = FetexImage (verbose = True )
450
+ # load the saved model
451
+ classifier = cPickle .load (open ('best_model.pkl' ))
452
+
453
+ layer0 = cPickle .load (open ('../data/layer0.pkl' ))
454
+ layer1 = cPickle .load (open ('../data/layer1.pkl' ))
455
+ # layer2 = cPickle.load(open('../data/layer2.pkl'))
456
+ layer3 = cPickle .load (open ('../data/layer3.pkl' ))
457
+ layer4 = cPickle .load (open ('../data/layer4.pkl' ))
458
+
459
+ #layer0_input = x.reshape((batch_size, 3, 64, 64))
460
+
461
+ # predict = theano.function(
462
+ # outputs=layer4.y_pred,
463
+ # givens = {x : train_set_x[0] }
464
+ # )
465
+
466
+ # compile a predictor function
467
+ predict_model = theano .function (
468
+ inputs = [classifier .input ],
469
+ outputs = classifier .y_pred )
470
+
471
+ # We can test it on some examples from test test
472
+ dataset = 'mnist.pkl.gz'
473
+ datasets = load_data (dataset )
377
474
378
- # load the saved model
379
- classifier = cPickle . load ( open ( 'best_model.pkl' ) )
475
+ test_set_x , test_set_y = datasets [ 2 ]
476
+ test_set_x = test_set_x . get_value ( )
380
477
381
- # compile a predictor function
382
- predict_model = theano .function (
383
- inputs = [classifier .input ],
384
- outputs = classifier .y_pred )
478
+ train_set_x , train_set_y = datasets [0 ]
479
+ train_set_x = train_set_x .get_value ()
480
+
481
+ pkl_file = open ( '../data/X_original.pkl' , 'rb' )
482
+ X_original = cPickle .load (pkl_file )
483
+
484
+ a = X_original [0 ]
485
+ #fe.reconstructImage(a).show()
486
+
487
+ #predicted_values = predict_model([a])
488
+
489
+ get_input = theano .function (
490
+ inputs = [classifier .input ],
491
+ outputs = classifier .input
492
+ )
385
493
386
- # We can test it on some examples from test test
387
- dataset = 'mnist.pkl.gz'
388
- datasets = load_data (dataset )
389
- test_set_x , test_set_y = datasets [2 ]
390
- test_set_x = test_set_x .get_value ()
494
+ a = get_input (train_set_x [0 :1 ])
495
+ #print a.shape
391
496
392
- predicted_values = predict_model (test_set_x [:10 ])
393
- print ("Predicted values for the first 10 examples in test set:" )
394
- print predicted_values
497
+ x = T .matrix ('x' ) # the data is presented as rasterized images
498
+ predict = theano .function (
499
+ inputs = [x ],
500
+ outputs = layer3 .output
501
+ )
502
+ # givens = { x : train_set_x[0] }
503
+ #train_set_x = train_set_x[0:400]
504
+ #x = train_set_x.reshape((400, 3, 64, 64))
505
+ x = train_set_x .reshape (np .zeros ((400 ,3 ,64 ,64 )))
506
+ print predict (x )
507
+ #predicted_values = predict_model([train_set_x[0]])
508
+ #print predicted_values
509
+ return "fffff"
510
+
511
+
512
+ max_similarity = 0
513
+ max_similarity_pos = - 1
514
+ #for i in xrange(1,len(train_set_x)):
515
+ for i in xrange (1 ,1000 ):
516
+ b = get_input ([train_set_x [i ]])
517
+ d = cosine_distance (a , b )
518
+ if d > max_similarity :
519
+ max_similarity = d
520
+ max_similarity_pos = i
521
+
522
+ fe .reconstructImage (X_original [max_similarity_pos ]).show ()
523
+
524
+ #a = a.flatten(order='F')
525
+ # a = a * 256
526
+ # a = numpy.array(a,dtype=numpy.uint8)
527
+
528
+ #b = b.flatten(order='F')
529
+ # b = b * 256
530
+ # b = numpy.array(b,dtype=numpy.uint8)
531
+
532
+ # a = get_input([a])
533
+ # b = get_input([b])
534
+
535
+ print a .shape
536
+ print b .shape
537
+ print cosine_distance (a , b )
538
+
539
+ # #print get_input(test_set_x[0:1]).sum()
540
+
541
+ # print ("Predicted values for the first 10 examples in test set:")
542
+ # print predicted_values
395
543
396
544
if __name__ == '__main__' :
397
- evaluate_lenet5 ()
545
+ #evaluate_lenet5()
546
+ predict ()
398
547
399
548
400
549
def experiment (state , channel ):
0 commit comments