How to export Stanza to ONNX format?
It seems impossible to just simply train the model.
There is an explanation here: https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html
I created a fork from stanza for this experiment here https://github.com/vivkvv/stanza. See also my commits https://github.com/vivkvv/stanza/commits?author=vivkvv.
I used pipeline_demo.py for testing. The main thing I added is code just inside models/tokanization/trainer.py below the line 77
pred = self.model(units, features)
Due to explanation I added
torch.onnx.export(
self.model,
(units, features),
onnx_export_file_name,
opset_version=9,
export_params=True,
do_constant_folding=True,
input_names=['input'],
output_names=['output'],
dynamic_axes={
'input': {0: 'batch_size'},
'output': {0: 'batch_size'}
}
)
and it works for tokenization. But the same does not work for e.g. pos or lemmatizer (see my commit for PartOfSpeech). And I get different errors for different opset_version.
I created a question on github/stanza and you could see there https://github.com/stanfordnlp/stanza/issues/893
Related
I have generated an artificial neural network in R (architecture 10x30x1, activation function = ReLU), using Keras package. I want to save this model so that I can import it at any other time without having to train it. The code that I use to save it is the following:
model %>% save_model_tf("model")
The files are generated and appear in the directory.
list.files("model")
[1] "assets" "saved_model.pb" "variables"
However, when importing it, using the following code:
new_model <- load_model_tf("model")
It gives me this error:
Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'NoneType' object has no attribute 'get'
I don't know if you could give me a clue about what I'm doing wrong or if I'm missing a step in between.
Thanks!
Having just encountered this error today, I tracked down a solution.
You don't provide example code, but judging from the syntax it is adapted from the Rstudio tensorflow tutorials.
I found this thread on Github, where lots of people who had copy pasted that code had similar issues with loading models.
https://github.com/rstudio/tfdatasets/issues/53
It turns out the offending item is the normalizer_fn in spec. Getting rid of that worked on my system.
Replace:
spec <- feature_spec(train_df, label ~ . ) %>%
step_numeric_column(all_numeric(), normalizer_fn = scaler_standard()) %>%
fit()
With:
spec <- feature_spec(train_df, label ~ . ) %>%
step_numeric_column(all_numeric()) %>%
fit()
As to why? No idea. I literally started using tensorflow today.
I made a game and I am trying to make it work with stable baselines. I've tried different algorithms
I've tried reading stable baselines documentation, but i can't figure out where to start tuning.
My game is here: https://github.com/AbdullahGheith/BlockPuzzleGym
And this is the code i ran to train it:
import gym
import blockpuzzlegym
from stable_baselines import PPO2
env = gym.make("BlockPuzzleGym-v0")
model = PPO2('MlpPolicy', env, verbose=1)
model.learn(250000)
for _ in range(10000):
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
I tried 25000000 timesteps, but it still wouldn't work.
As you might tell, i am new to ML, and this is my first project. Any indications are welcome.
I tried using the parameters the MiniGrid parameters from stable baselines zoo (without the env wrapper)
You can see that you used simple default MlpPolicy that includes two fully connected layers with size 64. How to implement your own network?
Here is a docs how to implement your own network. https://stable-baselines.readthedocs.io/en/master/guide/custom_policy.html
You should understand that any model in stable-baselines corresponds to following structure:
feature_extractor -> mlp_extractor -> policy\value networks.
So, you could add policy_kwargs to the model, where policy_kwargs is a dict with:
policy_kwargs = dict(
feature extractor=custom_extractor,
net_arch=[64, dict(vf=[16], pi=[16])]
)
Here you can see net_arch corresponds to structure of mlp_extractor and for policy\value nets. custom_extractor is a funciton that you can implement that would process your input data to fully connected layer at the end. You can implement here any structure of a model that you want. For example, there is a default cnn_extractor from library:
def nature_cnn(scaled_images, **kwargs):
activ = tf.nn.relu
layer_1 = activ(conv(scaled_images, 'c1', n_filters=32, filter_size=8, stride=4, init_scale=np.sqrt(2), **kwargs))
layer_2 = activ(conv(layer_1, 'c2', n_filters=64, filter_size=4, stride=2, init_scale=np.sqrt(2), **kwargs))
layer_3 = activ(conv(layer_2, 'c3', n_filters=64, filter_size=3, stride=1, init_scale=np.sqrt(2), **kwargs))
layer_3 = conv_to_fc(layer_3)
return activ(linear(layer_3, 'fc1', n_hidden=512, init_scale=np.sqrt(2)))
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from engine import train_one_epoch, evaluate
import utils
import torchvision.transforms as T
num_epochs = 10
for epoch in range(num_epochs):
train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
lr_scheduler.step()
evaluate(model, data_loader_test, device=device)
I am using the same code as provided in this link Building Raccoon Model but mine is not working.
This is the error message I am getting
TypeError Traceback (most recent call last)
in ()
2 for epoch in range(num_epochs):
3 # train for one epoch, printing every 10 iterations
4 ----> train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
5 # update the learning rate
6 lr_scheduler.step()
7 frames
in getitem(self, idx)
29 target["iscrowd"] = iscrowd
30 if self.transforms is not None:
31 ---> img, target = self.transforms(img, target)
32 return img, target
33
TypeError: call() takes 2 positional arguments but 3 were given
The above answer is incorrect, I accidentally upvoted before noticing. You are using the wrong Compose, note that it says
https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#putting-everything-together
"In references/detection/, we have a number of helper functions to simplify training and evaluating detection models. Here, we will use references/detection/engine.py, references/detection/utils.py and references/detection/transforms.py. Just copy them to your folder and use them here."
there are helper scripts. They subclass the compose and flip methods
https://github.com/pytorch/vision/blob/6315358dd06e3a2bcbe9c1e8cdaa10898ac2b308/references/detection/transforms.py#L17
I did the same thing before noticing this. Do not use the compose method from torchvision.transforms, or else you will get the error above. Download their module and load it.
I am kind of a newbie at this and I was also having the same problem.
Upon doing more research, I found this where the accepted answer used:
img = self.transforms(img)
instead of:
img, target = self.transforms(img, target)
Removing "target" solved the error for me and should solve it for you as well. Not entirely sure why even the official PyTorch tutorial also has "target" included but it does not work for us.
I had the same issue, there is even an issue raised on Pytorch discussion forum using regarding the same T.Compose | TypeError: call() takes 2 positional arguments but 3 were given
I was able to overcome this issue by copy and pasting the files on the for a specific version v0.3.0 on the vision/reference/detection of the tutorial I am following building-your-own-object-detector-pytorch-vs-tensorflow-and-how-to-even-get-started
Just to fall into another issue I have raised here ValueError: All bounding boxes should have positive height and width. Found invaid box [500.728515625, 533.3333129882812, 231.10546875, 255.2083282470703] for target at index 0. #2740
I have downloaded Conll 2003 corpus ("eng.train"). I want to use it to extract entity using python crfsuite training. But I don't know how to load this file for training.
I found this example, but it is not for English.
train_sents = list(nltk.corpus.conll2002.iob_sents('esp.train'))
test_sents = list(nltk.corpus.conll2002.iob_sents('esp.testb'))
Also in future I would like to train new entities other than POS or location. How can I add those.
Also please suggest how to handle multiple words.
You can use ConllCorpusReader.
Here a general impelemantation:
ConllCorpusReader('file path', 'file name', columntypes=['','',''])
Here a list of column types which you can use: 'WORDS', 'POS', 'TREE', 'CHUNK', 'NE', 'SRL', 'IGNORE'
Example:
from nltk.corpus.reader import ConllCorpusReader
train = ConllCorpusReader('CoNLL-2003', 'eng.train', ['words', 'pos', 'ignore', 'chunk'])
test = ConllCorpusReader('CoNLL-2003', 'eng.testa', ['words', 'pos', 'ignore', 'chunk'])
I am using openMDAO to construct a co-kriging metamodel that I would like to export and then import in another python code.
I've found a message on the old forum (http://openmdao.org/forum/questions/444/how-can-i-save-the-metamodel-for-later-use?sort=votes) in which someone used pickle to save a meta-model.
I have also read about the recorders however I didn't succeed in the different tests I performed.
Is there a way to save the meta-model and use it in a future code?
EDIT: I think I found a kind of solution using 'pickle'. I succeded to do this with a kriging meta-model but i assume I would work the same with the co-kriging.
Like in the post on the 'old' forum of openMDAO, I saved the trained meta-model in a file and then reuse it in another python script. I joined here the part of the code saving the trained meta-model:
cok = MultiFiCoKrigingSurrogate()
prob = Problem(Simulation(cok, nfi=2))
prob.setup(check=False)
prob['mm.train:x1'] = DATA_HF_dim
prob['mm.train:x1_fi2'] = DATA_LF_dim
prob['mm.train:y1'] = rastri_e
prob['mm.train:y1_fi2'] = rastri_c
prob.run()
import pickle
f = open('meta_model_info.p','wb')
pickle.dump(prob,f)
f.close
Once the trained meta-model is saved in the file meta_model_info.p, I can load it in another script, skipping the learning phase. Part of the code of the second script is here:
class Simulation(Group):
def __init__(self, surrogate, nfi):
super(Simulation, self).__init__()
self.surrogate = surrogate
mm = self.add("mm", MultiFiMetaModel(nfi=nfi))
mm.add_param('x1', val=0.)
mm.add_output('y1', val=(0.,0.), surrogate=surrogate)
cok = MultiFiCoKrigingSurrogate()
prob = Problem(Simulation(cok, nfi=2))
prob.setup(check=False)
import pickle
f = open('meta_model_info.p','rb')
clf = pickle.load(f)
pred_cok_clf = []
for x in inputs:
clf['mm.x1'] = x
clf.run()
pred_cok_clf.append(clf['mm.y1'])
pred_mu_clf = np.array([float(p[0]) for p in pred_cok_clf])
pred_sigma_clf = np.array([float(p[1]) for p in pred_cok_clf])
However I was forced to redefine the class of the problem and to setup the problem either in this second script.
I don't know if it is a proper use of 'pickle' or if there is another way to do this, if you have any suggestion :)
There is not currently any provision for saving and reloading the surrogate model. You have two options:
1) Save off the training data, then import and re-train the model in your other script. You can call the fit and predict methods of the surrogate model directly for this by importing them from the library.
2) If you want to skip the cost of re-training each time, then you need to modify the surrogate model itself to save off the result of the fitting process, then re-load it into a new instance later: https://github.com/OpenMDAO/OpenMDAO/blob/c69e00f6f9eeb617863e782246e2e7ed1fe9e019/openmdao/surrogate_models/multifi_cokriging.py#L322