You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/01/10 00:53:17 UTC

[GitHub] gigasquid commented on a change in pull request #13769: Clojure example for fixed label-width captcha recognition

gigasquid commented on a change in pull request #13769:  Clojure example for fixed label-width captcha recognition 
URL: https://github.com/apache/incubator-mxnet/pull/13769#discussion_r246604276
 
 

 ##########
 File path: contrib/clojure-package/examples/captcha/src/captcha/train_ocr.clj
 ##########
 @@ -0,0 +1,156 @@
+;;
+;; Licensed to the Apache Software Foundation (ASF) under one or more
+;; contributor license agreements.  See the NOTICE file distributed with
+;; this work for additional information regarding copyright ownership.
+;; The ASF licenses this file to You under the Apache License, Version 2.0
+;; (the "License"); you may not use this file except in compliance with
+;; the License.  You may obtain a copy of the License at
+;;
+;;    http://www.apache.org/licenses/LICENSE-2.0
+;;
+;; Unless required by applicable law or agreed to in writing, software
+;; distributed under the License is distributed on an "AS IS" BASIS,
+;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;; See the License for the specific language governing permissions and
+;; limitations under the License.
+;;
+
+(ns captcha.train-ocr
+  (:require [captcha.consts :refer :all]
+            [clojure.java.io :as io]
+            [clojure.java.shell :refer [sh]]
+            [org.apache.clojure-mxnet.callback :as callback]
+            [org.apache.clojure-mxnet.context :as context]
+            [org.apache.clojure-mxnet.eval-metric :as eval-metric]
+            [org.apache.clojure-mxnet.initializer :as initializer]
+            [org.apache.clojure-mxnet.io :as mx-io]
+            [org.apache.clojure-mxnet.module :as m]
+            [org.apache.clojure-mxnet.ndarray :as ndarray]
+            [org.apache.clojure-mxnet.optimizer :as optimizer]
+            [org.apache.clojure-mxnet.symbol :as sym])
+  (:gen-class))
+
+(when-not (.exists (io/file "captcha_example/captcha_train.lst"))
+  (sh "./get_data.sh"))
+
+(defonce train-data
+  (mx-io/image-record-iter {:path-imgrec "captcha_example/captcha_train.rec"
+                            :path-imglist "captcha_example/captcha_train.lst"
+                            :batch-size batch-size
+                            :label-width label-width
+                            :data-shape data-shape
+                            :shuffle true
+                            :seed 42}))
+
+(defonce eval-data
+  (mx-io/image-record-iter {:path-imgrec "captcha_example/captcha_test.rec"
+                            :path-imglist "captcha_example/captcha_test.lst"
+                            :batch-size batch-size
+                            :label-width label-width
+                            :data-shape data-shape}))
+
+(defn accuracy
+  [label pred & {:keys [by-character]
+                 :or {by-character false} :as opts}]
+  (let [[nr nc] (ndarray/shape-vec label)
+        pred-context (ndarray/context pred)
+        label-t (-> label
+                    ndarray/transpose
+                    (ndarray/reshape [-1])
+                    (ndarray/as-in-context pred-context))
+        pred-label (ndarray/argmax pred 1)
+        matches (ndarray/equal label-t pred-label)
+        [digit-matches] (-> matches
+                            ndarray/sum
+                            ndarray/->vec)
+        [complete-matches] (-> matches
+                               (ndarray/reshape [nc nr])
+                               (ndarray/sum 0)
+                               (ndarray/equal label-width)
+                               ndarray/sum
+                               ndarray/->vec)]
+    (if by-character
+      (float (/ digit-matches nr nc))
+      (float (/ complete-matches nr)))))
+
+(defn get-data-symbol
+  []
+  (let [data (sym/variable "data")
+        ;; normalize the input pixels
+        scaled (sym/div (sym/- data 127) 128)
 
 Review comment:
   Great way to normalize the pixels 💯 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services