我正在尝试建立一个神经网络,以便对人的图像进行分类,并预测他们的年龄。
代码如下:
library(EBImage)
library(keras)
# Read images
setwd('C:\\Users\\smbsv\\Desktop\\UTKFace')
# Get file list with full path and file names
files <- list.files('C:\\Users\\smbsv\\Desktop\\UTKFace', full.names = TRUE, pattern = "jpg$|JPG$")
files
# Select the desired % or number of file by simple random sampling
randomize <- sample(seq(files))
files2analyse <- files[randomize]
files2analyse <- files2analyse[(1:5000)]
files2analyse
mypic <- list()
for (i in 1:length(files2analyse)) {mypic[[i]] <- readImage(files2analyse[i])}
mypic
display(mypic[[1]])
display(mypic[[100]])
display(mypic[[2400]])
display(mypic[[4720]])
# Resize
for (i in 1:length(mypic)) {mypic[[i]] <- resize(mypic[[i]], 128, 128)}
display(mypic[[2400]])
display(mypic[[4720]])
# Reshape
for (i in 1:length(mypic)) {mypic[[i]] <- array_reshape(mypic[[i]], c(128, 128, 3))}
#make this example reproducible
set.seed(1493)
#use 70% of dataset as training set and 30% as test set
sample <- sample(c(TRUE, FALSE), length(mypic), replace=TRUE, prob=c(0.7,0.3))
train <- mypic[sample]
test <- mypic[!sample]
filenamesTRAIN <- files2analyse[sample]
filenamesTEST <- files2analyse[!sample]
filenamesTRAIN[10]
display(train[[10]])
filenamesTRAIN[362]
display(train[[362]])
filenamesTEST[25]
display(test[[25]])
filenamesTEST[189]
display(test[[189]])
length(train)
length(test)
trainx <- NULL
trainx <- rbind(trainx, train)
trainx
str(trainx)
testx <- NULL
testx <- rbind(testx, test)
str(testx)
#change files names to only images names (TRAIN)
for (i in 1:length(filenamesTRAIN)) {filenamesTRAIN[i] <- substring(filenamesTRAIN[i], 32)}
#change files names to only images names (TRAIN)
for (i in 1:length(filenamesTEST)) {filenamesTEST[i] <- substring(filenamesTEST[i], 32)}
filenamesTRAIN[27]
filenamesTRAIN[147]
filenamesTEST[189]
filenamesTEST[4]
#extract classes from filename (pattern: [age]_[gender(0=male,1=female)]_[race(from 0 to 4, denoting White, Black, Asian, Indian, and Others)]_[date&time].jpg)
library(stringr)
trainy <- NULL
for (i in 1:length(filenamesTRAIN)) {trainy[i] <- str_split(filenamesTRAIN[i], "_")[[1]][1]}
filenamesTRAIN[20]
trainy[20]
filenamesTRAIN[100]
trainy[100]
testy <- NULL
for (i in 1:length(filenamesTEST)) {testy[i] <- (str_split(filenamesTEST[i], "_")[[1]][1])}
filenamesTEST[16]
testy[16]
filenamesTEST[185]
testy[185]
# One Hot Encoding
class(trainy[4])
for (i in 1:length(trainy)) {trainy[i] <- as.numeric(trainy[i]) - 1} # -1 avoid 1 more column when use "to_categorical"
for (i in 1:length(testy)) {testy[i] <- as.numeric(testy[i]) - 1}
trainLabels <- to_categorical(trainy)
testLabels <- to_categorical(testy)
filenamesTEST[7]
testLabels # rows: people ; columns : labels (age)
# Model
model <- keras_model_sequential()
model %>%
layer_dense(units = 256, activation = 'relu', input_shape = c(3498)) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = 116, activation = 'softmax')
summary(model)
# Compile
model %>%
compile(loss = 'crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy'))
# Fit Model
history <- model %>%
fit(trainx,
trainLabels,
epochs = 30,
batch_size = 32,
validation_split = 0.2)
一切正常,直到我尝试拟合模型,它给了我这个错误:
Error: Matrix type cannot be converted to python (only integer, numeric, complex, logical, and character matrixes can be converted
我认为问题是“trainx”和“trainLabels”的数据类型。 如果有用的话,这是这些变量的返回类型:
火车x:
>str(trainx)
List of 3498
$ : num [1:128, 1:128, 1:3] 0.113 0.123 0.121 0.117 0.118 ...
$ : num [1:128, 1:128, 1:3] 0.108 0.127 0.163 0.222 0.29 ...
...
- attr(*, "dim")= int [1:2] 1 3498
- attr(*, "dimnames")=List of 2
..$ : chr "train"
..$ : NULL
火车标签:
> str(trainLabels)
num [1:3498, 1:116] 0 0 0 0 0 0 0 0 0 0 ...
我该如何解决这个错误?