Last active
August 24, 2016 00:58
-
-
Save bhaskarvk/1cd37cc42c6a12eb0731aa35d44c807c to your computer and use it in GitHub Desktop.
Comparison of image read performance of imager and magick packages.
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
# You will need to generate images of varying size using ImageMagic's convert utility on the shell. | |
# The shell code below will not work under Windows. Sorry! bash-only. | |
# Run the following shell code to generate some random images | |
# mkdir -p ./data/RandomNoise | |
# (cd ./data/RandomNoise && for i in `seq 100 100 4000`; do convert -size ${i}x${i} xc: +noise Random random-${i}_$i.png;done) | |
# You'll need the following R packages | |
# dplyr, purrr, magick, imager, magrittr, gridExtra, ggplot2, stringr | |
d <- './data/RandomNoise' | |
files <- list.files(d) | |
files.sizes <- purrr::map_dbl(files,function(x) file.size(sprintf("%s/%s",d,x))) | |
files.df <- data.frame(path=stringr::str_c(d,'/',files),name=files,size=files.sizes, stringsAsFactors = F) | |
files.df %<>% | |
dplyr::arrange(size) | |
# To format our X axis | |
format_si <- function(...) { | |
# Format a vector of numeric values according | |
# to the International System of Units. | |
# http://en.wikipedia.org/wiki/SI_prefix | |
# | |
# Based on code by Ben Tupper | |
# https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html | |
# Args: | |
# ...: Args passed to format() | |
# | |
# Returns: | |
# A function to format a vector of strings using | |
# SI prefix notation | |
# | |
function(x) { | |
limits <- c(1e-24, 1e-21, 1e-18, 1e-15, 1e-12, | |
1e-9, 1e-6, 1e-3, 1e0, 1e3, | |
1e6, 1e9, 1e12, 1e15, 1e18, | |
1e21, 1e24) | |
prefix <- c("y", "z", "a", "f", "p", | |
"n", "µ", "m", " ", "k", | |
"M", "G", "T", "P", "E", | |
"Z", "Y") | |
# Vector with array indices according to position in intervals | |
i <- findInterval(abs(x), limits) | |
# Set prefix to " " for very small values < 1e-24 | |
i <- ifelse(i==0, which(limits == 1e0), i) | |
paste(format(round(x/limits[i], 1), | |
trim=TRUE, scientific=FALSE, ...), | |
prefix[i]) | |
} | |
} | |
# magick performance | |
magick_image_read.times <- purrr::map(files.df$path, function(x) system.time(magick::image_read(x))) | |
magick_image_read.times.elapsed <- sapply(magick_image_read.times,function(x) x[[3]]) | |
magick_image_read.times.system <- sapply(magick_image_read.times,function(x) x[[2]]) | |
magick_image_read.times.user <- sapply(magick_image_read.times,function(x) x[[1]]) | |
magick_image_read.times.df = data.frame(size=files.df$size, user=magick_image_read.times.user, | |
system=magick_image_read.times.system, | |
elapsed=magick_image_read.times.elapsed, | |
stringsAsFactors = F) | |
magick_image_read.times.df.tidy <- magick_image_read.times.df %>% tidyr::gather(key='metric',value = 'time',-size) | |
magick_image_read.plot <- ggplot(magick_image_read.times.df.tidy,aes(x=size,y=time,color=metric)) + geom_point() + | |
geom_smooth( se=FALSE, alpha=0.4) + theme_minimal() + | |
scale_x_continuous(labels=format_si()) | |
# imager performance ---- | |
imager_image_read.times <- purrr::map(files.df$path, function(x) system.time(imager::load.image(x))) | |
imager_image_read.times.elapsed <- sapply(imager_image_read.times,function(x) x[[3]]) | |
imager_image_read.times.system <- sapply(imager_image_read.times,function(x) x[[2]]) | |
imager_image_read.times.user <- sapply(imager_image_read.times,function(x) x[[1]]) | |
imager_image_read.times.df = data.frame(size=files.df$size, user=imager_image_read.times.user, | |
system=imager_image_read.times.system, | |
elapsed=imager_image_read.times.elapsed, | |
stringsAsFactors = F) | |
imager_image_read.times.df.tidy <- imager_image_read.times.df %>% tidyr::gather(key='metric',value = 'time',-size) | |
imager_image_read.plot <- ggplot(imager_image_read.times.df.tidy,aes(x=size,y=time,color=metric)) + geom_point() + | |
geom_smooth( se=FALSE, alpha=0.4) + theme_minimal() + | |
scale_x_continuous(labels=format_si()) | |
gridExtra::grid.arrange(magick_image_read.plot + labs(x='Image Size', y='Time (sec)', title='magick::image_read()', colour='Time'), | |
imager_image_read.plot + labs(x='Image Size', y='Time (sec)', title='imager::load.image()', colour='Time'), | |
ncol=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment