Unable to create an image classification model in R due to an error in fit

I am new to R and have to get an image classification model ready. I'm using a dataset of plastic bottles and plastic bags. 10 images for training ( 5 of plastic bags and 5 of plastic bottles and 2 images ( 1 of a plastic bag and 1 of a plastic bottle ) for testing. The images are from Google and have been stored in a file on my desktop.

library(keras)
library(EBImage)

Read Images

setwd('C:/Users/kezia/OneDrive/Desktop/Pictures for diss')
pic1<- c('bottle 1.jpg','bottle 2.jpg','bottle 3.jpg','bottle 4.jpg','bottle 5.jpg','plastic-bag-1.jpg','plastic-bag-2.jpg','plastic-bag-3.jpg','plastic-bag-4.jpg','plastic-bag-5.jpg')
train<-list()
for(i in 1:10) {train[[i]]<-readImage(pic1[i])}

pic2<- c('bottle 6.jpg','plastic-bag-6.jpg')
test<-list()
for(i in 1:2) {test[[i]]<- readImage(pic2[i])}

Explore

print(train[[10]])
#> Image
#> colorMode : Color
#> storage.mode : double
#> dim : 1400 800 3
#> frames.total : 3
#> frames.render: 1
#>
#> imageData(object)[1:5,1:6,1]
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 1 1 1 1 1 1
#> [2,] 1 1 1 1 1 1
#> [3,] 1 1 1 1 1 1
#> [4,] 1 1 1 1 1 1
#> [5,] 1 1 1 1 1 1
summary(train[[10]])
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.0000 1.0000 1.0000 0.9501 1.0000 1.0000
display(train[[10]])
plot(train[[10]])

par(mfrow=c(2,5))
for(i in 1:10) plot(train[[i]])

par(mfrow =c(1,1))

Resize & combine

str(train)
#> List of 10
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:255, 1:198, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 255 198 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:995, 1:1400, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 995 1400 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:500, 1:500, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 500 500 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:452, 1:452, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 452 452 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:1000, 1:1250, 1:3] 0.784 0.788 0.788 0.788 0.788 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 1000 1250 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:800, 1:800, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 800 800 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:794, 1:900, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 794 900 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:600, 1:683, 1:3] 0.992 0.992 0.992 0.992 0.992 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 600 683 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:733, 1:900, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 733 900 3
#> :Formal class 'Image' [package "EBImage"] with 2 slots #> .. ..@ .Data : num [1:1400, 1:800, 1:3] 1 1 1 1 1 1 1 1 1 1 ... #> .. ..@ colormode: int 2 #> .. .. dim: int [1:3] 1400 800 3
for (i in 1:10) {train[[i]]<- resize(train[[i]],100,100)}
for (i in 1:2) {test[[i]] <- resize(test[[i]],100,100)}

library(EBImage)

train <- combine(train)
x <- tile(train, 5)
display(x, title='Pictures')

test <- combine(test)
y <- tile(test,2)
display(y,title='Pics')

Reorder dimension

train<- aperm(train,c(4,1,2,3))
test<- aperm(test, c(4,1,2,3))
str(train)
#> num [1:10, 1:100, 1:100, 1:3] 1 1 1 1 0.788 ...

Response

trainy<- c(0,0,0,0,0,1,1,1,1,1)
testy<- c(0,1)

One hot encoding

library(keras)
trainLabels <- to_categorical(trainy)
#> Error in py_get_attr_impl(x, name, silent): AttributeError: module 'tensorflow' has no attribute 'version'
testLabels <- to_categorical(testy)
#> List of 20
#> python : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python.exe" #> libpython : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python38.dll"
#> pythonhome : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate" #> pythonpath : chr "C:\Users\kezia\AppData\Local\R-MINI~1\envs\R-RETI~1\python38.zip;C:\Users\kezia\AppData\Local\R-MI"| truncated
#> prefix : chr "C:\\Users\\kezia\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1" #> exec_prefix : chr "C:\Users\kezia\AppData\Local\R-MINI~1\envs\R-RETI~1"
#> base_exec_prefix : chr "C:\\Users\\kezia\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1" #> virtualenv : chr ""
#> virtualenv_activate : chr "" #> version_string : chr "3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)]"
#> version : chr "3.8" #> architecture : chr "64bit"
#> anaconda : logi FALSE #> conda : logi TRUE
#> numpy :List of 2 #> .. path : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy"
#> .. version:Class 'numeric_version' hidden list of 1 #> .. .. : int [1:3] 1 23 0
#> required_module : NULL #> required_module_path: NULL
#> available : logi TRUE #> python_versions : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python.exe"
#> $ forced : NULL
#> - attr(*, "class")= chr "py_config"
#> Error: Python module tensorflow.keras was not found.
#>
#> Detected Python configuration:

Model

model<- keras_model_sequential()
#> List of 20
#> python : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python.exe" #> libpython : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python38.dll"
#> pythonhome : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate" #> pythonpath : chr "C:\Users\kezia\AppData\Local\R-MINI~1\envs\R-RETI~1\python38.zip;C:\Users\kezia\AppData\Local\R-MI"| truncated
#> prefix : chr "C:\\Users\\kezia\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1" #> exec_prefix : chr "C:\Users\kezia\AppData\Local\R-MINI~1\envs\R-RETI~1"
#> base_exec_prefix : chr "C:\\Users\\kezia\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1" #> virtualenv : chr ""
#> virtualenv_activate : chr "" #> version_string : chr "3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)]"
#> version : chr "3.8" #> architecture : chr "64bit"
#> anaconda : logi FALSE #> conda : logi TRUE
#> numpy :List of 2 #> .. path : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy"
#> .. version:Class 'numeric_version' hidden list of 1 #> .. .. : int [1:3] 1 23 0
#> required_module : NULL #> required_module_path: NULL
#> available : logi TRUE #> python_versions : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python.exe"
#> $ forced : NULL
#> - attr(*, "class")= chr "py_config"
#> Error: Python module tensorflow.keras was not found.
#>
#> Detected Python configuration:

model %>%
layer_conv_2d(filters = 32,
kernel_size = c(3,3),
activation='relu',
input_shape = c(100,100,3)) %>%
layer_conv_2d(filters=32,
kernel_size = c(3,3),
activation = 'relu') %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_dropout(rate= 0.25)%>%
layer_conv_2d(filters = 64,
kernel_size = c(3,3),
activation = 'relu')%>%
layer_conv_2d(filters = 64,
kernel_size = c(3,3),
activation = 'relu')%>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_dropout(rate= 0.25)%>%
layer_flatten()%>%
layer_dense(units = 256, activation = 'relu')%>%
layer_dropout(rate=0.25)%>%
layer_dense(units = 2,activation = 'softmax')%>%

compile(loss='loss_categorical_crossentropy',
optimizer=optimizer_sgd(lr=0.01,
decay=1e-6,
momentum = 0.9,
nesterov = T),
metrics = c('accuracy'))
#> List of 20
#> python : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python.exe" #> libpython : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python38.dll"
#> pythonhome : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate" #> pythonpath : chr "C:\Users\kezia\AppData\Local\R-MINI~1\envs\R-RETI~1\python38.zip;C:\Users\kezia\AppData\Local\R-MI"| truncated
#> prefix : chr "C:\\Users\\kezia\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1" #> exec_prefix : chr "C:\Users\kezia\AppData\Local\R-MINI~1\envs\R-RETI~1"
#> base_exec_prefix : chr "C:\\Users\\kezia\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1" #> virtualenv : chr ""
#> virtualenv_activate : chr "" #> version_string : chr "3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)]"
#> version : chr "3.8" #> architecture : chr "64bit"
#> anaconda : logi FALSE #> conda : logi TRUE
#> numpy :List of 2 #> .. path : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy"
#> .. version:Class 'numeric_version' hidden list of 1 #> .. .. : int [1:3] 1 23 0
#> required_module : NULL #> required_module_path: NULL
#> available : logi TRUE #> python_versions : chr "C:/Users/kezia/AppData/Local/r-miniconda/envs/r-reticulate/python.exe"
#> $ forced : NULL
#> - attr(*, "class")= chr "py_config"
#> Error: Python module tensorflow.keras was not found.
#>
#> Detected Python configuration:

summary(model)
#> Error in summary(model): object 'model' not found

Fit model

install.packages("tensorflow")
#> Installing package into 'C:/Users/kezia/AppData/Local/R/win-library/4.2'
#> (as 'lib' is unspecified)
#> package 'tensorflow' successfully unpacked and MD5 sums checked
#>
#> The downloaded binary packages are in
#> C:\Users\kezia\AppData\Local\Temp\RtmpIVL0MC\downloaded_packages
library(tensorflow)
library(keras)
library(EBImage)
reticulate::install_miniconda(force = TRUE)
#> * Installing Miniconda -- please wait a moment ...
#> * Downloading "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" ...
#> + "C:/Users/kezia/AppData/Local/r-miniconda/condabin/conda.bat" "create" "--yes" "--name" "r-reticulate" "python=3.8" "numpy" "--quiet" "-c" "conda-forge"
#> * Miniconda has been successfully installed at "C:/Users/kezia/AppData/Local/r-miniconda".
#> [1] "C:/Users/kezia/AppData/Local/r-miniconda"

history <- model %>%
fit(train,
trainLabels,
epochs=100,
batch_size = 40,
validation_split = 0.2,
)
#> Error in fit(., train, trainLabels, epochs = 100, batch_size = 40, validation_split = 0.2, : object 'model' not found
Created on 2022-06-28 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.