rstudio / tfdatasets

R interface to TensorFlow Datasets API
https://tensorflow.rstudio.com/tools/tfdatasets/
34 stars 12 forks source link

Error on performing single predictions on Keras with RStudio #20

Closed felipefmoreira closed 5 years ago

felipefmoreira commented 5 years ago

Hello, I'm really new to this thing so the chances I'm asking something extremely dumb is huge. I'm working on converting a Python script I got on a online Deep Learning course to R and I'm having issues.

The script is to fit a classification model with images of dogs and cats (yeah, I'm that kind of newbie). The problem arises with the last part, where I try to read single photos on my PC so the model can predict whether it is a dog or a cat (I tried marking it bold, but I couldn't make it, lol).

# Definindo a CNN

modelo <- keras_model_sequential()
epochs <- 25
bs <- 32

modelo %>% 
  # Primeira camada de convolução + pooling

  layer_conv_2d(filters = 32, kernel_size = c(3,3), input_shape = c(64,64,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%

  # Segunda camada de convolução + pooling

  layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%

  # Terceira camada de convolução + pooling

  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%

  # Quarta camada de convolução + pooling

  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%

  # Camada de "achatamento"

  layer_flatten() %>%

  # Conexão dos nós numa rede em quatro camadas intermediárias

  layer_dense(64, activation = 'relu') %>%
  layer_dropout(0.60) %>%
  layer_dense(64, activation = 'relu') %>%
  layer_dense(64, activation = 'relu') %>%
  layer_dropout(0.60/2) %>%

  # Camada de output

  layer_dense(1, activation = 'sigmoid')

# Compilação do Modelo e Visualização do Modelo

modelo %>%
  compile(loss='binary_crossentropy', optimizer = optimizer_adam(), metrics = 'accuracy')
# Pré-Processamento de Dados

train_gen <- image_data_generator(rescale = 1/255, shear_range = 0.2, 
                                  zoom_range = 0.2, horizontal_flip = TRUE)

test_gen <- image_data_generator(rescale = 1/255)

train_set <- flow_images_from_directory('dataset/training_set',
                                        train_gen,
                                        target_size = c(64,64),
                                        batch_size = bs,
                                        class_mode = 'binary')

test_set <- flow_images_from_directory('dataset/test_set',
                                       test_gen, 
                                       target_size = c(64,64), 
                                       batch_size = bs, 
                                       class_mode = 'binary')

# Ajuste do Modelo

evol <- modelo %>% fit_generator(train_set, steps_per_epoch = 8000/32, epochs = epochs,
                                 validation_data = test_set, validation_steps = 2000/32, verbose = 0)
plot(evol)

# Resultados obtidos
# Train acc = 0.8397
# Test acc = 0.8293

# Single Predictions

pred_image <- image_load('dataset/single_prediction/cat_or_dog_1.jpg', target_size = c(64,64))
pred_image <- image_to_array(pred_image)
pred_image <- k_expand_dims(pred_image, axis = 0)
previsao <- modelo %>% predict_classes(pred_image)

The last 4 lines of code are the main trouble source. First, I tried predict_classes, but it retrieved the following error message:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: If your data is in the form of symbolic tensors, you should specify the `steps` argument (instead of the `batch_size` argument, because symbolic tensors are expected to produce batches of input data). 

I then tried to add the steps argument, but it didn't work either.

skeydan commented 5 years ago

Hi,

this would be better located in the keras repo (https://github.com/rstudio/keras/), as it's not using tfdatasets (just FYI, no problem).

The code will run with the following changes:

# R uses 1-based indexing
pred_image <- k_expand_dims(pred_image, axis = 1)

# use steps = 1 b/c 1 image
model %>% predict(pred_image, steps = 1)
felipefmoreira commented 5 years ago

That worked amazingly. Thank you so much.