Skip to content

Instantly share code, notes, and snippets.

@daniel-perry
Created October 12, 2015 18:09
Show Gist options
  • Save daniel-perry/cd8ccaf80f4fac84d7cf to your computer and use it in GitHub Desktop.
Save daniel-perry/cd8ccaf80f4fac84d7cf to your computer and use it in GitHub Desktop.
sample.py from overfeat library translated into julia
using PyPlot
using PyCall
@pyimport overfeat
@pyimport scipy.ndimage as pyndimage
@pyimport scipy.misc as pymisc
# read image
image = pyndimage.imread("../../samples/bee.jpg")
# resize and crop into a 231x231 image
h0 = size(image,1) #.shape[0]
w0 = size(image,2) #.shape[1]
d0 = float(min(h0, w0))
image = image[int(round((h0-d0)/2.))+1:int(round((h0-d0)/2.)+d0),
int(round((w0-d0)/2.))+1:int(round((w0-d0)/2.)+d0), :]
image = pymisc.imresize(image, (231, 231)) #.astype(numpy.float32)
image = convert(Array{Float32}, image)
im2 = copy(image)
# numpy loads image with colors as last dimension, transpose tensor
h = size(image,1) #.shape[0]
w = size(image,2) #.shape[1]
c = size(image,3) #.shape[2]
image = reshape(image, w*h, c)
image = image' #.transpose()
image = reshape(image, c, h, w)
println( "Image size :", size(image))
figure()
imshow(reshape(reshape(image, c, w*h)', w,h,c))
# initialize overfeat. Note that this takes time, so do it only once if possible
overfeat.init("../../data/default/net_weight_0", 0)
# run overfeat on the image
b = overfeat.fprop(image)
# display top 5 classes
b = b[:]
top = [(b[i], i-1) for i in 1:length(b)]
sort!(top) #.sort()
println("\nTop classes :")
for i=0:4
println(overfeat.get_class_name(top[end-i][2]))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment