I am trying to perform image classification using Sentinel 1.
I am new to coding so I am using this: http://mortcanty.github.io/src/s1class.html
I got an error saying: auxil is not a module so I thought of installing auxil.py from git hub:
https://github.com/mortcanty/CRCPython/blob/master/src/auxil/auxil.py
but some modules/libraries are outdated so I keep getting errors
any help is much appreciated
It appears the repository you linked is using python 2.7. Are you using python 2 or python 3? Python 2 is no longer being supported (as of 1/1/2020). But, you can still use it--it's just not advised.
You could just define the classes and methods from that repository inside your own code and they will work. For instance, I needed the class Cpm:
class Cpm(object):
'''Provisional means algorithm'''
def __init__(self,N):
self.mn = np.zeros(N)
self.cov = np.zeros((N,N))
self.sw = 0.0000001
def update(self,Xs,Ws=None):
n,N = np.shape(Xs)
if Ws is None:
Ws = np.ones(n)
sw = ctypes.c_double(self.sw)
mn = self.mn
cov = self.cov
provmeans(Xs,Ws,N,n,ctypes.byref(sw),mn,cov)
self.sw = sw.value
self.mn = mn
self.cov = cov
def covariance(self):
c = np.mat(self.cov/(self.sw-1.0))
d = np.diag(np.diag(c))
return c + c.T - d
def means(self):
return self.mn
Related
I want to design a python block to remove the white noise of my received signal
As shown in the red circle:
So I first tested on Spyder whether my code can remove noise normally
I got the result as pictured:
Just when I finished the test, I wanted to transfer it to the python block, but it couldn't execute normally, causing a crash
Below is the test program on my python block and spyder:
i = 0
j = 0
t = 0
data=[]
buf=[]
wav = numpy.fromfile(open('C:/Users/user/Desktop/datas'), dtype=numpy.uint8)
for i in range(int(len(wav)/10)):
for j in range(10):
buf.append(wav[(i*10)+j])
if (buf[j]<=180)and(buf[j]>=90):
t = t+1
if t < 6:
data = numpy.append(data,buf)
# else:
# data = numpy.append(data,numpy.zeros(10))
t= 0
j = 0
buf.clear()
"""
Embedded Python Blocks:
Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__ will
be the parameters. All of them are required to have default values!
"""
import numpy as np
from gnuradio import gr
i = 0
j = 0
t = 0
data=[]
buf=[]
class blk(gr.sync_block): # other base classes are basic_block, decim_block, interp_block
"""Embedded Python Block example - a simple multiply const"""
def __init__(self): # only default arguments here
"""arguments to this function show up as parameters in GRC"""
gr.sync_block.__init__(
self,
name='noise out', # will show up in GRC
in_sig=[np.float32],
out_sig=[np.float32]
)
# if an attribute with the same name as a parameter is found,
# a callback is registered (properties work, too).
def work(self, input_items, output_items):
"""example: multiply with constant"""
np.frombuffer(input_items, dtype=np.uint8)
for i in range(int((len(input_items[0]))/10)):
for j in range(10):
buf.append(input_items[0][(i*10)+j])
if (buf[j]<=180)and(buf[j]>=90):
t = t+1
if t < 6:
data = numpy.append(data,buf)
else:
data = numpy.append(data,numpy.zeros(10))
t= 0
j = 0
buf.clear()
for i in range(len(output_items[0])):
output_items[0][i]=data[i]
return len(output_items[0])
What should I do to modify it if I want it to run normally?
This is the code. I think the solver could be glpk instead of gurobi. I got the error before it tries to solve the problem.
from pyomo.environ import *
from pyomo.opt import SolverFactory, SolverStatus
PrintSolverOutput = False ###
model = ConcreteModel()
model.dual = Suffix(direction =Suffix.IMPORT)
model.X = Var(I,T, within = NonNegativeReals)
model.In = Var(I,T, within = NonNegativeReals)
model.S = Var(T, within = NonNegativeReals)
def Objetivo(model):
return (sum(C[i]*model.X[i,t]*((1+tau[i])**(t-1))+Ch[i]*model.In[i,t]
for i in I for t in T)
+sum(Cs*model.S[t]
for t in T)
)
model.Total_Cost = Objective(rule = Objetivo, sense= minimize)
def Balance(model,i,t):
if t==1:
return model.X[i,t]+I0[i]-model.In[i,t]==D[i,t]
elif t>=2:
return model.X[i,t]+model.IN[i,t-1]-model.In[i,t]==D[i,t]
def Horas(model, t):
return sum(r[i]*model.X[i,t] for i in I) <= H+model.S[t]
def Limite(model,t):
return model.S[T]<=LS
model.RBalance = Constraint(I,T,rule=Balance)
model.RHoras = Constraint(T,rule=Horas)
model.RLimiteoras = Constraint(T,rule=Limite)
opt = SolverFactory("gurobi")
This is the data
I forgot to put the data before.
T = [1,2,3,4,5,6]
I = ['A','B']
D = {('A',1):300,
('A',2):400,
('A',3):500,
('A',4):600,
('A',5):800,
('A',6):700,
('B',1):700,
('B',2):600,
('B',3):500,
('B',4):400,
('B',5):300,
('B',6):400}
tau = {'A':0.02,'B':0.01}
r = {'A':5,'B':3}
H = 3520
LS = 800
C = {'A':150,'B':120}
Ch = {'A':8,'B':4}
Cs = 6
I0 = {'A':100,'B':250}
error
The code is from a youtube tutorial and it worked for him but not for me. Why?
Aside from fixing two typos in your code, this model computes and solves for me without that error. Make these fixes, run it again, re-post the exact error with line number and the exact code that produces the error if stuck...
Here is my LossFunction, when I use this function, it will make this error.
And I have tested that using the nn.L1Loss() instead of my LossFunction, and the network is ok.
what should I do? Thanks for your help!
class LossV1(nn.Module):
def __init__(self,weight=1,pos_weight=1,scale_factor=2.5):
super(LossV1,self).__init__()
self.weight = weight
self.pos_weight = pos_weight
self.scale_factor = scale_factor
def forward(self,pred,truth):
objmask = torch.tensor(truth[:,:6,:,:],dtype=torch.float32,requires_grad=False)
#没有物体的Boxes,置信度损失*0.4
objmask[objmask<0.65] = 0.4
#辅助Boxes,系数0.8
objmask[(objmask>0.649)*(objmask<0.949)] = 0.8
objLoss = torch.sum(objmask*self.myBCEWithLogitsLoss(pred[:,:6,:,:],truth[:,:6,:,:]))
#没有物体的Boxes,只计算置信度损失
objmask[objmask<0.41] = 0
personLoss = torch.sum(objmask*self.myBCEWithLogitsLoss(pred[:,6:12,:,:],truth[:,6:12,:,:]))
carLoss = torch.sum(objmask*self.myBCEWithLogitsLoss(pred[:,12:18,:,:],truth[:,12:18,:,:]))
wLoss = torch.sum(objmask*self.myL2Loss(pred[:,18:24,:,:],truth[:,18:24,:,:]))
hLoss = torch.sum(objmask*self.myL2Loss(pred[:,24:,:,:],truth[:,24:,:,:]))
return objLoss+personLoss+carLoss+wLoss+hLoss
def myBCEWithLogitsLoss(self,x,y):
#pos_weight>1增加召回,pos_weight<1提高精度
return -self.weight*(self.pos_weight*y*torch.log(torch.sigmoid(x))+(1-y)*torch.log(1-torch.sigmoid(x)))
def myL2Loss(self,x,y):
return torch.pow(self.scale_factor*torch.sigmoid(x/self.scale_factor) - y,2)
I just remove the objmask,and compute it in my generation function,then pass it on to LossFunction with truth label,and the network works.
I can't understand I have already done that making the requires_grad=False,why pytroch still compute the gradient.
Code &source
ubuntu 16.04
GNU radio 3.7.12
UHD 3.10.1.1
Numpy 1.13.1
Scipy 0.19.1
I met this problem when I use Gnuradio to genarate a dataset,These codes used to work well until I change another computer,I search this problem and find that someone think it's a problem of Numpy,but I'm not surely understand .
If anybody has any idea how I could debug this or what could possibly be the cause of this; all tips are welcome!
Here is the error report:
StackTrace:
double free or corruption (out): 0x0000000001e4b030
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffa89d707e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7ffa89d7937a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffa89d7d53c]
/usr/local/lib/libgnuradio-runtime-3.7.12git.so.0.0.0(_ZN2gr5blockD1Ev+0x77)[0x7ffa882768e7]
/usr/local/lib/libgnuradio-mediatools.so(+0xd065)[0x7ffa6940b065]
/usr/local/lib/python2.7/dist-packages/mediatools/_mediatools_swig.so(+0xbd2a)[0x7ffa6962bd2a]
/usr/local/lib/python2.7/dist-packages/mediatools/_mediatools_swig.so(+0xfe60)[0x7ffa6962fe60]
python(PyObject_Call+0x43)[0x4b0cb3]
python(PyObject_CallFunctionObjArgs+0x16a)[0x4b97fa]
/usr/local/lib/python2.7/dist-packages/mediatools
/_mediatools_swig.so(+0x12640)[0x7ffa69632640]
python[0x4fd4e6]
python[0x4fd4e6]
python(PyDict_SetItem+0x442)[0x4a0d22]
python(_PyModule_Clear+0x133)[0x502bf3]
python(PyImport_Cleanup+0x1ac)[0x50275c]
python(Py_Finalize+0x89)[0x500489]
python(Py_Main+0x46f)[0x49df2f]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7ffa89d19830]
python(_start+0x29)[0x49d9d9]
======= Memory map: ========
and Here is the code:
#!/usr/bin/env python
from gnuradio import gr, blocks
import mediatools
import numpy as np
class source_alphabet(gr.hier_block2):
def __init__(self, dtype="discrete", limit=10000, randomize=False):
if(dtype == "discrete"):
gr.hier_block2.__init__(self, "source_alphabet",
gr.io_signature(0,0,0),
gr.io_signature(1,1,gr.sizeof_char))
self.src = blocks.file_source(gr.sizeof_char, "source_material/gutenberg_shakespeare.txt")
self.convert = blocks.packed_to_unpacked_bb(1, gr.GR_LSB_FIRST);
#self.convert = blocks.packed_to_unpacked_bb(8, gr.GR_LSB_FIRST);
self.limit = blocks.head(gr.sizeof_char, limit)
self.connect(self.src,self.convert)
last = self.convert
# whiten our sequence with a random block scrambler (optionally)
if(randomize):
rand_len = 256
rand_bits = np.random.randint(2, size=rand_len)
self.randsrc = blocks.vector_source_b(rand_bits, True)
self.xor = blocks.xor_bb()
self.connect(self.randsrc,(self.xor,1))
self.connect(last, self.xor)
last = self.xor
else: # "type_continuous"
gr.hier_block2.__init__(self, "source_alphabet",
gr.io_signature(0,0,0),
gr.io_signature(1,1,gr.sizeof_float))
self.src = mediatools.audiosource_s(["source_material/serial-s01-e01.mp3"])
self.convert2 = blocks.interleaved_short_to_complex()
self.convert3 = blocks.multiply_const_cc(1.0/65535)
self.convert = blocks.complex_to_float()
self.limit = blocks.head(gr.sizeof_float, limit)
# print(self.limit)
self.connect(self.src,self.convert2,self.convert3, self.convert)
last = self.convert
# connect head or not, and connect to output
if(limit==None):
self.connect(last, self)
else:
self.connect(last, self.limit, self)
if __name__ == "__main__":
print "QA..."
# Test discrete source
tb = gr.top_block()
src = source_alphabet("discrete", 1000)
snk = blocks.vector_sink_b()
tb.run()
# Test continuous source
tb = gr.top_block()
src = source_alphabet("continuous", 1000)
snk = blocks.vector_sink_f()
tb.run()
Trying to run the paraboloid example from OpenMDAO documentation but using the ScipyOptimizer, and I get the error in the title. Not sure what I'm doing wrong. I've attached a MWE below.
OpenMDAO 1.7.3, Python 3.6.0.
# -*- coding: utf-8 -*-
"""
Paraboloid tutorial from OpenMDAO documentation:
https://openmdao.readthedocs.io/en/latest/usr-guide/tutorials/paraboloid-tutorial.html
"""
# import print function from Python 3 if we are using Python 2
from __future__ import print_function
# import all the components we need from openmdao module
from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer
class Paraboloid(Component):
""" Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """
def __init__(self):
super(Paraboloid, self).__init__()
self.add_param('x', val=0.0)
self.add_param('y', val=0.0)
self.add_output('f_xy', shape=1)
def solve_nonlinear(self, params, unknowns, resids):
"""f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
"""
x = params['x']
y = params['y']
unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
def linearize(self, params, unknowns, resids):
""" Jacobian for our paraboloid."""
x = params['x']
y = params['y']
J = {}
J['f_xy', 'x'] = 2.0*x - 6.0 + y
J['f_xy', 'y'] = 2.0*y + 8.0 + x
return J
# This if statement executes only if the script is run as a script, not if the
# script is imported as a module. It's not necessary for this demo, but it
# is good coding practice.
if __name__ == "__main__":
# initiallize the overall problem
top = Problem()
# each problem has a root "group" that contains the component evaluating
# the objective function
root = top.root = Group()
# define components with the independent variables and their initial guesses
root.add('p1', IndepVarComp('x', 3.0))
root.add('p2', IndepVarComp('y', -4.0))
# add the component that defines our objective function
root.add('p', Paraboloid())
# connect the components together
root.connect('p1.x', 'p.x')
root.connect('p2.y', 'p.y')
# specify our driver for the problem (optional)
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['disp'] = True # don't display optimizer output
# set up the problem
top.setup()
# run the optimization
top.run()
print(top['p.f_xy'])
I figured it out. In a classic moment of brain-fartiness, I forgot to add my design variables and objective functions to the driver. Something like this is what I was missing:
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.add_desvar('p1.x', lower=-50, upper=50)
top.driver.add_desvar('p2.y', lower=-50, upper=50)
top.driver.add_objective('p.f_xy')