Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. library(mxnet)
  2. library(EBImage)
  3. library(pbapply)
  4. library(caret)
  5. image_dir<-"C:\\Users\\Marcin\\Desktop\\train"
  6. example_cat_image <- readImage(file.path(image_dir, "cat.0.jpg"))
  7. display(example_cat_image)
  8.  
  9. example_dog_image <- readImage(file.path(image_dir, "dog.0.jpg"))
  10. display(example_dog_image)
  11.  
  12. width <- 28
  13. height <- 28
  14.  
  15. extract_feature <- function(dir_path, width, height, is_cat = TRUE, add_label = TRUE) {
  16. img_size <- width*height
  17. ## List images in path
  18. images_names <- list.files(dir_path)
  19. if (add_label) {
  20. ## Select only cats or dogs images
  21. images_names <- images_names[grepl(ifelse(is_cat, "cat", "dog"), images_names)]
  22. ## Set label, cat = 0, dog = 1
  23. label <- ifelse(is_cat, 0, 1)
  24. }
  25. print(paste("Start processing", length(images_names), "images"))
  26. ## This function will resize an image, turn it into greyscale
  27. feature_list <- pblapply(images_names, function(imgname) {
  28. ## Read image
  29. img <- readImage(file.path(dir_path, imgname))
  30. ## Resize image
  31. img_resized <- resize(img, w = width, h = height)
  32. ## Set to grayscale
  33. grayimg <- channel(img_resized, "gray")
  34. ## Get the image as a matrix
  35. img_matrix <- grayimg@.Data
  36. ## Coerce to a vector
  37. img_vector <- as.vector(t(img_matrix))
  38. return(img_vector)
  39. })
  40. ## bind the list of vector into matrix
  41. feature_matrix <- do.call(rbind, feature_list)
  42. feature_matrix <- as.data.frame(feature_matrix)
  43. ## Set names
  44. names(feature_matrix) <- paste0("pixel", c(1:img_size))
  45. if (add_label) {
  46. ## Add label
  47. feature_matrix <- cbind(label = label, feature_matrix)
  48. }
  49. return(feature_matrix)
  50. }
  51.  
  52. cats_data <- extract_feature(dir_path = image_dir, width = width, height = height)
  53. dogs_data <- extract_feature(dir_path = image_dir, width = width, height = height, is_cat = FALSE)
  54. dim(cats_data)
  55. dim(dogs_data)
  56.  
  57. saveRDS(cats_data, "cat.rds")
  58. saveRDS(dogs_data, "dog.rds")
  59.  
  60. ## Bind rows in a single dataset
  61. complete_set <- rbind(cats_data, dogs_data)
  62. ## test/training partitions
  63. training_index <- createDataPartition(complete_set$label, p = .9, times = 1)
  64. training_index <- unlist(training_index)
  65. train_set <- complete_set[training_index,]
  66. dim(train_set)
  67.  
  68. test_set <- complete_set[-training_index,]
  69. dim(test_set)
  70.  
  71.  
  72. train_data <- data.matrix(train_set)
  73. train_x <- t(train_data[, -1])
  74. train_y <- train_data[,1]
  75. train_array <- train_x
  76. dim(train_array) <- c(28, 28, 1, ncol(train_x))
  77.  
  78. test_data <- data.matrix(test_set)
  79. test_x <- t(test_set[,-1])
  80. test_y <- test_set[,1]
  81. test_array <- test_x
  82. dim(test_array) <- c(28, 28, 1, ncol(test_x))
  83.  
  84.  
  85. mx_data <- mx.symbol.Variable('data')
  86. ## 1st convolutional layer 5x5 kernel and 20 filters.
  87. conv_1 <- mx.symbol.Convolution(data = mx_data, kernel = c(5, 5), num_filter = 20)
  88. tanh_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh")
  89. pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2,2 ))
  90. ## 2nd convolutional layer 5x5 kernel and 50 filters.
  91. conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5,5), num_filter = 50)
  92. tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh")
  93. pool_2 <- mx.symbol.Pooling(data = tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  94. ## 1st fully connected layer
  95. flat <- mx.symbol.Flatten(data = pool_2)
  96. fcl_1 <- mx.symbol.FullyConnected(data = flat, num_hidden = 500)
  97. tanh_3 <- mx.symbol.Activation(data = fcl_1, act_type = "tanh")
  98. ## 2nd fully connected layer
  99. fcl_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 2)
  100. ## Output
  101. NN_model <- mx.symbol.SoftmaxOutput(data = fcl_2)
  102.  
  103. ## Set seed for reproducibility
  104. mx.set.seed(100)
  105.  
  106. ## Device used. Sadly not the GPU :-(
  107. device <- mx.cpu()
  108.  
  109. ## Train on 1200 samples
  110. model <- mx.model.FeedForward.create(NN_model, X = train_array, y = train_y,
  111. ctx = device,
  112. num.round = 30,
  113. array.batch.size = 100,
  114. learning.rate = 0.05,
  115. momentum = 0.9,
  116. wd = 0.00001,
  117. eval.metric = mx.metric.accuracy,
  118. epoch.end.callback = mx.callback.log.train.metric(100))
  119.  
  120. predict_probs <- predict(model, test_array)
  121. predicted_labels <- max.col(t(predict_probs)) - 1
  122. table(test_data[, 1], predicted_labels)
  123.  
  124. sum(diag(table(test_data[, 1], predicted_labels)))/2500
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement