ModuleNotFoundError: No module named 'models' on converting .pt to onnx - torch

I am trying to convert my yolov5 model .pt file to onnx format.It shows this error
Traceback (most recent call last): File "onnx.py", line 10, in <module> model = torch.load(weights) File "/home/nawinrajkumar/TrafficFlow/Traffic-flow-kgx/venv/lib/python3.8/site-packages/torch/serialization.py", line 789, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "/home/nawinrajkumar/TrafficFlow/Traffic-flow-kgx/venv/lib/python3.8/site-packages/torch/serialization.py", line 1131, in _load result = unpickler.load() File "/home/nawinrajkumar/TrafficFlow/Traffic-flow-kgx/venv/lib/python3.8/site-packages/torch/serialization.py", line 1124, in find_class return super().find_class(mod_name, name)
I used this code for the conversion of my model.
import torch
import torch.onnx
import sys
# Load your PyTorch model
weights = '/home/nawinrajkumar/TrafficFlow/Traffic-flow-kgx/yolov5s.pt'
model = torch.load(weights)
# Export the model to ONNX format
torch.onnx.export(model,
torch.randn(1, 3, 224, 224),
"model.onnx",
export_params=True)
What should I do to convert my .pt model to .onnx format. I also tried the export.py on the yolov5 repository and it doesn't help too.

Related

How to give R's "matchit" a formula object via Python's r2py?

I use rpy2 to use R in Python. Especially I want to use the MatchIt package but stuck on a detail. The call to the primary function of that package in R looks like this
# R code
m.out1 <- matchit(
gruppe ~ geschlecht + alter + pflege,
data = df,
method = "nearest",
distance = "glm"
)
The first argument is a "formula". I don't have an idea how to create such an object/argument in Python code? The other three arguments are no problem. The error from rpy2 is this:
[WARNING] R[write to console]: Error: 'formula' must be a formula object.
Traceback (most recent call last):
...
File "...\AppData\Roaming\Python\Python39\site-packages\rpy2\rinterface.py", line 813, in __call__
raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Error: 'formula' must be a formula object.
This is the Python code producing that problem.
# Python code
import pandas
import rpy2
from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
import rpy2.robjects.pandas2ri as pandas2ri
r_package_matchit = robjects.packages.importr('MatchIt')
func_matchit = robjects.r['matchit']
df = pandas.DataFrame({
'gruppe': list('IICC'),
'geschlecht': list('mwmw'),
'alter': range(4),
'pflege': range(4)
})
# convert the data frame from Pandas to R
with robjects.conversion.localconverter(
robjects.default_converter + pandas2ri.converter):
rdf = robjects.conversion.py2rpy(df)
func_matchit(formula='gruppe ~ geschlecht + alter + pflege',
data=rdf, method='nearest',
distance='glm')
Mutch easier then I thougt. rpy2 offers a Formula() for cases like this.
import rpy2
from rpy2.robjects import Formula
# ...
func_matchit(formula=Formula('gruppe ~ geschlecht + alter + pflege'),
data=rdf, method='nearest',
distance='glm')

R.keras error during call to fit function

I am new to R keras, so please bear with me. I am trying to build a simple model using variables that are categorical, but I've recast as numeric.
I can get examples working from various tutorials in R/keras with my current installation so I know its not in reticulate or tensorflow or even R. However, when I try to use my own data to create the simple model, I obtain the following errors during the "fit" execution:
I'm pretty sure its my training data format, but I cannot for the life of me figure out what is going wrong. Thank you kindly in advance.
# Fit
model_one <- model %>%
+ fit(training,
+ trainLabels,
+ epochs = 100,
+ batch_size = 32,
+ validation_split = 0.2)
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: in user code:
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function *
outputs = self.distribute_strategy.run(
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\engine\training.py:533 train_step **
y, y_pred, sample_weight, regulari
I've upload my script and sample data file to github:
Sample Data and Script to reproduce error
Actually, found the error:
One hot encoding generates a 2 column matrix:
# One hot encoding
trainLabels <- to_categorical(trainingtarget)
testLabels <- to_categorical(testtarget)
print(testLabels[1:10,])
and the model was expecting 3 columns.
I changed the model call to automatically accept the correct number of columns based on the variables instead:
model %>%
layer_dense(units = 8, activation = 'relu', input_shape = ncol(test)) %>%
layer_dense(units = ncol(trainLabels), activation = 'softmax')

NameError: name 'gensim' is not defined (doc2vec similarity)

I have gensim installed in my system. I did the summarization with gensim. NOw I want to find the similarity between the sentence and it showing an error. sample code is given below. I have downloaded the Google news vectors.
from gensim.models import KeyedVectors
#two sample sentences
s1 = 'the first sentence'
s2 = 'the second text'
#model = gensim.models.KeyedVectors.load_word2vec_format('../GoogleNews-vectors-negative300.bin', binary=True)
model = gensim.models.KeyedVectors.load_word2vec_format('./data/GoogleNews-vectors-negative300.bin.gz', binary=True)
#calculate distance between two sentences using WMD algorithm
distance = model.wmdistance(s1, s2)
print ('distance = %.3f' % distance)
Error#################################################
****Traceback (most recent call last): File "/home/abhi/Desktop/CHiir/CLustering &
summarization/.idea/FInal_version/sentence_embedding.py", line 7, in
model = gensim.models.KeyedVectors.load_word2vec_format('./data/GoogleNews-vectors-negative300.bin.gz',
binary=True) NameError: name 'gensim' is not defined****
Importing with from x import y only lets you use y, but not x.
You can either do import gensim instead of from gensim.models import KeyedVectors, or you can directly use the imported KeyedVectors:
model = KeyedVectors.load_word2vec_format('./data/GoogleNews-vectors-negative300.bin.gz', binary=True)

Keras Model doesn't accept input_layer

I am a newbie to Deep Learning, and am trying to build a model in R using Keras. I have 20,000 32x32x3 images stored in an array for model training.When I run:
model = keras_model_sequential()
model %>% layer_input(shape = c(32,32,3))
I get the following error :
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: int() argument must be a string or a number, not 'Sequential'
Detailed traceback:
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 1380, in Input
input_tensor=tensor)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 1287, in __init__
name=self.name)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/backend.py", line 545, in placeholder
x = array_ops.placeholder(dtype, shape=shape, name=name)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1499, in placeholder
shape = tensor_shape.as_shape(shape)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 80
can anyone help me figure out how to setup an input layer for my model?
When using Sequential API you don't use the layer_input function.
Your first layer will need to have an input_shape argument that will act as layer_input. For example:
model %>%
layer_dense(units = 32, input_shape = c(784)) %>%
layer_activation('relu') %>%
layer_dense(units = 10) %>%
layer_activation('softmax')
You can use the layer_input function when using the Functional API. See more here.

I have much more than three elements in every class, but I get this error: "class cannot be less than k=3 in scikit-learn"

This is my target (y):
target = [7,1,2,2,3,5,4,
1,3,1,4,4,6,6,
7,5,7,8,8,8,5,
3,3,6,2,7,7,1,
10,3,7,10,4,10,
2,2,2,7]
I do not know why while I'm executing:
...
# Split the data set in two equal parts
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0)
# Set the parameters by cross-validation
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
'C': [1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
scores = ['precision', 'recall']
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(SVC(C=1), tuned_parameters)#scoring non esiste
# I get an error in the line below
clf.fit(X_train, y_train, cv=5)
...
I get this error:
Traceback (most recent call last):
File "C:\Python27\SVMpredictCROSSeGRID.py", line 232, in <module>
clf.fit(X_train, y_train, cv=5) #The minimum number of labels for any class cannot be less than k=3.
File "C:\Python27\lib\site-packages\sklearn\grid_search.py", line 354, in fit
return self._fit(X, y)
File "C:\Python27\lib\site-packages\sklearn\grid_search.py", line 372, in _fit
cv = check_cv(cv, X, y, classifier=is_classifier(estimator))
File "C:\Python27\lib\site-packages\sklearn\cross_validation.py", line 1148, in check_cv
cv = StratifiedKFold(y, cv, indices=is_sparse)
File "C:\Python27\lib\site-packages\sklearn\cross_validation.py", line 358, in __init__
" be less than k=%d." % (min_labels, k))
ValueError: The least populated class in y has only 1 members, which is too few. The minimum number of labels for any class cannot be less than k=3.
The algorithm requires that there be at least 3 instances for a label in your training set. Although your target array contains at least 3 instances of each label, but when you split the data between training and testing, not all the training labels have 3 instances.
You either need to merge some class labels or increase your training samples to solve the problem.
If you can't split the test and training set with each class populated enough in each fold, then try updating the Scikit library.
pip install -U scikit-learn
You'll get the same message as a warning, so you can run the code.

Resources