Notes I have provided a reproducible example below:
#if prompted with updates after run line 5/6 type n in console and ignore
#the model utilises tensorflow in the backend
# Install EBImage
> source("https://bioconductor.org/biocLite.R")
> biocLite("EBImage",suppressUpdates=TRUE)
> # Load Packages
> library(EBImage)
> library(keras)
>
> # Read images these images are just bunch of planes and cars images from google
> setwd("C:/Users/Joel/Desktop/SJU/Semester 3/R programming")
> pics <- c('p1.jpg', 'p2.jpg', 'p3.jpg', 'p4.jpg', 'p5.jpg', 'p6.jpg',
> 'c1.jpg', 'c2.jpg', 'c3.jpg', 'c4.jpg', 'c5.jpg', 'c6.jpg')
> mypic <- list()
> for (i in 1:12) {mypic[[i]] <- readImage(pics[i])}
>
> # Explore
> print(mypic[[1]])
> display(mypic[[8]])
> summary(mypic[[1]])
> hist(mypic[[2]])
> str(mypic)
>
> # Resize
> for (i in 1:12) {mypic[[i]] <- resize(mypic[[i]],28,28)}
> str(mypic)
> #we want a single venctor of dim 2352(28*28*3) hence we reshape
> # Reshape
> for (i in 1:12) {mypic[[i]] <- array_reshape(mypic[[i]], c(28, 28,3))}
> str(mypic)
> # Row Bind
> trainx <- NULL
> for (i in 7:11) {trainx <- rbind(trainx, mypic[[i]])}
> str(trainx)
> testx <- rbind(mypic[[6]], mypic[[12]])
> #lets represent train by 0 and car by 1
> trainy <- c(0,0,0,0,0,1,1,1,1,1 )
> testy <- c(0,1)
>
> # One Hot Encoding
> trainLabels <- to_categorical(trainy)
> testLabels <- to_categorical(testy)
> trainLabels #you can notice it creates 2 dummy variables, in col 1, "1" represents plane and in column 2 "1" represents car
> # Model
> model <- keras_model_sequential()
> model %>%
> layer_dense(units = 256, activation = "relu", input_shape = c(2352)) %>%
> layer_dense(units = 128, activation = 'relu') %>%
> layer_dense(units = 2, activation = "softmax")
> summary(model)
>
> # Compile
> model %>%
> compile(loss = "binary_crossentropy",
> optimizer = optimizer_rmsprop(),
> metrics = c('accuracy'))
>
> # Fit Model
> history <- model %>%
> fit(trainx,
> trainLabels,
> epochs = 30,
> batch_size = 32,
> validation_split = 0.2)
The error I receive after running the above line is
Error in py_call_impl(callable, dots$args, dots$keywords) :
**ValueError: Input arrays should have the same number of samples as target arrays. Found 5 input samples and 10 target samples.**
Detailed traceback:
** File "D:\Users\Joel\Anaconda3\lib\site-packages\keras\engine\training.py", line 952, in fit**
** batch_size=batch_size)**
** File "D:\Users\Joel\Anaconda3\lib\site-packages\keras\engine\training.py", line 804, in _standardize_user_data**
** check_array_length_consistency(x, y, sample_weights)**
** File "D:\Users\Joel\Anaconda3\lib\site-packages\keras\engine\training_utils.py", line 237, in check_array_length_consistency**
** 'and ' + str(list(set_y)[0]) + ' target samples.')**
**>**