Skip to content

Instantly share code, notes, and snippets.

@kumeS
Created June 12, 2020 09:00
Show Gist options
  • Save kumeS/3e5d09654497db85dccd1e806e0f45fd to your computer and use it in GitHub Desktop.
Save kumeS/3e5d09654497db85dccd1e806e0f45fd to your computer and use it in GitHub Desktop.
##実行環境
#macOS Catalina 10.15.5
#R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
#Copyright (C) 2020 The R Foundation for Statistical Computing
#Platform: x86_64-apple-darwin15.6.0 (64-bit)
#RStudio Version 1.2.5042
#rm(list=ls())
#sudo rm -rf /Library/Frameworks/R.framework /Applications/R.app #/usr/local/bin/R /usr/local/bin/Rscript
#sudo rm -rf /Users/[YourHostName]/Library/R
#library()
#Install & Load Packages
#install.packages(c("assertthat", "memoise", "pkgload", "sessioninfo", "fs", "devtools"))
#install.packages("Rcpp") # "Select: No"
#devtools::install_github("rstudio/keras")
library(keras)
#不要
#install_keras()
##Choose to use Python (/usr/local/bin/python)
reticulate::use_python("/usr/local/bin/python", required =T)
reticulate::py_config()
#python: /usr/local/bin/python
#libpython: /usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin/libpython3.7.dylib
#pythonhome: /usr/local/opt/python/Frameworks/Python.framework/Versions/3.7:/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7
#version: 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33.17)]
#numpy: /Users/skume/Library/Python/3.7/lib/python/site-packages/numpy
#numpy_version: 1.16.3
#tensorflow: /usr/local/lib/python3.7/site-packages/tensorflow
#NOTE: Python version was forced by use_python function
library(tensorflow)
#Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.の場合
#options(buildtools.check = function(action) TRUE )
#devtools::install_github("rstudio/reticulate")
library(reticulate)
#devtools::install_github("rstudio/tfhub")
library(tfhub)
#devtools::install_github("rstudio/tfds")
library(tfds)
#devtools::install_github("rstudio/tfdatasets")
library(tfdatasets)
##Install Python module
#pip install "tensorflow>=2.0.0"
#pip install --upgrade tensorflow-hub
#pip install tensorflow-datasets
#pip install pillow
#TensorFlow Hub with Keras
library(keras)
library(tfhub)
#install.packages("magick")
library(magick)
#install.packages("abind")
library(abind)
#install.packages("pins")
library(pins)
##An ImageNet classifier
#Download the classifier
classifier_url <- "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2"
mobilenet_layer <- layer_hub(handle = classifier_url)
#Create the Keras model
input <- layer_input(shape = c(224, 224, 3))
output <- input %>% mobilenet_layer()
model <- keras_model(input, output)
model
#Download the test image
img <- magick::image_read('https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg')
img
img <- img %>%
magick::image_resize(geometry = "224x224x3!") %>%
magick::image_data() %>%
as.numeric() %>%
abind::abind(along = 0) # expand to batch dimension
result <- predict(model, img)
mobilenet_decode_predictions(result[,-1, drop = FALSE])
#class_name class_description score
#1 n03763968 military_uniform 9.355025
#2 n03787032 mortarboard 5.400678
#3 n02817516 bearskin 5.297816
#4 n04350905 suit 5.200008
#5 n09835506 ballplayer 4.792094
##Simple transfer learning
## Downloads Dataset
## 花の5クラス分類: daisy, dandelion, roses, sunflowers, tulips
data_root <- pins::pin("https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz", "flower_photos")
data_root <- fs::path_dir(fs::path_dir(data_root[100]))
image_generator <- image_data_generator(rescale = 1/255, validation_split = 0.2)
training_data <- flow_images_from_directory(
directory = data_root,
generator = image_generator,
target_size = c(224, 224),
subset = "training"
)
validation_data <- flow_images_from_directory(
directory = data_root,
generator = image_generator,
target_size = c(224, 224),
subset = "validation"
)
#Download the model
feature_extractor_url <- "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"
feature_extractor_layer <- layer_hub(handle = feature_extractor_url)
#Create the Model
input <- layer_input(shape = c(224, 224, 3))
output <- input %>%
feature_extractor_layer() %>%
layer_dense(units = training_data$num_classes, activation = "softmax")
model <- keras_model(input, output)
summary(model)
#Compile
model %>%
compile(
loss = "categorical_crossentropy",
optimizer = "adam",
metrics = "acc"
)
#Train the model
model %>%
fit_generator(
training_data,
epochs = 10,
steps_per_epoch = 20,
validation_data = validation_data,
verbose = 2
)
#Evaluate the model
Result <- model %>%
predict_generator(training_data, steps =10)
head(Result)
#Epoch 1/10
#20/20 - 7s - loss: 0.3130 - acc: 0.8898
#20/20 - 18s - loss: 0.3130 - acc: 0.8898 - val_loss: 0.3598 - val_acc: 0.8769
#Epoch 2/10
#20/20 - 8s - loss: 0.2853 - acc: 0.9062
#20/20 - 17s - loss: 0.2853 - acc: 0.9062 - val_loss: 0.3845 - val_acc: 0.8700
#Epoch 3/10
#20/20 - 8s - loss: 0.2705 - acc: 0.9109
#20/20 - 18s - loss: 0.2705 - acc: 0.9109 - val_loss: 0.3561 - val_acc: 0.8741
#Epoch 4/10
#20/20 - 8s - loss: 0.2429 - acc: 0.9359
#20/20 - 17s - loss: 0.2429 - acc: 0.9359 - val_loss: 0.3597 - val_acc: 0.8659
#Epoch 5/10
#20/20 - 8s - loss: 0.2451 - acc: 0.9312
#20/20 - 17s - loss: 0.2451 - acc: 0.9312 - val_loss: 0.3368 - val_acc: 0.8810
#Epoch 6/10
#20/20 - 8s - loss: 0.2273 - acc: 0.9297
#20/20 - 17s - loss: 0.2273 - acc: 0.9297 - val_loss: 0.3329 - val_acc: 0.8824
#Epoch 7/10
#20/20 - 8s - loss: 0.2114 - acc: 0.9312
#20/20 - 17s - loss: 0.2114 - acc: 0.9312 - val_loss: 0.3519 - val_acc: 0.8646
#Epoch 8/10
#20/20 - 8s - loss: 0.2063 - acc: 0.9339
#20/20 - 17s - loss: 0.2063 - acc: 0.9339 - val_loss: 0.3405 - val_acc: 0.8687
#Epoch 9/10
#20/20 - 8s - loss: 0.1731 - acc: 0.9594
#20/20 - 18s - loss: 0.1731 - acc: 0.9594 - val_loss: 0.3377 - val_acc: 0.8700
#Epoch 10/10
#20/20 - 9s - loss: 0.1617 - acc: 0.9516
#20/20 - 19s - loss: 0.1617 - acc: 0.9516 - val_loss: 0.3413 - val_acc: 0.8728
#Transfer learning with tfhub
#This tutorial classifies movie reviews as positive or negative using the text of the review.
#This is an example of binary — or two-class — classification, an important and widely applicable kind of machine learning problem.
#We’ll use the IMDB dataset that contains the text of 50,000 movie reviews from the Internet Movie Database. These are split into 25,000 reviews for training and 25,000 reviews for testing.
#The training and testing sets are balanced,
#meaning they contain an equal number of positive and negative reviews.
#https://tensorflow.rstudio.com/tutorials/beginners/basic-ml/tutorial_basic_text_classification_with_tfhub/
#Download the IMDB dataset
imdb <- tfds_load(
"imdb_reviews:1.0.0",
split = list("train[:60%]", "train[-40%:]", "test"),
as_supervised = TRUE
)
summary(imdb)
#To create a dataset you can use:
first <- imdb[[1]] %>%
dataset_batch(1) %>%
reticulate::as_iterator() %>%
reticulate::iter_next()
str(first)
#Build the model
embedding_layer <- layer_hub(handle = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1")
embedding_layer(first[[1]])
#For this example we will use a pre-trained text embedding model from TensorFlow Hub called google/tf2-preview/gnews-swivel-20dim/1.
##build the model
model <- keras_model_sequential() %>%
layer_hub(
handle = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1",
input_shape = list(),
dtype = tf$string,
trainable = TRUE
) %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
summary(model)
#Model: "sequential"
#__________________________________________________________________________
#Layer (type) Output Shape Param #
#==========================================================================
# keras_layer_1 (KerasLayer) (None, 20) 400020
#__________________________________________________________________________
#dense (Dense) (None, 16) 336
#__________________________________________________________________________
#dense_1 (Dense) (None, 1) 17
#==========================================================================
#Total params: 400,373
#Trainable params: 400,373
#Non-trainable params: 0
#__________________________________________________________________________
#Optimizer & Loss function:
model %>%
compile(
optimizer = "adam",
loss = "binary_crossentropy",
metrics = "accuracy")
#Train the model
model %>%
fit(
imdb[[1]] %>% dataset_shuffle(10000) %>% dataset_batch(512),
epochs = 20,
validation_data = imdb[[2]] %>% dataset_batch(512),
verbose = 2
)
#Evaluate the model
model %>%
evaluate(imdb[[3]] %>% dataset_batch(512), verbose = 0)
#loss accuracy
#0.3220522 0.8619600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment