def run(config): if args.plain: if args.evaluate: KBC(config, args).evaluate() else: KBC(config, args).train() elif args.cpm: if args.evaluate: CPM(config, args).evaluate_online() elif args.explain: CPM(config, args).predict_and_explain() elif args.analyse: CPM(config, args).analyse_relevance_scores() else: CPM(config, args).train()
def critical_path_cut(self, m_sub): # Find critical path sdag = copy.deepcopy(self.wf) for u in sdag.nodes(): sdag.node[u]['weight'] = int(m_sub.t_e[u] - m_sub.t_s[u]) if u == m_sub.des_node: break for v in list(sdag.successors(u)): ru = m_sub.tsk_placement[u] rv = m_sub.tsk_placement[v] if ru == rv: sdag[u][v]['weight'] = 0 else: sdag[u][v]['weight'] = int(sdag[u][v]['data'] / self.crs[ru][rv]['bandwidth']) for r in self.crs.nodes(): for (u, v) in zip(m_sub.rbt_occupy[r][:-1], m_sub.rbt_occupy[r][1:]): if u == 'src' or v == 'des' or u == 'des' or v == 'src': continue sdag.add_edge(u, v, weight=0) sdag = CPM(sdag) # Multi critical paths path = sdag.criticalPath.nodes() self.critical(m_sub, sdag, path) # for i in self.criticalPath: # # self.m.addConstr(sum(self.x_jk[t, m_sub.tsk_placement[t]] # for t in i # if t != 'src' and t != 'des' # ) # <= len(i) - 3, 'path_cut%d' % self.n_ctc_cut) # self.n_ctc_cut += 1 # total = sum(self.wf.node[t][m_sub.tsk_placement[t]] # for t in path) # subsetlsit = [total - self.wf.node[t][m_sub.tsk_placement[t]] # for t in path if t != 'src' and t != 'des'] # print("total: ", total, [i for i in subsetlsit if i > self.ub]) # sdag.draw() self.m.addConstr( sum(self.x_jk[t, m_sub.tsk_placement[t]] for t in path if t != 'src' and t != 'des') <= len(path) - 3, 'path_cut%d' % self.n_ctc_cut) self.n_ctc_cut += 1
def critical_path_cut(self, m_sub): # Find critical path sdag = copy.deepcopy(self.wf) for u in sdag.nodes(): sdag.node[u]['weight'] = int(m_sub.t_e[u] - m_sub.t_s[u]) if u == m_sub.des_node: break for v in list(sdag.successors(u)): ru = m_sub.tsk_placement[u] rv = m_sub.tsk_placement[v] if ru == rv: sdag[u][v]['weight'] = 0 else: sdag[u][v]['weight'] = int(sdag[u][v]['data'] / self.crs[ru][rv]['bandwidth']) for r in self.crs.nodes(): for (u, v) in zip(m_sub.rbt_occupy[r][:-1], m_sub.rbt_occupy[r][1:]): if u == 'src' or v == 'des' or u == 'des' or v == 'src': continue sdag.add_edge(u, v, weight=0) sdag = CPM(sdag) # nx.draw(sdag, with_labels=True) # plt.show() # Multi critical paths path = sdag.criticalPath.nodes() self.critical(m_sub, sdag, path) # self.cover_cut_generation(m_sub, gz) for i in self.criticalPath: self.m.addConstr(sum(self.x_jk[t, m_sub.tsk_placement[t]] for t in i if t != 'src' and t != 'des' ) <= len(i) - 3, 'path_cut%d' % self.n_ctc_cut) self.n_ctc_cut += 1 self.m.addConstr(sum(self.x_jk[t, m_sub.tsk_placement[t]] for t in path if t != 'src' and t != 'des' ) <= len(path) - 3, 'path_cut%d' % self.n_ctc_cut) self.n_ctc_cut += 1
class RoboMan(object): """Ampelsystem verwalten""" def __init__(self): self.cpm = CPM() self.robos = [AutoRobo('gruen'),PedesRobo('gruen'), AutoRobo('rot'),PedesRobo('rot'), AutoRobo('gruen'),PedesRobo('gruen'), AutoRobo('rot'),PedesRobo('rot')] def switchRobos(self): """switch according to plan""" signals = self.cpm.getCurrentSectionSignal() for i in range(len(self.robos)): if signals[i] == True: self.robos[i].switch() def getStates(self) -> 'as string list': """get state of all robos clockwise (NOSW)""" states = [] for robo in self.robos: states.append(robo.getState()) return states
import chainer from chainer import function as F from chainer import Variable, cuda, optimizers, serializers from make_gaussian import make_dataset from cpm import CPM import numpy as np from PIL import Image import os, sys import cv2 GPU = 0 net = CPM(n_point=5, n_stage=3) xp = np if GPU >= 0: cuda.get_device(GPU).use() xp = cuda.cupy net.to_gpu(GPU) dir = sys.argv[1] pict_list = os.listdir(dir + '/') serializers.load_npz(sys.argv[2], net) face = cv2.imread('./face.png') eye = cv2.imread('./eye.png', -1) head_width = 37 / 2 def max_coo(heatmap):
def ctbd(wf, crs): ts = time.time() m_master = MProblem(crs, wf) lb = m_master.optim() ub = greedy(wf, crs) zs = (lb + ub) / 2 obj = ub opt_x = {} opt_y = {} # print('init master obj_val: ', lb) iter = 0 while abs(ub - lb) > 2: print('ub, lb, zs: ', ub, lb, zs) m_master.m.reset() m_master.update_DL(zs) m_master.knapsack_cut() m_master.write() try: m_obj = m_master.optim() except: # print("master infeasible") if abs(lb - zs) <= 0.5: lb += 1 else: lb = zs zs = (lb + ub) / 2 continue if abs(lb - zs) <= 0.5: ub = min(obj, ub) m_sub = SCProblem(crs, wf) for (k, v) in m_master.x_jk.items(): if v.X >= 0.8: m_sub.set_task_placement(k) m_sub.set_start() z_sp = m_sub.optim() # print('sub problem objective value: ', z_sp) if z_sp < ub: m_sub.write() m_master.keycut = m_master.n_cover_cut for (k, v) in m_master.x_jk.items(): opt_x[k] = v.X for (k, v) in m_sub.y_ij.items(): opt_y[k] = v.X ub = z_sp zs = (lb + ub) / 2 else: zs = math.floor((lb + zs) / 2) obj = min(obj, z_sp) print('cur optimal obj: ', obj) ass_keys = [k for (k, v) in m_master.x_jk.items() if v.X >= 0.99] m_master.cover_cut(ass_keys) m_master.write() # Find critical path sdag = copy.deepcopy(wf) for u in sdag.nodes(): sdag.node[u]['weight'] = int(m_sub.t_e[u].X - m_sub.t_s[u].X) if u == m_sub.des_node: break for v in list(sdag.successors(u)): ru = m_sub.tsk_placement[u] rv = m_sub.tsk_placement[v] if ru == rv: sdag[u][v]['weight'] = 0 else: sdag[u][v]['weight'] = int(sdag[u][v]['data'] / crs[ru][rv]['bandwidth']) for r in crs.nodes(): for (u, v) in zip(m_sub.rbt_occupy[r][:-1], m_sub.rbt_occupy[r][1:]): sdag.add_edge(u, v, weight=0) sdag = CPM(sdag) path = sdag.criticalPath.nodes() m_master.critical_path_cut(path, m_sub) m_master.write() iter += 1 # print('current execution time: ', time.time() - ts) if time.time() - ts > MOWS.TTL: status, obj = m_master.solve_dependency(opt_x, opt_y) print('final obj : ', obj) if status == GRB.OPTIMAL: return obj print('hybrid ttl reached! ttl =', MOWS.TTL) return obj print('optimal solution found: %d, iteration: %d ' % (obj, iter)) return round(obj)
import chainer from chainer import function as F from chainer import Variable, cuda, optimizers, serializers from make_gaussian import make_dataset from cpm import CPM import numpy as np from PIL import Image import os GPU = 0 net = CPM(n_point=5, n_stage=3) xp = np if GPU >= 0: cuda.get_device(GPU).use() xp = cuda.cupy net.to_gpu(GPU) optimizer = optimizers.Adam(alpha=1e-4) optimizer.setup(net) x, t = make_dataset('dataset/') for epoch in range(10000): print(epoch) if epoch % 10 == 0: os.mkdir('Results/' + str(epoch)) for i in range(len(x)): x_train = Variable(xp.asarray([x[i]], dtype=np.float32)) t_train = Variable(xp.asarray([t[i]], dtype=np.float32))
def __init__(self): self.cpm = CPM() self.robos = [AutoRobo('gruen'),PedesRobo('gruen'), AutoRobo('rot'),PedesRobo('rot'), AutoRobo('gruen'),PedesRobo('gruen'), AutoRobo('rot'),PedesRobo('rot')]
if not os.path.exists(save_dir_label): os.mkdir(save_dir_label) json.dump(label_dict, open(save_dir_label + '/' + str(step) + '.json', 'w'), sort_keys=True, indent=4) return pck_dict # ************************************ Build dataset ************************************ test_data = UCIHandPoseDataset(data_dir=test_data_dir, label_dir=test_label_dir) print 'Test dataset total number of images sequence is ----' + str(len(test_data)) # Data Loader test_dataset = DataLoader(test_data, batch_size=batch_size, shuffle=True) # Build model net = CPM(21) if cuda: net = net.cuda() net = nn.DataParallel(net, device_ids=device_ids) # multi-Gpu model_path = os.path.join('ckpt/model_epoch' + str(best_model)+'.pth') state_dict = torch.load(model_path) net.load_state_dict(state_dict) # **************************************** test all images **************************************** print '********* test data *********' net.eval() all_pcks = {} # {0005:[[], [],[]], 0011:[[], [],[]] }