我将图像放在两个单独的文件夹中(培训和测试,没有验证,因为我的图像很少(406(,吸引力评级(标签(是数据帧CFDNEW_all_males中的一列。同样在CFDNEW_all_males(数据帧(中,我有模型的文件名id,它(几乎(与每个图片的名称相同。因此,在阅读Francois Chollet和J.J.Allaire的《R深度学习》时,他们使用了flow_images_from_directory函数。然而,对于我的情况,我发现我不能使用它,因为标签存储在数据帧中,应该使用flow_images_from_dataframe函数设置class_mode=";其他";而不是二元的因为回归类型的问题。因此,当我尝试运行该函数时,我会收到以下错误:
Pandas(python模块(必须安装在与Keras相同的环境中。根据您使用的环境类型,使用reticulate::virtualenv_Install("pandas",envname="r-tensorflow"(或reticulater::conda_Install。
代码:
setwd('/Users/e-mashine/Desktop')
train_image_files_path <- "/Users/e-mashine/Desktop/resized cfd images/training re2"
test_image_files_path <- "/Users/e-mashine/Desktop/resized cfd images/testing re2"
train_files <- list.files(path = train_image_files_path, pattern = "*.jpg", full.names=FALSE)
test_files <- list.files(path = test_image_files_path, pattern = "*.jpg", full.names=FALSE)
for (i in 1:305) {train_dataset$File_Name[i] <- train_files[i]}
for (i in 1:101) {test_dataset$File_Name[i] <- test_files[i]}
train_dataset <- train_dataset[order(train_dataset$File_Name),]
test_dataset <- test_dataset[order(test_dataset$File_Name),]
train_datagen <- image_data_generator(rescale = 1/255)
test_datagen <- image_data_generator(rescale = 1/255)
train_generator <- flow_images_from_dataframe(dataframe = train_dataset, directory = train_image_files_path,
x_col = list(train_dataset$File_Name), y_col = list(train_dataset$Attractive), generator = train_datagen,
class_mode = "other", target_size = c(640, 450), batch_size = 20, classes = list(train_dataset$Attractive))
flow_images_from_dataframe中出错(数据帧=CFDNEW_all_males,目录=train_image_files_path,:Pandas(python模块(必须安装在与Keras相同的环境中。根据您使用的环境类型,使用reticulate::virtualenv_Install("pandas",envname="r-tensorflow"(或reticulater::conda_Install。
然而,我已经做到了:(我使用的是windows,所以我指定了conda(
reticulate::conda_install("pandas", envname = "r-tensorflow")
正在收集包元数据(current_repodata.json(:…正在工作…已完成解决环境:。。。工作完成
已安装所有请求的程序包。
最后我也做过(以前(:
install.packages("reticulate")
library(reticulate)
install.packages("remotes")
remotes::install_github("rstudio/tensorflow")
library(tensorflow)
install_tensorflow(version = "2.5.0", method = "conda", envname = "r-reticulate")
library(tensorflow)
tf_config()
2021-07-10 14:30:29.462901:W tensorflow/stream_executor/platform/default/ddso_loader.cc:64]无法加载动态库'cudart64_110.dll';dleerror:cudart64_110.dll未找到2021-07-10 14:30:29.6464762:I tensorflow/stream_executor/cuda/cudart_stub.cc:29]如果您的机器上没有设置GPU,请忽略上述cudart错误。TensorFlow v2.5.0((Python v3.7(C:\Users/e-mashine/AppData/Local/r-miniconda/envs/r-reticulate/Python.exe(
我也试过:
reticulate::virtualenv_install("pandas", envname = "r-tensorflow")
但与上述错误相同。。。有什么帮助吗?
EDIT:我使用了一个自定义函数,它起作用了,但当我试图查看其中一个生成器的输出时,出现了这个错误:
# custom function, this works..
flow_images_from_dataframe<-function (dataframe,
directory=NULL,x_col="filename",y_col="class",generator =
image_data_generator(),target_size = c(640,450), color_mode = "rgb",
classes = NULL, class_mode = "other", batch_size = 32, shuffle = TRUE,
seed = NULL, save_to_dir = NULL, save_prefix = "", save_format =
"jpg",dtype="float32",
subset = NULL, interpolation = "nearest",drop_duplicates=TRUE)
{args <- list(dataframe=as.data.frame(dataframe), directory =
normalizePath(directory), #directory = as.character(directory),
x_col=x_col,y_col=y_col,target_size = as.integer(target_size),
color_mode = color_mode, classes = classes, class_mode = class_mode,
batch_size = as.integer(batch_size), shuffle = shuffle, seed = NULL,
#as_nullable_integer(seed),
#save_to_dir = normalizePath(save_to_dir),
save_to_dir = NULL,
save_prefix = save_prefix, save_format =
save_format,dtype=dtype,
drop_duplicates=drop_duplicates)
#if (keras_version() >= "2.1.2")
# args$interpolation <- interpolation
#if (keras_version() >= "2.1.5")
# args$subset <- subset
#do.call(generator$flow_from_dataframe, args)
}
environment(flow_images_from_dataframe) <- asNamespace('keras')
batch <- generator_next(train_generator, completed = NULL)
#Error: object is not iterable
str(batch)
#Error: object 'batch' not found
它应该是num[20,1:640,1:450,1:3]11 11 11 11 1 1 1 1。。。还有一份2人的名单?
我认为这在最近版本的keras包中得到了修复。如果您仍然遇到此问题,请在此处提交问题:https://github.com/rstudio/keras/issues/new