Más contenido relacionado
○○でかんたんお部屋探し!
- 18. # インストール
sudo apt-get install imagemagick
# 対象のディレクトリに移動
cd washiki
# すべて100x100に上書き変換
mogrify -geometry 100x100! *.jpg
cd ../youshiki
mogrify -geometry 100x100! *.jpg
cd ../senmen
mogrify -geometry 100x100! *.jpg
- 20. library(raster)
as.grayscale.array <- function(file, flag){
# 画像サイズ変換はImageMagick等で行っておく
image <- brick( file )
# 画像をRGBの配列に変換
image.rgb <- getValues(image)
# グレースケールの配列に変換
image.bw <- image.rgb[,1]*0.21 +
image.rgb[,2]*0.72 + image.rgb[,3]*0.07
return( c(flag, image.bw) )
}
- 22. # データフレーム化
washiki <- list.files('~/apps/toilet/washiki',
full.names=T)
yoshiki <- list.files('~/apps/toilet/yoshiki', full.names=T)
senmen <- list.files('~/apps/toilet/senmen', full.names=T)
target <- rbind(t(sapply(washiki, as.grayscale.array, 1)),
t(sapply(yoshiki, as.grayscale.array, 0)),
t(sapply(senmen, as.grayscale.array, 0)) )
target <- data.frame(target)
target[,1] <- as.integer(target[,1])
rownames(target) <- c(washiki, yoshiki, senmen)
- 23. library('h2o')
# Deep learningで学習させる
localH2O <- h2o.init(ip = "localhost",
port = 54321,
startH2O = TRUE,
nthreads=-1)
res.dl <- h2o.deeplearning(
x = 2:10001,
y = 1,
training_frame = as.h2o(target),
activation = "TanhWithDropout",
hidden=rep(160,5),
epochs = 20)
pred.dl <- h2o.predict(object=res.dl,
newdata = as.h2o(target))
pred <- as.data.frame(pred.dl)
# 正解率を確認
print(1-sum(abs(round(pred[,1]) -
target[,1]))/length(target[,1]))
- 25. library('h2o')
library(raster)
as.grayscale.array <- function(file, flag){
# 画像サイズ変換はImageMagick等で行っておく
image <- brick( file )
# 画像をRGBの配列に変換
image.rgb <- getValues(image)
# グレースケールの配列に変換
image.bw <- image.rgb[,1]*0.21 + image.rgb[,2]*0.72 + image.rgb[,3]*0.07
return( c(flag, image.bw) )
}
# データフレーム化
washiki <- list.files('~/apps/toilet/washiki', full.names=T)
yoshiki <- list.files('~/apps/toilet/yoshiki', full.names=T)
senmen <- list.files('~/apps/toilet/senmen', full.names=T)
target <- rbind(t(sapply(washiki, as.grayscale.array, 1)),
t(sapply(yoshiki, as.grayscale.array, 0)),
t(sapply(senmen, as.grayscale.array, 0)) )
target <- data.frame(target)
target[,1] <- as.integer(target[,1])
rownames(target) <- c(washiki, yoshiki, senmen)
# Deep learningで学習させる
localH2O <- h2o.init(ip = "localhost", port = 54321, startH2O = TRUE, nthreads=-1)
res.dl <- h2o.deeplearning(x = 2:10001, y = 1, training_frame = as.h2o(target), activation = "TanhWithDropout",
hidden=rep(160,5), epochs = 20)
pred.dl <- h2o.predict(object=res.dl, newdata = as.h2o(target))
pred <- as.data.frame(pred.dl)
# 正解率を確認
print(1-sum(abs(round(pred[,1]) - target[,1]))/length(target[,1]))