问题描述
我有以下代码.可以在此处或此处.数据集包含归类为cat
或dog
的图像.
I have the following code. The data set can be downloaded here or here. The data set contains images categorized as cat
or dog
.
此代码的任务是训练猫和狗的图像数据.这样,给定一张图片,它就可以分辨出是猫还是狗.它是由页面激发的.下面是成功运行的代码:
The task of this code is for training cats and dogs image data.So that given a picture, it can tell whether it's cat's or dog.It is motivated by this page. Below is the sucessfully running code:
library(keras)
# Organize dataset --------------------------------------------------------
#options(warn = -1)
# Ths input
original_dataset_dir <- "data/kaggle_cats_dogs/original/"
# Create new organized dataset directory ----------------------------------
base_dir <- "data/kaggle_cats_dogs_small/"
dir.create(base_dir)
train_dir <- file.path(base_dir, "train")
dir.create(train_dir)
validation_dir <- file.path(base_dir, "validation")
dir.create(validation_dir)
test_dir <- file.path(base_dir, "test")
dir.create(test_dir)
train_cats_dir <- file.path(train_dir, "cats")
dir.create(train_cats_dir)
train_dogs_dir <- file.path(train_dir, "dogs")
dir.create(train_dogs_dir)
validation_cats_dir <- file.path(validation_dir, "cats")
dir.create(validation_cats_dir)
validation_dogs_dir <- file.path(validation_dir, "dogs")
dir.create(validation_dogs_dir)
test_cats_dir <- file.path(test_dir, "cats")
dir.create(test_cats_dir)
test_dogs_dir <- file.path(test_dir, "dogs")
dir.create(test_dogs_dir)
# Copying files from original dataset to newly created directory
fnames <- paste0("cat.", 1:1000, ".jpg")
dum <- file.copy(file.path(original_dataset_dir, fnames),
file.path(train_cats_dir))
fnames <- paste0("cat.", 1001:1500, ".jpg")
dum <- file.copy(file.path(original_dataset_dir, fnames),
file.path(validation_cats_dir))
fnames <- paste0("cat.", 1501:2000, ".jpg")
dum <- file.copy(file.path(original_dataset_dir, fnames),
file.path(test_cats_dir))
fnames <- paste0("dog.", 1:1000, ".jpg")
dum <- file.copy(file.path(original_dataset_dir, fnames),
file.path(train_dogs_dir))
fnames <- paste0("dog.", 1001:1500, ".jpg")
dum <- file.copy(file.path(original_dataset_dir, fnames),
file.path(validation_dogs_dir))
fnames <- paste0("dog.", 1501:2000, ".jpg")
dum <- file.copy(file.path(original_dataset_dir, fnames),
file.path(test_dogs_dir))
options(warn = -1)
# Making model ------------------------------------------------------------
conv_base <- application_vgg16(
weights = "imagenet",
include_top = FALSE,
input_shape = c(150, 150, 3)
)
model <- keras_model_sequential() %>%
conv_base %>%
layer_flatten() %>%
layer_dense(units = 256, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
summary(model)
length(model$trainable_weights)
freeze_weights(conv_base)
length(model$trainable_weights)
# Train model -------------------------------------------------------------
train_datagen = image_data_generator(
rescale = 1/255,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = TRUE,
fill_mode = "nearest"
)
# Note that the validation data shouldn't be augmented!
test_datagen <- image_data_generator(rescale = 1/255)
train_generator <- flow_images_from_directory(
train_dir, # Target directory
train_datagen, # Data generator
target_size = c(150, 150), # Resizes all images to 150 × 150
batch_size = 20,
class_mode = "binary" # binary_crossentropy loss for binary labels
)
validation_generator <- flow_images_from_directory(
validation_dir,
test_datagen,
target_size = c(150, 150),
batch_size = 20,
class_mode = "binary"
)
# Compile model -----------------------------------------------------------
model %>% compile(
loss = "binary_crossentropy",
optimizer = optimizer_rmsprop(lr = 2e-5),
metrics = c("accuracy")
)
# Fit ---------------------------------------------------------------
history <- model %>% fit_generator(
train_generator,
steps_per_epoch = 100,
epochs = 30,
validation_data = validation_generator,
validation_steps = 50
)
# Plot --------------------------------------------------------------------
plot(history)
我的问题是如何 evaluate()和 predict_class()使用基于 image_data_generator()和 flow_images_from_directory().
My question is how can I evaluate() and predict_class() the data using data in test_dir
based on the image_data_generator() and flow_images_from_directory().
我尝试了此方法,但不起作用:
I tried this but doesn't work:
test_generator <- flow_images_from_directory(
test_dir, # Target directory
train_datagen, # Data generator
target_size = c(150, 150), # Resizes all images to 150 × 150
batch_size = 20,
class_mode = "binary" # binary_crossentropy loss for binary labels
)
model %>% evaluate(test_generator, test_generator)
# Error in dim(x) <- length(x) : invalid first argument
model %>% predict_classes(test_generator)
# Error in dim(x) <- length(x) : invalid first argument
推荐答案
由于使用的是生成器,因此应使用函数valuate_generator/predict_generator:
Since you are using a generator, you should use the function evaluate_generator/predict_generator:
model %>% evaluate_generator(test_generator, steps=num_test_images)
model %>% predict_generator(test_generator, steps=num_test_images)
predict_generator将返回一个列表或概率,您可以使用ifelse将其转换成类:
predict_generator will return a list or probabilites which you can turn into classes using ifelse:
ifelse(predictions > 0.5, 1, 0)
和train_generator$class_indices
将为您提供类索引的字典(例如{'cat':0,'dog':1})
and train_generator$class_indices
will give you a dictionary of the class indices (e.g. {'cat': 0, 'dog': 1})
这篇关于如何从R keras中的类似数据的生成器评估()和预测()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!