3

I was able to create a docker environment and then follow this thread I have a high-performant function written in Julia, how can I use it from Python? ..but it seems the suggestion that is made there only works for pure Julia and pure Python projects.

More concretely I created a function diff(img,magnitude) in Julia that changes an image in a certain way and I wanted to implement it into a PyTorch learning algorithm, such that images (let's say from CIFER) would first be changed by the Julia file and then be learned. At the moment I am running into the problem that the Python file does not execute functions with dependencies (Below is the example):

#MyPackage.jl
        
module MyPackage
    
using TestImages
    
function img()
    return imresize(testimage("mandrill"),32,32)
end

end

And in my Jypiter-Notebook I have:

#main.py
!pip install julia

import julia
julia.install()

from julia.api import Julia
jl = Julia(compiled_modules=False)

from julia import Pkg
Pkg.activate("MyPackage")

from julia import MyPackage
MyPackage.img()

When I try to run the Julia function from the Python file I receive this error: enter image description here

D. Sukhov
  • 361
  • 1
  • 5
  • what do you mean by "pure Python projects". The link you mentioned explains how to call a Julia function from Python. Maybe you could make a MWE showing what you need? – Przemyslaw Szufel May 22 '22 at 14:25
  • Please replace the image of text with the actual text, formatted as a code sample. This ensures that your question remains readable in the future (by avoiding problems when image links expire), that it is more discoverable (error messages in the text can be found using searches), and more accessible (people with screen readers can read your question). – larsks May 22 '22 at 15:17
  • You are citing "package" but all you need is a simpler module. So, in a file `MyModule.jl`write your `module MyModule... end`, then, in Python, type `from julia import Main`, `Main.include("MyModule.jl")` to load your module (but actulally you could skip even it and just put code statements), `Main.eval("using .MyModule")` to bring the functions defined there into scope and then `Main.img(...)` to actually evaluate the function defined. [More](https://sylvaticus.github.io/SPMLJ/stable/01_-_JULIA1_-_Basic_Julia_programming/0106_-_Further_topics.html#Running-Julia-libraries-and-code-in-Python) – Antonello May 22 '22 at 20:40

1 Answers1

3

With the caveat that I know absolutely nothing about Julia, it looks like imresize is part of the ImageTransformations package, which I think means your Julia code needs:

using ImageTransformations, TestImages

Instead of:

using TestImages

Update

...which means, upon further research, that you also need to add the ImageTransformations package as a dependency to MyPackage. There may be a different way of doing this, but this seemed to work for me:

$ cd MyPackage
$ julia
[...]
(@v1.7) pkg> activate .
  Activating project at `~/tmp/python/MyPackage`

(MyPackage) pkg> add ImageTransformations
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Most likely D. Sukhov was using the package `Images` not `ImageTransformations`. Other than that your answer looks to be perfectly correct. – Przemyslaw Szufel May 22 '22 at 15:40