global_dl.R

Published by onesixx on

# DL ---------------------------------------------------------------------------
library(reticulate)
# install_miniconda()
# ➜ ~/.local/share/r-miniconda/bin/conda init bash 
# ➜ source ~/.bashrc

# ➜ conda create --clone r-reticulate --name sixxDL
# ➜ conda activate sixxDL
# ➜ python -m pip install --upgrade pip

myENV='sixxDL'
use_condaenv(condaenv=myENV, required=T)
# conda_install(envname=myENV, c("matplotlib","pandas","seaborn","scikit-learn"))


library(keras)
library(tensorflow)
if (Sys.info()["sysname"]=="Darwin"){
  use_backend(backend=c("plaidml"))
  #keras::k_backend()
  #system("plaidbench keras mobilenet")
} else {
  ### "keras2.4.3","tensorflow-hub","h5py","pyyaml==3.12","requests","Pillow","scipy"
  # install_keras(method="conda", tensorflow="2.1.2-gpu")
  myGPU <- tf$config$experimental$get_visible_devices('GPU')[[1]]
  tf$config$experimental$set_memory_growth(device=myGPU, enable=TRUE)
  
  use_backend(backend="tensorflow")
}
#~$ nvidia-smi -l 1

# Library ----------------------------------------------------------------------
pLIST<- c(
  'tidyverse', 'data.table','fst', 'stringr', 'glue',
  'lubridate', 'anytime','hms', 'zoo', # DateTime manipulation
  'egg', 'plotly', 'hrbrthemes', 'ggpmisc'
)
pLIST_NEW <- pLIST[!(pLIST %in% installed.packages()[,"Package"])]
if(length(pLIST_NEW)){
  install.packages(pLIST_NEW, dependencies=T)
  sapply(pLIST, require, character.only=T)
} else{
  sapply(pLIST, require, character.only=T)
}
rm(pLIST,pLIST_NEW)
# https://github.com/Rdatatable/data.table/wiki/Installation#openmp-enabled-compiler-for-mac

# Set CONSTANT Value ---------------------------------------------------------------
options(encoding="UTF-8")
options(digits=6, scipen=666, max.print=666, digits.secs=6)
set.seed(666)
Pjt_PATH =getwd()
DATA_PATH=file.path("~","DATA",str_split(Pjt_PATH, "/")[[1]] %>% tail(1))
ifelse(dir.exists(DATA_PATH), FALSE, dir.create(DATA_PATH))
#import_roboto_condensed()       # using Font Book app
theme_set(
  hrbrthemes::theme_ipsum(base_size=9) +
    theme(
      #legend.position = "none",
      #axis.ticks = element_blank(),
      #panel.grid = element_blank()
      panel.background = element_rect(fill="white")
    )
)
# scales::show_col(palette_light())
# palette_light() %>% names()

# Function ---------------------------------------------------------------------
# uF_checkFolder <- function(Folder){
#   ifelse(dir.exists(Folder), FALSE, dir.create(Folder))
# }
# Function ---------------------------------------------------------------------
uF_checkFolder <- function(Folder){
  ifelse(dir.exists(Folder), FALSE, dir.create(Folder))
}

# read 한글 csv
uF_read_any <- function(text, sep="", ...) {
  encoding <- as.character(guess_encoding(text)[1,1])
  setting  <- as.character(tools::file_ext(text))
  if(setting=='xlsx'){
      result <- read_excel(text)
  } else {
      if(sep!="" | !(setting  %in% c("csv","txt"))){ setting <- "custom" }
      separate <- list(csv=",", txt="\
", custom=sep)
      result   <- fread(text, sep=separate[[setting]], fileEncoding=encoding, ...)
  }
  return(result)
}

uF_getOS <- function(){
  sysinf <- Sys.info()
  if (!is.null(sysinf)){
    os <- sysinf['sysname']
    if (os=='Darwin') os <- "osx"
  } else { ## mystery machine
    os <- .Platform$OS.type
    if (grepl("^darwin", R.version$os))    os <- "osx"
    if (grepl("linux-gnu", R.version$os))  os <- "linux"
  }
  tolower(os)
}


# plot ------------------
uF_histPlot <-  function(history){
  p <- as.data.table(history) %>% 
    ggplot(aes(x=epoch, y=value, group=data, color=data)) + 
    geom_point(size=.6) + geom_line()  + 
    stat_peaks(colour="red")+ 
    stat_peaks(geom="text", colour="red", vjust=-0.5, check_overlap=T, span=NULL)+
    stat_valleys(colour="blue")+ 
    stat_valleys(geom="text", colour="blue", vjust=-0.5, check_overlap=T, span=NULL)+
    facet_grid(metric~., scales='free') + 
    scale_y_continuous(labels=function(x) sprintf("%.2f", x)) 
  return(p)
}

uF_vc_plot <-  function(dataset, totalTime=NULL, gg=FALSE){
  # dataset <- cyc_info[cur>0, ]
  p <- dataset %>% ggplot(aes(x=tot_time)) + 
    geom_path(aes(y=cur,     color =as.factor(step)))    + geom_point(aes(y=cur), size=0.1) +
    geom_path(aes(y=vol*15,  color =as.factor(step)))    + geom_point(aes(y=vol*15), size=0.1) +
    scale_y_continuous(sec.axis=sec_axis(~./15, name="Voltage")) +
    labs(y="Current") +
    theme(legend.position="bottom") 
  if(!is.null(vertical)){
    vertical <- range(totalTime)
    p <- p + 
      geom_vline(xintercept = vertical[1], linetype="twodash", color = "red", size=0.1) +
      geom_vline(xintercept = vertical[2], linetype="twodash", color = "red", size=0.1) 
  }
  if(gg){
    return(ggplotly(p))
  } else{
    return(p)
  }
}


uF_mulit_plot <-  function(dataset1, dataset2){
  p1 <- uF_vc_plot(dataset1)
  p2 <- uF_vc_plot(dataset2)
  ggarrange(p1,p2, nrow=1,ncol=2)
}

Categories: Keras

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x