def step(self, action): done = False #action = np.clip(action-self.action_bound[1], *self.action_bound) r = 0 self.focal_new = int( np.clip((self.focal_old + action), FOCAL_DOWN, FOCAL_UP)) for i in range(0, self.state_dim - 1): if i < self.state_dim - 2: self.hist_focal[i] = self.hist_focal[i + 1] if i == self.state_dim - 2: self.hist_focal[i] = self.focal_new if (self.focal_new == FOCAL_UP) or (self.focal_new == FOCAL_DOWN): r -= 5 # normalize features grey_avg_old = 0 grey_avg_new = 0 else: # normalize features grey_avg_old = ifa(self.img_stack[self.focal_old]) grey_avg_new = ifa(self.img_stack[self.focal_new]) # done and reward if (self.focal_new == self.focal_old) and (grey_avg_new * 255 == self.max_grey): self.focused += 1 if self.focused > 20: self.hist_focal[self.state_dim - 1] = 1 r += 10 done = True self.focused = 0 print("hold position at focal plane for 20 times: ", self.focal_new) else: r -= 0.01 else: self.focused = 0 # r -= np.sum(np.sqrt(np.asarray(feature_new) - # np.asarray(feature_old))) r += (grey_avg_new - grey_avg_old) * 255 # state #s = self.hist_focal s = np.asarray([self.focal_new, 1 if done else 0]) self.focal_old = self.focal_new if grey_avg_new > grey_avg_old: self.max_grey = grey_avg_new * 255 return s, r, done
def reset(self): self.focal_new = int(np.random.rand() * 200) self.focal_old = int(np.random.rand() * 200) self.focused = 0 area_sel_new, grey_sum_new, grey_avg_new, threshold_new, grey_std_new = ifa( self.img_stack[self.focal_new]) # state s = self.focal_new return s
def step(self, action): done = False r = 0 exceed_bound = False area_sel_old = 0 grey_sum_old = 0 grey_avg_old = 0 threshold_old = 0 grey_std_old = 0 area_sel_new = 0 grey_sum_new = 0 grey_avg_new = 0 threshold_new = 0 grey_std_new = 0 if action == 0: # large forward step if self.focal_old < (FOCAL_UP - 10): self.focal_new = int(self.focal_old + 10) else: exceed_bound = True elif action == 1: # small forward step if self.focal_old < (FOCAL_UP - 1): self.focal_new = int(self.focal_old + 1) else: exceed_bound = True elif action == 2: # hold focal position self.focal_new = self.focal_old elif action == 3: # small backward step if self.focal_old > (FOCAL_DOWN + 1): self.focal_new = int(self.focal_old - 1) else: exceed_bound = True elif action == 4: # large backward step if self.focal_old > (FOCAL_DOWN + 10): self.focal_new = int(self.focal_old - 10) else: exceed_bound = True if (exceed_bound): done = False r -= 5 # state # normalize features area_sel_old, grey_sum_old, grey_avg_old, threshold_old, grey_std_old = ifa( self.img_stack[self.focal_old]) s = self.focal_new else: # normalize features area_sel_old, grey_sum_old, grey_avg_old, threshold_old, grey_std_old = ifa( self.img_stack[self.focal_old]) area_sel_new, grey_sum_new, grey_avg_new, threshold_new, grey_std_new = ifa( self.img_stack[self.focal_new]) r += (grey_avg_new - grey_avg_old) * grey_avg_new / 50 # done and reward if (self.focal_new == self.focal_old) and (grey_avg_new == self.max_grey): r += 0.001 self.focused += 1 if self.focused > 20: done = True print('hold position at focal plane: ', self.focal_new) else: self.focused = 0 # state s = self.focal_new if grey_avg_new > grey_avg_old: self.max_grey = grey_avg_new self.focal_old = self.focal_new return s, r, done
def step(self, a): done = False reward = 0 exceed_bound = False action = np.argmax(a) if action == 0: # large forward step if self.focal_old < (FOCAL_UP - 10): self.focal_new = int(self.focal_old + 10) else: exceed_bound = True elif action == 1: # small forward step if self.focal_old < (FOCAL_UP - 1): self.focal_new = int(self.focal_old + 1) else: exceed_bound = True elif action == 2: # hold focal position self.focal_new = self.focal_old elif action == 3: # small backward step if self.focal_old > (FOCAL_DOWN + 1): self.focal_new = int(self.focal_old - 1) else: exceed_bound = True elif action == 4: # large backward step if self.focal_old > (FOCAL_DOWN + 10): self.focal_new = int(self.focal_old - 10) else: exceed_bound = True if exceed_bound: reward -= 5 grey_avg_new = 0 grey_avg_old = 0 else: grey_avg_new = ifa(self.img_stack[self.focal_new]) grey_avg_old = ifa(self.img_stack[self.focal_old]) if (self.focal_new == self.focal_old) and (grey_avg_new*255 == self.max_grey): # reward += 1 self.focused += 1 if self.focused > 20: done = True self.focused = 0 reward += 10 print("hold the position at focal plane 20 times:", self.focal_new) else: reward -= 0.01 else: self.focused = 0 # np.sign(grey_avg_new - grey_avg_old) reward += (grey_avg_new - grey_avg_old) * 255 #next_state = self.focal_new s = np.asarray([self.focal_new, 1 if done else 0]) if grey_avg_new > grey_avg_old: self.max_grey = grey_avg_new*255 self.focal_old = self.focal_new return s, reward, done