[incubator-mxnet] branch master updated: Extend Clojure BERT example (#15023)
This is an automated email from the ASF dual-hosted git repository. kedarb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git The following commit(s) were added to refs/heads/master by this push: new f44f6cf Extend Clojure BERT example (#15023) f44f6cf is described below commit f44f6cfbe752fd8b8036307cecf6a30a30ad8557 Author: Dave Liepmann AuthorDate: Sat Jun 22 19:35:45 2019 +0200 Extend Clojure BERT example (#15023) * Clojure predictor example: add rich comment This provides an entry point for folks working on this example in their REPL rather than the command line. * Clojure BERT example: refactor prepare-data fn for purity * Clojure BERT example: test fitted model on samples * Clojure BERT example: namespace docstring & comment * Clojure BERT example: format intro, add references * Clojure BERT example: minor refactor * Clojure BERT example: trim sentence pair explorations * Clojure BERT example: port experiment to iPynb * Clojure BERT example: fix test Underlying fn was refactored * Clojure BERT example: add sentence-pair prediction test --- .../examples/bert/fine-tune-bert.ipynb | 145 +++-- .../bert/src/bert/bert_sentence_classification.clj | 113 .../bert/bert_sentence_classification_test.clj | 24 +++- .../predictor/src/infer/predictor_example.clj | 6 + 4 files changed, 248 insertions(+), 40 deletions(-) diff --git a/contrib/clojure-package/examples/bert/fine-tune-bert.ipynb b/contrib/clojure-package/examples/bert/fine-tune-bert.ipynb index 425a999..5934477 100644 --- a/contrib/clojure-package/examples/bert/fine-tune-bert.ipynb +++ b/contrib/clojure-package/examples/bert/fine-tune-bert.ipynb @@ -10,15 +10,16 @@ "\n", "Pre-trained language representations have been shown to improve many downstream NLP tasks such as question answering, and natural language inference. To apply pre-trained representations to these tasks, there are two strategies:\n", "\n", -"feature-based approach, which uses the pre-trained representations as additional features to the downstream task.\n", -"fine-tuning based approach, which trains the downstream tasks by fine-tuning pre-trained parameters.\n", -"While feature-based approaches such as ELMo [3] (introduced in the previous tutorial) are effective in improving many downstream tasks, they require task-specific architectures. Devlin, Jacob, et al proposed BERT [1] (Bidirectional Encoder Representations from Transformers), which fine-tunes deep bidirectional representations on a wide range of tasks with minimal task-specific parameters, and obtained state- of-the-art results.\n", +" - **feature-based approach**, which uses the pre-trained representations as additional features to the downstream task.\n", +" - **fine-tuning based approach**, which trains the downstream tasks by fine-tuning pre-trained parameters.\n", +" \n", +"While feature-based approaches such as ELMo [1] are effective in improving many downstream tasks, they require task-specific architectures. Devlin, Jacob, et al proposed BERT [2] (Bidirectional Encoder Representations from Transformers), which fine-tunes deep bidirectional representations on a wide range of tasks with minimal task-specific parameters, and obtained state- of-the-art results.\n", "\n", "In this tutorial, we will focus on fine-tuning with the pre-trained BERT model to classify semantically equivalent sentence pairs. Specifically, we will:\n", "\n", -"load the state-of-the-art pre-trained BERT model and attach an additional layer for classification,\n", -"process and transform sentence pair data for the task at hand, and\n", -"fine-tune BERT model for sentence classification.\n", +" 1. load the state-of-the-art pre-trained BERT model and attach an additional layer for classification\n", +" 2. process and transform sentence pair data for the task at hand, and \n", +" 3. fine-tune BERT model for sentence classification.\n", "\n" ] }, @@ -59,6 +60,7 @@ "[org.apache.clojure-mxnet.callback :as callback]\n", "[org.apache.clojure-mxnet.context :as context]\n", "[org.apache.clojure-mxnet.dtype :as dtype]\n", +"[org.apache.clojure-mxnet.infer :as infer]\n", "[org.apache.clojure-mxnet.eval-metric :as eval-metric]\n", "[org.apache.clojure-mxnet.io :as mx-io]\n", "[org.apache.clojure-mxnet.layout :as layout]\n", @@
[incubator-mxnet] branch master updated: [clojure] clojurify function names in image.clj namespace (#15121)
This is an automated email from the ASF dual-hosted git repository. kedarb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git The following commit(s) were added to refs/heads/master by this push: new 28c528e [clojure] clojurify function names in image.clj namespace (#15121) 28c528e is described below commit 28c528e16be70b31287b65d949205396bbfec6e8 Author: Arthur Caillau AuthorDate: Tue Jun 4 15:37:04 2019 +0200 [clojure] clojurify function names in image.clj namespace (#15121) * [clojure] clojurify function names in image.clj namespace * move deprecated to the proper location for defn * rename color-flag to color and use :color :grayscale as values * add rm dest-path in with-file * change `color-flag` to `color` in `color->int` --- .../src/org/apache/clojure_mxnet/image.clj | 116 +++-- .../test/org/apache/clojure_mxnet/image_test.clj | 63 +++ 2 files changed, 151 insertions(+), 28 deletions(-) diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj index f81a358..68dcbfe 100644 --- a/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj +++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj @@ -17,6 +17,7 @@ (ns org.apache.clojure-mxnet.image "Image API of Clojure package." + (:refer-clojure :exclude [read]) (:require [t6.from-scala.core :refer [$ $$] :as $] [org.apache.clojure-mxnet.dtype :as dtype] [org.apache.clojure-mxnet.ndarray :as ndarray] @@ -38,8 +39,10 @@ (s/def ::decode-image-opts (s/keys :opt-un [::color-flag ::to-rgb ::output])) -(defn decode-image - "Decodes an image from an input stream with OpenCV +(defn ^:deprecated decode-image + "DEPRECATED: use `decode` instead. + + Decodes an image from an input stream with OpenCV `input-stream`: `InputStream` - Contains the binary encoded image `color-flag`: 0 or 1 - Convert decoded image to grayscale (0) or color (1) `to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB @@ -60,14 +63,47 @@ ([input-stream] (decode-image input-stream {}))) +(s/def ::color #{:grayscale :color}) +(s/def ::decode-image-opts-2 (s/keys :opt-un [::color ::to-rgb ::output])) + +(defn- color->int [color] + (case color +:grayscale 0 +:color 1)) + +(defn decode + "Decodes an image from an input stream with OpenCV. +`input-stream`: `InputStream` - Contains the binary encoded image +`color`: keyword in `#{:color :grayscale}` - Convert decoded image to + grayscale or color +`to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB +format (instead of opencv's default BGR) +`output`: nil or `NDArray` +returns: `NDArray` with dtype uint8 + + Ex: +(decode input-stream) +(decode input-stream {:color :color}) +(decode input-stream {:color :grayscale :output nd})" + ([input-stream {:keys [color to-rgb output] + :or {color :color to-rgb true output nil} + :as opts}] + (util/validate! ::input-stream input-stream "Invalid input stream") + (util/validate! ::decode-image-opts-2 opts "Invalid options for decoding") + (Image/imDecode input-stream (color->int color) to-rgb ($/option output))) + ([input-stream] + (decode input-stream {}))) + (s/def ::filename string?) (s/def ::optional-color-flag (s/or :none nil? :some ::color-flag)) (s/def ::optional-to-rgb (s/or :none nil? :some ::to-rgb)) -(defn read-image - "Reads an image file and returns an ndarray with OpenCV. It returns image in +(defn ^:deprecated read-image + "DEPRECATED: use `read` instead. + + Reads an image file and returns an ndarray with OpenCV. It returns image in RGB by default instead of OpenCV's default BGR. `filename`: string - Name of the image file to be loaded `color-flag`: 0 or 1 - Convert decoded image to grayscale (0) or color (1) @@ -95,11 +131,43 @@ ([filename] (read-image filename {}))) +(defn read + "Reads an image file and returns an ndarray with OpenCV. It returns image in + RGB by default instead of OpenCV's default BGR. +`filename`: string - Name of the image file to be loaded +`color`: keyword in `#{:color :grayscale}` - Convert decoded image to + grayscale or color +`to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB +format (instead of opencv's default BGR) +`output`: nil or `NDArray` +returns: `NDArray` with dtype uint8 + + Ex: + (read \"cat.jpg\") + (read \"cat.jpg\" {:color :grayscale}) + (read \"cat.jpg\" {:color :color :output nd})" + ([filename {:keys [color to-rgb output] + :or {c
[incubator-mxnet] branch master updated: [clojure] fix: image test does not rely on s3 to run (#15122)
This is an automated email from the ASF dual-hosted git repository. kedarb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git The following commit(s) were added to refs/heads/master by this push: new 52f77c9 [clojure] fix: image test does not rely on s3 to run (#15122) 52f77c9 is described below commit 52f77c971e8a6e5c8c6942345fd6cfd058377d69 Author: Arthur Caillau AuthorDate: Sun Jun 2 18:20:49 2019 +0200 [clojure] fix: image test does not rely on s3 to run (#15122) * [clojure] fix: image test does not rely on s3 to run * rename `with-image` to `with-file` --- .../test/org/apache/clojure_mxnet/image_test.clj | 34 +- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj index 23b88d0..b543b2d 100644 --- a/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj +++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj @@ -19,32 +19,40 @@ (:require [org.apache.clojure-mxnet.image :as image] [org.apache.clojure-mxnet.ndarray :as ndarray] [clojure.java.io :as io] -[clojure.test :refer :all]) +[clojure.test :refer [deftest is use-fixtures]]) (:import (javax.imageio ImageIO) (java.io File))) (def tmp-dir (System/getProperty "java.io.tmpdir")) (def image-path (.getAbsolutePath (io/file tmp-dir "Pug-Cookie.jpg"))) +(def image-src-path "test/test-images/Pug-Cookie.jpg") -(defn download-image [] - (with-open [in (io/input-stream "https://s3.amazonaws.com/model-server/inputs/Pug-Cookie.jpg;) - out (io/output-stream (io/file image-path))] +(defn- cp + "Copy from filepath `from` to filepath `to`." + [from to] + (with-open [in (io/input-stream (io/file from)) + out (io/output-stream (io/file to))] (io/copy in out))) -(defn delete-image [] - (io/delete-file image-path)) +(defn- rm + "Removes `filepath`." + [filepath] + (io/delete-file filepath)) -(defn with-downloaded-image [f] - (download-image) - (f) - (delete-image)) +(defn- with-file + "Provides `src-path` in `dest-path` for the test function `f` to use." + [src-path dest-path] + (fn [f] +(cp src-path dest-path) +(f) +(rm dest-path))) -(use-fixtures :once with-downloaded-image) +(use-fixtures :once (with-file image-src-path image-path)) (deftest test-decode-image - (let [img-arr (image/decode-image + (let [img-arr (image/decode-image (io/input-stream image-path)) -img-arr-2 (image/decode-image +img-arr-2 (image/decode-image (io/input-stream image-path) {:color-flag image/GRAYSCALE})] (is (= [576 1024 3] (ndarray/shape-vec img-arr)))