Created
October 25, 2020 12:33
-
-
Save jakewilliami/b5ddc3d349f4e02e700700bec0688ae5 to your computer and use it in GitHub Desktop.
Time efficient MWE for algorithm involving image IO (MEMORY INEFFICIENT)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 load_images(image_dir::AbstractString) | |
files = filter!(f -> ! occursin(r".*\.DS_Store", f), readdir(image_dir, join=true, sort=false)) | |
images = [] | |
for file in files | |
images = push!(images, get_image_matrix(file)) | |
end | |
return images | |
end | |
function get_image_matrix(image_file::AbstractString; scale_up::Bool=true) | |
img = load(image_file) | |
img_arr = convert(Array{Float64}, Gray.(img)) | |
return img_arr | |
end | |
function get_vote(f::Number, i::AbstractArray) | |
return f .* rand() | |
end | |
function learn(positive_iis::AbstractArray,negative_iis::AbstractArray) | |
num_pos = length(positive_iis) | |
num_neg = length(negative_iis) | |
num_imgs = num_pos + num_neg | |
images = vcat(positive_iis, negative_iis) | |
votes = zeros((num_imgs, 3000)) | |
for t in 1:num_imgs | |
votes[t, :] = Array(map(f -> get_vote(f, images[t]), 1:3000)) | |
end # end show progress in for loop | |
end | |
function main() | |
p_training = load_images(joinpath(homedir(), "projects", "FaceDetection.jl", "data", "main", "trainset", "faces")) | |
p_ii_training = map(to_integral_image, p_training) | |
n_training = load_images(joinpath(homedir(), "projects", "FaceDetection.jl", "data", "main", "trainset", "non-faces")) | |
n_ii_training = map(to_integral_image, n_training) | |
learn(p_ii_training, n_ii_training) | |
end | |
@btime main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment