Skip to content

Instantly share code, notes, and snippets.

@Whitetigerswt
Created June 21, 2016 19:14
Show Gist options
  • Save Whitetigerswt/16ff146d7ca6ab0f44f4ca2bb936a96b to your computer and use it in GitHub Desktop.
Save Whitetigerswt/16ff146d7ca6ab0f44f4ca2bb936a96b to your computer and use it in GitHub Desktop.
from PIL import Image
import numpy as np
def main():
im = Image.open("image.jpg").convert('LA') #Can be many different formats.
pix = im.load()
#emulate CaffeNet implementation.
im = im.resize((227, 227))
kernel_size = 3
stride = 2
padding = 0
pooling_type = max
pooling_height = (im.size[1] + (2 * padding) - kernel_size) / (stride + 1);
pooling_width = (im.size[0] + (2 * padding) - kernel_size) / (stride + 1);
im = im.resize((pooling_width, pooling_height))
val = []
secondval = []
for i in xrange(0, im.size[1] + padding, stride):
for x in xrange(0, im.size[0] + padding, stride):
for y in xrange(0, kernel_size**2, 2):
#since we're assuming this is single channel, they will all ahve the same RGB value for R G and B.
#This is why we are only looking at the first element in the tuple.
if(i >= im.size[1] or x >= im.size[0]):
val.append(0)
continue
val.append(pix[x, i][0])
if x+1 >= im.size[0]:
secondval.append(0)
continue
secondval.append(pix[x+1, i][0])
pix[x,i] = (pooling_type(val), pooling_type(secondval))
val = []
secondval = []
print im.size
im.save('pooled_image.png')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment