Neural network "playground"

imports


In [1]:
import numpy as np
from cStringIO import StringIO
import matplotlib.pyplot as plt
import caffe
from IPython.display import clear_output, Image, display
import cv2
import PIL.Image
import os

In [2]:
os.chdir("start_deep/")

Caffe computation mode

CPU


In [3]:
caffe.set_mode_cpu()

GPU

Make sure you enabled GPU suppor, and have a compatible (ie. nvidia) GPU


In [4]:
caffe.set_device(0)
caffe.set_mode_gpu()

In [4]:
net = caffe.Net('deploy.prototxt', "facenet_iter_200000.caffemodel", caffe.TEST)
#solver = caffe.SGDSolver('facenet_solver.prototxt')
#test_net = solver.testnets[0]

In [5]:
def showarray(a, fmt='jpeg'):
    a = np.uint8(np.clip(a, 0, 255))
    f = StringIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))

In [6]:
#im = np.array(PIL.Image.open('train_images/0/137021_102_88_72_72.pgm'))/256.0
im = np.array(PIL.Image.open('train_images/1/image000619.pgm'))/256.0
im_input = im[np.newaxis, np.newaxis, :, :]
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = im
showarray(im)
print (len(im))
print (len(im[0]))
print (im)
output = net.forward()
print(output)
if output['prob'][0][0] >0.9:
    print "visage"


36
36
[[ 0.20703125  0.30078125  0.3046875  ...,  0.3359375   0.78125     0.9609375 ]
 [ 0.21875     0.34765625  0.36328125 ...,  0.3671875   0.7734375   0.96875   ]
 [ 0.24609375  0.41015625  0.44140625 ...,  0.3515625   0.703125    0.9609375 ]
 ..., 
 [ 0.22265625  0.3515625   0.640625   ...,  0.296875    0.4765625
   0.61328125]
 [ 0.24609375  0.3671875   0.65234375 ...,  0.1953125   0.22265625
   0.2109375 ]
 [ 0.26171875  0.37109375  0.640625   ...,  0.2421875   0.2421875
   0.23046875]]
{'prob': array([[  7.51064799e-04,   9.99248922e-01]], dtype=float32)}

In [ ]:


In [7]:
print cv2.__version__


2.4.13

In [8]:
img = cv2.imread('test_vis.jpg')

In [9]:
lower_res = cv2.pyrDown(img)
even_lower_res = cv2.pyrDown(lower_res)
tiny_res = cv2.pyrDown(even_lower_res)
ridiculous_res = cv2.pyrDown(tiny_res)
we_need_to_go_deeper = cv2.pyrDown(ridiculous_res)

In [10]:
plt.axis("off")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
plt.show()



In [10]:
im =  cv2.imread('test_vis2.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im = cv2.pyrDown(im)
im = cv2.pyrDown(im)
imbase = im
scale = 1
while len(im) >=36*2 and len(im[0]) >36*2:
    im = cv2.pyrDown(im)
    scale*=2
    
    print len(im)
    showarray(im)
    w = len(im)
    h = len(im[0])
    i = 0
    j = 0
    while i < w-36:
        while j < h-36:
            imtmp = np.array(im [i:i+36, j:j+36]/256.0)
            
            im_input = imtmp[np.newaxis, np.newaxis, :, :]
            net.blobs['data'].reshape(*im_input.shape)
            net.blobs['data'].data[...] = imtmp
            output = net.forward()
            if output['prob'][0][1] > 0.99:
                if output['prob'][0][0] < 0.001:
                    """print output
                    print "visage trouvé @ %i , %i"%(i, j)
                    showarray(imtmp*255)"""
                    for x in range (i*scale, (i+36)*scale):
                        imbase[x][j*scale]=255
                        imbase[x][(j+36)*scale]=255
                    for y in range(j*scale, (j+36)*scale):
                        imbase[i*scale][y]=255
                        imbase[(i+36)*scale][y]=255
                else:
                    #print "visage found but discarded due to low confidence"
                    pass
            j+=2
        i+=2
        j = 0
showarray(imbase)


152
76
38

In [21]:
display(Image('cat_other.jpg'))



In [ ]:


In [ ]: