Code conversion from python 2 to python 3 - python-2.7

I'm setting up a new algorithm which combines an object detector(bounding box detector) which is in python 3 and a mask generator which is in python 2. The problem here is I have several python 2 files which is required for the mask generation algorithm. So I tried 2to3 to convert all my python 2 files to python 3. The script seemed like working but as it was a deep learning algorithm(for mask generation when bounding box coordinates are given as input) which needs some pytorch weights to be loaded, while testing the model in python 3 the program was throwing out error like
"RuntimeError: Expected object of type torch.FloatTensor but found
type torch.cuda.FloatTensor for argument #2 ‘weight’"
I have searched in PyTorch forums but none of the posts were useful to me. Is it because my mask generation code is trained in python 2 ?
Does that means while loading the weights and testing the model I should use python 2 not python 3 ? It would be great if someone can shed some light on this. As a work around I can still use the object detector code downgraded to python 2. But still I want to know why it was throwing the error.

The problem is(can) that cpu object is expected but it gpu object. Try to put the object to cpu:
mask.cpu()

I just resolved the issue by re-installing the torch(0.4.0) and torchvision(0.2.1) for my conda environment. I had to downgrade the versions of both of them. Finally I was successful in converting my python 2.7 code to python 3. Thanks to 2to3 library. Actually this error was happening in the image normalize function of the PyTorch. That was an internal function which accepts the image array as tensors.
tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
RuntimeError: expected type torch.cuda.FloatTensor but got torch.FloatTensor

Related

What could be the cause of "ERROR: Unexpected exception while running model: 31704179198" in pyomo?

I get this error message whenever I try to run code that uses the .dae extension.
I've been getting this error on code for a problem I've been working on. Eventually, I ran code that I found online for a different problem and got the same error. This makes me think that it's some error with how I'm running the solver (ipopt) rather than the code itself
Here's a link to the code from GitHub that I'm getting an error message with: https://github.com/Pyomo/pyomo/blob/master/examples/dae/Optimal_Control.py
I've been evoking the solve with:
pyomo solve --solver=ipopt filename.py
Most of the DAE examples come with a second Python script for running the model. Models formulated with Pyomo.DAE are meant to be run from Python scripts and not using the pyomo solve command line. The optimal control problem you're looking at can be solved by using the command
python run_Optimal_Control.py
If you take a look at this file you'll see the syntax for applying a discretization scheme to the dynamic model, creating a solver object using the SolverFactory, solving the model, and plotting the results.

Python 2.7 fitting returns wrong values from multiple methods

I am using Python 2.7. to general data analysis of various sorts however I am facing a problem with fitting my data.
If I try to fit my data using Binned LH, UnBinned LH, Chi2, Kmeans etc - the software will always return the same value (exact!), even if i supply different starting parameters to the the fit. For some method, the returned value is completely off - see attached KMeans plot.
Following packages are showing problems
Probfit
Iminuit
Sklearn
Scipy
Any ideas what is going on?
My own
Same code on a coworkers computer (same setup)

h2o tensorflow deep learning demo fails

I watched the video demo
http://www.lectoro.com/index.php?action=search&ytq=H2O%20TensorFlow%20Deep%20Learning%20Demo
I am able to set up the env using the same spark and sparkling-water versions. The tensorflow runs on python3. Apparently, the example uses python2 code. I am getting SyntaxError: Missing parentheses in call to 'print'. It points to a file with an extreme long path starting with /private ends in context.py.
Is there a way I can get this demo to work with my python3 environment?
yes, right now demo is Python 2 specific. However, we will update it to match Python 3 syntax. I meantime feel free to modify code or look at DeepWater which introduces Deep Learning on top of MxNet (and TF, and Caffe - in progress) https://github.com/h2oai/deepwater
Assuming you are using this python notebook: https://github.com/h2oai/sparkling-water/blob/master/py/examples/notebooks/TensorFlowDeepLearning.ipynb
The changes for Python 3:
In [8] put parantheses around:
print( [c.dim for c in H2O_w] )
print( [c.dim for c in H2O_b] )
In [3] add a list() around the use of range():
sc.parallelize(list(range(NODES)), NODES).map(map_fun).collect()
And [4]:
self._x = list( range(784) )
(I notice this change had already been done in the call to train().)
I couldn't spot anything else, and those changes should be compatible with Python 2. If you still get errors can you post in which section of the notebook that the error happens in?

Python sklearn linear_model object throwing a ValueError when passed a list

I'm running into a weird issue with some linear regression stuff from sklearn. Specifically, linear_model.
I'm trying to do some basic machine learning, and so I have a part of my script that combs through my data and extracts features into a list (of lists) X, and then another part that feeds those features into the fit function. So I've got (roughly)
from sklearn import linear_model
X, y = extractFeaturesFromData(data,numfeatures) # my homemade function
reg = linear_model.LinearRegression()
reg.fit(X,y)
When I run this, I get (along with the traceback)
ValueError: setting an array element with a sequence.
The example here ran fine. And the X and y that extractFeaturesFromData returns are of type 'list', same as in the example. If I use the dummy X and y from the example page, it works fine, but using mine causes it to throw an error.
I've tried varying the number of features extracted into X, and printing out the X and y returned from my function (which shows them to be the same format as their dummy counterparts from the example), but haven't had any luck so far. I'm running python 2.7 on a macbook running 10.9.5. Any idea why this might be happening? Any help would be much appreciated.
Figured it out! It was completely unrelated to my code itself; one of the files that I was importing was a good bit larger than the others, and (I think) was being automatically split into an array, causing the error. Removing that file made everything run fine.

Getting Variables in and out with scipy weave

I'm trying to use SciPy Weave to speed up a loop intensive task in a Python code I'm translating from MatLab. In MatLab I went to great lengths to vectorise it which had it running really fast. I've already tried it in Python with loops and it is unacceptably slow, so although I could probably do what I did in MatLab with NumPy and get it running just as fast I want to learn to use Weave because it will be really useful in the future. I've checked my C++ works here: http://ideone.com/E8ACaq (the variables above the first comment are the ones that I pass to weave) and I have simpler examples working in weave, but I can't get the actual code to play ball in weave!
What I need to know is what format my variables need to be in to get them in and out. Ideally I want a numpy array out and I want to pass in a variety of integers, numpy arrays and doubles as shown below (Python indents not shown because of how code works on here).
def GoodManLookup(CycleData,MaterialID,Materials):
nCyc=len(CycleData)
NRVals=int(Materials[MaterialID]['rvalues'])
NLevels=np.product(Materials[MaterialID]['levels'].shape)
GoodmanAmp=Materials[MaterialID]['data'][:,1]
GoodmanMean=Materials[MaterialID]['data'][:,0]
RValAngles=np.array([np.arctan2(GoodmanData[0:NRVals,1],GoodmanData[0:NRVals,0])*180/math.pi])
CycleAngles=np.arctan2(CycleData[:,0],CycleData[:,1])*180/math.pi
CycleAngles[CycleAngles==180]=179.99
CycleAngles[CycleAngles==90]=89.99
CycleAngles[CycleAngles==0]=0.01
#Get sector of each cycle
SectB=np.tile(RValAngles[0,0:NRVals-1],(nCyc,1))
SectT=np.tile(RValAngles[0,1:NRVals],(nCyc,1))
Angles=np.tile(np.array([CycleAngles]).swapaxes(1,0),(1,NRVals-1))
Sect=np.array([np.sum(np.bitwise_and(Angles>SectB,Angles<SectT)*np.tile(np.array([range(0,NRVals-1)]),(nCyc,1)),axis=1)]).swapaxes(1,0)
Amplitude=CycleData[:,0];
Mean=CycleData[:,1];
N=Materials[MaterialID]['levels'];
if CurveFit==1:
PowerLaw=True
elif CurveFit==0:
PowerLaw=False
code_cpp ="""//code on IDE one
"""
P = weave.inline(code_cpp,['Sect','Amplitude','Mean','nCyc','NLevels','PowerLaw','N','NRVals','GoodmanMean','GoodmanAmp'],compiler='gcc')
I'm new to Python and C++ so this is what you might call a perfect storm of incompetence!