---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [1], in <cell line: 3>()
1 import torch
----> 3 model = torch.hub.load('C:/Users/user/Desktop/***/model/', 'custom', path='runs/train/***/weights/best.pt', force_reload = True, source='local')
4 # Images
5 imgs = ['/kaggle/input/***/images/image-1.png'] # batch of images
File C:\ProgramData\Anaconda3\lib\site-packages\torch\hub.py:404, in load(repo_or_dir, model, source, force_reload, verbose, skip_validation, *args, **kwargs)
401 if source == 'github':
402 repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, verbose, skip_validation)
--> 404 model = _load_local(repo_or_dir, model, *args, **kwargs)
405 return model
File C:\ProgramData\Anaconda3\lib\site-packages\torch\hub.py:432, in _load_local(hubconf_dir, model, *args, **kwargs)
429 hubconf_path = os.path.join(hubconf_dir, MODULE_HUBCONF)
430 hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
--> 432 entry = _load_entry_from_hubconf(hub_module, model)
433 model = entry(*args, **kwargs)
435 sys.path.remove(hubconf_dir)
File C:\ProgramData\Anaconda3\lib\site-packages\torch\hub.py:240, in _load_entry_from_hubconf(m, model)
237 func = _load_attr_from_module(m, model)
239 if func is None or not callable(func):
--> 240 raise RuntimeError('Cannot find callable {} in hubconf'.format(model))
242 return func
RuntimeError: Cannot find callable custom in hubconf
I use pytorch on my project
I would use saved model which on wandb by using torch.hub.load()
But there is some entry point error, It's not work
I have read related documents and discussions, but I couldn't find a solution.
please help.....
Related
I have the following function to run Inference using TensorFlow. The script runs without problem, but when I try to use Ray.io to distribute the workload, it shows errors.
N_CHANNELS = 3
def load_image_into_numpy_array(image):
"""
Converts a PIL image into a numpy array (height x width x channels).
:param image: PIL image
:return: numpy array
"""
(width, height) = image.size
return np.array(image.getdata()) \
.reshape((height, width, N_CHANNELS)).astype(np.uint8)
#ray.remote
def run_inference(graph, image_np):
"""
Runs the inference on the given image.
:param graph: tensorflow graph
:param image_np: numpy image
:return: dictionary with detected classes
and their corresponding scores and boxes
"""
output_tensor_dict = {
DETECTION_BOXES_KEY: DETECTION_BOXES_KEY + TENSOR_SUFFIX,
DETECTION_SCORES_KEY: DETECTION_SCORES_KEY + TENSOR_SUFFIX,
DETECTION_CLASSES_KEY: DETECTION_CLASSES_KEY + TENSOR_SUFFIX
}
with graph.as_default():
with tf.compat.v1.Session() as sess:
input_tensor = tf.compat.v1.get_default_graph()\
.get_tensor_by_name(IMAGE_TENSOR_KEY + TENSOR_SUFFIX)
image_np_expanded = np.expand_dims(image_np, axis=0)
input_tensor_dict = {input_tensor: image_np_expanded}
output_dict = sess.run(output_tensor_dict,
feed_dict=input_tensor_dict)
return {
DETECTION_BOXES_KEY:
output_dict[DETECTION_BOXES_KEY][0],
DETECTION_SCORES_KEY:
output_dict[DETECTION_SCORES_KEY][0],
DETECTION_CLASSES_KEY:
output_dict[DETECTION_CLASSES_KEY][0].astype(np.int64)
}
# =====================
# Running the inference
# =====================
tic = time.perf_counter()
print("* Stage 9: Running the inference")
IMAGE_NP_KEY = 'image_np'
RESULTS_KEY = 'results'
file_result_dict = {}
for filename in TEST_IMAGES:
image_np = load_image_into_numpy_array(Image.open(filename))
output_dict = run_inference.remote(graph, image_np)
results = process_output(output_dict[DETECTION_CLASSES_KEY],
output_dict[DETECTION_SCORES_KEY],
output_dict[DETECTION_BOXES_KEY],
category_index)
file_result_dict[filename] = { IMAGE_NP_KEY: image_np, RESULTS_KEY: results }
toc = time.perf_counter()
print("Stage 9 completed in", round(toc - tic, 2), "seconds")
I encountered the following error while sending the function to #ray.remote:
* Stage 9: Running the inference
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
python/ray/_raylet.pyx in ray._raylet.prepare_args_internal()
/opt/conda/lib/python3.8/site-packages/ray/serialization.py in serialize(self, value)
412 else:
--> 413 return self._serialize_to_msgpack(value)
/opt/conda/lib/python3.8/site-packages/ray/serialization.py in _serialize_to_msgpack(self, value)
390 metadata = ray_constants.OBJECT_METADATA_TYPE_PYTHON
--> 391 pickle5_serialized_object = self._serialize_to_pickle5(
392 metadata, python_objects
/opt/conda/lib/python3.8/site-packages/ray/serialization.py in _serialize_to_pickle5(self, metadata, value)
352 self.get_and_clear_contained_object_refs()
--> 353 raise e
354 finally:
/opt/conda/lib/python3.8/site-packages/ray/serialization.py in _serialize_to_pickle5(self, metadata, value)
347 self.set_in_band_serialization()
--> 348 inband = pickle.dumps(
349 value, protocol=5, buffer_callback=writer.buffer_callback
/opt/conda/lib/python3.8/site-packages/ray/cloudpickle/cloudpickle_fast.py in dumps(obj, protocol, buffer_callback)
72 )
---> 73 cp.dump(obj)
74 return file.getvalue()
/opt/conda/lib/python3.8/site-packages/ray/cloudpickle/cloudpickle_fast.py in dump(self, obj)
619 try:
--> 620 return Pickler.dump(self, obj)
621 except RuntimeError as e:
TypeError: cannot pickle '_thread.RLock' object
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-17-e130c8b3d1e6> in <module>
12 image_np = load_image_into_numpy_array(Image.open(filename))
13
---> 14 output_dict = run_inference.remote(graph, image_np)
15
16 results = process_output(output_dict[DETECTION_CLASSES_KEY],
/opt/conda/lib/python3.8/site-packages/ray/remote_function.py in _remote_proxy(*args, **kwargs)
155 #wraps(function)
156 def _remote_proxy(*args, **kwargs):
--> 157 return self._remote(args=args, kwargs=kwargs)
158
159 self.remote = _remote_proxy
/opt/conda/lib/python3.8/site-packages/ray/util/tracing/tracing_helper.py in _invocation_remote_span(self, args, kwargs, *_args, **_kwargs)
301 if kwargs is not None:
302 assert "_ray_trace_ctx" not in kwargs
--> 303 return method(self, args, kwargs, *_args, **_kwargs)
304
305 assert "_ray_trace_ctx" not in kwargs
/opt/conda/lib/python3.8/site-packages/ray/remote_function.py in _remote(self, args, kwargs, num_returns, num_cpus, num_gpus, memory, object_store_memory, accelerator_type, resources, max_retries, retry_exceptions, placement_group, placement_group_bundle_index, placement_group_capture_child_tasks, runtime_env, name, scheduling_strategy)
443 invocation = self._decorator(invocation)
444
--> 445 return invocation(args, kwargs)
446
447 def bind(self, *args, **kwargs):
/opt/conda/lib/python3.8/site-packages/ray/remote_function.py in invocation(args, kwargs)
419 not self._is_cross_language
420 ), "Cross language remote function cannot be executed locally."
--> 421 object_refs = worker.core_worker.submit_task(
422 self._language,
423 self._function_descriptor,
python/ray/_raylet.pyx in ray._raylet.CoreWorker.submit_task()
python/ray/_raylet.pyx in ray._raylet.CoreWorker.submit_task()
python/ray/_raylet.pyx in ray._raylet.prepare_args_and_increment_put_refs()
python/ray/_raylet.pyx in ray._raylet.prepare_args_and_increment_put_refs()
python/ray/_raylet.pyx in ray._raylet.prepare_args_internal()
TypeError: Could not serialize the argument <tensorflow.python.framework.ops.Graph object at 0x7f1c68de2160> for a task or actor __main__.run_inference. Check https://docs.ray.io/en/master/serialization.html#troubleshooting for more information.
How can I resolve this serialization issue?
TensorFlow: 2.9.1
Python: 3.8.5
Ray: 1.12.1
2 Ray Computing Nodes:
CPU: Intel® Xeon® Silver 4210R 13.75M Cache, 2.40 GHz
RAM: 128GB RAM
GPU: NVIDIA GeForce RTX 2080 Ti TURBO 11GB (rev. 2.0) x4 (Driver version: 450.156.00)
As mentioned by Robert in the above comment,
The workaround could be to just pass in numpy arrays and regular Python objects into run_inference and then create the TensorFlow graph inside of run_inference
Goal: Amend this Notebook to work with albert-base-v2 model.
Kernel: conda_pytorch_p36. I did Restart & Run All, and refreshed file view in working directory.
In order to evaluate and to export this Quantised model, I need to setup a Tokenizer.
Error occurs in Section 1.3.
Both parameters in AutoTokenizer.from_pretrained() throw the same error.
Section 1.3 Code:
# define the tokenizer
tokenizer = AutoTokenizer.from_pretrained(
configs.output_dir, do_lower_case=configs.do_lower_case)
Parameters:
# The output directory for the fine-tuned model, $OUT_DIR.
configs.output_dir = "./MRPC/"
# Prepare GLUE task
...
configs.do_lower_case = True
Values and DTypes:
-- configs.output_dir --
./MRPC/
<class 'str'>
-- configs.do_lower_case --
True
<class 'bool'>
Traceback:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-18c5137aacf4> in <module>
140 # define the tokenizer
141 tokenizer = AutoTokenizer.from_pretrained(
--> 142 configs.output_dir, do_lower_case=configs.do_lower_case)
143
144 # Evaluate the original FP32 BERT model
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/transformers/models/auto/tokenization_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
548 tokenizer_class_py, tokenizer_class_fast = TOKENIZER_MAPPING[type(config)]
549 if tokenizer_class_fast and (use_fast or tokenizer_class_py is None):
--> 550 return tokenizer_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
551 else:
552 if tokenizer_class_py is not None:
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/transformers/tokenization_utils_base.py in from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs)
1752 use_auth_token=use_auth_token,
1753 cache_dir=cache_dir,
-> 1754 **kwargs,
1755 )
1756
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/transformers/tokenization_utils_base.py in _from_pretrained(cls, resolved_vocab_files, pretrained_model_name_or_path, init_configuration, use_auth_token, cache_dir, *init_inputs, **kwargs)
1776 copy.deepcopy(init_configuration),
1777 *init_inputs,
-> 1778 **(copy.deepcopy(kwargs)),
1779 )
1780 else:
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/transformers/tokenization_utils_base.py in _from_pretrained(cls, resolved_vocab_files, pretrained_model_name_or_path, init_configuration, use_auth_token, cache_dir, *init_inputs, **kwargs)
1880 # Instantiate tokenizer.
1881 try:
-> 1882 tokenizer = cls(*init_inputs, **init_kwargs)
1883 except OSError:
1884 raise OSError(
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/transformers/models/albert/tokenization_albert.py in __init__(self, vocab_file, do_lower_case, remove_space, keep_accents, bos_token, eos_token, unk_token, sep_token, pad_token, cls_token, mask_token, sp_model_kwargs, **kwargs)
179
180 self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
--> 181 self.sp_model.Load(vocab_file)
182
183 #property
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/sentencepiece/__init__.py in Load(self, model_file, model_proto)
365 if model_proto:
366 return self.LoadFromSerializedProto(model_proto)
--> 367 return self.LoadFromFile(model_file)
368
369
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/sentencepiece/__init__.py in LoadFromFile(self, arg)
169
170 def LoadFromFile(self, arg):
--> 171 return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
172
173 def DecodeIdsWithCheck(self, ids):
TypeError: not a string
Please let me know if there's anything else I can add to post.
Passing just the model name suffices.
tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
List of model_types can be found here.
I have a question regarding Chainer's iterator interface, and how it interfaces with a Trainer, Updater and Model.
My data are graphs, and hence are of varying matrix shapes. I have concatenated the features matrix into one large dense matrix, adjacency matrices into one large sparse COO matrix, and the summation operator into one large sparse COO matrix. Because this is done with molecular data, I have an atom graph and a bond graph per sample. Hence, the input data is a six-tuple, which for the purposes of deep learning, I consider to be one big giant data point for training. (Until I get this working with this giant matrix, I am not planning on doing train/test splits just yet, to keep my code simple.)
xs = (atom_Fs, atom_As, atom_Ss, bond_Fs, bond_As, bond_Ss)
ts = data['target'].values
dataset = [(xs, ts)]
My model forward pass is as follows:
# model boilerplate above this comment
def forward(self, data):
atom_feats, atom_adjs, atom_sums, bond_feats, bond_adjs, bond_sums = data
atom_feats = self.atom_mp1(atom_feats, atom_adjs)
atom_feats = self.atom_mp2(atom_feats, atom_adjs)
atom_feats = self.atom_gather(atom_feats, atom_sums)
bond_feats = self.atom_mp1(bond_feats, bond_adjs)
bond_feats = self.atom_mp2(bond_feats, bond_adjs)
bond_feats = self.atom_gather(bond_feats, bond_sums)
feats = F.hstack([atom_feats, bond_feats])
feats = F.tanh(self.dense1(feats))
feats = F.tanh(self.dense2(feats))
feats = self.dense3(feats)
return feats
I then pass everything into a Trainer:
from chainer import iterators, training
from chainer.optimizers import SGD, Adam
iterator = iterators.SerialIterator(dataset, batch_size=1)
optimizer = Adam()
optimizer.setup(mpnn)
updater = training.updaters.StandardUpdater(iterator, optimizer)
max_epoch = 50
trainer = training.Trainer(updater, (max_epoch, 'epoch'))
trainer.run()
However, when I run the trainer, I get the following error:
Exception in main training loop: forward() takes 2 positional arguments but 3 were given
Traceback (most recent call last):
File "/home/ericmjl/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/trainer.py", line 315, in run
update()
File "/home/ericmjl/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 165, in update
self.update_core()
File "/home/ericmjl/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 177, in update_core
optimizer.update(loss_func, *in_arrays)
File "/home/ericmjl/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/optimizer.py", line 680, in update
loss = lossfun(*args, **kwds)
File "/home/ericmjl/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
out = forward(*args, **kwargs)
Will finalize trainer extensions and updater before reraising the exception.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-ea26cece43b3> in <module>
9 max_epoch = 50
10 trainer = training.Trainer(updater, (max_epoch, 'epoch'))
---> 11 trainer.run()
~/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/trainer.py in run(self, show_loop_exception_msg)
327 f.write('Will finalize trainer extensions and updater before '
328 'reraising the exception.\n')
--> 329 six.reraise(*sys.exc_info())
330 finally:
331 for _, entry in extensions:
~/anaconda/envs/mpnn/lib/python3.7/site-packages/six.py in reraise(tp, value, tb)
691 if value.__traceback__ is not tb:
692 raise value.with_traceback(tb)
--> 693 raise value
694 finally:
695 value = None
~/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/trainer.py in run(self, show_loop_exception_msg)
313 self.observation = {}
314 with reporter.scope(self.observation):
--> 315 update()
316 for name, entry in extensions:
317 if entry.trigger(self):
~/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py in update(self)
163
164 """
--> 165 self.update_core()
166 self.iteration += 1
167
~/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py in update_core(self)
175
176 if isinstance(in_arrays, tuple):
--> 177 optimizer.update(loss_func, *in_arrays)
178 elif isinstance(in_arrays, dict):
179 optimizer.update(loss_func, **in_arrays)
~/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/optimizer.py in update(self, lossfun, *args, **kwds)
678 if lossfun is not None:
679 use_cleargrads = getattr(self, '_use_cleargrads', True)
--> 680 loss = lossfun(*args, **kwds)
681 if use_cleargrads:
682 self.target.cleargrads()
~/anaconda/envs/mpnn/lib/python3.7/site-packages/chainer/link.py in __call__(self, *args, **kwargs)
240 if forward is None:
241 forward = self.forward
--> 242 out = forward(*args, **kwargs)
243
244 # Call forward_postprocess hook
TypeError: forward() takes 2 positional arguments but 3 were given
This baffles me, because I have set up the dataset in the same way as the mnist example, in which the input data are paired with the output data in a tuple. Because of the layers of abstractions in Chainer, I'm not quite sure how to debug this issue. Does anybody have any insight into this?
Are you using mpnn model which gets only xs (or data) and outputs feats?
I think the problem is in the model, not the iterator nor dataset.
You need to prepare a model which gets xs and ts as input argument and calculate loss as output. For example,
class GraphNodeClassifier(chainer.Chain):
def __init__(self, mpnn):
with self.init_scope():
self.mpnn = mpnn
def forward(self, xs, ts):
feat = self.mpnn(xs)
loss = "calculate loss between `feat` and `ts` here..."
return loss
and use this GraphNodeClassifier as optimizer's setup argument.
In the MNIST example above it uses chainer's built-in L.Classifier class which wraps MLP model (which only get x) to get x and t to calculate classification loss.
I successfully run the function for the zero inflated Poisson model:
(Successfully = it seems to converge, when I print the summary)
PoissonZiGMLE:
PZI = PoissonZiGMLE(df_zip['obs'],Xmat,offset=df_zip['offsetv'])
result = PZI.fit(maxiter = 1000)
print result.summary()
however when I try:
result.predict(df_zip, offset=offsetv)
I get this error:
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call
last)
in ()
----> 1 result.predict(df_zip, offset=offsetv)
/software/centos6/x86_64/canopy-1.7.4/Canopy_64bit/User/lib/python2.7/site-packages/statsmodels/base/model.py
in predict(self, exog, transform, *args, **kwargs)
747 exog = np.atleast_2d(exog) # needed in count model shape1
748
--> 749 return self.model.predict(self.params, exog, *args, **kwargs)
750
751
/software/centos6/x86_64/canopy-1.7.4/Canopy_64bit/User/lib/python2.7/site-packages/statsmodels/base/model.py
in predict(self, params, exog, *args, **kwargs)
175 This is a placeholder intended to be overwritten by individual models.
176 """
--> 177 raise NotImplementedError
178
179
NotImplementedError:
before submitting an issue on github i was wondering if anyone has used PoissonZiGMLE and has any insight on how i can bypass the predict function if not implemented.
How I can use the predict method
after fitting a model (http://nbviewer.ipython.org/urls/umich.box.com/shared/static/6tfc1e0q6jincsv5pgfa.ipynb)
With the simple dietox example I get an error.
data = pd.read_csv("dietox.csv")
model = sm.MixedLM.from_formula("Weight ~ Time", data, groups=data["Pig"])
result = model.fit()
print result.summary()
#this and other attempts doesn't work
result.predict(data.ix[1])
NotImplementedError Traceback (most recent call last)
<ipython-input-7-ba818b886233> in <module>()
----> 1 result.predict(data.ix[1])
C:\Anaconda\lib\site-packages\statsmodels\base\model.pyc in predict(self, exog, transform, *args, **kwargs)
747 exog = np.atleast_2d(exog) # needed in count model shape[1]
748
--- > 749 return self.model.predict(self.params, exog, *args, **kwargs)
750
751
C:\Anaconda\lib\site-packages\statsmodels\base\model.pyc in predict(self, params, exog, *args, **kwargs)
175 This is a placeholder intended to be overwritten by individual models.
176 """
--> 177 raise NotImplementedError
178
179
NotImplementedError: