Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bad choice of "window_samples" crashes PyCall #4

Open
ifmelchor opened this issue Apr 24, 2023 · 1 comment
Open

A bad choice of "window_samples" crashes PyCall #4

ifmelchor opened this issue Apr 24, 2023 · 1 comment

Comments

@ifmelchor
Copy link

Hi,

I executed the following code:

using SeisIO
using PhaseNet

S = read_data("mseed", MSEED_FILE) # MSEED_FILE is a three-component miniSEED file of less than 5 minutes of duration
detrend!(S)
sync!(S)

window_samples = 100
batch_size = 256
X = seisdata2torch(S,window_samples)
model_P = load_model("P")
probs_P = detect(X,model_P,batch_size)

And I have this error:

PyError ($(Expr(:escape, :(ccall(#= /home/ivan/.julia/packages/PyCall/twYvK/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'RuntimeError'>
RuntimeError('Sizes of tensors must match except in dimension 1. Expected size 15 but got size 14 for tensor number 1 in the list.')
  File "/home/ivan/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "PyCall", line 1, in <lambda>

Stacktrace:
  [1] pyerr_check
    @ ~/.julia/packages/PyCall/twYvK/src/exception.jl:75 [inlined]
  [2] pyerr_check
    @ ~/.julia/packages/PyCall/twYvK/src/exception.jl:79 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall ~/.julia/packages/PyCall/twYvK/src/exception.jl:96
  [4] macro expansion
    @ ~/.julia/packages/PyCall/twYvK/src/exception.jl:110 [inlined]
  [5] #107
    @ ~/.julia/packages/PyCall/twYvK/src/pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ ./c.jl:473 [inlined]
  [7] __pycall!
    @ ~/.julia/packages/PyCall/twYvK/src/pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{PyCall.PyObject}, nargs::Int64, kw::Ptr{Nothing})
    @ PyCall ~/.julia/packages/PyCall/twYvK/src/pyfncall.jl:29
  [9] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{PyCall.PyObject}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ PyCall ~/.julia/packages/PyCall/twYvK/src/pyfncall.jl:11
 [10] (::PyCall.PyObject)(::PyCall.PyObject, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ PyCall ~/.julia/packages/PyCall/twYvK/src/pyfncall.jl:86
 [11] (::PyCall.PyObject)(::PyCall.PyObject, ::Vararg{Any})
    @ PyCall ~/.julia/packages/PyCall/twYvK/src/pyfncall.jl:86
 [12] macro expansion
    @ ~/.julia/packages/PhaseNet/ZNXkE/src/detection.jl:120 [inlined]
 [13] macro expansion
    @ ~/.julia/packages/PyCall/twYvK/src/PyCall.jl:660 [inlined]
 [14] detect(X::PyCall.PyObject, model::PyCall.PyObject, batch_size::Int64; device::Nothing)
    @ PhaseNet ~/.julia/packages/PhaseNet/ZNXkE/src/detection.jl:119
 [15] detect(X::PyCall.PyObject, model::PyCall.PyObject, batch_size::Int64)
    @ PhaseNet ~/.julia/packages/PhaseNet/ZNXkE/src/detection.jl:105
 [16] top-level scope
    @ ~/Desktop/phasent_test.jl:16
 [17] include(fname::String)
    @ Base.MainInclude ./client.jl:476
 [18] top-level scope
    @ REPL[1]:1

However, if I set the window_samples to 160, I have no errors, and the code is executed successfully.

So, Are there any combinations of window_samples, batch_size, and npts to bear in mind before applying the detector?

@tclements
Copy link
Owner

I believe this error is being caused by the maxpool operation. When window_samples = 100, there aren't enough time samples to pool. I would suggest using a minimum window_samples = 256 and increase from there. The batch_size is number of samples in the first dimension of the X tensor to process at once. This is mainly important if you're using a GPU. For instance, here is a SeisData object with 34.8 seconds of data at 100 Hz:

julia> S = read_data("seisio", seisiopath)[1:3]
SeisData with 3 channels (3 shown)
    ID: KI.ABSH01..HNE                     KI.ABSH01..HNN                     KI.ABSH01..HNZ
  NAME: KI.ABSH01..EW2                     KI.ABSH01..NS2                     KI.ABSH01..UD2
   LOC: 44.5276 N, 142.844 E, 105.0 m      44.5276 N, 142.844 E, 105.0 m      44.5276 N, 142.844 E, 105.0 m
    FS: 100.0                              100.0                              100.0
  GAIN: 1.0                                1.0                                1.0
  RESP: a0 1.0, f0 1.0, 0z, 0p             a0 1.0, f0 1.0, 0z, 0p             a0 1.0, f0 1.0, 0z, 0p
 UNITS: cm/s^2                             cm/s^2                             cm/s^2
   SRC:
  MISC: 0 entries                          0 entries                          0 entries
 NOTES: 3 entries                          3 entries                          3 entries
     T: 2012-12-07T17:18:43 (0 gaps)       2012-12-07T17:18:43 (0 gaps)       2012-12-07T17:18:43 (0 gaps)
     X: -8.015e-04                         +2.753e-03                         -5.642e-04
        +8.900e-04                         +6.775e-04                         -3.672e-04
            ...                                ...                                ...
        -1.340e-02                         -5.468e-02                         -7.772e-02
        (nx = 34800)                       (nx = 34800)                       (nx = 34800)

Using a window_samples = 400, there are 87 total batches in X:

julia> window_samples = 400

julia> X = seisdata2torch(S,window_samples)

julia> X.shape
(87, 3, 400)

Since X is fairly small, we can match the batch_size to the size of X's first dimension:

batch_size = X.shape[1]

For much larger X, say using an entire day of continuous data, I would increase the batch_size until running out of GPU or CPU memory, which will depend on your hardware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants