Skip to content

Instantly share code, notes, and snippets.

@jakewilliami
Created October 25, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakewilliami/7b76c85157750b86ecfaddc34016acab to your computer and use it in GitHub Desktop.
Save jakewilliami/7b76c85157750b86ecfaddc34016acab to your computer and use it in GitHub Desktop.
Memory efficient MWE for algorithm involving image IO (TIME INEFFICIENT)
using Images
using BenchmarkTools
import Base: size, getindex, LinearIndices
using Images: Images, coords_spatial
struct IntegralArray{T, N, A} <: AbstractArray{T, N}
data::A
end
function to_integral_image(img_arr::AbstractArray)
array_size = size(img_arr)
integral_image_arr = Array{Images.accum(eltype(img_arr))}(undef, array_size)
sd = coords_spatial(img_arr)
cumsum!(integral_image_arr, img_arr; dims=sd[1])#length(array_size)
for i = 2:length(sd)
cumsum!(integral_image_arr, integral_image_arr; dims=sd[i])
end
return Array{eltype(img_arr), ndims(img_arr)}(integral_image_arr)
end
LinearIndices(A::IntegralArray) = Base.LinearFast()
size(A::IntegralArray) = size(A.data)
getindex(A::IntegralArray, i::Int...) = A.data[i...]
getindex(A::IntegralArray, ids::Tuple...) = getindex(A, ids[1]...)
function filtered_ls(path::AbstractString)::Array{String, 1}
return filter!(f -> ! occursin(r".*\.DS_Store", f), readdir(path, join=true, sort=false))
end
function load_image(
image_path::AbstractString
)::Array{Float64, 2}
img = load(image_path)
img = convert(Array{Float64}, Gray.(img))
return to_integral_image(img)
end
function get_vote(f::Number, i::AbstractArray)
return f .* rand()
end
function learn(positive_path::AbstractString,negative_path::AbstractString)
positive_files = filtered_ls(positive_path)
negative_files = filtered_ls(negative_path)
num_pos = length(positive_files)
num_neg = length(negative_files)
num_imgs = num_pos + num_neg
image_files = vcat(positive_files, negative_files)
votes = zeros((num_imgs, 3000))
num_processed = 0
for image_file in image_files
ii_img = load_image(image_file)
num_processed += 1
# votes[num_processed, :] = rand(3000)
votes[num_processed, :] = Array(map(f -> get_vote(f, ii_img), 1:3000))
end # end loop through images
end
@btime learn(joinpath(dirname(@__DIR__), "data", "main", "trainset", "faces"), joinpath(dirname(@__DIR__), "data", "main", "trainset", "non-faces"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment