楼主: ReneeBK
1641 7

Image Recognition Tutorial in R MXNet Package [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4897份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49635 个
通用积分
55.6937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-4-26 03:01:57 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. This is a detailed tutorial on image recognition in R using a deep convolutional neural network provided by the MXNet package. After a short post I wrote some times ago I received a lot of requests and emails for a much more detailed explanation, therefore I decided to write this tutorial. This post will show a reproducible example on how to get 97.5% accuracy score on a faces recognition task in R.
复制代码

本帖隐藏的内容

Image Recognition Tutorial in R MXNet Package.pdf (2.66 MB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Recognition cognition Tutorial package image

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
crystal8832 + 40 + 2 + 2 + 2 奖励积极上传好的资料

总评分: 论坛币 + 40  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

沙发
ReneeBK 发表于 2017-4-26 03:02:21
  1. # -*- coding: utf-8 -*-

  2. # Imports
  3. from sklearn.datasets import fetch_olivetti_faces
  4. import numpy as np

  5. # Download Olivetti faces dataset
  6. olivetti = fetch_olivetti_faces()
  7. x = olivetti.images
  8. y = olivetti.target

  9. # Print info on shapes and reshape where necessary
  10. print("Original x shape:", x.shape)
  11. X = x.reshape((400, 4096))
  12. print("New x shape:", X.shape)
  13. print("y shape", y.shape)

  14. # Save the numpy arrays
  15. np.savetxt("C://olivetti_X.csv", X, delimiter = ",")
  16. np.savetxt("C://olivetti_y.csv", y, delimiter = ",", fmt = '%d')

  17. print("\nDownloading and reshaping done!")
复制代码

藤椅
ReneeBK 发表于 2017-4-26 03:02:42
  1. # This script is used to resize images from 64x64 to 28x28 pixels

  2. # Clear workspace
  3. rm(list=ls())

  4. # Load EBImage library
  5. require(EBImage)

  6. # Load data
  7. X <- read.csv("olivetti_X.csv", header = F)
  8. labels <- read.csv("olivetti_y.csv", header = F)

  9. # Dataframe of resized images
  10. rs_df <- data.frame()

  11. # Main loop: for each image, resize and set it to greyscale
  12. for(i in 1:nrow(X))
  13. {
  14.     # Try-catch
  15.     result <- tryCatch({
  16.     # Image (as 1d vector)
  17.     img <- as.numeric(X[i,])
  18.     # Reshape as a 64x64 image (EBImage object)
  19.     img <- Image(img, dim=c(64, 64), colormode = "Grayscale")
  20.     # Resize image to 28x28 pixels
  21.     img_resized <- resize(img, w = 28, h = 28)
  22.     # Get image matrix (there should be another function to do this faster and more neatly!)
  23.     img_matrix <- img_resized@.Data
  24.     # Coerce to a vector
  25.     img_vector <- as.vector(t(img_matrix))
  26.     # Add label
  27.     label <- labels[i,]
  28.     vec <- c(label, img_vector)
  29.     # Stack in rs_df using rbind
  30.     rs_df <- rbind(rs_df, vec)
  31.     # Print status
  32.     print(paste("Done",i,sep = " "))},
  33.     # Error function (just prints the error). Btw you should get no errors!
  34.     error = function(e){print(e)})
  35. }


  36. # Set names. The first columns are the labels, the other columns are the pixels.
  37. names(rs_df) <- c("label", paste("pixel", c(1:784)))

  38. # Train-test split
  39. #-------------------------------------------------------------------------------
  40. # Simple train-test split. No crossvalidation is done in this tutorial.

  41. # Set seed for reproducibility purposes
  42. set.seed(100)

  43. # Shuffled df
  44. shuffled <- rs_df[sample(1:400),]

  45. # Train-test split
  46. train_28 <- shuffled[1:360, ]
  47. test_28 <- shuffled[361:400, ]

  48. # Save train-test datasets
  49. write.csv(train_28, "C://train_28.csv", row.names = FALSE)
  50. write.csv(test_28, "C://test_28.csv", row.names = FALSE)

  51. # Done!
  52. print("Done!")
复制代码

板凳
ReneeBK 发表于 2017-4-26 03:03:04
  1. # Clean workspace
  2. rm(list=ls())

  3. # Load MXNet
  4. require(mxnet)

  5. # Loading data and set up
  6. #-------------------------------------------------------------------------------

  7. # Load train and test datasets
  8. train <- read.csv("train_28.csv")
  9. test <- read.csv("test_28.csv")

  10. # Set up train and test datasets
  11. train <- data.matrix(train)
  12. train_x <- t(train[, -1])
  13. train_y <- train[, 1]
  14. train_array <- train_x
  15. dim(train_array) <- c(28, 28, 1, ncol(train_x))

  16. test_x <- t(test[, -1])
  17. test_y <- test[, 1]
  18. test_array <- test_x
  19. dim(test_array) <- c(28, 28, 1, ncol(test_x))

  20. # Set up the symbolic model
  21. #-------------------------------------------------------------------------------

  22. data <- mx.symbol.Variable('data')
  23. # 1st convolutional layer
  24. conv_1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 20)
  25. tanh_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh")
  26. pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  27. # 2nd convolutional layer
  28. conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5, 5), num_filter = 50)
  29. tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh")
  30. pool_2 <- mx.symbol.Pooling(data=tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  31. # 1st fully connected layer
  32. flatten <- mx.symbol.Flatten(data = pool_2)
  33. fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
  34. tanh_3 <- mx.symbol.Activation(data = fc_1, act_type = "tanh")
  35. # 2nd fully connected layer
  36. fc_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 40)
  37. # Output. Softmax output since we'd like to get some probabilities.
  38. NN_model <- mx.symbol.SoftmaxOutput(data = fc_2)

  39. # Pre-training set up
  40. #-------------------------------------------------------------------------------

  41. # Set seed for reproducibility
  42. mx.set.seed(100)

  43. # Device used. CPU in my case.
  44. devices <- mx.cpu()

  45. # Training
  46. #-------------------------------------------------------------------------------

  47. # Train the model
  48. model <- mx.model.FeedForward.create(NN_model,
  49.                                      X = train_array,
  50.                                      y = train_y,
  51.                                      ctx = devices,
  52.                                      num.round = 480,
  53.                                      array.batch.size = 40,
  54.                                      learning.rate = 0.01,
  55.                                      momentum = 0.9,
  56.                                      eval.metric = mx.metric.accuracy,
  57.                                      epoch.end.callback = mx.callback.log.train.metric(100))

  58. # Testing
  59. #-------------------------------------------------------------------------------

  60. # Predict labels
  61. predicted <- predict(model, test_array)
  62. # Assign labels
  63. predicted_labels <- max.col(t(predicted)) - 1
  64. # Get accuracy
  65. sum(diag(table(test[, 1], predicted_labels)))/40
复制代码

报纸
ReneeBK 发表于 2017-4-26 03:07:34
https://firsttimeprogrammer.blogspot.ca/2016/08/image-recognition-tutorial-in-r-using.html
  1. # Clean workspace
  2. rm(list=ls())

  3. # Load MXNet
  4. require(mxnet)

  5. # Loading data and set up
  6. #-------------------------------------------------------------------------------

  7. # Load train and test datasets
  8. train <- read.csv("train_28.csv")
  9. test <- read.csv("test_28.csv")

  10. # Set up train and test datasets
  11. train <- data.matrix(train)
  12. train_x <- t(train[, -1])
  13. train_y <- train[, 1]
  14. train_array <- train_x
  15. dim(train_array) <- c(28, 28, 1, ncol(train_x))

  16. test_x <- t(test[, -1])
  17. test_y <- test[, 1]
  18. test_array <- test_x
  19. dim(test_array) <- c(28, 28, 1, ncol(test_x))

  20. # Set up the symbolic model
  21. #-------------------------------------------------------------------------------

  22. data <- mx.symbol.Variable('data')
  23. # 1st convolutional layer
  24. conv_1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 20)
  25. tanh_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh")
  26. pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  27. # 2nd convolutional layer
  28. conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5, 5), num_filter = 50)
  29. tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh")
  30. pool_2 <- mx.symbol.Pooling(data=tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  31. # 1st fully connected layer
  32. flatten <- mx.symbol.Flatten(data = pool_2)
  33. fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
  34. tanh_3 <- mx.symbol.Activation(data = fc_1, act_type = "tanh")
  35. # 2nd fully connected layer
  36. fc_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 40)
  37. # Output. Softmax output since we'd like to get some probabilities.
  38. NN_model <- mx.symbol.SoftmaxOutput(data = fc_2)

  39. # Pre-training set up
  40. #-------------------------------------------------------------------------------

  41. # Set seed for reproducibility
  42. mx.set.seed(100)

  43. # Device used. CPU in my case.
  44. devices <- mx.cpu()

  45. # Training
  46. #-------------------------------------------------------------------------------

  47. # Train the model
  48. model <- mx.model.FeedForward.create(NN_model,
  49.                                      X = train_array,
  50.                                      y = train_y,
  51.                                      ctx = devices,
  52.                                      num.round = 480,
  53.                                      array.batch.size = 40,
  54.                                      learning.rate = 0.01,
  55.                                      momentum = 0.9,
  56.                                      eval.metric = mx.metric.accuracy,
  57.                                      epoch.end.callback = mx.callback.log.train.metric(100))

  58. # Testing
  59. #-------------------------------------------------------------------------------

  60. # Predict labels
  61. predicted <- predict(model, test_array)
  62. # Assign labels
  63. predicted_labels <- max.col(t(predicted)) - 1
  64. # Get accuracy
  65. sum(diag(table(test[, 1], predicted_labels)))/40

  66. ################################################################################
  67. #                           OUTPUT
  68. ################################################################################
  69. #
  70. # 0.975
  71. #
复制代码

地板
bbslover 在职认证  发表于 2017-4-26 05:32:00
thanks for sharing

7
nkunku 发表于 2017-4-26 06:53:46
Image Recognition Tutorial in R MXNet Package

8
franky_sas 发表于 2017-4-26 10:07:40

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-30 22:28