# -*- coding: utf-8 -*- # from sklearn.datasets import fetch_olivetti_faces import numpy as np # Olivetti olivetti = fetch_olivetti_faces() x = olivetti.images y = olivetti.target # print("Original x shape:", x.shape) X = x.reshape((400, 4096)) print("New x shape:", X.shape) print("y shape", y.shape) # numpy np.savetxt("C://olivetti_X.csv", X, delimiter = ",") np.savetxt("C://olivetti_y.csv", y, delimiter = ",", fmt = '%d') print("\nDownloading and reshaping done!") ################################################################################ # ################################################################################ # # Original x shape: (400, 64, 64) # New x shape: (400, 4096) # y shape (400,) # # Downloading and reshaping done!
# 64 64 28 28 # rm(list=ls()) # EBImage require(EBImage) # X <- read.csv("olivetti_X.csv", header = F) labels <- read.csv("olivetti_y.csv", header = F) # rs_df <- data.frame() # : for(i in 1:nrow(X)) { # Try-catch result <- tryCatch({ # ( ) img <- as.numeric(X[i,]) # 64x64 ( EBImage) img <- Image(img, dim=c(64, 64), colormode = "Grayscale") # 28x28 img_resized <- resize(img, w = 28, h = 28) # ( , !) img_matrix <- img_resized@.Data # img_vector <- as.vector(t(img_matrix)) # label <- labels[i,] vec <- c(label, img_vector) # rs_df rbind rs_df <- rbind(rs_df, vec) # print(paste("Done",i,sep = " "))}, # ( ). ! error = function(e){print(e)}) } # . - , - . names(rs_df) <- c("label", paste("pixel", c(1:784))) # #------------------------------------------------------------------------------- # . . # set.seed(100) # df shuffled <- rs_df[sample(1:400),] # train_28 <- shuffled[1:360, ] test_28 <- shuffled[361:400, ] # write.csv(train_28, "C://train_28.csv", row.names = FALSE) write.csv(test_28, "C://test_28.csv", row.names = FALSE) # ! print("Done!")
# rm(list=ls()) # MXNet require(mxnet) # #------------------------------------------------------------------------------- # train <- read.csv("train_28.csv") test <- read.csv("test_28.csv") # train <- data.matrix(train) train_x <- t(train[, -1]) train_y <- train[, 1] train_array <- train_x dim(train_array) <- c(28, 28, 1, ncol(train_x)) test_x <- t(test[, -1]) test_y <- test[, 1] test_array <- test_x dim(test_array) <- c(28, 28, 1, ncol(test_x)) # #------------------------------------------------------------------------------- data <- mx.symbol.Variable('data') # conv_1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 20) tanh_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh") pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2)) # conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5, 5), num_filter = 50) tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh") pool_2 <- mx.symbol.Pooling(data=tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2)) # flatten <- mx.symbol.Flatten(data = pool_2) fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500) tanh_3 <- mx.symbol.Activation(data = fc_1, act_type = "tanh") # fc_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 40) # . , .. - . NN_model <- mx.symbol.SoftmaxOutput(data = fc_2) # #------------------------------------------------------------------------------- # mx.set.seed(100) # . CPU. devices <- mx.cpu() # #------------------------------------------------------------------------------- # model <- mx.model.FeedForward.create(NN_model, X = train_array, y = train_y, ctx = devices, num.round = 480, array.batch.size = 40, learning.rate = 0.01, momentum = 0.9, eval.metric = mx.metric.accuracy, epoch.end.callback = mx.callback.log.train.metric(100)) # #------------------------------------------------------------------------------- # predicted <- predict(model, test_array) # predicted_labels <- max.col(t(predicted)) - 1 # sum(diag(table(test[, 1], predicted_labels)))/40 ################################################################################ # ################################################################################ # # 0.975 #
data.matrix
function to turn each dataset into a numeric matrix. Remember, the first column of data is the tags associated with each picture. Make sure you remove the tags from train_array
and test_array
. After separating the tags and dependent variables, you need to tell MXNet to process the data. I do this on line 19 with the following piece of code: “dim (train_array) <- c (28, 28, 1, ncol (train_x))” for the training set and on line 24 for the test set. Thus, we actually tell the model that the training data consists of ncol (train_x) samples (360 pictures) of size 28x28. The number 1 indicates that the pictures are in grayscale, i.e., that they have only 1 channel. If the pictures were in RGB, 1 would have to be replaced by 3, just as many channels would have pictures.Source: https://habr.com/ru/post/307242/
All Articles