Skip to content

Commit 7261753

Browse files
committed
Adding classification exception and adding extra javadoc
1 parent 36a1e80 commit 7261753

File tree

5 files changed

+80
-80
lines changed

5 files changed

+80
-80
lines changed

src/main/java/javax/visrec/ml/classification/AbstractImageClassifier.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javax.visrec.ml.classification;
22

3-
import javax.visrec.ml.classification.ImageClassifier;
3+
import javax.visrec.ImageFactory;
4+
import javax.visrec.ml.model.ModelProvider;
45
import javax.visrec.spi.ServiceProvider;
56
import java.awt.image.BufferedImage;
67
import java.io.File;
@@ -9,28 +10,28 @@
910
import java.util.Map;
1011
import java.util.Objects;
1112
import java.util.Optional;
12-
import javax.visrec.ImageFactory;
13-
import javax.visrec.ml.model.ModelProvider;
1413

1514
/**
1615
* Skeleton abstract class to make it easier to implement image classifier.
1716
* It provides implementation of Classifier interface for images, along with
1817
* image factory for specific type of images.
1918
* This class solves the problem of using various implementation of images and machine learning models in Java,
2019
* and provides standard Classifier API for clients.
21-
*
20+
* <p>
2221
* By default the type of key in the Map the {@link ImageClassifier} is {@code String}
2322
*
24-
* @author Zoran Sevarac
25-
*
23+
* @param <IMAGE_CLASS> class to classify
2624
* @param <MODEL_CLASS> class of machine learning model
25+
* @author Zoran Sevarac
26+
* @since 1.0
2727
*/
28-
public abstract class AbstractImageClassifier<IMAGE_CLASS, MODEL_CLASS> implements ImageClassifier<IMAGE_CLASS>, ModelProvider { // could also implement binary classifier
28+
public abstract class AbstractImageClassifier<IMAGE_CLASS, MODEL_CLASS> implements ImageClassifier<IMAGE_CLASS>, ModelProvider<MODEL_CLASS> {
2929

30-
private ImageFactory<IMAGE_CLASS> imageFactory; // image factory impl for the specified image class
31-
private MODEL_CLASS model; // the model could be injected from machine learning container?
30+
private final ImageFactory<IMAGE_CLASS> imageFactory;
31+
private MODEL_CLASS model;
3232

33-
private float threshold=0.0f; // this should ba a part of every classifier
33+
// TODO: this should ba a part of every classifier
34+
private float threshold = 0.0f;
3435

3536
protected AbstractImageClassifier(final Class<IMAGE_CLASS> imgCls, final MODEL_CLASS model) {
3637
final Optional<ImageFactory<IMAGE_CLASS>> optionalImageFactory = ServiceProvider.current()
@@ -48,27 +49,27 @@ public ImageFactory<IMAGE_CLASS> getImageFactory() {
4849
}
4950

5051
@Override
51-
public Map<String, Float> classify(File file) {
52+
public Map<String, Float> classify(File file) throws ClassificationException {
5253
IMAGE_CLASS image;
5354
try {
5455
image = imageFactory.getImage(file);
5556
return classify(image);
5657
} catch (IOException e) {
57-
throw new RuntimeException("Couldn't transform input into a BufferedImage", e);
58+
throw new ClassificationException("Failed to transform input into a BufferedImage", e);
5859
}
5960
}
6061

6162
@Override
62-
public Map<String, Float> classify(InputStream inputStream) {
63+
public Map<String, Float> classify(InputStream inputStream) throws ClassificationException {
6364
IMAGE_CLASS image;
6465
try {
6566
image = imageFactory.getImage(inputStream);
6667
return classify(image);
6768
} catch (IOException e) {
68-
throw new RuntimeException("Couldn't transform input into a BufferedImage", e);
69-
}
69+
throw new RuntimeException("Failed to transform input into a BufferedImage", e);
70+
}
7071
}
71-
72+
7273
// todo: provide get top 1, 3, 5 results; sort and get
7374

7475
@Override
@@ -77,7 +78,7 @@ public MODEL_CLASS getModel() {
7778
}
7879

7980
protected final void setModel(MODEL_CLASS model) {
80-
this.model = Objects.requireNonNull(model, "Model cannot bu null!");
81+
this.model = Objects.requireNonNull(model, "Model cannot bu null!");
8182
}
8283

8384
public float getThreshold() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package javax.visrec.ml.classification;
2+
3+
/**
4+
* Exception thrown if anything fails in the execution of a classifier.
5+
*
6+
* @author Kevin Berendsen
7+
* @since 1.0
8+
*/
9+
public class ClassificationException extends RuntimeException {
10+
11+
/**
12+
* Creates a new instance of the exception
13+
*
14+
* @param message additional message of the cause.
15+
*/
16+
public ClassificationException(String message) {
17+
super(message);
18+
}
19+
20+
/**
21+
* Creates a new instance of the exception
22+
*
23+
* @param message additional message of the cause.
24+
* @param throwable caused by throwable.
25+
*/
26+
public ClassificationException(String message, Throwable throwable) {
27+
super(message, throwable);
28+
}
29+
30+
}

src/main/java/javax/visrec/ml/classification/Classifier.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
/**
44
* Classifier answers the question what is the category/type of an input object.
55
* Each category/type has corresponding label or class name, which can be String, Enum or custom user defined class.
6-
* This is a generic classifier interface, that all classifiers should implement, and
7-
* it provides a method to classify given instances of some class.
8-
*
6+
* This is a generic classifier interface, that all classifiers should implement, and
7+
* it provides a method to classify given instances of some class.
8+
* <p>
99
* Implementations should specify input type <T> of instances that are classified,
1010
* and type of the returned vales <R>.
11-
*
11+
* <p>
1212
* Usually implementations predict category of instances with some probability,
1313
* and cannot guarantee 100% accuracy.
1414
*
15-
* @author Zoran Sevarac
1615
* @param <T> type of input objects to classify (eg. User, Product, Transaction, Image, etc.)
1716
* @param <R> type of classification result (String, Enum, custom class).
18-
*
17+
* @author Zoran Sevarac
1918
* @see BinaryClassifier
2019
* @see MultiClassClassifier
2120
* @see ImageClassifier
@@ -29,7 +28,8 @@ public interface Classifier<T, R> {
2928
*
3029
* @param input some instance to classify
3130
* @return classification results for the specified instance
31+
* @throws ClassificationException if the input could not be classified
3232
*/
33-
R classify(T input);
34-
33+
R classify(T input) throws ClassificationException;
34+
3535
}

src/main/java/javax/visrec/ml/classification/ClassifierBuilder.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/main/java/javax/visrec/ml/classification/ImageClassifier.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,31 @@
55
import java.io.InputStream;
66
import java.util.Map;
77

8-
public interface ImageClassifier<IMAGE_CLASS> extends Classifier<IMAGE_CLASS, Map<String, Float>>{
8+
/**
9+
* Classifier interface specialized in image classification
10+
*
11+
* @param <IMAGE_CLASS> type of input objects to classify (eg. User, Product, Transaction, Image, etc.)
12+
* @author Zoran Sevarac
13+
* @since 1.0
14+
*/
15+
public interface ImageClassifier<IMAGE_CLASS> extends Classifier<IMAGE_CLASS, Map<String, Float>> {
916

10-
Map<String, Float> classify(File input);
17+
/**
18+
* Classify the input and get a map of classification results as output
19+
*
20+
* @param input {@link File} to use as input
21+
* @return {@code Map} with key as classification label and with value as accuracy percentage of likelihood
22+
* @throws ClassificationException if the file couldn't be found or classified
23+
*/
24+
Map<String, Float> classify(File input) throws ClassificationException;
1125

12-
Map<String, Float> classify(InputStream input);
26+
/**
27+
* Classify the input and get a map of classification results as output
28+
*
29+
* @param input {@link InputStream} to use as input
30+
* @return {@code Map} with key as classification label and with value as accuracy percentage of likelihood
31+
* @throws ClassificationException if input couldn't be classified
32+
*/
33+
Map<String, Float> classify(InputStream input) throws ClassificationException;
1334

1435
}

0 commit comments

Comments
 (0)