我一直在运行一些带有彩色图像的CNN程序(3层),并且可以正常工作。我想尝试使用灰度图像运行,但它不起作用。 这是我的代码:
library(keras)
library(tensorflow)
library(EBImage)
search()
setwd("D://databaru/")
save_in<-("D://datahasilbaru/")
gambar<-list.files()
w<-100
h<-100
for(i in 1:length(gambar))
{result<-tryCatch({
imgname<-gambar[i]
img<-readImage(imgname)
img_resized<-resize(img,w=w,h=h)
path<-paste(save_in,imgname,sep=" ")
writeImage(img_resized,path,quality=70)
print(paste("done",i,sep=" "))
},
error=function(e){print(e)}
)}
setwd("D://datahasilbaru/")
gambar2<-list.files()
gambar2
gambar2<-lapply(gambar2,readImage)
str(gambar2)
display(gambar2[[1]])
dim(gambar2[[1]])
train<-gambar2[c(1:143,205:314)]
test<-gambar2[c(144:204,315:361)]
train[[6]]
display(train[[6]])
write.csv(train[[6]],"D://dataskripsi/matrix.csv")
train2<-combine(train)
x<-tile(train2,253)
display(x)
dim(train2)
test2<-combine(test)
y<-tile(test2,108)
display(y)
dim(test2)
#susun ulang
trainfix<-aperm(train2,c(3,1,2))
testfix<-aperm(test2,c(3,1,2))
dim(trainfix)
dim(testfix)
#label
trainnn<-c(rep(0,143),rep(1,110))
testtt<-c(rep(0,61),rep(1,47))
trainnn
testtt
trainLabels<-to_categorical(trainnn)
testLabels<-to_categorical(testtt)
testLabels
#model CNN
model<-keras_model_sequential()
model%>%
layer_conv_2d(filters = 32,
kernel_size = c(3,3),
activation = 'relu',
input_shape = c(100,100,1))%>%
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.01)%>%
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.01)%>%
layer_conv_2d(filters=32,
kernel_size=c(4,4),
activation = 'relu')%>%
layer_max_pooling_2d(pool_size = c(2,2))%>%
layer_dropout(rate=0.01)%>%
layer_flatten()%>%
layer_dense(units=320,activation = 'relu')%>%
layer_dropout(rate=0.01)%>%
layer_dense(units=2,activation='softmax')%>%
compile(loss='categorical_crossentropy',
optimizer_adam(lr = 0.001, beta_1 = 0.9, beta_2 = 0.999,
epsilon = NULL, decay = 0, amsgrad = FALSE, clipnorm = NULL,
clipvalue = NULL),
metrics=c('accuracy'))
summary(model)
proses<-model%>%
fit(trainfix,
trainLabels,
epoch=50,
batch_size=32,
validation_split=0.2)
plot(proses)
model%>%evaluate(trainfix,trainLabels)
pred<-model%>%predict_classes(trainfix)
pred
table(Predicted=pred, Actual=trainnn)
model%>%evaluate(testfix,testLabels)
pred<-model%>%predict_classes(testfix)
在proses<-model%>%
部分之前一切都很好。
我收到此错误消息:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (253, 100, 100)
有人可以帮助我解决此问题吗?我只是将图像更改为灰度,将无法正常工作。
* trainfix的尺寸为(253,100,100)