Esempio n. 1
0
    def predict(self, test_dir: str, dataset_name: str = "", save: bool = True):
        """Evaluates a new set of images using the trained CNN.

        Args:
            test_dir: Relative path to the validation directory (e.g., 'dataset/test').
            dataset_name: Dataset descriptive name.
            save: Save results to an Excel file.

        """
        # Configure loading and pre-processing functions
        print('Reading test data...')
        test_datagen = ImageDataGenerator(preprocessing_function=self._preprocessing_function)

        test_generator = test_datagen.flow_from_directory(
            test_dir,
            target_size=self._target_size,
            batch_size=1,  # A batch size of 1 ensures that all test images are processed
            class_mode='categorical',
            shuffle=False
        )

        # Predict categories
        predictions = self._model.predict_generator(test_generator, steps=test_generator.samples)
        predicted_labels = np.argmax(predictions, axis=1).ravel().tolist()

        # Format results and compute classification statistics
        results = Results(test_generator.class_indices, dataset_name=dataset_name)
        accuracy, confusion_matrix, classification = results.compute(test_generator.filenames, test_generator.classes,
                                                                     predicted_labels)
        # Display and save results
        results.print(accuracy, confusion_matrix)

        if save:
            results.save(confusion_matrix, classification)
Esempio n. 2
0
    def predict(self, dataset: List[str], dataset_name: str = "", save: bool = True) -> \
            Tuple[float, np.ndarray, List[Tuple[str, str, str]]]:
        """Evaluates a new set of images using the trained classifier.

        Args:
            dataset: Paths to the test images.
            dataset_name: Dataset descriptive name.
            save: Save results to an Excel file.

        Returns:
            Classification accuracy.
            Confusion matrix.
            Detailed per image classification results.

        """
        # Extract features
        test_desc = []
        test_labels = []

        for path in dataset:
            test_desc.extend(self._extract_bow_features(path))
            test_labels.append(self._labels.get(Dataset.get_label(path)))

        # Predict categories
        predicted_labels = (self._classifier.predict(np.array(test_desc, np.float32))[1]).ravel().tolist()
        predicted_labels = [int(label) for label in predicted_labels]

        # Format results and compute classification statistics
        results = Results(self._labels, dataset_name=dataset_name)
        accuracy, confusion_matrix, classification = results.compute(dataset, test_labels, predicted_labels)
        results.print(accuracy, confusion_matrix)

        if save:
            results.save(confusion_matrix, classification)

        return accuracy, confusion_matrix, classification