I try to write a program based on Deep Networks to read handwritten numbers. I found a code in Youtube (https://www.youtube.com/watch?v=5bso_5X7Zu4) which works there but it does not work for me. The problem is that I get error when I try to predict my handwritten number (namely number 5) that I have made in Windows Paint.
My number in Paint which its file name is number5.jpg is:
My complete code is:
library("pkgdown")
library(keras)
# devtools::install_github("rstudio/reticulate")
mnist <- dataset_mnist()
# str(mnist)
trainx <- mnist$train$x
trainy <- mnist$train$y
testx <- mnist$test$x
testy <- mnist$test$y
table(mnist$train$y, mnist$train$y)
table(mnist$test$y, mnist$test$y)
# plot images
windows()
par(mfrow = c(3,3))
for (i in 1:9) plot(as.raster(trainx[i,,], max=255))
trainx[2,,]
windows()
hist(trainx[1,,])
# Reshape & rescale
trainx <- array_reshape(trainx, c(nrow(trainx), 784))
testx <- array_reshape(testx, c(nrow(testx), 784))
trainx <- trainx / 255
testx <- testx / 255
#windows()
#hist(trainx[1,])
# One hot encoding
trainy <- to_categorical(trainy, 10)
testy <- to_categorical(testy, 10)
# trainx <- as.matrix(trainx)
# trainy <- as.matrix(trainy)
# Model
model <- keras_model_sequential()
model %>%
layer_dense(units = 128, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 64, activation = 'relu') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 10, activation = 'softmax')
summary(model)
# Compile
model %>%
compile(loss ='categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = 'accuracy')
# Fit model
history <- model %>%
fit(trainx,
trainy,
epochs = 30,
batch_size = 32,
validation_split = 0.2)
plot(history)
# Evaluation and Precition - Test data
model %>% evaluate(testx, testy)
pred <- model %>% predict(testx) %>% k_argmax() %>% as.integer() %>% .[1:7840000]
prob <- model %>% predict(testx)
cbind(Predicted_class = pred , Actual = mnist$test$y)[1:150,]
# New data
#install.packages("BiocManager")
#BiocManager::install("EBImage")
library(EBImage)
setwd("C:/Users/hofo/Arbeidsmapper/Documents/NLP/NLP_BOSTOTTE/JPG_filer")
mypic <- readImage("number5.jpg")
mypic <- resize(mypic, 28, 28)
mypic <- array_reshape(mypic, c(28, 28, 3))
new <- NULL
new <- rbind(new, mypic)
str(new)
newx <- new[1:1,1:784]
newy <- c(5)
pred <- model %>% predict(newx) %>% k_argmax() %>% as.integer() %>% .[1:9]
The error when I run the last row is:
Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code:
C:\Users\hofo\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\keras\engine\training.py:1586 predict_function *
return step_function(self, iterator)
C:\Users\hofo\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\keras\engine\training.py:1576 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\hofo\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\hofo\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\hofo\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
Can you also please help me with this?
