title: captcha recognition using tensorflow
body: <p>I'm trying to learn captcha recognition using CNN, there are a example I'm studying that is captcha recognition using tensor flow. But I have some questions about the example.</p>
  
  <p>In this case, using captcha generator, we can get the text and image about captcha </p>
  
  <h1>text, image = gen_captcha_text_and_image()</h1>
  
  <pre><code>def get_next_batch(batch_size = 128):
      batch_x = np.zeros([batch_size, IMAGE_HEIGHT*IMAGE_WIDTH])
      batch_y = np.zeros([batch_size, MAX_CAPTCHA*CHAR_SET_LEN])
  
  
      def wrap_gen_captcha_text_and_image():
          while True:
              text, image = gen_captcha_text_and_image()
              if image.shape == (60, 160, 3):
                  return text, image
  
      for i in range(batch_size):
          text, image = wrap_gen_captcha_text_and_image()
          image = convert2gray(image)
  
          batch_x[i,:] = image.flatten() / 255
          batch_y[i,:] = text2vec(text)
  
  
      return batch_x, batch_y
  </code></pre>
  
  <h1>Training</h1>
  
  <p>when computing the recognition accuracy, need batch_x and batch_y</p>
  
  <pre><code>def train_crack_captcha_cnn():
  
      output = crack_captcha_cnn()
  
      loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y))
  
      optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
  
      predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN])
      max_idx_p = tf.argmax(predict, 2)
      max_idx_l = tf.argmax(tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
      correct_pred = tf.equal(max_idx_p, max_idx_l)
      accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  
      saver = tf.train.Saver()
      with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
  
          step = 0
          while True:
              batch_x, batch_y = get_next_batch(64)
              _, loss_ = sess.run([optimizer, loss], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.75})
              print(step, loss_)
  
  
              if step % 100 == 0:
                  batch_x_test, batch_y_test = get_next_batch(100)
                  acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.})
                  print(step, acc)
              # when accuracy &gt;80%, save model, finish train
                  if acc &gt; 0.8:
                      saver.save(sess, "crack_capcha.model", global_step=step)
                      break
              step += 1
  </code></pre>
  
  <p>the result is</p>
  
  <pre><code>6291 0.0297899
  6292 0.024521
  6293 0.0278097
  6294 0.0307284
  6295 0.027667
  6296 0.026684
  6297 0.0264348
  6298 0.0280007
  6299 0.0298581
  6300 0.0278418
  6300 0.815
  </code></pre>
  
  <p><strong>But when I get big training data from internet, not using the captcha generator create.</strong> </p>
  
  <pre><code>batch_y[i,:] = text2vec(text)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/eeUL1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eeUL1.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/NDsy0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NDsy0.jpg" alt="enter image description here"></a></p>
  
  <h1>can't get the text in the captcha, batch_y[i,:] is null.</h1>
  
  <p>when computing the accuracy
  the result is </p>
  
  <pre><code>0 0.69474
  0 0.0
  1 0.66162
  2 0.605672
  3 0.509237
  4 0.367824
  5 0.201136
  6 0.0657851
  7 0.0105976
  8 0.000712345
  9 2.42625e-05
  10 4.68649e-07
  11 4.37849e-09
  12 1.94186e-11
  13 9.17299e-14
  14 2.86322e-16
  15 6.33826e-19
  16 1.34238e-21
  17 1.64283e-24
  18 4.54483e-27
  19 4.26165e-30
  20 7.20557e-33
  21 4.39262e-35
  22 1.08497e-37
  23 0.0
  24 0.0
  25 0.0
  ...
  n  0.0
  </code></pre>
  
  <p>There are no text about captcha, so text2vec() is not useful. Then the accuracy can't be computed...</p>
  
  <hr>
  
  <p>I tried to use training model from captcha generator to identify captcha from web. The result is terrible!</p>
  
  <p><strong>The following is captcha from captcha generator</strong>
  <a href="https://i.stack.imgur.com/70egm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/70egm.png" alt="enter image description here"></a>
  <strong>convert2gray</strong>
  <a href="https://i.stack.imgur.com/9vlfV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9vlfV.png" alt="enter image description here"></a>
  Training model can identify success! </p>
  
  <p><strong>However, the captcha from the web</strong>
  <a href="https://i.stack.imgur.com/FAKGw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FAKGw.png" alt="enter image description here"></a>
  <strong>resize the image from (25, 52) to (60, 160)</strong>
  <a href="https://i.stack.imgur.com/OGY4Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OGY4Q.png" alt="enter image description here"></a>
  <strong>convert2gray</strong>
  <a href="https://i.stack.imgur.com/ycrZf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ycrZf.png" alt="enter image description here"></a></p>
  
  <p>It can't be identify success !</p>