R/processImg.R

#'prepcrocess
#'
#'@export
#'@import reticulate
#'@references \insertRef{Gatys2016}{neuralstyleR}
#'
prepcrocess<-function(img){
    torch <- import("torch")
    t_img <- torch$from_numpy(aperm(as.array(img)[,,1,], c(3,1,2)))
    mean_pixel <- torch$DoubleTensor(c(103.939, 116.779, 123.68))
    perm <- torch$LongTensor(c(2L,1L,0L))
    t_img <- t_img$index_select(0L, perm)$mul(256.0)

    mean_pixel<-mean_pixel$view(3L,1L,1L)$expand_as(t_img)

    t_img <- t_img$add(-1, mean_pixel)
    t_img <- t_img$reshape(c(1L,
                             t_img$size(0L),
                             t_img$size(1L),
                             t_img$size(2L)))
    return(t_img $float())

}

#'depcrocess
#'
#'@export
#'@import reticulate
#'@references \insertRef{Gatys2016}{neuralstyleR}
#'
deprocess<-function(t_img){
    torch <- import("torch")

    mean_pixel <- torch$DoubleTensor(c(103.939, 116.779, 123.68))
    mean_pixel <- mean_pixel$view(3L,1L,1L)$expand_as(t_img)
    t_img <- t_img$add(mean_pixel)

    perm <- torch$LongTensor(c(2L,1L,0L))
    t_img <- t_img$index_select(0L, perm)$div(256.0)


    return(as.cimg(aperm(t_img$numpy(), c(2,3,1))))
}
David-J-R/neuralstyleR documentation built on May 8, 2019, 1:54 p.m.