如何在R Keras中基于生成器对测试集执行evaluate()和predict_classes()
Hey there! Let's sort out this frustrating error you're seeing when trying to evaluate and predict with your VGG16 transfer learning model. That Error in dim(x) <- length(x) : invalid first argument almost always ties back to misconfiguring your test data generator or misusing the Keras evaluation/prediction functions with generators. Let's break down the fix step by step.
1. First: Fix Your Test Data Generator Configuration
Test data shouldn't use data augmentation (we only augment training data!), and your generator settings need to match your model's setup. Here's the correct way to set it up, depending on your test directory structure:
Case 1: Test set has labeled subdirectories (e.g., test/cat/, test/dog/)
If you're evaluating accuracy (and have true labels for test data), use this:
# No augmentation for test data—only rescale to match training preprocessing test_datagen <- image_data_generator(rescale = 1/255) test_generator <- flow_images_from_directory( test_dir, target_size = c(150, 150), # Must match the target_size used in training batch_size = 32, class_mode = "binary", # Match your model's output layer (binary for 2-class) shuffle = FALSE # Critical! Don't shuffle test data—keeps labels/predictions aligned )
Case 2: Test set has no labeled subdirectories (just raw images)
If you're only predicting labels without ground truth, set class_mode = "none":
test_generator <- flow_images_from_directory( test_dir, target_size = c(150, 150), batch_size = 32, class_mode = "none", # No labels provided shuffle = FALSE )
2. Correctly Evaluate the Model
Your original call model %>% evaluate(test_generator, test_generator) is wrong—you don't need to pass the generator twice. Instead, specify the number of steps (total test samples divided by batch size) to tell Keras how many batches to process:
# Get total number of test images test_sample_count <- length(list.files(test_dir, recursive = TRUE, pattern = "\\.(jpg|png)$")) # Calculate steps (round up to avoid missing samples) test_steps <- ceiling(test_sample_count / test_generator$batch_size) # Run evaluation eval_results <- model %>% evaluate( test_generator, steps = test_steps, verbose = 1 # Shows progress bar ) # Print results (loss, accuracy, etc.) print(eval_results)
3. Properly Generate Predictions
predict_classes is deprecated in newer Keras versions, so it's better to use predict to get probabilities, then convert them to classes manually. Here's how:
# Generate predicted probabilities pred_probs <- model %>% predict( test_generator, steps = test_steps, verbose = 1 ) # Convert probabilities to class labels (0 = cat, 1 = dog, adjust as needed) pred_classes <- round(pred_probs) # If you need to map predictions to filenames (super useful for submission!) image_filenames <- test_generator$filenames results <- data.frame( Image = image_filenames, Predicted_Class = ifelse(pred_classes == 0, "cat", "dog"), Probability = pred_probs ) # If you're using an older Keras version where predict_classes still works: # pred_classes <- model %>% predict_classes(test_generator, steps = test_steps)
Quick Troubleshooting Tips
- Double-check that your test images are the same format (JPG/PNG) and size as your training data—corrupted or mismatched images can cause weird errors.
- Ensure your model's output layer matches the
class_mode: for binary classification, uselayer_dense(units = 1, activation = "sigmoid")withclass_mode = "binary". - If you still hit issues, try reducing the
batch_size(e.g., to 16 or 8)—sometimes large batches cause memory-related glitches that manifest as odd error messages.
内容的提问来源于stack exchange,提问作者pdubois

