bnosac / image

Computer Vision and Image Recognition algorithms for R users
272 stars 65 forks source link

Object recognition: apply *h5 cnn model to find an specific handwritten digit #17

Open Leprechault opened 4 years ago

Leprechault commented 4 years ago

I don't find any material about object detection using *h5 cnn adjusted model. I try to create a simple example because I don't find in any book/web, I've like to apply any object detection technique to find handwritten digits in an image with several digits (wh_img). First, a create a CNN using the classic MNIST example:

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)

Save the model

model %>% save_model_hdf5("cnn_digits.h5")

Now, I've to apply any object detection technique in my y.png image with several handwritten digits to find only 4 numerals using my trained model :

Open an image with handwrite numbers collections

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,0,0,1,1)

library(image.darknet)

Try to use *h5 model to object detection only 4 handwritten digits

yolo_digits <- image_darknet_model(type = ‘detect’, labels ="4", model = "cnn_digits.h5")

x <- image_darknet_detect(file = "/Documents/numbers.png", object = yolo_digits, threshold = 0.4) #

This is possible in R? Because in Python there are a lot of functions, choose the window size, etc. but in R not :(

jwijffels commented 4 years ago

You have build a model with keras and saved it as cnn_digits.h5. You can not pass such a model to work with darknet as that requires a .cfg file. Keras and darknet are 2 different frameworks.