2

I want to finetune vgg in keras. I successfully performed the classification task of classifying images of fruit. Let's say I want a model to be able to predict both the fruit name and fruit color. I successfully trained two different models for predicting fruit type and color. But I want to have a model which can perform both. How can I achieve this? Can I use this solution:

How does Keras handle multilabel classification?

Another solution that came to mind, was using vgg to extract and store features, and then using those feature vectors to perform multiclass multilabel classification using sci-kit learn which I am more familiar with than keras. ButI'm mainly interested in using deep learning to perform the whole thing. Another thing is not all images have all the labels, i.e some images may not have fruit color for example. What can I do about this? One thing that comes to mind is removing these images from dataset, but this will also mean losing around 2 thousand of my training data.

L1nu5
  • 79
  • 2
  • 9
  • 1
    The solution provided in the link is exactly what you are looking for. With missing data - in case of 2 cases - you can try to label it manually. In case of exclusion - what percentage of your data doesn't have a full label set? – Marcin Możejko Feb 10 '18 at 11:15
  • @MarcinMożejko Around %5 of my data don't have full labels. Thanks for your comment. – L1nu5 Feb 10 '18 at 11:50
  • So I wouldn't bother :) – Marcin Możejko Feb 10 '18 at 11:50
  • Won't it affect the prediction results? As I am using %20 of my data as test set, and %20 of training set for validation. – L1nu5 Feb 10 '18 at 11:53
  • It's only 5% - so you cannot be 100% sure - but in general - this shouldn't be the amount of data that would affect training process. – Marcin Możejko Feb 10 '18 at 11:54
  • @MarcinMożejko well the link isn't exactly what he's trying to do. he does not want a M out of N classification, but 2 times a 1 out of N classification. so i would rather split the model towards the end and feed it into 2 separate softmax outputs. all that said, this example is rather useless, because if you can predict the fruit, you implicitely have a prediction for the color already. – pietz Feb 16 '18 at 09:26
  • I’m voting to close this question because it is not about programming as defined in the [help] but about ML theory and/or methodology. – desertnaut May 23 '21 at 21:57

1 Answers1

1

You have to use a sigmoid activation function for the output layer and binary_crossentropy as the loss function.

nn = Sequential()
nn.add(Dense(10, activation="relu", input_shape=(10,)))
nn.add(Dense(5, activation="sigmoid"))
nn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

You can find more details in this article I wrote a while ago.