I have datasets of images in several folders ConvNet/train_set/cats, ConvNet/train_set/dogs, ConvNet/test_set/cats and ConvNet/test_set/dogs. I need to read these in and run them through a CNN code using Keras and Tensorflow. I have the CNN built, but I'm having issues reading in the image directories. Can someone help?
Hello,
Can you provide a reprex to help us know what you already tried ?
You could be interested in this example
https://tensorflow.rstudio.com/tutorials/beginners/load/load_image/
library(tidyverse)
library(tensorflow)
#> Warning: package 'tensorflow' was built under R version 3.6.3
library(keras)
#> Warning: package 'keras' was built under R version 3.6.3
library(tfdatasets)
#> Warning: package 'tfdatasets' was built under R version 3.6.3
library(readr)
data_dir <- unzip("C:/Users/tniebank/OneDrive - Cox Automotive/Documents/School/MachineLearning/ConvNet_dataset.zip")
images <- list.files(data_dir, pattern = ".jpg", recursive = TRUE)
length(images)
#> [1] 0
classes <- list.dirs(data_dir, full.names = FALSE, recursive = FALSE)
classes
#> character(0)
par(mfcol = c(5,6), mar = rep(1, 4), oma = rep(0.2, 4))
conv$train_set$x[index,,,] %>%
purrr::array_tree(1) %>%
purrr::set_names(class_names[conv$train_set$y[index] + 1]) %>%
purrr::map(as.raster, max = 255) %>%
purrr::iwalk(~{plot(.x); title(.y)})
#> Error in eval(lhs, parent, parent): object 'conv' not found
model <- keras_model_sequential() %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = "relu",
input_shape = c(64,64,3)) %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = "relu") %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = "relu")
summary(model)
#> Model: "sequential"
#> ________________________________________________________________________________
#> Layer (type) Output Shape Param #
#> ================================================================================
#> conv2d (Conv2D) (None, 62, 62, 32) 896
#> ________________________________________________________________________________
#> max_pooling2d (MaxPooling2D) (None, 31, 31, 32) 0
#> ________________________________________________________________________________
#> conv2d_1 (Conv2D) (None, 29, 29, 32) 9248
#> ________________________________________________________________________________
#> max_pooling2d_1 (MaxPooling2D) (None, 14, 14, 32) 0
#> ________________________________________________________________________________
#> conv2d_2 (Conv2D) (None, 12, 12, 32) 9248
#> ================================================================================
#> Total params: 19,392
#> Trainable params: 19,392
#> Non-trainable params: 0
#> ________________________________________________________________________________
model %>%
layer_flatten() %>%
layer_dense(units = 128, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
summary(model)
#> Model: "sequential"
#> ________________________________________________________________________________
#> Layer (type) Output Shape Param #
#> ================================================================================
#> conv2d (Conv2D) (None, 62, 62, 32) 896
#> ________________________________________________________________________________
#> max_pooling2d (MaxPooling2D) (None, 31, 31, 32) 0
#> ________________________________________________________________________________
#> conv2d_1 (Conv2D) (None, 29, 29, 32) 9248
#> ________________________________________________________________________________
#> max_pooling2d_1 (MaxPooling2D) (None, 14, 14, 32) 0
#> ________________________________________________________________________________
#> conv2d_2 (Conv2D) (None, 12, 12, 32) 9248
#> ________________________________________________________________________________
#> flatten (Flatten) (None, 4608) 0
#> ________________________________________________________________________________
#> dense (Dense) (None, 128) 589952
#> ________________________________________________________________________________
#> dense_1 (Dense) (None, 1) 129
#> ================================================================================
#> Total params: 609,473
#> Trainable params: 609,473
#> Non-trainable params: 0
#> ________________________________________________________________________________
model %>% compile(
optimizer = "adam",
loss = "sparse_categorical_crossentropy",
metrics = "accuracy"
)
history <- model %>%
fit(
x = conv$train_set$x, y = conv$train_set$y,
epochs = 25, batch_size = 8000,
validation_data = unname(conv$test_set),
verbose = 2
)
#> Error in unname(conv$test_set): object 'conv' not found
plot(history)
#> Error in .External2(C_savehistory, file): 'savehistory' can only be used in Rgui and Rterm
evaluate(model, conv$test_set$x, conv$test_set$y, verbose = 0)
#> Error in is_tensorflow_dataset(x): object 'conv' not found
Created on 2020-03-30 by the reprex package (v0.3.0)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.