Answers.Body: <p>Your problem is a classification problem. If you are following the tutorial, it is using the ResNet50 network, which is a convolutional neural network with one fully connected layer at the end. At the end the activation function is softmax. Detailed description of the activation function can be found here: <a href="https://medium.com/data-science-bootcamp/understand-the-softmax-function-in-minutes-f3a59641e86d" rel="nofollow noreferrer">Softmax function explained</a></p>
  
  <p><a href="https://i.sstatic.net/McVBq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/McVBq.png" alt="Udacity Deep Learning Slide on Softmax"></a></p>
  
  <p>Basically, softmax increases the difference between the higher probability and lower probability. It also limits the output between 0 and 1. </p>
  
  <h2>Problem Origin</h2>
  
  <p>Due to the nature of the softmax function, it always chooses the best one and enlarge the value to be a value near one, even if the range of the output predictions is very small like 0-0.1. Also you training data only have the data of the 10 logos labelled, so if the network see unseen images with no logo it recognizes, it predicts the one with the most similarity. If you want to classify images with no logos in it, you should add an extra class in the training dataset and also the code to train the network to learn to classify images which is not in the 5 class into a separate maybe called unlabeled. Hope I can help you and have a nice day.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-05T17:38:52.083Z
Answers.Text: Thanks anyways. As I was thinking myself, looks like I should really add another class "unknown". But the problem is how big it should be? We can not put whatever on earth as a non-logo class :-/
Answers.UserDisplayName: Mary
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-06T00:08:00.290Z
Answers.Text: You should try putting at least the same amount of unknown data as the other class each have. If it doesn't work, put more of the unknown data until it works. Experimenting and trying is the key. Hope it helps
Answers.UserDisplayName: Clement
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-06T14:34:54.707Z
Answers.Text: @TinaJ  You could use a GAN like logic to produce random non-logo images (without training the generator of course)
Answers.UserDisplayName: Fnguyen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-06T16:58:08.983Z
Answers.Text: @Fnguyen that's also interesting...hopefully if the images are not very random! Needs to be a bit realistic also. Any code repository or samples for that?
Answers.UserDisplayName: Mary
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-06T17:31:40.167Z
Answers.Text: @TinaJ There are tons of examples if you Google GAN. I'd recommend machinelearningmastery because he has the whole Keras code. If you want it to be somewhat realistic you could train the generator a bit so it's not just random.
Answers.UserDisplayName: Fnguyen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-05T14:27:54.977Z
Answers.LastActivityDate: 2019-11-05T14:27:54.977Z
Answers.UserDisplayName: Clement
Body: <p>I'm new to Deep Learning, and I have some conceptual problems. I followed a simple tutorial <a href="https://towardsdatascience.com/transfer-learning-for-image-classification-using-keras-c47ccf09c8c8" rel="nofollow noreferrer">here</a>, and trained a model in Keras to do image classification on 10 classes of logos. I prepared 10 classes with each class having almost 100 images. My trained <code>Resnet50</code> model performs exceptionally great when the image <strong><em>is</em></strong> one of those 10 logos, with 1.00 probability. But the problem is if I pass a <strong><em>non-logo</em></strong> item, a random image totally unrelated visually, still it marks it as one of those logos with close to 1.00 probability! </p>
  
  <p>I'm confused. Am I missing anything? Why is this happening? How to find a solution? I need to find logos in video frames. But right now, with a high possbility each frame is marked as a logo!</p>
  
  <p>Here is my simple training code:</p>
  
  <pre><code>def build_finetune_model(base_model, dropout, fc_layers, num_classes):
      for layer in base_model.layers:
          layer.trainable = False
  
      x = base_model.output
      x = Flatten()(x)
      for fc in fc_layers:
          # New FC layer, random init
          x = Dense(fc, activation='relu')(x) 
          x = Dropout(dropout)(x)
  
      # New softmax layer
      predictions = Dense(num_classes, activation='softmax')(x) 
      finetune_model = Model(inputs=base_model.input, outputs=predictions)
      return finetune_model
  finetune_model = build_finetune_model(base_model, dropout=dropout, fc_layers=FC_LAYERS, num_classes=len(class_list))
  adam = Adam(lr=0.00001)
  finetune_model.compile(adam, loss='categorical_crossentropy', metrics=['accuracy'])
  filepath="./checkpoints/" + "ResNet50" + "_model_weights.h5"
  checkpoint = ModelCheckpoint(filepath, monitor=["acc"], verbose=1, mode='max')
  callbacks_list = [checkpoint]
  
  history = finetune_model.fit_generator(train_generator, epochs=NUM_EPOCHS, workers=8, 
                                         steps_per_epoch=steps_per_epoch, 
                                         shuffle=True, callbacks=callbacks_list)
  
  plot_training(history)
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-04T23:55:07.963Z
Comments.Text: This sounds like a classic example of training data that is not representative of the input your network will be receiving. Since the network has only seen input data that is expected to produce a value of `1.00` at one of the nodes, after significant training that's what it will do for all inputs, because it would be punished if it ever did otherwise. This is a really common problem, that has many solutions, but this answer from a while back has some good suggestions on how to solve for input entirely out of the scope of the problem: https://stackoverflow.com/a/52831580/9546874
Comments.UserDisplayName: Recessive
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T01:16:09.457Z
Comments.Text: Too add to @Recessive's answer -- answer (2) in that link is good, but to make it work you also need some training data which doesn't contain logos! Since you have a softmax final layer, I guess your labels are (currently) vectors which are all 0 except for a single 1. In a training image with no logo, it will be *all zeros*.
Comments.UserDisplayName: jmmcd
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T17:38:09.873Z
Comments.Text: Thanks guys. As I was thinking myself, looks like I should really add another class "unknown". But the problem is how big it should be? We can not put whatever on earth as a non-logo class :-/
Comments.UserDisplayName: Mary
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T23:50:22.570Z
Comments.Text: @TinaJ Another alternative is to use an object *detection* model instead - one that answers the question "of the objects I know, how many appear in this image, and which?" compared to image classification, which instead asks "of the objects that I know, which is the one in this image most similar to?"
Comments.UserDisplayName: gmds
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-06T16:58:41.623Z
Comments.Text: @gmds can you describe more?! What do you mean object detection model?
Comments.UserDisplayName: Mary
Tags: <machine-learning><deep-learning><classification><image-recognition><keras>
Title: Why is this ResNet50 misclassifying objects?
UserDisplayName: Mary
ThreadText: # Why is this ResNet50 misclassifying objects?
  
  (Asked by: username_0 on 2019-11-04)
  
  I'm new to Deep Learning, and I have some conceptual problems. I followed a simple tutorial [here](https://towardsdatascience.com/transfer-learning-for-image-classification-using-keras-c47ccf09c8c8), and trained a model in Keras to do image classification on 10 classes of logos. I prepared 10 classes with each class having almost 100 images. My trained `Resnet50` model performs exceptionally great when the image ***is*** one of those 10 logos, with 1.00 probability. But the problem is if I pass a ***non-logo*** item, a random image totally unrelated visually, still it marks it as one of those logos with close to 1.00 probability!
  
  I'm confused. Am I missing anything? Why is this happening? How to find a solution? I need to find logos in video frames. But right now, with a high possbility each frame is marked as a logo!
  
  Here is my simple training code:
  
  ```
  def build_finetune_model(base_model, dropout, fc_layers, num_classes):
      for layer in base_model.layers:
          layer.trainable = False
  
      x = base_model.output
      x = Flatten()(x)
      for fc in fc_layers:
          # New FC layer, random init
          x = Dense(fc, activation='relu')(x) 
          x = Dropout(dropout)(x)
  
      # New softmax layer
      predictions = Dense(num_classes, activation='softmax')(x) 
      finetune_model = Model(inputs=base_model.input, outputs=predictions)
      return finetune_model
  finetune_model = build_finetune_model(base_model, dropout=dropout, fc_layers=FC_LAYERS, num_classes=len(class_list))
  adam = Adam(lr=0.00001)
  finetune_model.compile(adam, loss='categorical_crossentropy', metrics=['accuracy'])
  filepath="./checkpoints/" + "ResNet50" + "_model_weights.h5"
  checkpoint = ModelCheckpoint(filepath, monitor=["acc"], verbose=1, mode='max')
  callbacks_list = [checkpoint]
  
  history = finetune_model.fit_generator(train_generator, epochs=NUM_EPOCHS, workers=8, 
                                         steps_per_epoch=steps_per_epoch, 
                                         shuffle=True, callbacks=callbacks_list)
  
  plot_training(history)
  
  ```
  
  **Tags:** machine-learning, deep-learning, classification, image-recognition, keras
  
  > **Comments:**
  > **username_1**: This sounds like a classic example of training data that is not representative of the input your network will be receiving. Since the network has only seen input data that is expected to produce a value of `1.00` at one of the nodes, after significant training that's what it will do for all inputs, because it would be punished if it ever did otherwise. This is a really common problem, that has many solutions, but this answer from a while back has some good suggestions on how to solve for input entirely out of the scope of the problem: https://stackoverflow.com/a/52831580/9546874  
  > **username_2**: Too add to @username_1's answer -- answer (2) in that link is good, but to make it work you also need some training data which doesn't contain logos! Since you have a softmax final layer, I guess your labels are (currently) vectors which are all 0 except for a single 1. In a training image with no logo, it will be *all zeros*.  
  > **username_0**: Thanks guys. As I was thinking myself, looks like I should really add another class "unknown". But the problem is how big it should be? We can not put whatever on earth as a non-logo class :-/  
  > **username_3**: @TinaJ Another alternative is to use an object *detection* model instead - one that answers the question "of the objects I know, how many appear in this image, and which?" compared to image classification, which instead asks "of the objects that I know, which is the one in this image most similar to?"  
  > **username_0**: @username_3 can you describe more?! What do you mean object detection model?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2019-11-05. Score: 1
  
  Your problem is a classification problem. If you are following the tutorial, it is using the ResNet50 network, which is a convolutional neural network with one fully connected layer at the end. At the end the activation function is softmax. Detailed description of the activation function can be found here: [Softmax function explained](https://medium.com/data-science-bootcamp/understand-the-softmax-function-in-minutes-f3a59641e86d)
  
  [![Udacity Deep Learning Slide on Softmax](https://i.sstatic.net/McVBq.png)](https://i.sstatic.net/McVBq.png)
  
  Basically, softmax increases the difference between the higher probability and lower probability. It also limits the output between 0 and 1.
  
  ## Problem Origin
  
  Due to the nature of the softmax function, it always chooses the best one and enlarge the value to be a value near one, even if the range of the output predictions is very small like 0-0.1. Also you training data only have the data of the 10 logos labelled, so if the network see unseen images with no logo it recognizes, it predicts the one with the most similarity. If you want to classify images with no logos in it, you should add an extra class in the training dataset and also the code to train the network to learn to classify images which is not in the 5 class into a separate maybe called unlabeled. Hope I can help you and have a nice day.
  
  > **Comments:**
  > **username_0**: Thanks anyways. As I was thinking myself, looks like I should really add another class "unknown". But the problem is how big it should be? We can not put whatever on earth as a non-logo class :-/  
  > **username_4**: You should try putting at least the same amount of unknown data as the other class each have. If it doesn't work, put more of the unknown data until it works. Experimenting and trying is the key. Hope it helps  
  > **username_5**: @TinaJ  You could use a GAN like logic to produce random non-logo images (without training the generator of course)  
  > **username_0**: @username_5 that's also interesting...hopefully if the images are not very random! Needs to be a bit realistic also. Any code repository or samples for that?  
  > **username_5**: @TinaJ There are tons of examples if you Google GAN. I'd recommend machinelearningmastery because he has the whole Keras code. If you want it to be somewhat realistic you could train the generator a bit so it's not just random.  
  
  ---
  
  