def DP_main_2(datas, interDict, sampleSize=0.5, n_iter=50, distError=0.005): output = {"matching": [], "pending": []} rests = copy.deepcopy(interDict) # 境界が 0 番, 500番以外にないデータはここで除外する temprests = {} for r in rests: if len(rests[r]) > 2: temprests[r] = rests[r] rests = temprests for r in rests: print(rests[r]) # 最初の stateDict を作る stateDict = {"before": [], "after": [], "fname": [], "isadd": []} for fname in rests: fdata = datas[fname] stateDict["before"].append(fdata[rests[fname].pop(0)]) stateDict["after"].append(fdata[rests[fname].pop(0)]) stateDict["fname"].append(fname) stateDict["isadd"].append(False) while True: # rests が空なら終了 flg = True for fname in rests: if len(rests[fname]) != 0: flg = False break if flg == True: break # サンプリングを用いて vp を取得 vp = manager.getViewPointwithSampling(stateDict, sampleSize, n_iter) print("get vp:" + str(vp[0]["base"]) + "," + str(vp[0]["ref"])) # これをもとに predicts を作成 predicts = {} moved = manager.getMovedObject(stateDict) for i in range(len(stateDict["before"])): # step を使う場合は -1 predicts[stateDict["fname"][i]] = getAdditionalIntermediate( datas[stateDict["fname"][i]], stateDict["before"][i]["step"] - 1, vp) # predicts と after の関係から,動作を決定する matching = {"before": [], "after": [], "fname": [], "isadd": []} pending = {} for i in range(len(stateDict["before"])): # predicts と after の差が score の一定倍程度なら # matching として保存 matching["before"].append(stateDict["before"][i]) matching["after"].append(predicts[stateDict["fname"][i]]) matching["fname"].append(stateDict["fname"][i]) matching["isadd"].append(stateDict["isadd"][i]) # 新手法は matching すべてに対して predicts を適用していくという # ものなので,ignored に対して特別な処理はしない # pending であるもののみ,rests に返却するために保存する print("aaa" + str(predicts[stateDict["fname"][i]])) print("bbb" + str(stateDict["after"][i])) if manager.calcDifference( predicts[stateDict["fname"][i]], stateDict["after"][i], objs=moved) >= vp[0]["score"] * distError and predicts[ stateDict["fname"] [i]]["step"] < stateDict["after"][i]["step"]: pending[stateDict["fname"][i]] = stateDict["after"][i]["step"] # rests に pending を返却 for fname in pending: # インデックスのずれを修正するために -1 rests[fname] = [pending[fname] - 1] + rests[fname] # output に matching と pending を追加 output["matching"].append(matching) output["pending"].append(pending) with open("tmp/log_MakerMain/dills/DP_main_2_temp.dill", "wb") as f: dill.dump(output, f) print("new Matching") print([[matching["before"][i]["step"], matching["after"][i]["step"]] for i in range(len(matching["after"]))]) print("new Pending") print([pending[fname] for fname in pending]) # 次の stateDict を作る stateDict = {"before": [], "after": [], "fname": [], "isadd": []} for fname in rests: fdata = datas[fname] stateDict["before"].append(predicts[fname]) if len(rests[fname]) > 0: stateDict["after"].append(fdata[rests[fname].pop(0)]) else: print("EMPTY - " + fname) stateDict["after"].append(fdata[-1]) stateDict["fname"].append(fname) stateDict["isadd"].append(False) return output
if __name__ == "__main__": filepaths = glob.glob("tmp/forTest_predictMatching/*") # filepaths = glob.glob("tmp/log_MakerMain/*") # データ取得 datas = manager.getStateswithViewPoint(filepaths, [], []) stateDict = {} stateDict["before"] = [] stateDict["after"] = [] stateDict["fname"] = [] stateDict["isadd"] = [] for count, d in enumerate(sorted(list(datas.keys()))): stateDict["before"].append(datas[d][0]) stateDict["after"].append(datas[d][200]) stateDict["fname"].append(d) stateDict["isadd"].append(False) if count >= 249: break if count >= 239: stateDict["before"].append(datas[d][0]) stateDict["after"].append(datas[d][300]) stateDict["fname"].append(d) stateDict["isadd"].append(False) if count >= 244: stateDict["before"].append(datas[d][0]) stateDict["after"].append(datas[d][100]) stateDict["fname"].append(d) stateDict["isadd"].append(False) print(manager.getViewPointwithSampling(stateDict))
def DP_main(datas, interDict_, sampleSize=0.7, n_iter=50, distError=50): interDict = pruningInterDict_2(datas, interDict_) output = {"matching":[], "viewpoint":[]} rests = copy.deepcopy(interDict) # 最初の stateDict を作る stateDict = {} for fname in rests.keys(): fdata = datas[fname] stateDict[fname] = fdata[rests[fname].pop(0)] # matching に登録する output["matching" ].append(stateDict) output["viewpoint"].append(None) while True: # rests が空なら終了 flg = True for fname in rests.keys(): if len(rests[fname]) != 0: flg = False break if flg == True: break # 次の stateDict を作る stateDict = {} for fname in rests.keys(): fdata = datas[fname] while True: if len(rests[fname])>0 and rests[fname][0] < output["matching"][-1][fname]["step"]+10: rests[fname].pop(0) else: break if len(rests[fname]) > 0: stateDict[fname] = fdata[rests[fname].pop(0)] else: # print("EMPTY - " + fname) stateDict[fname] = fdata[-1] # サンプリングを用いて vp を取得 fnameList = sorted(list(stateDict.keys())) beforeDict = output["matching"][-1] beforeList = [beforeDict[fn] for fn in fnameList] afterList = [stateDict[fn] for fn in fnameList] tempDict = {"before":beforeList, "after":afterList, "fname":fnameList} vp = manager.getViewPointwithSampling(tempDict,sampleSize,n_iter) print("get vp:" + str(vp["base"]) + "," + str(vp["ref"])) matching = {} moved = manager.getMovedObject(tempDict) for fname in stateDict.keys(): # vp を用いて,境界の推定をやり直す bStep = output["matching"][-1][fname]["step"] addInter = getAdditionalIntermediate(datas[fname], bStep, vp) # matching の更新 matching[fname] = addInter # output に matching と pending と vp を追加 output["matching" ].append(matching) output["viewpoint"].append(vp) with open("tmp/log_MakerMain/dills/DP_main_temp.dill", "wb") as f: dill.dump(output, f) return output