def __init__(self, max_torque=float('inf'), dt=.02, seed=None, init_state=(-pi / 4, 3 * pi / 4, 0.025, .5, 0, 0, 0, 0), init_state_weights=(pi, pi, 0, .5, 0, 0, 0, 0), reward_fn=lambda s, a: s[3], done_criteria=lambda s: s[3] < (.3 * np.cos(s[0]) + .3 * np.cos(s[0] + s[1]))): self.eng = matlab.engine.start_matlab() self.eng.addpath(seagul.envs.matlab.bball_src.__path__[0], nargout=0) self.max_torque = max_torque self.dt = dt self.init_state = matlab.single(init_state, size=(8, 1)) self.init_state_weights = matlab.single(init_state_weights, size=(8, 1)) np.random.seed(seed) self.reward_fn = reward_fn self.done_criteria = done_criteria low = np.array([-pi, -pi, -5, -5, -10, -30, -10, -10]) self.observation_space = spaces.Box(low=low, high=-low, dtype=np.float32) self.action_space = spaces.Box(low=np.array([-max_torque, -max_torque]), high=np.array([max_torque, max_torque]), dtype=np.float32) self.reset()
def step(self, action): action = np.clip(action, -self.max_torque, self.max_torque) action = matlab.single(action.tolist()) action.reshape((2, 1)) tout, xout = self.eng.integrateODE(matlab.single( [self.t, self.t + self.dt]), self.state, self.dt, action, nargout=2) impactState, impactTime = self.eng.detectImpact(tout, xout, nargout=2) if impactTime == -1: #No contact self.state = matlab.single(xout[-1]) self.state.reshape((8, 1)) #self.state.reshape((8,1)) self.t = np.array(tout[-1]).item() else: self.state = self.eng.impact(matlab.single(impactState)) #self.state.reshape((8,1)) self.t = np.array(impactTime).item() reward = self.reward_fn(np.array(self.state), action) done = self.done_criteria(np.array(self.state)) return np.array(self.state).reshape((8, )), reward.item(), done, { "tout": tout, "xout": xout }
def __init__( self, max_torque=5.0, dt=.02, seed=None, init_state=(-pi / 4, 0.0, -3 * pi / 4, 0.025, .5, 0, 0, 0, 0, 0), init_state_weights=(pi, pi, pi, .5, .5, 0, 0, 0, 0, 0), reward_fn=lambda s, a: s[4], max_steps=100, ): self.eng = matlab.engine.start_matlab() self.eng.addpath(seagul.envs.matlab.bball3_src.__path__._path[0], nargout=0) self.max_torque = max_torque self.dt = dt self.init_state = matlab.single(init_state, size=(10, 1)) self.init_state_weights = matlab.single(init_state_weights, size=(10, 1)) np.random.seed(seed) self.reward_fn = reward_fn low = np.array([-pi, -pi, -pi, -5, -5, -10, -30, -30, -10, -10]) self.observation_space = spaces.Box(low=low, high=-low, dtype=np.float32) self.action_space = spaces.Box( low=np.array([-max_torque, -max_torque, -max_torque]), high=np.array([max_torque, max_torque, max_torque]), dtype=np.float32) self.max_steps = max_steps self.cur_step = 0 self.reset()
def get_scale_subwindow(self, im, pos, base_target_sz, scaleFactors, scale_model_sz): """ Obtain sub-window from image, with replication-padding. Returns sub-window of image IM centered at POS ([y, x] coordinates), with size SZ ([height, width]). If any pixels are outside of the image, they will replicate the values at the borders. The subwindow is also normalized to range -0.5 .. 0.5, and the given cosine window COS_WINDOW is applied (though this part could be omitted to make the function more general). """ out_pca = [] for s in range(len(scaleFactors)): patch_sz = np.floor(base_target_sz * scaleFactors[s]) ys = np.floor(pos[0]) + np.arange( patch_sz[0], dtype=int) - np.floor(patch_sz[0] / 2) xs = np.floor(pos[1]) + np.arange( patch_sz[1], dtype=int) - np.floor(patch_sz[1] / 2) ys = ys.astype(int) xs = xs.astype(int) # check for out-of-bounds coordinates and set them to the values at the borders ys[ys < 0] = 0 ys[ys >= self.im_sz[0]] = self.im_sz[0] - 1 xs[xs < 0] = 0 xs[xs >= self.im_sz[1]] = self.im_sz[1] - 1 # extract image im_patch = im[np.ix_(ys, xs)] im_gray = self.rgb2gray(im_patch) # extract scale features if self.matlab_legacy: img_matlab = matlab.single(im_gray.tolist()) scale_model_sz_matlab = matlab.single(scale_model_sz.tolist()) img_matlab_resize = self.eng.mexResize(img_matlab, scale_model_sz_matlab, 'auto') temp_pca_matlab = self.eng.fhog(img_matlab_resize, 4) temp_hog = np.array(temp_pca_matlab._data).reshape( temp_pca_matlab.size[::-1]).T else: im_patch_resized = imresize(np.array(im_gray, dtype=np.uint8), scale_model_sz.astype(int)) temp_hog = pyhog.features_pedro( im_patch_resized.astype(np.float64) / 255.0, int(self.feature_ratio)) neg_idx = temp_hog < 0 temp_hog[neg_idx] = 0 out_pca.append(temp_hog[:, :, :31].flatten(order='F')) return np.asarray(out_pca, dtype='float')
def _solve_ldax(self, Sw: Tensor, Sb: Tensor, argmin: Boolean = False, *args, **kwargs) -> Tensor: Sw, Sb = self.__check_Sw_Sb(Sw, Sb) if self.implementation == EPImplementation.matlab: self._W = self._regularize(Sw).inverse() @ Sb ret = self.engine.LDAX_SwSb(matlab.single(Sw.cpu().tolist()), matlab.single(Sb.cpu().tolist())) evecs = torch.tensor(ret._data).view(ret.size).t() evecs = self._revert(evecs) if argmin else evecs return evecs
def _solve_eig(self, Sw: Tensor, Sb: Tensor, argmin: Boolean = False, *args, **kwargs) -> Tensor: Sw, Sb = self.__check_Sw_Sb(Sw, Sb) if self.implementation == EPImplementation.pytorch: self._W = self._regularize(Sw).inverse() @ Sb evals, evecs = torch.eig(self._W, eigenvectors=True) # epairs = [[evals[_][0], evecs.t()[_]] for _ in range(evals.shape[0])] # epairs = sorted(epairs, key=lambda ep: torch.abs(ep[0]).item(), reverse=not argmin) # evecs = torch.cat([eigen_pair[1].unsqueeze(0) for eigen_pair in epairs], dim=0).t() evecs = evecs[:, torch. argsort(evals[:, 0].abs(), descending=not argmin)] return evecs elif self.implementation == EPImplementation.scipy or self.implementation == EPImplementation.numpy: Sw, Sb = self._numpify(Sw), self._numpify(Sb) self._W = self.linalg.inv(self._regularize(Sw)) @ Sb evals, evecs = self.linalg.eig(self._W) order = np.argsort( np.abs(evals))[::-1] if not argmin else np.argsort( np.abs(evals)) evecs = evecs.real[:, order] return torch.from_numpy(evecs.astype(np.float32)) elif self.implementation == EPImplementation.matlab: self._W = self._regularize(Sw).inverse() @ Sb W = matlab.single(self._W.cpu().tolist()) ret = self.engine.sorted_eig(W, not argmin) evecs = torch.from_numpy(np.array(ret).real.astype( np.float32)).view(ret.size) return evecs
def _solve_svd(self, Sw: Tensor, Sb: Tensor, argmin: Boolean = False, *args, **kwargs) -> Tensor: Sw, Sb = self.__check_Sw_Sb(Sw, Sb) if self.implementation == EPImplementation.pytorch: self._W = self._regularize(Sw).inverse() @ Sb U = self._W.svd()[0] U = self._revert(U) if argmin else U return U elif self.implementation == EPImplementation.scipy or self.implementation == EPImplementation.numpy: Sw, Sb = self._numpify(Sw), self._numpify(Sb) self._W = self.linalg.inv(self._regularize(Sw)) @ Sb U = self.linalg.svd(self._W)[0] U = self._revert(U) if argmin else U return torch.from_numpy(U.astype(np.float32)) elif self.implementation == EPImplementation.matlab: self._W = self._regularize(Sw).inverse() @ Sb W = matlab.single(self._W.cpu().tolist()) ret = self.engine.single_value_decomposition(W) U = torch.tensor(ret._data).view(ret.size).t() U = self._revert(U) if argmin else U return U
def step(self, action): action = np.clip(action, -self.max_torque, self.max_torque) action = matlab.single(action.tolist()) action.reshape((3, 1)) tout, xout = self.eng.integrateODE(matlab.single( [self.t, self.t + self.dt]), self.state, self.dt, action, nargout=2) impactState, impactTime = self.eng.detectImpact(tout, xout, nargout=2) if np.array(xout)[-1, -1] >= 0 and impactTime != -1: self.impact_miss += 1 impactTime = -1 print(f"Impact miss, total for this env: {self.impact_miss}") #input() if impactTime == -1: # No contact self.state = matlab.single(xout[-1]) self.state.reshape((10, 1)) # self.state.reshape((8,1)) self.t = np.array(tout[-1]).item() else: #print("contact!") #print(impactState) impactState.reshape((10, 1)) self.state = self.eng.impact(matlab.single(impactState)) # self.state.reshape((8,1)) self.t = np.array(impactTime).item() reward = self.reward_fn(np.array(self.state), action) done = self.eng.constraint(self.state) # if done: # print(done) self.cur_step += 1 if self.cur_step > self.max_steps: done = True return np.array(self.state, dtype=np.float32).reshape( (10, )), reward.item(), done, { "tout": tout, "xout": xout }
def extract_descriptor(self, image, feature): image = feature_utils.all_to_gray(image) image = image / 255.0 feature, descriptor = eng.vl_sift( matlab.single(image.tolist()), 'Magnif', self.Magnif, nargout=2) # , peak_thresh=self.peak_thresh descriptor = np.transpose(np.array(descriptor)) return descriptor
def __init__( self, max_torque=5.0, dt=.02, seed=None, init_state=(-pi / 4, 0.0, -3 * pi / 4, 0.025, .5, 0, 0, 0, 0, 0), init_state_weights=(pi, pi, pi, .5, .5, 0, 0, 0, 0, 0), reward_fn=lambda s, a: s[4], max_steps=100, ): print("before engine") self.eng = matlab.engine.start_matlab() print("got here") self.eng.addpath(os.path.dirname(__file__) + "/../matlab/", nargout=0) self.max_torque = max_torque self.dt = dt self.init_state = matlab.single(init_state, size=(10, 1)) self.init_state_weights = matlab.single(init_state_weights, size=(10, 1)) np.random.seed(seed) self.reward_fn = reward_fn low = np.array([-pi, -pi, -pi, -5, -5, -10, -30, -30, -10, -10]) self.observation_space = spaces.Box(low=low, high=-low, dtype=np.float32) self.action_space = spaces.Box( low=np.array([-max_torque, -max_torque, -max_torque]), high=np.array([max_torque, max_torque, max_torque]), dtype=np.float32) self.impact_miss = 0 self.max_steps = max_steps self.cur_step = 0 self.reset()
def get_features(self, im_crop): """ :param im_crop: :return: """ # because the hog output is (dim/4)>1: # if self.patch_size.min() < 12: # scale_up_factor = 12. / np.min(im_crop) # im_crop = imresize(np.array(im_crop, dtype=np.uint8), np.asarray(self.patch_size * scale_up_factor).astype('int')) xo_npca, xo_pca = [], [] im_gray = self.rgb2gray(im_crop) cell_gray = self.cell_gray(im_gray) if self.non_compressed_features == 'gray': xo_npca = im_gray / 255. - 0.5 if self.compressed_features == 'cn': xo_pca_temp = self.im2c(im_crop) xo_pca = np.reshape( xo_pca_temp, (np.prod([xo_pca_temp.shape[0], xo_pca_temp.shape[1] ]), xo_pca_temp.shape[2])) elif self.compressed_features == 'gray_hog': if self.matlab_legacy: img_matlab = matlab.single(im_gray.tolist()) temp_pca_matlab = self.eng.fhog(img_matlab, 4) features_hog = np.array(temp_pca_matlab._data).reshape( temp_pca_matlab.size[::-1]).T else: features_hog = pyhog.features_pedro( im_crop.astype(np.float64) / 255.0, int(self.feature_ratio)) neg_idx = features_hog < 0 features_hog[neg_idx] = 0 temp_pca = np.concatenate( [features_hog[:, :, :31], cell_gray[:, :, np.newaxis]], axis=2) xo_pca = temp_pca.reshape( [temp_pca.shape[0] * temp_pca.shape[1], temp_pca.shape[2]], order='F') return xo_npca, xo_pca
# clear memory for saving X_POS and X_NEG if (save_features): sio.savemat('counter.mat', {'counter': counter}) print('saved featurs done, current counter = %d' % counter) X_POS = [] X_NEG = [] if counter >= max_item: print('ok, we have trained with enough items (%d)' % max_item) break print('counter is :', counter) print('#################################') print('start to train logistic regression ............') print('#################################') for i in range(1, num_joints): for j in range(i + 1, num_joints + 1): tmp = sio.loadmat('%sfeat_spatial_%d_%d.mat' % (pairwiseDir, i, j)) train_X_pos = matlab.single(tmp['X_pos'].tolist()) train_X_neg = matlab.single(tmp['X_neg'].tolist()) matlab_eng.train_logistic_model(i, j, train_X_pos, train_X_neg, pairwiseDir, nargout=0) print('trained with %d-%d' % (i, j)) print('finished all !!!!')
os.mkdir(DirSaveData) #%% for num in range(len(name)): if networkType == 'ABO': endFile = str(L.index(name[num]) + 1) ind = L.index(name[num]) DirModel = os.path.join(dirpath, 'models', networkType, 'Trained Network Weights', dataType, endFile) ## read saved threshold values thresh = matlab.double([optThresh['ProbThresh'][0][ind]]) if networkType == 'Neurofinder': minArea = matlab.single([optThresh[AreaName][0][0] * 0.78**2]) else: minArea = matlab.single([optThresh[AreaName][0][ind]]) ## Check if HomoFiltered downsampled data is available data_file = Path( os.path.join(DirSaveData, name[num] + '_dsCropped_HomoNorm.nii.gz')) if not data_file.exists(): print('Preparing data {} for network...'.format(name[num])) data_file = os.path.join(DirData, name[num] + '_processed.nii.gz') s = 30 matlabLib.HomoFilt_Normalize(data_file, DirSaveData, name[ind], s, nargout=0)
## Check if save directories exist if not os.path.exists(DirSaveMask): os.makedirs(DirSaveMask) if not os.path.exists(DirSaveData): os.makedirs(DirSaveData) #% Set parameters pixSize = 0.78 #um meanR = 5.85 # neuron radius in um AvgArea = round(math.pi * (meanR / pixSize)**2) Thresh = 0.5 # IoU threshold for matching SZ = matlab.double([487, 487]) #x and y dimension of data ## read saved threshold values optThresh = sio.loadmat(os.path.join(DirThresh, ThreshFile)) thresh = matlab.single([optThresh['ProbThresh'][0][ind]]) minArea = matlab.single([optThresh[AreaName][0][ind]]) ## Check if HomoFiltered downsampled data is available data_file = Path( os.path.join(DirSaveData, name[0] + '_dsCropped_HomoNorm.nii.gz')) if not data_file.exists(): data_file = os.path.join(DirData, name[0] + '_processed.nii.gz') s = 30 matlabLib.HomoFilt_Normalize(data_file, DirSaveData, name[0], s, nargout=0) #%% ## Run data through the trained network # first create a new config file based on the current data f = open("demo_config_empty.ini") mylist = f.readlines() f.close()
def _matlabfy(self, *args: torch.Tensor): return (matlab.single(arg.cpu().tolist()) for arg in args)