How to perform a multi label classification with tensorflow?

Angelo Neves picture Angelo Neves · Feb 13, 2017 · Viewed 8.5k times · Source

I'm new to tensorflow and would like to know if there is any tutorial or example of a multilabel classification with multiple network outputs.

I'm asking this because I have a collection of images, in which, each image can belong to several classes and my output needs to have a score of each class.

I also do not know if the tensorflow follows some file pattern of the images and the classes, so if someone has some example it would facilitate a lot.

Thank you.

Answer

niedakh picture niedakh · Feb 13, 2017

You can also try transforming your problem from a multi-label to multi-class classification using a Label Powerset approach. Label Powerset transformation treats every label combination attested in the training set as a different class and constructs one instance of a multi-class clasifier - and after prediction converts the assigned classes back to multi-label case. It is provided in scikit-multilearn and scikit-compatibility wrapper over the tensorflow Estimator or via an input_fn or use skflow. Then just plug it into an instance of LabelPowerset.

The code could go as follows:

from skmultilearn.problem_transform import LabelPowerset
import tensorflow.contrib.learn as skflow

# assume data is loaded using
# and is available in X_train/X_test, y_train/y_test

# initialize LabelPowerset multi-label classifier
# with tensor flow DNN base classifier
classifier = LabelPowerset(skflow.TensorFlowDNNClassifier(OPTIONS))

# train
classifier.fit(X_train, y_train)

# predict
predictions = classifier.predict(X_test)