Skip to content

Commit

Permalink
merge with libpython-clj-update
Browse files Browse the repository at this point in the history
Libpython clj update to 1.32
  • Loading branch information
rybandrei2014 committed Jan 23, 2020
2 parents a561af3 + 2951b51 commit 990c00f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.1.3-ALPHA
### Changed
- changes due to update of libpython-clj to version 1.32
- updated README.md

## 0.1.2-ALPHA
### Changed
- changed arguments order in separate and write-estimates funcs
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,37 @@ pip3 install -r requirements.txt

* add dependency to the project.clj
```clj
[dragoon000320/open-unmix-pytorch-clj "0.1.2-SNAPSHOT"]
[dragoon000320/open-unmix-pytorch-clj "0.1.3-ALPHA"]
```

## Usage

A little demo how to use it
```clj
;; require namespaces
(require '[open-unmix-pytorch-clj.core :as core])
(require '[open-unmix-pytorch-clj.core :refer :all])

(require '[open-unmix-pytorch-clj.io :as oup.io])
(require '[open-unmix-pytorch-clj.io :refer :all])

(require '[open-unmix-pytorch-clj.convert :as conv])
(require '[open-unmix-pytorch-clj.convert :refer :all])

(-> "your-audio-file.wav"
;; reads audio file
oup.io/soundfile-read
soundfile-read
;; converts audio data to 2 channel one
conv/->2-channels
->2-channels
;; separates audio data into required audio sources
(core/separate ["vocals" "drums" "other" "bass"])
(separate ["vocals" "drums" "other" "bass"]
:device "cpu"
;; or if you have cuda enabled uncomment line below
;; :device "cuda")
;; writes estimates for each audio source to the output directory
(oup.io/writes-estimates "out-dir"))
(writes-estimates "out-dir"))
```

## Disclaimer

This is __early alpha__, current state of library is described below:
The library is in __alpha__ at the moment, current state of library is described below:
* for now only __separation__ of audio source implemented
* so there is no implementation of __model training__
* performance must be further improved
Expand Down
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(defproject dragoon/open-unmix-pytorch-clj "0.1.2-ALPHA"
(defproject dragoon/open-unmix-pytorch-clj "0.1.3-ALPHA"
:description "Clojure bindings for PyTorch implementation of Open-Unmix"
:url "https://github.com/dragoon000320/open-unmix-pytorch-clj"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.1"]
[cnuernber/libpython-clj "1.30"]]
[cnuernber/libpython-clj "1.32"]]
:plugins [[lein-cloverage "1.0.13"]
[lein-shell "0.5.0"]
[lein-ancient "0.6.15"]
Expand Down
5 changes: 3 additions & 2 deletions src/open_unmix_pytorch_clj/convert.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
Encapsulates functionality for conversions
"
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :refer [$a get-item]]
[libpython-clj.python :refer [py.. get-item]]
[open-unmix-pytorch-clj.validator :refer [is-ndarray?]]
[open-unmix-pytorch-clj.model :refer [AudioMap?]]))

(require-python '[numpy :as np])
(require-python '[scipy.signal :as sig])
(require-python '[builtins :refer [slice]])

Expand Down Expand Up @@ -36,7 +37,7 @@
:or {sample-rate 44100.0, window "hann"
n-fft 4096, n-hopsize 1024}}]
{:pre [(is-ndarray? X)]}
(let [Zxx ($a X "__truediv__" (/ n-fft 2))
(let [Zxx (py.. X (__truediv__ (/ n-fft 2)))
n-overlap (- n-fft n-hopsize)]
(sig/istft Zxx
sample-rate
Expand Down
47 changes: 24 additions & 23 deletions src/open_unmix_pytorch_clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
(:require [open-unmix-pytorch-clj.model :refer :all]
[open-unmix-pytorch-clj.validator :refer :all]
[libpython-clj.require :refer [require-python]]
[libpython-clj.python :refer [$. $.. $a get-item]]
[libpython-clj.python :refer [py. py.. get-item]]
[open-unmix-pytorch-clj.convert :refer [istft]]))

(require-python '[numpy :as np])
(require-python '[norbert])
(require-python '[torch])
(require-python '[torch.hub])
Expand Down Expand Up @@ -57,11 +58,11 @@
use-softmask (not= alpha 1.0)
;; convert numpy audio data to torch
audio-torch (-> audio-data
($. T)
(py.. -T)
(get-item [nil Ellipsis])
torch/tensor
($a float)
($a "to" device))
(py.. (float))
(py.. (to device)))
;; for each target load an open-unmix-pytorch model
unmix-targets (mapv #(torch.hub/load
"sigsep/open-unmix-pytorch"
Expand All @@ -74,29 +75,29 @@
(= (count targets) 1) (conj "accompaniment")
residual-model (conj "residual"))
X (-> first-unmix
($a stft audio-torch)
($a detach)
($a cpu)
($a numpy)
(py.. (stft audio-torch))
(py.. (detach))
(py.. (cpu))
(py.. (numpy))
;; converts to complex numpy type
(as-> x
; X = X[..., 0] + X[..., 1]*1j
($a (get-item x [Ellipsis 0])
"__add__"
($a (get-item x [Ellipsis 1])
"__mul__"
(complex 0 1))))
(py.. (get-item x [Ellipsis 0])
(__add__
(py.. (get-item x [Ellipsis 1])
(__mul__
(complex 0 1))))))
(get-item [0])
($a transpose [2 1 0]))
(py.. (transpose [2 1 0])))
V (cond->
(-> (mapv #(-> audio-torch
%
($a cpu)
($a detach)
($a numpy)
(py.. (cpu))
(py.. (detach))
(py.. (numpy))
(cond->
;; exponentiate the model if use a softmask V**alpha
use-softmask ($a "__pow__" alpha))
use-softmask (py.. (__pow__ alpha)))
;; remove sample dim V[:, 0, ...]
(get-item [(slice nil) 0 Ellipsis]))
unmix-targets)
Expand All @@ -107,7 +108,7 @@
(= (count targets) 1))
(norbert/residual_model X :alpha alpha))
Y (norbert/wiener V
($a X astype np/complex128)
(py.. X (astype np/complex128))
niter
use-softmask)]
(->SeparationResult
Expand All @@ -117,11 +118,11 @@
[(keyword target-name)
(-> Y
(get-item [Ellipsis idx])
($. T)
(py.. -T)
(istft :sample-rate sample-rate
:n-fft ($.. first-unmix stft n_fft)
:n-hopsize ($.. first-unmix stft n_hop))
:n-fft (py.. first-unmix -stft -n_fft)
:n-hopsize (py.. first-unmix -stft -n_hop))
last
($. T))]))
(py.. -T))]))
(into {}))
sample-rate)))
4 changes: 2 additions & 2 deletions src/open_unmix_pytorch_clj/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(:require [open-unmix-pytorch-clj.model :refer :all]
[open-unmix-pytorch-clj.validator :refer :all]
[libpython-clj.require :refer [require-python]]
[libpython-clj.python :refer [$.]]
[libpython-clj.python :refer [py..]]
[clojure.zip :as zip]))

(require-python '[soundfile :as sf])
Expand Down Expand Up @@ -75,5 +75,5 @@
"
[file-path]
(let [[audio sample-rate] (sf/read file-path :always_2d true)
shape ($. audio shape)]
shape (py.. audio -shape)]
(->AudioMap audio sample-rate (first shape) (last shape))))

0 comments on commit 990c00f

Please sign in to comment.