Skip to content

Instantly share code, notes, and snippets.

View gigasquid's full-sized avatar

Carin Meier gigasquid

View GitHub Profile
@gigasquid
gigasquid / seaborn-data.clj
Created February 10, 2020 17:27
seaborn-data
(ns gigasquid.seaborn
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py :refer [py. py.. py.-]]
[gigasquid.plot :as plot]
[clojure.data.csv :as csv]))
(require-python '[seaborn :as sns])
(require-python '[matplotlib.pyplot :as pyplot])
(require-python '[pandas :as pandas])
@gigasquid
gigasquid / gpt2.clj
Created January 10, 2020 21:45
GPT2 with libpython-clj
(ns gigasquid.gpt2
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py]))
;https://huggingface.co/transformers/quickstart.html - OpenAI GPT-2
(require-python '(transformers))
(require-python '(torch))
@gigasquid
gigasquid / mxnet.clj
Last active January 14, 2020 19:43
libpython-clj-mxnet
(ns gigasquid.mxnet
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py]
[clojure.string :as string]))
(require-python '(mxnet mxnet.ndarray mxnet.module mxnet.io))
(require-python '(mxnet.test_utils))
(require-python '(mxnet.initializer))
(require-python '(mxnet.metric))
(require-python '(mxnet.symbol))
@gigasquid
gigasquid / mxnet-deps.sh
Last active January 28, 2019 23:34
Next Journal Deps for Clojure MXNet
#!/usr/bin/env bash
sudo apt-get update || true
sudo apt-get install -y build-essential
sudo apt-get install -y software-properties-common
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y libopenblas-dev
sudo apt-get install -y libcurl3
sudo add-apt-repository ppa:timsc/opencv-3.4
sudo apt-get update
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] MXNet Scala Package - Parent
[INFO] MXNet Scala Package - Initializer
[INFO] MXNet Scala Package - Initializer Native
[INFO] MXNet Scala Package - Macros
[INFO] MXNet Scala Package - Native
[INFO] MXNet Scala Package - Core
(ns dev.generator
(:require [clojure.reflect :as r])
(:import (org.apache.mxnet NDArray Symbol))
(:gen-class))
(->> (:members (r/reflect (Symbol/api)))
(filter #(clojure.string/includes? (:name %) "FullyConnected")))
;;=> output
(as-> (sym/variable "data") data
(sym/fully-connected "fc1" {:data data :num-hidden 128})
(sym/activation "relu1" {:data data :act-type "relu"})
(sym/fully-connected "fc2" {:data data :num-hidden 64})
(sym/activation "relu2" {:data data :act-type "relu"})
(sym/fully-connected "fc3" {:data data :num-hidden 10})
(sym/softmax-output "softmax" {:data data}))
(predict "https://upload.wikimedia.org/wikipedia/commons/0/0c/American_Shorthair.jpg" true)
;; ({:prob 0.294283, :label "n02123045 tabby, tabby cat"}
;; {:prob 0.25543088, :label "n02122878 tabby, queen"}
;; {:prob 0.18031825, :label "n02123159 tiger cat"}
;; {:prob 0.06245274, :label "n01318894 pet"}
;; {:prob 0.04261507, :label "n02120997 feline, felid"})
(defn predict [img-url show?]
(let [mod (m/load-checkpoint {:prefix (str model-dir "/resnet-152") :epoch 0})
labels (-> (slurp (str model-dir "/synset.txt"))
(string/split #"\n"))
nd-img (get-image img-url show?)
prob (-> mod
(m/bind {:for-training false
:data-shapes [{:name "data" :shape [1 num-channels h w]}]})
(m/forward {:data [nd-img]})
(m/outputs)
(ns graal-test.core
(:import (org.graalvm.polyglot Context)))
(def py-context (Context/create (into-array ["python"])))
(.eval py-context "python" "print('Hello polyglot world Python!');")
(.eval py-context "python" "
import time;
time.clock()
") ;=> #object[org.graalvm.polyglot.Value 0x4a6b3b70 "1.508202803249E9"]