2

I am beginner to Julia though I have experience with Python and some other languages. I get that this is probably a very simple/beginner issue, but I fail to understand how it should work in Julia.

I want to create a Julia module. I saw recommendations to create it with PkgTemplates, so that is exactly what I have done. My directory structure is thus:

enter image description here

It is located at the default path proposed by PkgTemplates: /home/username/.julia/dev/Keras2Flux.

I want to develop it with Revise package due to the slow start-up time of the Julia REPL. However, I fail to import my module to the Julia REPL in the terminal.

So, I cd to the directory mentioned above, use julia command and try using Keras2Flux. I get the error:

ERROR: ArgumentError: Package Keras2Flux not found in current path:

I tried both using Keras2Flux and using Keras2Flux.jl, and I also tried to call it from one level above in my directory structure (i.e. /home/username/.julia/dev). All has the same problem.

What is wrong (more importantly, why?) and how to fix it?

Current contents of the module (not really relevant to the question but still):

module Keras2Flux

import JSON
using Flux

export convert

function create_dense(config)
    in = config["input_dim"]
    out = config["output_dim"]
    dense = Dense(in, outо)
    return dense
end

function create_dropout(config)
    p = config["p"]
    dropout = Dropout(p)
    return dropout
end

function create_model(model_config)
    layers = []
    for layer_config in model_config
        if layer_config["class_name"] == "Dense"
            layer = create_dense(layer_config["config"])
        elseif layer_config["class_name"] == "Dropout"
            layer = create_dropout(layer_config["config"])
        else
            println(layer_config["class_name"])
            throw("unimplemented")
        end
        push!(layers, layer)
    end
    model = Chain(layers)
end

function convert(filename)
    jsontxt = ""
    open(filename, "r") do f
        jsontxt = read(f, String)  
    end
    model_params = JSON.parse(jsontxt)  
    if model_params["keras_version"] == "1.1.0"
        create_model(model_params["config"])
    else
        throw("unimplemented")
    end
end

end
Valeria
  • 1,508
  • 4
  • 20
  • 44

1 Answers1

4

Here is a full recipe to get you going:

cd("/home/username/.julia/dev")
using Pkg
pkg"generate Keras2Flux"
cd("Keras2Flux")
pkg"activate ."
pkg"add JSON Flux"
# now copy-paste whatever you need to Keras2Flux\src\Keras2Flux.jl
using Revise
using Keras2Flux
# happy development!
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • That worked indeed. Thank you. On a side note, how can I then import this module to the other script? I tried to follow instructions from https://stackoverflow.com/questions/37200025/how-to-import-custom-module-in-julia (namely, `include("../src/Keras2Flux.jl")` and `using Keras2Flux`, but I get `Package Keras2Flux not found in current path` error despite using the `include`. – Valeria Jun 17 '20 at 12:56
  • 1
    `pkg"activate /home/username/.julia/dev/Keras2Flux";using Keras2Flux`. The problem starts when you have more than one unregistered module. Basically, in such situation the best thing is to keep each module as a separate GitHub repo - but if you need that it is a subject for a new question. – Przemyslaw Szufel Jun 17 '20 at 15:21
  • So, in Julia it would not be a good workflow to use unregistered modules? In Python, I'd just add their locations to the path in case I don't want to keep it as a separate repo. – Valeria Jun 18 '20 at 09:23
  • The good workflow would be to have each module as a GitHub repo either private or public. The workflow with public repos is obviously more convenient/easier. With registered modules it is much easier to manage dependencies if there are more of them. – Przemyslaw Szufel Jun 18 '20 at 11:08
  • If you are a software house and developing proprietary non Open Source Julia solutions than you need to setup your own Package Registry https://julialang.github.io/Pkg.jl/v1.1/registries/ As far as I know JuliaPro paid tools can decrease your effort in doing that (but are not strictly required) – Przemyslaw Szufel Jun 18 '20 at 11:10
  • Thank you for the answers. I guess I will just setup a public GitHub repo, as it is just some small project I am doing to learn Julia. – Valeria Jun 18 '20 at 11:12