<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Neural network "playground"
imports
Step1:  Caffe computation mode
CPU
Step2:  GPU
Make sure you enabled GPU suppor, and have a compatible (ie. nvidia) GPU
Step3:  Network loading and tests
Step4:  The cell bellows checks that opencv and its python bindings are properly installed
Step6:  Face detection in image
This block performs the actual processing of an image (specified in the code btw), and checks for faces in it
Step7:  Redraw
Allows you to define a new threshold (provided it's more aggressive than the original), without calculating everything.
Step8:  Train dataset appender
<ASSISTANT_TASK:>
Python Code:
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
os.chdir("start_deep/")
Explanation: Neural network "playground"
imports
End of explanation
caffe.set_mode_cpu()
Explanation: Caffe computation mode
CPU
End of explanation
caffe.set_device(0)
caffe.set_mode_gpu()
Explanation: GPU
Make sure you enabled GPU suppor, and have a compatible (ie. nvidia) GPU
End of explanation
net = caffe.Net('deploy.prototxt', "facenet_iter_200000.caffemodel", caffe.TEST)
#training = caffe.Net('facenet_train_test.prototxt', "facenet_iter_200000.caffemodel", caffe.TRAIN)
#solver = caffe.SGDSolver('facenet_solver.prototxt')
#test_net = solver.testnets[0]
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()))
#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"
Explanation: Network loading and tests
End of explanation
print cv2.__version__
Explanation: The cell bellows checks that opencv and its python bindings are properly installed
End of explanation
orig_scale = 4
def process_chunk(chunk):
    im_input = chunk[np.newaxis, np.newaxis, :, :]
    net.blobs['data'].reshape(*im_input.shape)
    net.blobs['data'].data[...] = imtmp
    return net.forward()['prob'][0][1]
def get_face_prob(chunk):
    return process_chunk(chunk)
def add_outline(img, i, j, scale, confidence):
    color = (255,255,255)
    if confidence > 0.985:
        color = (0,255,0)
    elif confidence >0.96:
        color = (255,255,0)
    else:
        color  =(255,0,0)
    for x in range (int(i*scale), int((i+36)*scale)):
        img[x][int(j*scale)]=color
        img[x][int((j+36)*scale-1)]=color
    for y in range(int(j*scale), int((j+36)*scale)):
        img[int(i*scale)][y]=color
        img[int((i+36)*scale-1)][y]=color
    return img
img =  PIL.Image.open('test_vis4.jpg')
w, h = img.size
img = img.resize((int(w/orig_scale),int(h/orig_scale)), PIL.Image.NEAREST)
imbase = img
img = img.convert('L')
w, h = img.size
scale = 1
print "starting processing"
found_pairs = []
next_scale = 1.3
next_i = 4
while w >=36*2 and h >36*2:
    img = img.resize((int(w/next_scale),int(h/next_scale)), PIL.Image.NEAREST)
    w, h = img.size
    print img.size
    im = np.array(img)
    scale*=next_scale
    
    i = 0
    j = 0
    last_result = 0
    while i < int(h-36):
        next_i = 4
        while j < int(w-36):
            imtmp = np.array(im [i:i+36, j:j+36]/256.0)
            face_prob = get_face_prob(imtmp)
            if  face_prob > 0.5:
                next_i = 2
                while j < int(w-36) and face_prob > last_result:
                    imtmp = np.array(im [i:i+36, j:j+36]/256.0)
                    last_result = face_prob
                    face_prob = get_face_prob(imtmp)
                    j += 1
                if last_result > 0.92:
                    next_i = 1
                    matched = False
                    print last_result
                    print "visage trouvé @ %i , %i"%(i, j)
                    showarray(imtmp*255)
                    for pair in found_pairs[:]: # copy the list to remove while working in it
                        if (abs(pair[0]*pair[3] - i*scale) + abs(pair[1]*pair[3] - j*scale)) < 20*scale :
                            matched = True
                            if pair[2] < last_result:
                                found_pairs.remove(pair)
                                found_pairs.append((i, j, last_result, scale))
                    if not matched:
                        found_pairs.append((i, j, last_result, scale))
                    j+=36
                last_result = 0
                
            j+= 4
        i+=next_i
        j = 0
print "adding overlay"
for pair in found_pairs:
    imbase = add_outline(np.array(imbase),pair[0],pair[1],pair[3], pair[2])
showarray(imbase)
Explanation: Face detection in image
This block performs the actual processing of an image (specified in the code btw), and checks for faces in it
End of explanation
threshold = 0.96
img =  PIL.Image.open('test_vis4.jpg')
w, h = img.size
img = img.resize((int(w/orig_scale),int(h/orig_scale)), PIL.Image.NEAREST)
imbase = img
print "adding overlay"
for pair in found_pairs:
    if pair[2]>threshold:
        imbase = add_outline(np.array(imbase),pair[0],pair[1],pair[3], pair[2])
showarray(imbase)
img = PIL.Image.open('test_vis4.jpg').convert('LA')
arr = np.array(img)
w, h = img.size
print w
a
Explanation: Redraw
Allows you to define a new threshold (provided it's more aggressive than the original), without calculating everything.
End of explanation
img =  PIL.Image.open('neg1.jpg')
w, h = img.size
#img = img.resize((int(w/4),int(h/4)), PIL.Image.NEAREST)
imbase = img
img = img.convert('L')
w, h = img.size
scale = 1
width = 36
height = 36
import array
findex = 4327
with open('posneg.txt', 'a') as f:
    print "starting processing"
    while w >=36*2 and h >36*2:
        img = img.resize((int(w/1.3),int(h/1.3)), PIL.Image.NEAREST)
        w, h = img.size
        print img.size
        im = np.array(img)
        scale*=1.3
        found_pairs = []
        i = 0
        j = 0
        last_result = 0
        while i < int(h-36):
            while j < int(w-36):
                imtmp = np.array(im [i:i+36, j:j+36]/256.0)
                face_prob = get_face_prob(imtmp)
                if  face_prob > 0.9:
                    showarray(imtmp*255)
                    buff=array.array('B')
                    for k in range(0, 36):
                        for l in range(0, 36):
                            buff.append(int(imtmp[k][l]*255))
                    findex += 1 
                    # open file for writing 
                    filename = '0/lbe%i.pgm'%findex
                    try:
                        fout=open("train_images/"+filename, 'wb')
                    except IOError, er:
                        print "Cannot open file ", filename, "Exiting … \n", er
                    # define PGM Header
                    pgmHeader = 'P5' + '\n' + str(width) + '  ' + str(height) + '  ' + str(255) + '\n'
                    # write the header to the file
                    fout.write(pgmHeader)
                    # write the data to the file 
                    buff.tofile(fout)
                    # close the file
                    fout.close()
                    f.write(filename + " 0\n")
                j+=4
            i+=4
            j = 0
        print "adding overlay"
        for pair in found_pairs:
            imbase = add_outline(np.array(imbase),pair[0],pair[1],scale, pair[2])
showarray(imbase)
print findex
Explanation: Train dataset appender
End of explanation
<END_TASK>