Difference between revisions of "Example Image Classification"

From BoofCV
m
m
 
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
Example Code:
 
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.25/examples/src/boofcv/examples/recognition/ExampleImageClassification.java ExampleImageClassification.java ]
+
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.37/examples/src/boofcv/examples/recognition/ExampleImageClassification.java ExampleImageClassification.java ]
  
 
Concepts:
 
Concepts:
Line 21: Line 21:
 
Videos:
 
Videos:
 
* [https://youtu.be/qMTtdiujAtQ?t=347 Example]
 
* [https://youtu.be/qMTtdiujAtQ?t=347 Example]
 
  
 
= Example Code =
 
= Example Code =
Line 33: Line 32:
 
  */
 
  */
 
public class ExampleImageClassification {
 
public class ExampleImageClassification {
 
+
public static void main( String[] args ) throws IOException {
public static void main(String[] args) throws IOException {
 
 
ClassifierAndSource cs = FactoryImageClassifier.vgg_cifar10();  // Test set 89.9% for 10 categories
 
ClassifierAndSource cs = FactoryImageClassifier.vgg_cifar10();  // Test set 89.9% for 10 categories
 
// ClassifierAndSource cs = FactoryImageClassifier.nin_imagenet(); // Test set 62.6% for 1000 categories
 
// ClassifierAndSource cs = FactoryImageClassifier.nin_imagenet(); // Test set 62.6% for 1000 categories
  
File path = DeepBoofDataBaseOps.downloadModel(cs.getSource(),new File("download_data"));
+
File modelPath = DeepBoofDataBaseOps.downloadModel(cs.getSource(), new File("download_data"));
  
 
ImageClassifier<Planar<GrayF32>> classifier = cs.getClassifier();
 
ImageClassifier<Planar<GrayF32>> classifier = cs.getClassifier();
classifier.loadModel(path);
+
classifier.loadModel(modelPath);
 
List<String> categories = classifier.getCategories();
 
List<String> categories = classifier.getCategories();
  
String regex = UtilIO.pathExample("recognition/pixabay")+"/^\\w*.jpg";
+
String imagePath = UtilIO.pathExample("recognition/pixabay");
List<File> images = Arrays.asList(BoofMiscOps.findMatches(regex));
+
List<String> images = UtilIO.listByPrefix(imagePath, null, ".jpg");
 
Collections.sort(images);
 
Collections.sort(images);
  
Line 51: Line 49:
 
ShowImages.showWindow(gui, "Image Classification", true);
 
ShowImages.showWindow(gui, "Image Classification", true);
  
for( File f : images ) {
+
for (String path : images) {
BufferedImage buffered = UtilImageIO.loadImage(f.getPath());
+
File f = new File(path);
if( buffered == null)
+
BufferedImage buffered = UtilImageIO.loadImage(path);
 +
if (buffered == null)
 
throw new RuntimeException("Couldn't find input image");
 
throw new RuntimeException("Couldn't find input image");
  
Planar<GrayF32> image = new Planar<>(GrayF32.class,buffered.getWidth(), buffered.getHeight(), 3);
+
Planar<GrayF32> image = new Planar<>(GrayF32.class, buffered.getWidth(), buffered.getHeight(), 3);
ConvertBufferedImage.convertFromMulti(buffered,image,true,GrayF32.class);
+
ConvertBufferedImage.convertFromPlanar(buffered, image, true, GrayF32.class);
  
 
classifier.classify(image);
 
classifier.classify(image);
  
 
// add image and results to the GUI for display
 
// add image and results to the GUI for display
gui.addImage(buffered,f.getName(),classifier.getAllResults(),categories);
+
gui.addImage(buffered, f.getName(), classifier.getAllResults(), categories);
 
}
 
}
 
}
 
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 19:34, 21 December 2020

Example of how to use a previously trained neural network (trained using Torch loaded and run in Java using DeepBoof) and apply it the problem of image classification. Model data is often quite large and so you will need to download it from an external source. Locations for where you can download the model from are included with the high level interface.

Example Code:

Concepts:

  • Image Classification
  • Deep Neural Networks
  • Torch

Related Examples:

Videos:

Example Code

/**
 * This example shows how to create an image classifier using the high level factory, download the model, load it,
 * process images, and then look at the results.
 *
 * @author Peter Abeles
 */
public class ExampleImageClassification {
	public static void main( String[] args ) throws IOException {
		ClassifierAndSource cs = FactoryImageClassifier.vgg_cifar10();  // Test set 89.9% for 10 categories
//		ClassifierAndSource cs = FactoryImageClassifier.nin_imagenet(); // Test set 62.6% for 1000 categories

		File modelPath = DeepBoofDataBaseOps.downloadModel(cs.getSource(), new File("download_data"));

		ImageClassifier<Planar<GrayF32>> classifier = cs.getClassifier();
		classifier.loadModel(modelPath);
		List<String> categories = classifier.getCategories();

		String imagePath = UtilIO.pathExample("recognition/pixabay");
		List<String> images = UtilIO.listByPrefix(imagePath, null, ".jpg");
		Collections.sort(images);

		ImageClassificationPanel gui = new ImageClassificationPanel();
		ShowImages.showWindow(gui, "Image Classification", true);

		for (String path : images) {
			File f = new File(path);
			BufferedImage buffered = UtilImageIO.loadImage(path);
			if (buffered == null)
				throw new RuntimeException("Couldn't find input image");

			Planar<GrayF32> image = new Planar<>(GrayF32.class, buffered.getWidth(), buffered.getHeight(), 3);
			ConvertBufferedImage.convertFromPlanar(buffered, image, true, GrayF32.class);

			classifier.classify(image);

			// add image and results to the GUI for display
			gui.addImage(buffered, f.getName(), classifier.getAllResults(), categories);
		}
	}
}