我想应用一个滑动窗口来查找具有多个数字(wh_img
)的图像中的手写数字。首先,使用经典的MNIST示例创建cnn:
#Preparing the Data - using MNIST dataset is included with Kera
library(keras)
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y
# reshape
x_train <- array_reshape(x_train,c(nrow(x_train),784))
x_test <- array_reshape(x_test,c(nrow(x_test),784))
# rescale
x_train <- x_train / 255
x_test <- x_test / 255
y_train <- to_categorical(y_train,10)
y_test <- to_categorical(y_test,10)
#cnn model
model <- keras_model_sequential()
model %>%
layer_dense(units = 256,activation = 'relu',input_shape = c(784)) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 128,activation = 'relu') %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 10,activation = 'softmax')
model %>% compile(
loss = 'categorical_crossentropy',optimizer = optimizer_rmsprop(),metrics = c('accuracy')
)
#Training and Evaluation
history <- model %>% fit(
x_train,y_train,epochs = 30,batch_size = 128,validation_split = 0.2
)
plot(history)
现在,我必须在y.png
图像中应用带有几个手写数字的滑动窗口,以使用经过训练的model
和predict_classes
函数仅查找4个数字:
# Open an image with handwrite numbers colections
y = "http://mariakravtsova.us/img/numbers.png"
download.file(y,'y.png',mode = 'wb')
library("png")
wh_img <- readPNG("y.png")
plot.new()
rasterImage(wh_img,1,1)
# Applied cnn model to wh_img using sliding window
predictions <- predict_classes(model,??????)
这在R中可能吗?因为在Python中有很多功能,请选择窗口大小等。有什么想法吗?