如何通过对 MNIST 数据 (R) 中每个 4 × 4 子方块的灰色调进行平均,将每个 28 × 28 图像投影为 7 × 7 图像?
How to project each 28×28 image to a 7 × 7 image by averaging the grey tone in each 4 × 4 subsquare in MNIST data (R)?
MNIST 数据包含 785 列,第一列是标签,其余是像素。
我想通过平均图像中每个 4x4 子空间的灰色调来将每个图像的尺寸减小到 7x7。
我怎样才能做到这一点?
示例数据:
label v1 v2 v3....v784
1 0 0 13 100
3 9 0 200 0
library(tensorflow)
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)
img <- matrix(mnist$train$images[1, ], 28, 28)
avg <- function(x) {
output <- c()
for (i in 0:(length(x)/4-1)){
output <- c(output, mean(x[(1 + 4 * i):(4 * i + 4)]))
}
return(output)
}
img <- apply(img, 2, avg)
img <- apply(img, 1, avg)
print(img)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,] 0.000000 0.00000000 0.000000000 0.00000000 0.0000000 0.00000000 0
#[2,] 0.000000 0.00000000 0.023774512 0.08627452 0.0000000 0.00000000 0
#[3,] 0.264951 0.65612750 0.596078470 0.56985298 0.6960785 0.03333334 0
#[4,] 0.000000 0.00000000 0.006127452 0.19632354 0.7279412 0.30784316 0
#[5,] 0.000000 0.19411766 0.595833369 0.49632356 0.6409314 0.31446080 0
#[6,] 0.000000 0.03480392 0.000000000 0.00000000 0.7100491 0.05465687 0
#[7,] 0.000000 0.00000000 0.000000000 0.10318628 0.2573530 0.00000000 0
##img_dataset is MNIST rowmajor like image dataset
##nrow and ncol of images should be dividable by "windowsize"
apply_mean_filter <- function(img_dataset,image_x_y_size=28,windowsize=4,mc.cores=1){
require(parallel)
mclapply(1:nrow(img_dataset),function(img_idx){temp_data_mat <- matrix(img_dataset[img_idx,],ncol=image_x_y_size,nrow=image_x_y_size);return(sapply(0:((image_x_y_size/windowsize)-1), function(x) sapply(0:((image_x_y_size/windowsize)-1), function(y) mean(temp_data_mat[(x*windowsize+1):((x+1)*windowsize),(y*windowsize+1):((y+1)*windowsize)]))))},mc.cores=mc.cores)
}
MNIST <- as.matrix(train$x)#MNIST without labels
mean_MNIST_list <-
apply_mean_filter(MNIST,image_x_y_size=28,windowsize=4,mc.cores=2) ##return list of filtered images
MNIST 数据包含 785 列,第一列是标签,其余是像素。
我想通过平均图像中每个 4x4 子空间的灰色调来将每个图像的尺寸减小到 7x7。
我怎样才能做到这一点?
示例数据:
label v1 v2 v3....v784
1 0 0 13 100
3 9 0 200 0
library(tensorflow)
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)
img <- matrix(mnist$train$images[1, ], 28, 28)
avg <- function(x) {
output <- c()
for (i in 0:(length(x)/4-1)){
output <- c(output, mean(x[(1 + 4 * i):(4 * i + 4)]))
}
return(output)
}
img <- apply(img, 2, avg)
img <- apply(img, 1, avg)
print(img)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,] 0.000000 0.00000000 0.000000000 0.00000000 0.0000000 0.00000000 0
#[2,] 0.000000 0.00000000 0.023774512 0.08627452 0.0000000 0.00000000 0
#[3,] 0.264951 0.65612750 0.596078470 0.56985298 0.6960785 0.03333334 0
#[4,] 0.000000 0.00000000 0.006127452 0.19632354 0.7279412 0.30784316 0
#[5,] 0.000000 0.19411766 0.595833369 0.49632356 0.6409314 0.31446080 0
#[6,] 0.000000 0.03480392 0.000000000 0.00000000 0.7100491 0.05465687 0
#[7,] 0.000000 0.00000000 0.000000000 0.10318628 0.2573530 0.00000000 0
##img_dataset is MNIST rowmajor like image dataset
##nrow and ncol of images should be dividable by "windowsize"
apply_mean_filter <- function(img_dataset,image_x_y_size=28,windowsize=4,mc.cores=1){
require(parallel)
mclapply(1:nrow(img_dataset),function(img_idx){temp_data_mat <- matrix(img_dataset[img_idx,],ncol=image_x_y_size,nrow=image_x_y_size);return(sapply(0:((image_x_y_size/windowsize)-1), function(x) sapply(0:((image_x_y_size/windowsize)-1), function(y) mean(temp_data_mat[(x*windowsize+1):((x+1)*windowsize),(y*windowsize+1):((y+1)*windowsize)]))))},mc.cores=mc.cores)
}
MNIST <- as.matrix(train$x)#MNIST without labels
mean_MNIST_list <-
apply_mean_filter(MNIST,image_x_y_size=28,windowsize=4,mc.cores=2) ##return list of filtered images