Classification (not detection!) of several objects in one image is the problem. How can I do this using keras.
For example if I have 6 classes (dogs,cats,birds,...) and two different objects (a cat and a bird) in this image. The label would be of the form: [0,1,1,0,0,0] Which metric, loss function and optimizer is recommended? I would like to use CNN.
Answer
The keyword is "multilabel classification".
In the output layer you have multiple neurons, each neuron representing one of your classes.
Now you should use a binary classification for each neuron independently.
So if you have 3 classes, the output of your network could be [0.1, 0.8, 0.99] which means the following: The first class is true for your image with the probability 10 %, the second is true with 80 % and the last class is true with 99 %. So the network decided for two classes to be true at the same time for a single input image!
It's pretty easy to implement this into Keras/Tensorflow.
You could use some binary_crossentropy as your loss function and Sigmoid Function as the activation in your last layer. Therefore you get values in the interval (0, 1) for every output neuron.
As the metric you could use accuracy, which tells you how many images are classified in the right way (as relative frequency).
See the following example:
from tensorflow.keras.layers import *
from tensorflow.keras.activations import *
from tensorflow.keras.models import *
from tensorflow.keras.optimizers import *
import numpy as np# put your data right here:
num_classes = 3 # here I'm assuming 3 classes, e.g. dog, cat and bird
x_train = np.zeros((100, 128, 128, 3)) # I'm just using zeros to simplify the example
y_train = np.zeros((100, num_classes))model = Sequential()
# put your conv layer / conv blocks here:
model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(128, 128, 3)))
model.add(Flatten())
model.add(Dense(units=num_classes, activation='sigmoid'))model.compile(loss="binary_crossentropy",optimizer=Adam(0.005),metrics=["accuracy"])
training_history = model.fit(x=x_train, y=y_train, epochs=5)
I'm using Tensorflow 2.2.0.
I hope this will help you :)
Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …
Maximum Value of the LootProblem Introduction:
A thief finds much more loot than his bag can fit. Help him to find the most valuable combination of items assuming that any fraction of a loot item can b…
This question already has an answer here:Making a countdown timer with Python and Tkinter?(1 answer)Closed 8 years ago.Im having problems with a countdown clock that I was making in Python for a Raspb…
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…
Hello my assignment is :Create a system that allows the user to enter their name, title, surname, Dob, email and phone number. Once details are submitted, they should be written to a file. Surnames tha…
I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."Here is what I came up …
I am new and learning python 3.6 and Ive almost completed my first code project. After doing an exhaustive search to resolve my problem I have not been able to find the answer to what I am sure is a si…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…
I have two lists:a - dictionary which contains keywords such as ["impeccable", "obvious", "fantastic", "evident"] as elements of the listb - sentences which cont…