Answers.Body: <p>I am assuming the question you are asking is how to prevent over-fitting on the maximum accuracy. Your graph does show that your model over-fits. </p>
  
  <p>There is a couple of different methods to prevent over-fitting from happening. You can specify training to stop after a certain amount of epochs. In your case it seems to be 2 or 3 epochs. Take care as a new initialization might require more or less epochs to reach optimal accuracy thus it is required run your model a couple of times to determine the correct amount of epochs.</p>
  
  <p>You can also specify the accuracy you want your model to reach before you stop training, this can be dangerous as a local-minima could be found below your expected accuracy thus resulting in your training to run indefinitely. Also your model could have become more accurate then your expected accuracy but stopped prematurely due to the model reaching your stopping-condition.</p>
  
  <p>You can combine the two. Terminate training once it reach your expected accuracy otherwise terminate it if it reaches a certain amount of epochs. This way you prevent your training from happening indefinitely.</p>
  
  <p>In Tensorflow you would want to build your own custom training seen <a href="https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough" rel="nofollow noreferrer">here</a></p>
  
  <p>The method you are interested is </p>
  
  <pre><code>epoch_accuracy = tfe.metrics.Accuracy()
  </code></pre>
  
  <p>In their example they get the accuracy after each epoch, you would want to create your own batches and apply it there. </p>
  
  <p>If you really want to dive deeper, you can implement methods for the model to detect when it starts over-fitting such as looking at the standard deviation of the accuracy across a certain measure (usually epochs but your model starts over-fitting after 5 so its not a good measure in your case, maybe do standard deviation over a certain amount of batches trained) if the accuracy leaves the standard deviation space then it might be good idea to stop training. The problem with this technique is that your model has to over-fit a bit to know it needs to stop. The other problem is that your model might seem it is over-fitting but was just a dip in accuracy and was stopped prematurely. (There is methods around this but can't recall them) </p>
  
  <p>Another technique is to add drop out. This introduce noise and pushes your model out of a local-minima. Also forces nodes that was ignored to be used and optimized. This surprisingly works well but is not a surefire way to prevent over-fitting. This technique is built into Tensorflow and don't need custom training. You can find more reading about Tensorflow dropout <a href="https://www.tensorflow.org/tutorials/keras/overfit_and_underfit" rel="nofollow noreferrer">here</a>, also they have more techniques on how to overcome over-fitting but their solutions is to change the model and your model seems to be fine. </p>
  
  <pre><code>tf.keras.layers.Dropout(0.2),
  </code></pre>
  
  <p>These are just some techniques I listed but there is a lot more out there. Unfortunately there is no concrete method to prevent it but steps can be taken to stop well before over-fitting occurs. The first two techniques are mostly used together.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-09T13:34:27.290Z
Answers.Text: Thanks for your response, yes you are right,  my model is overfitted, I will try your ways for preventing overfitting , and you mean is my dataset okay ? Can I also prevent overfitting with add some noise or blur to my dataset ?
Answers.UserDisplayName: Shayan Tabatabaee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T08:08:00.050Z
Answers.Text: I am not to sure what the impact would be. If I have to predict the outcome it might rip your weights out of a local minima but bring down overall accuracy.
Answers.UserDisplayName: SandMan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-09T12:18:20.373Z
Answers.LastActivityDate: 2019-09-09T12:35:17.263Z
Answers.LastEditDate: 2019-09-09T12:35:17.263Z
Answers.UserDisplayName: SandMan
Body: <p>Currently I'm working on a project for scanning credit card and text extraction from cards. So first of all I decided to preprocess my images with some filters like thresholding, dilation and some other stuff. But it was not successfully for OCR of every credit cards. So I learned a lot and I found a solution like <a href="https://matthewearl.github.io/2016/05/06/cnn-anpr/" rel="nofollow noreferrer">this</a> for number plate recognition that is very similar to my project.
  In the first step I want to generate a random dataset like my cards to locate card number region, and for every card that I've generated I cropped two images that one of them has numbers and another has not. I generated 2000 images for every cards.</p>
  
  <p>so I have some images like this:</p>
  
  <p><a href="https://i.sstatic.net/H4Bm3.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/H4Bm3.jpg" alt="enter image description here"></a> (does not have numbers)
  <a href="https://i.sstatic.net/mcqvc.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mcqvc.jpg" alt="enter image description here"></a> (has numbers)</p>
  
  <p>And after generating my dataset I used this model with tensorflow to train my network.</p>
  
  <pre><code>    model = models.Sequential()
      model.add(layers.Conv2D(8, (5, 5), padding='same', activation='relu', input_shape=(30, 300, 3)))
      model.add(layers.MaxPooling2D((2, 2)))
  
      model.add(layers.Conv2D(16, (5, 5), padding='same', activation='relu'))
      model.add(layers.MaxPooling2D((2, 2)))
  
      model.add(layers.Conv2D(32, (5, 5), padding='same', activation='relu'))
      model.add(layers.MaxPooling2D((2, 2)))
  
      model.add(layers.Flatten())
      model.add(layers.Dense(512, activation='relu'))
      model.add(layers.Dense(2, activation='softmax'))
  </code></pre>
  
  <p>Here is my plot for 5 epochs. </p>
  
  <p><a href="https://i.sstatic.net/zbPS4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zbPS4.png" alt="enter image description here"></a>
  <a href="https://i.sstatic.net/RqFVy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/RqFVy.png" alt="enter image description here"></a></p>
  
  <p>I almost get 99.5% of accuracy and It seems to be wrong, I think I have kind of overfitting in my data. Does it work correctly or my model is overfitted ? And how can I generate dataset for this purpose ?</p>
  
Tags: <neural-networks><convolutional-neural-networks><tensorflow><datasets><optical-character-recognition>
Title: Generate credit cards dataset for locating number region
UserDisplayName: Shayan Tabatabaee
ThreadText: # Generate credit cards dataset for locating number region
  
  (Asked by: username_0 on 2019-09-09)
  
  Currently I'm working on a project for scanning credit card and text extraction from cards. So first of all I decided to preprocess my images with some filters like thresholding, dilation and some other stuff. But it was not successfully for OCR of every credit cards. So I learned a lot and I found a solution like [this](https://matthewearl.github.io/2016/05/06/cnn-anpr/) for number plate recognition that is very similar to my project.
  In the first step I want to generate a random dataset like my cards to locate card number region, and for every card that I've generated I cropped two images that one of them has numbers and another has not. I generated 2000 images for every cards.
  
  so I have some images like this:
  
  [![enter image description here](https://i.sstatic.net/H4Bm3.jpg)](https://i.sstatic.net/H4Bm3.jpg) (does not have numbers)
  [![enter image description here](https://i.sstatic.net/mcqvc.jpg)](https://i.sstatic.net/mcqvc.jpg) (has numbers)
  
  And after generating my dataset I used this model with tensorflow to train my network.
  
  ```
      model = models.Sequential()
      model.add(layers.Conv2D(8, (5, 5), padding='same', activation='relu', input_shape=(30, 300, 3)))
      model.add(layers.MaxPooling2D((2, 2)))
  
      model.add(layers.Conv2D(16, (5, 5), padding='same', activation='relu'))
      model.add(layers.MaxPooling2D((2, 2)))
  
      model.add(layers.Conv2D(32, (5, 5), padding='same', activation='relu'))
      model.add(layers.MaxPooling2D((2, 2)))
  
      model.add(layers.Flatten())
      model.add(layers.Dense(512, activation='relu'))
      model.add(layers.Dense(2, activation='softmax'))
  
  ```
  
  Here is my plot for 5 epochs.
  
  [![enter image description here](https://i.sstatic.net/zbPS4.png)](https://i.sstatic.net/zbPS4.png)
  [![enter image description here](https://i.sstatic.net/RqFVy.png)](https://i.sstatic.net/RqFVy.png)
  
  I almost get 99.5% of accuracy and It seems to be wrong, I think I have kind of overfitting in my data. Does it work correctly or my model is overfitted ? And how can I generate dataset for this purpose ?
  
  **Tags:** neural-networks, convolutional-neural-networks, tensorflow, datasets, optical-character-recognition
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-09-09. Score: 2 | Accepted answer
  
  I am assuming the question you are asking is how to prevent over-fitting on the maximum accuracy. Your graph does show that your model over-fits.
  
  There is a couple of different methods to prevent over-fitting from happening. You can specify training to stop after a certain amount of epochs. In your case it seems to be 2 or 3 epochs. Take care as a new initialization might require more or less epochs to reach optimal accuracy thus it is required run your model a couple of times to determine the correct amount of epochs.
  
  You can also specify the accuracy you want your model to reach before you stop training, this can be dangerous as a local-minima could be found below your expected accuracy thus resulting in your training to run indefinitely. Also your model could have become more accurate then your expected accuracy but stopped prematurely due to the model reaching your stopping-condition.
  
  You can combine the two. Terminate training once it reach your expected accuracy otherwise terminate it if it reaches a certain amount of epochs. This way you prevent your training from happening indefinitely.
  
  In Tensorflow you would want to build your own custom training seen [here](https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough)
  
  The method you are interested is
  
  ```
  epoch_accuracy = tfe.metrics.Accuracy()
  
  ```
  
  In their example they get the accuracy after each epoch, you would want to create your own batches and apply it there.
  
  If you really want to dive deeper, you can implement methods for the model to detect when it starts over-fitting such as looking at the standard deviation of the accuracy across a certain measure (usually epochs but your model starts over-fitting after 5 so its not a good measure in your case, maybe do standard deviation over a certain amount of batches trained) if the accuracy leaves the standard deviation space then it might be good idea to stop training. The problem with this technique is that your model has to over-fit a bit to know it needs to stop. The other problem is that your model might seem it is over-fitting but was just a dip in accuracy and was stopped prematurely. (There is methods around this but can't recall them)
  
  Another technique is to add drop out. This introduce noise and pushes your model out of a local-minima. Also forces nodes that was ignored to be used and optimized. This surprisingly works well but is not a surefire way to prevent over-fitting. This technique is built into Tensorflow and don't need custom training. You can find more reading about Tensorflow dropout [here](https://www.tensorflow.org/tutorials/keras/overfit_and_underfit), also they have more techniques on how to overcome over-fitting but their solutions is to change the model and your model seems to be fine.
  
  ```
  tf.keras.layers.Dropout(0.2),
  
  ```
  
  These are just some techniques I listed but there is a lot more out there. Unfortunately there is no concrete method to prevent it but steps can be taken to stop well before over-fitting occurs. The first two techniques are mostly used together.
  
  > **Comments:**
  > **username_0**: Thanks for your response, yes you are right,  my model is overfitted, I will try your ways for preventing overfitting , and you mean is my dataset okay ? Can I also prevent overfitting with add some noise or blur to my dataset ?  
  > **username_1**: I am not to sure what the impact would be. If I have to predict the outcome it might rip your weights out of a local minima but bring down overall accuracy.  
  
  ---
  
  