Difference between revisions of "Example Image Classification"

From BoofCV
(Created page with "<center> <gallery widths=600px heights=300px> File:Example_image_classification.jpg | Image with overlayed labels from an image classifier </gallery> </center> Example of how...")
 
m
Line 5: Line 5:
 
</center>
 
</center>
  
Example of how to use a previously trained neural network (trained using [http://torch.ch/ Torch] loaded using [https://github.com/lessthanoptimal/DeepBoof 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 of how to use a previously trained neural network (trained using [http://torch.ch/ Torch] loaded and run in Java using [https://github.com/lessthanoptimal/DeepBoof 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:
 
Example Code:

Revision as of 17:24, 1 December 2016

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

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();
//		ClassifierAndSource cs = FactoryImageClassifier.nin_imagenet();

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

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

		String regex = UtilIO.pathExample("recognition/pixabay")+"/^\\w*.jpg";
		List<File> images = Arrays.asList(BoofMiscOps.findMatches(regex));
		Collections.sort(images);

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

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

			Planar<GrayF32> image = new Planar<>(GrayF32.class,buffered.getWidth(), buffered.getHeight(), 3);
			ConvertBufferedImage.convertFromMulti(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);
		}
	}
}