Saving FastText custom model binary with Gensim - save

I am trying to save a custom FastText model trained with gensim. I want to save the binary files to have the possibility of training again the model, if it may.
The code to save the binary file is the next one:
from gensim.models.fasttext import save_facebook_model
save_facebook_model(model,'own_fasttext_model.bin')
But I am obtaining the next error in that same line:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-192-c9c2c41985af> in <module>
2 from gensim.models.fasttext import save_facebook_model
3
----> 4 save_facebook_model(model,'own_fasttext_model.bin')
/opt/conda/lib/python3.7/site-packages/gensim/models/fasttext.py in save_facebook_model(model, path, encoding, lr_update_rate, word_ngrams)
1334 """
1335 fb_fasttext_parameters = {"lr_update_rate": lr_update_rate, "word_ngrams": word_ngrams}
-> 1336 gensim.models._fasttext_bin.save(model, path, fb_fasttext_parameters, encoding)
/opt/conda/lib/python3.7/site-packages/gensim/models/_fasttext_bin.py in save(model, fout, fb_fasttext_parameters, encoding)
666 if isinstance(fout, str):
667 with open(fout, "wb") as fout_stream:
--> 668 _save_to_stream(model, fout_stream, fb_fasttext_parameters, encoding)
669 else:
670 _save_to_stream(model, fout, fb_fasttext_parameters, encoding)
/opt/conda/lib/python3.7/site-packages/gensim/models/_fasttext_bin.py in _save_to_stream(model, fout, fb_fasttext_parameters, encoding)
629
630 # Save words and ngrams vectors
--> 631 _input_save(fout, model)
632 fout.write(struct.pack('#?', False)) # Save 'quot_', which is False for unsupervised models
633
/opt/conda/lib/python3.7/site-packages/gensim/models/_fasttext_bin.py in _input_save(fout, model)
573
574 assert vocab_dim == ngrams_dim
--> 575 assert vocab_n == len(model.wv.vocab)
576 assert ngrams_n == model.wv.bucket
577
AssertionError:
Any clue on what could be happening?
Thanks in advance.

The .save_facebook_model() method is a brand-new feature. Such an AssertionError typically indicate ssome bug: the assertion has caught a contradiction, in the current state, compared to what was expected or required for the code to safely proceed.
If you can put together a small, self-contained set of steps that reliably create the error, you can report it to the project's bug tracker at:
https://github.com/RaRe-Technologies/gensim/issues
(In particular be sure to review & note in any report: how was that model object created/trained? Was it modified in any non-standard ways?)

Opened the next issue in github and this will fix the problem.

Related

machine learning+deep learning+speech recognition

I run the code in my editor (VS Code) without any problems, but for next step and due to RAM and GPU limitation, I took it in colab, but got an error that seems to be due to mismatch of versions due to transfer from my editor to colab. how can i fix this problem?
The current version of python running on Google Colab is 3.8.16, I used tensorflow 2.3.0 and keras 2.4.3.
The error is related to this part of code when use the model.fit() for train the model:
(I use CTC_loss in model):
model.fit(
train_dg,
validation_data=val_dg,
epochs=args.epochs,
callbacks=[PlotLossesKeras(),
early_stopping,
cp,
csv_logger,
lrs]
)
But I got this error:
----------------------------------------------------------------------------------------------------
**Epoch 00001: LearningRateScheduler reducing learning rate to 0.001. Epoch 1/300
-----------**---------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) <ipython-input-87-2b4ea6811b43> in <module>
----> 1 model.fit(train_dg,validation_data=val_dg,epochs=args.epochs,callbacks=[PlotLossesKeras(),early_stopping,cp,csv_logger,lrs])
9 frames /usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
---> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
InvalidArgumentError: Saw a non-null label (index >= num_classes - 1) following a null label, batch: 2 num_classes: 16 labels: 16,0,0,0,0,0,0 labels seen so far: [[node functional_3/CTCloss/CTCLoss (defined at <ipython-input-17-1689d20fc46d>:887) ]] [Op:__inference_train_function_6401]
Function call stack: train_function
---------------------------------------------------------------------------------------
I try change the version of python in colab but it dosent work.
also change num_classes in the last layer of my model, it dosent work too.

Ran into "TypeError: '<' not supported between instances of 'Tensor' and 'list'" when going through dataset

I am replicating ResNet (source: https://arxiv.org/abs/1512.03385).
I ran into the error "TypeError: '<' not supported between instances of 'Tensor' and 'list'" when trying to go through several different dataset in different sections of my code.
I tried different fixes but none worked: (i) I deleted enumerate cause I worried that using this may cause the problem (ii) I tried to go through dataloader rather than dataset but it didn't work
1st time: When I tried to view images:
for images, _ in train_loader:
print('images.shape:', images.shape)
plt.figure(figsize=(16,8))
plt.axis('off')
plt.imshow(torchvision.utils.make_grid(images, nrow=16).permute((1, 2, 0)))
break
2nd/3rd time: when I tried to validate/test the resnet:
with torch.no_grad():
for j, inputs, labels in enumerate(test_loader, start=0):
outputs = resnet_models[i](inputs)
_, prediction = torch.max(outputs, dim=1)
You may notice that I didn't run into this error when training the resnet, and the code is quite similar:
for batch, data in enumerate(train_dataloader, start=0):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
Error message (taking the first error as an example. The rest is pretty much the same)
TypeError Traceback (most recent call last)
Input In [38], in <cell line: 8>()
6 print("Images AFTER NORMALIZATION")
7 print("--------------------------")
----> 8 for images, _ in training_data:
9 sort=False
10 print('images.shape:', images.shape)
File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torch/utils/data/dataset.py:471, in Subset.getitem(self, idx)
469 if isinstance(idx, list):
470 return self.dataset[[self.indices[i] for i in idx]]
--> 471 return self.dataset[self.indices[idx]]
File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torchvision/datasets/cifar.py:118, in CIFAR10.getitem(self, index)
115 img = Image.fromarray(img)
117 if self.transform is not None:
--> 118 img = self.transform(img)
120 if self.target_transform is not None:
121 target = self.target_transform(target)
File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torchvision/transforms/transforms.py:95, in Compose.call(self, img)
93 def call(self, img):
94 for t in self.transforms:
---> 95 img = t(img)
96 return img
File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks >or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torchvision/transforms/transforms.py:707, in RandomHorizontalFlip.forward(self, >img)
699 def forward(self, img):
700 """
701 Args:
702 img (PIL Image or Tensor): Image to be flipped.
(...)
705 PIL Image or Tensor: Randomly flipped image.
706 """
--> 707 if torch.rand(1) < self.p:
708 return F.hflip(img)
709 return img
TypeError: '<' not supported between instances of 'Tensor' and 'list'
I was having the same error message, probably under different circumstances, but I just found my own bug and figured I would share it anyway for various readers. I was using a torchvision transformation in my dataset, which the dataloader was loading from. The transformation was
torchvision.transforms.RandomHorizontalFlip([0.5]),
and the error is that the input to this transformation should not be a list but should be
torchvision.transforms.RandomHorizontalFlip(0.5),
So if there is anything I can recommend, it's just that maybe there is some list argument being passed through that shouldn't be in some transformation or otherwise.

TypeError: __call__() takes 2 positional arguments but 3 were given. To train Raccoon prediction model using FastRCNN through Transfer Learning

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

Keras Tokenization (fit on text)

When i am running this script-->
tokenizer.fit_on_texts(df['text'].values)
sequences = tokenizer.texts_to_sequences(df['text'].values)
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
I am getting this error
AttributeError Traceback (most recent call
last)
<ipython-input-4-7c08b89b116a> in <module>()
----> 1 tokenizer.fit_on_texts(df['text'].values)
2 sequences = tokenizer.texts_to_sequences(df['text'].values)
3 word_index = tokenizer.word_index
4 print('Found %s unique tokens.' % len(word_index))
/opt/conda/lib/python3.6/site-packages/keras_preprocessing/text.py in
fit_on_texts(self, texts)
220 self.filters,
221 self.lower,
--> 222 self.split)
223 for w in seq:
224 if w in self.word_counts:
/opt/conda/lib/python3.6/site-packages/keras_preprocessing/text.py in
text_to_word_sequence(text, filters, lower, split)
41 """
42 if lower:
---> 43 text = text.lower()
44
45 if sys.version_info < (3,):
AttributeError: 'float' object has no attribute 'lower'
My size of CSV file is 6970963 when I reduce the size it works, is there any size limit of keras Tokenizer or I am doing something wrong
I guess file size is not the issue, try using a try block and look at the data your are passing. Use some thing like this instead of the line
#instead of this
tokenizer.fit_on_texts(df['text'].values)
#use this to look at the data when it is causing that error.
try:
tokenizer.fit_on_texts(df['text'].values)
except Exception as e:
print("exceiption is", e)
print("data passedin ", df['text'].values)
Then you can accordingly fix the error you are getting.
Check the datatype of the text you are fitting the tokenizer on. It sees it as a float instead of string. You need to convert to string before fitting a tokenizer on it.
Try something like this:
train_x = [str(x[1]) for x in train_x]
Although it is an old thread, but still following could be answer.
You data may have nan, which are interpreted as a float instead of nan. either force the type as str(word) or remove the nan using data.fillna('empty')

No Operation named [input] in the Graph" error while fine tuning/retraining inceptionV1 slim model

I am trying to finetune/retrain InceptionV1 model here, on my own data. I was able to
Convert the Image data to TFR format data using this.
Pass the converted data to finetune_inception_v1_on_flowers
Complete the training and evaluation in according to the script file above, I am attaching the logs here.
INFO:tensorflow:global step 1000: loss = 0.1833 (20.37 sec/step) INFO:tensorflow:Stopping Training.
INFO:tensorflow:Finished training! Saving model to disk. INFO:tensorflow:Scale of 0 disables regularizer.
WARNING:tensorflow:From eval_image_classifier.py:157: streaming_recall_at_k (from tensorflow.contrib.metrics.python.ops.metric_ops) is deprecated and will be removed after 2016-11-08. Instructions for updating: Please use streaming_sparse_recall_at_k, and reshape labels from [batch_size] to [batch_size, 1].
INFO:tensorflow:Evaluating /tmp/flowers-models/inception_v1/all/model.ckpt-1000
INFO:tensorflow:Starting evaluation at 2017-04-26-14:59:28 INFO:tensorflow:Restoring parameters from /tmp/flowers-models/inception_v1/all/model.ckpt-1000
INFO:tensorflow:Evaluation [1/4]
INFO:tensorflow:Evaluation [2/4]
INFO:tensorflow:Evaluation [3/4]
INFO:tensorflow:Evaluation [4/4]
2017-04-26 20:30:23.505265: I tensorflow/core/kernels/logging_ops.cc:79] eval/Recall_5[1]
2017-04-26 20:30:23.505420: I tensorflow/core/kernels/logging_ops.cc:79] eval/Accuracy[1]
INFO:tensorflow:Finished evaluation at 2017-04-26-15:00:23
4.The training process genertaed many checkpoints, two graph.pbtxt files. I used the latest checkpoint and the graph.pbtxt file in the freeze tool and generated a .pb file, according to the discussion here, I used following parameters
--input_graph=/../../graph.pbtxt
--output_node_names=InceptionV1/Logits/Predictions/Softmax
 
Now I am trying to use the .pb file in tensorflow demo application, by making few changes toClassifierActivity.java in tensorflow demo android app and it shows an error to me,
No Operation named [input] in the Graph"
following are the changes I made toClassifierActivity.java
private static final int INPUT_SIZE = 224;//224//299
private static final int IMAGE_MEAN = 117;//117//128
private static final float IMAGE_STD = 1;//1//128
private static final String INPUT_NAME ="input";//input
private static final String OUTPUT_NAME ="InceptionV1/Logits/Predictions/Softmax";//output
private static final String MODEL_FILE ="file:///android_asset/frozen_1000_graph.pb";//tensorflow_inception_graph
private static final String LABEL_FILE ="file:///android_asset/labels.txt";//imagenet_comp_graph_label_strings
As suggested in discussion link above, I tried Summarize graph tool on my frozen_1000_graph.pb and got the following output.
No inputs spotted. No variables spotted. Found 1 possible outputs:
(name=InceptionV1/Logits/Predictions/Softmax, op=Softmax) Found
5598451 (5.60M) const parameters, 0 (0) variable parameters, and 114
control_edges Op types used: 472 Const, 230 Mul, 173 Add, 172 Sub, 116
Identity, 114 Sum, 58 Reshape, 58 Conv2D, 57 Rsqrt, 57 Relu, 57
Reciprocal, 57 Square, 57 SquaredDifference, 57 Mean, 57 StopGradient,
13 MaxPool, 9 ConcatV2, 1 Squeeze, 1 RandomUniform, 1 Softmax, 1
RealDiv, 1 QueueDequeueV2, 1 Floor, 1 FIFOQueueV2, 1 BiasAdd, 1
AvgPool.
Please help me understand, how I can fix this issue.
Here are the input to the network created, so if you can add
images = tf.identity(images, name='Inputs') to name the tensor to the network.

Resources