1
$\begingroup$

I am trying to implement a deep learning model in R using Keras. Let's say I had a dataset of people's faces and a CSV with information about the person's age, gender, and ethnicity. I want to train the model to predict a person's age from their photo.

For this, I have to use convolutional neural networks. Here is my pseudocode:

library(keras)

model <- keras_model_sequential() 

model %>% ## define CNN model’s architecture. 
## I will figure this out later

# configure model
model %>% compile(
    loss='mean_squared_error',
    optimizer='adam',
    metrics='mae'
)

history <- model %>% fit(
# train_array is a 300x100x100x1 array
# train_array stores the pixel values of 300 grayscale images 
# of resolution 100x100
    x = train_array, y = ???,
    epochs = 10, batch_size = 30,
 )

My question is regarding the ??? marked above. I have three pieces of useful information to train the model: age, gender, and ethnicity. How do I add this information to the model? Again, I want the model to predict a single prediction of age.

I looked at the Keras fit function documentation but couldn't figure it out. It says that y is a "Vector, matrix, or array of target (label) data (or list if the model has multiple outputs)..."


EDIT: That makes sense. Now the question is how do I set up the x list? Here's what I have so far:

trainFeatures <- list(pixels = train_array, gender = as.factor(trainGenders), ethnicity = as.factor(trainEthnicity))

history <- model %>% fit(
    x = trainFeatures, y = trainAge,
    epochs = 10, batch_size = 30,
)

I downloaded a toy dataset and set up a quick CNN model. I got the following error message:

Error in py_get_attr_impl(x, name, silent) : AttributeError: 'list' object has no attribute 'dtype'

$\endgroup$
  • $\begingroup$ I had the same error. I fixed it by transforming y to numeric type, it was factor and this raised the error. $\endgroup$ – kamel gaanoun Mar 15 at 22:07
1
$\begingroup$

x is an array/matrix of the inputs/features/independent variables.

y is an array/matrix of the output(s)/target(s)/dependent variable(s).

Your x array should contain the features extracted from the dataset of people's faces (however you may choose to do that). You can even include the gender and ethnicity alongside the features extracted from the dataset, if this information will be available to you while you deploy the model.

Your y array should contain your target variable, i.e., the age associated with each face.


Response to EDIT:

Unfortunately, I actually haven't used Keras with R. But judging by the error you are seeing (and the prefix of the function), I would say it is most likely compatibility issues between R lists and whatever Keras expects underneath. Keras is implemented in Python, so it is probably expecting a Numpy array or just a Python list. I suspect you will have better luck creating an R data.frame for your input features.

This tutorial I found might be helpful as it uses Keras and R to build a CNN.

$\endgroup$
  • $\begingroup$ I updated my question. How do I set up the x array/matrix of the inputs/features/independent variables? $\endgroup$ – cooldood3490 Oct 10 '18 at 23:28
  • $\begingroup$ @cooldood3490 updated the response $\endgroup$ – Anakimi Oct 10 '18 at 23:54
1
$\begingroup$

You need to convert your factor inputs to a binary matrix, checkout the dummy package.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.