Skip to content

Commit 79a24d9

Browse files
updating ImageNet classification example
1 parent 3b3d480 commit 79a24d9

File tree

2 files changed

+3585
-1970
lines changed

2 files changed

+3585
-1970
lines changed

examples/ijulia/ilsvrc12/imagenet-classifier.ipynb

Lines changed: 3572 additions & 1952 deletions
Large diffs are not rendered by default.

tools/image-classifier.jl

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Images # requires Package Images.jl
2+
using FileIO # requires Package FileIO.jl
23
using Colors # requires Package Colors.jl
34
using Mocha
45
using Compat
@@ -57,17 +58,17 @@ end
5758

5859
#-- Classify a bunch of images via filename
5960
function classify{T<:AbstractString}(classifier::ImageClassifier, filenames::Vector{T})
60-
return classify(classifier, map(Images.imread, filenames))
61+
return classify(classifier, map(FileIO.load, filenames))
6162
end
6263

6364
#-- Classify a single image
64-
function classify(classifier::ImageClassifier, image::Image)
65-
results = classify(classifier, Image[image])
65+
function classify(classifier::ImageClassifier, image::AbstractArray)
66+
results = classify(classifier, Array[image])
6667
return results[1]
6768
end
6869

6970
#-- Classify a bunch of images
70-
function classify(classifier::ImageClassifier, images::Vector{Image})
71+
function classify{T<:AbstractArray}(classifier::ImageClassifier, images::Vector{T})
7172
results = classify_batch(classifier, images, 1)
7273
idx = classifier.batch_size+1
7374
while idx <= length(images)
@@ -83,30 +84,24 @@ end
8384
# Implementations
8485
################################################################################
8586
#-- Propress images
86-
function preprocess(classifier::ImageClassifier, images::Vector{Image})
87+
function preprocess{T<:AbstractArray}(classifier::ImageClassifier, images::Vector{T})
8788
map(images) do image
8889
if (width(img), height(img)) != classifier.image_wh
8990
error("Image size ($(width(img)),$(height(img))) does not match the network input $(classifier.image_wh), and Julia does not have imresize yet........")
9091
end
9192

9293
if classifier.grayscale
93-
if colorspace(image) != "Gray"
94-
image = convert(Image{Gray}, image)
94+
if ~(eltype(image) <: ColorTypes.Gray)
95+
image = convert(Array{Gray}, image)
9596
end
9697
else
97-
image = convert(Image{RGB}, image)
98-
image = separate(image) # separate color channels
98+
image = convert(Array{RGB}, image)
99+
image = permutedims(channelview(image), [2,3,1]) # separate color channels
99100
end
100101

101-
data = convert(Array, image)
102+
data = convert(Array{Float32}, image)
102103
data = reshape(data, size(data,1),size(data,2),size(data,3))
103-
if spatialorder(image) == ["x","y"]
104-
# row major
105-
elseif spatialorder(image) == ["y","x"]
106-
data = permutedims(data, (2,1,3)) # permute to row-major
107-
else
108-
error("Unknown spatialorder: $(spatialorder(image))")
109-
end
104+
data = permutedims(data, (2,1,3)) # permute to row-major
110105

111106
# now data is in canonical row-major RGB/Gray format
112107
if classifier.sp_order != (1,2)
@@ -129,7 +124,7 @@ function preprocess(classifier::ImageClassifier, images::Vector{Image})
129124
end
130125

131126
#-- Classify a batch or less
132-
function classify_batch(classifier::ImageClassifier, images::Vector{Image}, idx::Int)
127+
function classify_batch{T<:AbstractArray}(classifier::ImageClassifier, images::Vector{T}, idx::Int)
133128
#-- prepare the data
134129
idx_end = min(idx+classifier.batch_size-1, length(images))
135130
images = preprocess(classifier, images[idx:idx_end])

0 commit comments

Comments
 (0)