I downloaded the code from here https://github.com/SpaceNetChallenge/SpaceNet_SAR_Buildings_Solutions, specifically using model 1. I downloaded the weights corresponding and created the following file to load the model and test. First, I copy the Unet part in main.py into a separate file umodel.py and the test file as follows
import torch
exec(open("./umodel.py").read())
network_data = torch.load('snapshot_fold_8_best')
print(network_data.keys())
import sys
sys.path.append("geffnet")
class Namespace:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
args = Namespace(extra_num = 1,
dec_ch = [32, 64, 128, 256, 1024],
stride=32,
net='b5',
bot1x1=True,
glob=True,
bn=True,
aspp=True,
ocr=True,
aux_scale=True)
def load_state_dict(model, state_dict):
missing_keys = []
# from UnetOS.umodel import Unet
exec(open("./umodel.py").read())
try:
from torch.hub import load_state_dict_from_url
except ImportError:
from torch.utils.model_zoo import load_url as load_state_dict_from_url
# from UnetOS.umodel import *
model = Unet(extra_num = args.extra_num, dec_ch = args.dec_ch, stride=args.stride, net=args.net, bot1x1 = args.bot1x1, glob=args.glob, bn=args.bn, aspp=args.aspp,
ocr=args.ocr, aux = args.aux_scale > 0).cuda()
load_state_dict(model, network_data)
My question is, why exec(open("./umodel.py").read()) works nicely but whenever I tried to import from umodel import Unet it has errors
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10492/1282530406.py in <module>
9 # ah
10 model = Unet(extra_num = args.extra_num, dec_ch = args.dec_ch, stride=args.stride, net=args.net, bot1x1 = args.bot1x1, glob=args.glob, bn=args.bn, aspp=args.aspp,
---> 11 ocr=args.ocr, aux = args.aux_scale > 0).cuda()
12 #model = Unet()
13 #print(network_data.key())
D:\hines\Pretrained\1-zbigniewwojna\UnetOS\umodel.py in __init__(self, extra_num, dec_ch, stride, net, bot1x1, glob, bn, aspp, ocr, aux)
238 ['ir_r4_k5_s2_e6_c192_se0.25'],
239 ['ir_r1_k3_s1_e6_c320_se0.25']]
--> 240 enc = GenEfficientNet(in_chans=3, block_args=decode_arch_def(arch_def, depth_multiplier),
241 num_features=round_channels(1280, channel_multiplier, 8, None), stem_size=32,
242 channel_multiplier=channel_multiplier, act_layer=resolve_act_layer({}, 'swish'),
NameError: name 'decode_arch_def' is not defined
The main file is as follow https://github.com/SpaceNetChallenge/SpaceNet_SAR_Buildings_Solutions/blob/master/1-zbigniewwojna/main.py
From the error message, it appears that decode_arch_def is not available and looking at your imports, that has to come from from geffnet.efficientnet_builder import * (it does https://github.com/rwightman/gen-efficientnet-pytorch/blob/master/geffnet/efficientnet_builder.py)
Your exec must have worked because it followed a similar import, that brought decode_arch_def in scope - exec() executes code in the current scope, so it will work because in that scope decode_arch_def is already defined.
However, when you import, the imported module itself doesn't have the function you need in scope. You should add the required import statements to the file you're importing to bring them into scope and it should work.
For example, with a mod.py containing this:
def my_func():
print(datetime.now())
This works:
from datetime import datetime
exec(open("./mod.py").read())
my_func()
But this does not:
from datetime import datetime
import mod
mod.my_func()
To make that work, mod.py would have to be:
from datetime import datetime
def my_func():
print(datetime.now())
And the import of datetime wouldn't be needed in the main program, since it's not referenced there. Your code has a similar issue - you need to determine all the dependencies of your Unet class and import them.
Related
I have following block of code:
i want to use ICCLIM (Indice Calculation CLIMate) is a Python library
To calculate some indices
how to solve this problem ??
files = ['tasmax_day_CNRM-CM5_historical_r1i1p1_19950101-19991231.nc', 'tasmax_day_CNRM-CM5_historical_r1i1p1_20000101-20041231.nc', 'tasmax_day_CNRM-CM5_historical_r1i1p1_20050101-20051231.nc']
dt1 = datetime.datetime(1998,1,1)
dt2 = datetime.datetime(2005,12,31)
out_f ='SU_JJA_CNRM-CM5_historical_r1i1p1_1998-2005.nc' # OUTPUT FILE: summer season values of SU
icclim.index(index_name='SU', in_files=files, var_name='tasmax', time_range=[dt1, dt2], slice_mode='JJA', out_file=out_f)
dt1 = datetime.datetime(1998,1,1)
dt2 = datetime.datetime(2005,12,31)
out_f = 'SU_JJA_CNRM-CM5_historical_r1i1p1_1998-2005.nc' # OUTPUT FILE: summer season values of SU
icclim.index(index_name='SU', in_files=files, var_name='tasmax', time_range=[dt1, dt2], slice_mode='JJA', out_file=out_f)
and the import list is
import numpy
import cython
import netcdftime
import cftime
import netCDF4
import sys
import glob
import os
import datetime
import dask
from netCDF4 import Dataset
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xarray
import nc_time_axis
import logging
import pytest
import setuptools
import xclim
import icclim
we i run the code the error keep show
here is the error
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2572/579676545.py in <module>
2 dt2 = datetime.datetime(1986,12,31)
3
----> 4 icclim.index(index_name='CD', in_files='C:/Users/Dana/Desktop/icclim/pr_year_1986.nc',time_range=[dt1, dt2], var_name='pr', slice_mode='year', out_file='C:/Users/Dana/Desktop/icclim/new_pr_year_1986.nc')
C:\ProgramData\Anaconda\lib\site-packages\icclim\main.py in index(in_files, var_name, index_name, slice_mode, time_range, out_file, threshold, transfer_limit_Mbytes, callback, callback_percentage_start_value, callback_percentage_total, base_period_time_range, window_width, only_leap_years, ignore_Feb29th, interpolation, out_unit, netcdf_version, user_index, save_percentile, logs_verbosity, indice_name, user_indice)
127 log.set_verbosity(logs_verbosity)
128
--> 129 log.start_message()
130 callback(callback_percentage_start_value)
131 if indice_name is not None:
C:\ProgramData\Anaconda\lib\site-packages\icclim\icclim_logger.py in start_message(self)
62
63 # flake8: noqa
---> 64 time_now = time.asctime(time.gmtime()) + " " + self.timezone
65 if self.verbosity == Verbosity.SILENT:
66 return
AttributeError: 'IcclimLogger' object has no attribute 'timezone'
Sorry for the late reply. This was due to a bug within icclim and it has been fixed in a later version.
I advise you to try the latest version (5.1.0) available on pypi or conda-forge.
I have a series of Python modules. Each one contains a class with a name matching its directory:
a/__init__.py
a/aa/__init__.py
a/ab/__init__.py
b/ba/__init__.py
b/bb/__init__.py
c/ca/__init__.py
c/ca/caa/__init__.py
utils/__init__.py
I have a free variable and function that maps the class in each module to a string, in utils/__init__.py:
import a
import a.aa
import a.ab
import b
import b.ba
import b.bb
import c
import c.ca
import c.ca.caa
MAPPING = {
"Foobar": a.A,
"Apple": a.aa.Aa,
"Banana": a.ab.Ab,
"Clementine": b.ba.Ba,
"Granola": b.B,
"Donut": b.bb.Bb,
"Hashbrowns": c.C,
"Egg": c.ca.Ca,
"Furniture": c.ca.caa.Caa,
}
def find_class(s):
return MAPPING[s]
However, I would like to use this function inside some of these classes. For example, b/ba/__init__.py may contain the following:
import utils
class Ba():
def mycall(self, s):
return utils.find_class(s)
This causes an issue trying to import that class:
Python 3.9.6 (default, Jun 29 2021, 05:25:02)
>>> from b.ba import Ba
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3imports/b/ba/__init__.py", line 1, in <module>
import utils
File ".../python3imports/utils/__init__.py", line 15, in <module>
"Clementine": b.ba.Ba,
AttributeError: module 'b' has no attribute 'ba'
>>>
Is there a way to have a "master list" of classes, and use that list within those classes?
It's a little hacky but this solution is functional:
MAPPING = {}
def _make_mapping(m):
import a
import a.aa
import a.ab
import b
import b.ba
import b.bb
import c
import c.ca
import c.ca.caa
m["Foobar"] = a.A
m["Apple"] = a.aa.Aa
m["Banana"] = a.ab.Ab
m["Clementine"] = b.ba.Ba
m["Granola"] = b.B
m["Donut"] = b.bb.Bb
m["Hashbrowns"] = c.C
m["Egg"] = c.ca.Ca
m["Furniture"] = c.ca.caa.Caa
def find_class(s):
if not MAPPING:
_make_mapping(MAPPING)
return MAPPING[s]
This prevents any class imports from being attempted until find_class is run for the first time, determines that MAPPING is empty, and populates it. In this way, importing utils in one of the listed classes keeps a circular import from occurring.
I have written a python ROS node to subscribe to two different topics and uses the ApproximateTimeSynchroniser to basically to match the messages via their timestamp and bundle these messages into one callback routine. If I use the callback to simply print out the two messages received it works fine.
However, I would like to populate a geometry_msgs/Pose message using the x and y positions from the received Pose 2D data in the callback routine.Using the code below, I have done this by making an empty Pose object in the callback routine:
#!/usr/bin/env python
import message_filters
import rospy
from laser_line_extraction.msg import LineSegmentList
from geometry_msgs.msg import Pose, Pose2D
from std_msgs.msg import Int32, Float32
rospy.init_node('simul-subscriber')
def callback(Pose2D, LineSegmentList):
pose = Pose()
pose.position.x = Pose2D.position.x
pose.position.y = Pose2D.position.y
pose.position.z = 0
#print(Pose2D, LineSegmentList, pose)
print(pose)
print(LineSegmentList)
line_segment_sub = message_filters.Subscriber('/line_segments', LineSegmentList)
pose_2D_sub = message_filters.Subscriber('/pose2D',Pose2D)
ts = message_filters.ApproximateTimeSynchronizer([line_segment_sub, pose_2D_sub], 10, 0.1, allow_headerless=True)
ts.registerCallback(callback)
rospy.spin()
Unfortunately, this gives a weird error when running this node:
[ERROR] [1535552711.709928, 1381.743000]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7f7af3cee9d0>>
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 75, in callback
self.signalMessage(msg)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 57, in signalMessage
cb(*(msg + args))
File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 287, in add
self.signalMessage(*msgs)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 57, in signalMessage
cb(*(msg + args))
File "/home/elisabeth/catkin_ws/src/hector_quadrotor/hector_quadrotor_demo/python_scripts/simul-subscriber.py", line 14, in callback
pose.position.x = Pose2D.position.x
AttributeError: 'LineSegmentList' object has no attribute 'position'
This is odd as the position attribute is only referenced for the Pose 2D and not the LineSegmentList msg. I suspect this is most a python issue than a ROS issue, any help somebody could give would be much appreciated!
I am following the example found at http://wiki.ros.org/message_filters where they took two different topics image and cameraInfo and passed them into the callback function
1 import message_filters
2 from sensor_msgs.msg import Image, CameraInfo
3
4 def callback(image, camera_info):
5 # Solve all of perception here...
6
7 image_sub = message_filters.Subscriber('image', Image)
8 info_sub = message_filters.Subscriber('camera_info', CameraInfo)
9
10 ts = message_filters.TimeSynchronizer([image_sub, info_sub], 10)
11 ts.registerCallback(callback)
12 rospy.spin()
You confuse class names with object names.
The fix to your program should be straight forward.
I've changed Pose2D to pose2d, and LineSegmentList to linesegmentlist where it was necessary and comented it with # --- Change ...:
#!/usr/bin/env python
import message_filters
import rospy
from laser_line_extraction.msg import LineSegmentList
from geometry_msgs.msg import Pose, Pose2D
from std_msgs.msg import Int32, Float32
rospy.init_node('simul-subscriber')
# --- CHANGE: using proper object names instread of class names
def callback(pose2d, linesegmentlist):
pose = Pose()
# --- CHANGE: using the object
pose.position.x = pose2d.position.x
pose.position.y = pose2d.position.y
pose.position.z = 0
#print(pose2d, linesegmentlist, pose)
print(pose)
print(linesegmentlist)
line_segment_sub = message_filters.Subscriber('/line_segments', LineSegmentList)
pose_2D_sub = message_filters.Subscriber('/pose2D',Pose2D)
ts = message_filters.ApproximateTimeSynchronizer([line_segment_sub, pose_2D_sub], 10, 0.1, allow_headerless=True)
ts.registerCallback(callback)
rospy.spin()
I have copied all the codes to the work directory on all my engine machines. And my code are:
my_test.py
my_startegy.py
main.py
So, main.py will be ran on Client Machine, the codes in main.py are:
from ipyparallel import Client
import my_test
import my_strategy as strategy
class Beck_Test_Parallel(object):
"""
"""
def __init__(self):
self.rc = None
self.dview = None
def start_client(self, path):
self.rc = Client(path)
self.dview = self.rc[:]
#self.dview.push(dict(
# Account=my_test.Account,
# dataImport=my_test.dataImport
# ))
def parallel_map(self, deal_function, accounts):
import my_test
return self.dview.map_sync(deal_function, accounts)
def create_accounts(time_list, account):
accounts = []
for index, time in enumerate(time_list):
acc = my_test.Account(
strategy.start,
strategy.end,
strategy.freq,
strategy.universe_code,
strategy.capital_base,
strategy.short_capital,
strategy.benchmark,
strategy.self_defined
)
account.share_data(acc)
acc.iniData2()
acc.iniData3()
acc.current_time = time
acc.days_counts = index+1
acc.dynamic_record['capital'] = acc.capital_base
del acc.connect
accounts.append(acc)
return accounts
def let_us_deal(account):
account = strategy.handle_data(account)
print ' >>>', account.current_time
return account
if __name__ == '__main__':
account = my_test.Account(
strategy.start,
strategy.end,
strategy.freq,
strategy.universe_code,
strategy.capital_base,
strategy.short_capital,
strategy.benchmark,
strategy.self_defined
)
account.iniData()
account.iniData2()
account.iniData3()
time_list = my_test.get_deal_time_list(account)
accounts = parallel.create_accounts(time_list, account)
back_test_parallel = parallel.Beck_Test_Parallel()
back_test_parallel.start_client(
'/home/fit/.ipython/profile_default/security/ipcontroller-client.json')
back_test_parallel.dview.execute('import my_test')
back_test_parallel.dview.execute('import my_strategy as strategy')
# get the result
result = back_test_parallel.parallel_map(let_us_deal, accounts)
for acc in result.get():
print acc.reselected_stocks, acc.current_time
And I have imported my_test module in parallel_map() function in Class Back_Test_Parallel and I have also imported my_test module in back_test_parallel.dview.execute('import my_test').
And the corresponding modules are on the engine machine's work directory. I have copied the ipcontroller-client.json and ipcontroller-engine.json to the work directory on engine machine.
But when it runs, the error is ImportError: No module named my_test, since the module my_test.py is already on the work directory. It really made me feel frustrated!
---------------------------------------------------------------------------
CompositeError Traceback (most recent call last)
/home/fit/log/1027/back_test/main.py in <module>()
119 import ipdb
120 ipdb.set_trace()
--> 121 for acc in result.get():
122 print acc.reselected_stocks, acc.current_time
123
/usr/local/lib/python2.7/dist-packages/ipyparallel/client/asyncresult.pyc in get(self, timeout)
102 return self._result
103 else:
--> 104 raise self._exception
105 else:
106 raise error.TimeoutError("Result not ready.")
CompositeError: one or more exceptions from call to method: let_us_deal
[0:apply]: ImportError: No module named my_test
[1:apply]: ImportError: No module named my_test
something about result:
In [2]: result
Out[2]: <AsyncMapResult: finished>
In [3]: type(result)
Out[3]: ipyparallel.client.asyncresult.AsyncMapResult
Note that, when it runs on single machine by using ipcluster start -n 8, it works fine, without any error.
Thanks in advance
I think my CWD is not in the right directory. So you can check your CWD
>>> import os
>>> print(dview.apply_sync(os.getcwd).get())
If it is in wrong directory, before parallel computing, you can set the right CWD to make sure you ipyparallel env is in the right work directory:
>>> import os
>>> dview.map(os.chdir, ['/path/to/my/project/on/engine']*number_of_engines)
>>> print(dview.apply_sync(os.getcwd).get())
You can also check your engines' name by
>>> import socket
>>> print(dview.apply_sync(socket.gethostname))
And it works fine!
pip install ipyparallel --upgrade
Sorry if this question is stupid. I created an unittest class which needs to take given inputs and outputs from outside. Thus, I guess these values should be initiated. However, I met some errors in the following code:
CODE:
import unittest
from StringIO import StringIO
##########Inputs and outputs from outside#######
a=[1,2]
b=[2,3]
out=[3,4]
####################################
def func1(a,b):
return a+b
class MyTestCase(unittest.TestCase):
def __init__(self,a,b,out):
self.a=a
self.b=b
self.out=out
def testMsed(self):
for i in range(self.tot_iter):
print i
fun = func1(self.a[i],self.b[i])
value = self.out[i]
testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("func1",i,value,fun)
self.assertEqual(round(fun,3),round(value,3),testFailureMessage)
if __name__ == '__main__':
f = MyTestCase(a,b,out)
from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream, verbosity=2)
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
print 'Tests run', result.testsRun
However, I got the following error
Traceback (most recent call last):
File "C:testing.py", line 33, in <module>
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
File "C:\Python27\lib\unittest\loader.py", line 310, in makeSuite
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
File "C:\Python27\lib\unittest\loader.py", line 50, in loadTestsFromTestCase
if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class
Can anyone give me some suggestions? Thanks!
The root of the problem is this line,
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
unittest.makeSuite expects a class, not an instance of a class. So just MyTestCase, not MyTestCase(a, b, out). This means that you can't pass parameters to your test case in the manner you are attempting to. You should probably move the code from init to a setUp function. Either access a, b, and out as globals inside setUp or take a look at this link for information regarding passing parameters to a unit test.
By the way, here is the source file within python where the problem originated. Might be informative to read.