2

I would like to classify images using Keras and Tensorflow in RStudio.

In a normal image classification model - how can I tell Tensorflow to take or NOT to take into account the position within the picture?

For example, if I want the model to say, whether or not there is a dog in the picture versus whether or not there is a dog in the bottom left corner of the picture?

I know that usually to NOT take it into account, I can for example offset my pictures and permutate them so the model learns that objects can be at all positions in the picture.

But how can I get it to do exactly that? To learn that a feature is only valid, if it is at the same position in the picture as it has found it in the training?

Example code (general image classification):

model <- keras_model_sequential() 
model %>%
  layer_dropout(rate = FLAGS$dropout1) %>%
  layer_conv_2d(filters = FLAGS$convol_filters1, kernel_size = c(3,3)
                , activation = "relu", padding = "same"
  ) %>%
  layer_conv_2d(filters = FLAGS$convol_filters1, kernel_size = c(3,3) 
                , activation = "relu"
                #, padding = "same"
  ) %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_dropout(rate = FLAGS$dropout1) %>%
  layer_conv_2d(filters = FLAGS$convol_filters2, kernel_size = c(3,3)
                , activation = "relu", padding = "same"
  ) %>%
  layer_conv_2d(filters = FLAGS$convol_filters2, kernel_size = c(3,3)
                , activation = "relu"
                #, padding = "same"
  ) %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_dropout(rate = FLAGS$dropout1) %>%
layer_conv_2d(filters = FLAGS$convol_filters3, kernel_size = c(3,3)
              , activation = "relu", padding = "same"
) %>%
layer_conv_2d(filters = FLAGS$convol_filters3, kernel_size = c(3,3)
              , activation = "relu"
              #, padding = "same"
) %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_flatten() %>%
layer_dense(units = FLAGS$dense_units1) %>%   
layer_activation_relu() %>%
layer_dropout(rate = FLAGS$dropout1) %>%
layer_dense(units = FLAGS$dense_units1) %>%   
layer_activation_relu() %>%
layer_dropout(rate = FLAGS$dropout1) %>%
layer_dense(units = FLAGS$dense_units1) %>%   
layer_activation_relu() %>%
layer_dense(units = 2, activation = 'softmax')

model %>% compile(loss = 'binary_crossentropy', optimizer = "adadelta", metrics = c("accuracy"))

model %>% fit(data.training, data.trainLabels, epochs = FLAGS$epochs, view_metrics = FALSE,
                         validation_split = 0.2, shuffle = TRUE)
Nicholas
  • 93
  • 1
  • 9
  • 1
    Did you refer the concepts of Object Detection, Bounding Boxes and Anchors mentioned in the link, https://blogs.rstudio.com/tensorflow/posts/2018-12-18-object-detection-concepts/ –  Aug 07 '19 at 10:39

0 Answers0