def forward(self, focus): focus = focus['__train__'] frames = util.extract_neighbor(self.frame_source, focus, np.arange(focus.shape[0]), nb_size=self.nb_size) frame_dev = self._deviation_1(frames) focus_dev = self._deviation_2(focus) frame_var = self._variance(frames) belief_dev = 0 if self.belief_dev_coeff != 0: pre_frames = util.extract_neighbor( self.frame_source, focus[1:], np.arange(focus.shape[0] - 1), nb_size=self.nb_size) # I_{t}(x_{t+1}) post_frames = util.extract_neighbor( self.frame_source, focus[:-1], np.arange(focus.shape[0] - 1) + 1, nb_size=self.nb_size) # I_{t+1}(x_{t}) belief_dev = self._belief_deviation(focus, frames, pre_frames, post_frames) if self.verbose: logger.info( 'frame_dev= %f, focus_dev= %f, frame_var= %f, belief_dev= %f' % (frame_dev, focus_dev, frame_var, belief_dev)) return self.frame_dev_coeff*frame_dev \ + self.focus_dev_coeff*focus_dev \ - self.frame_var_coeff*frame_var \ - self.belief_dev_coeff*belief_dev
def save_focus_img(dataset, all_focus, save_path, changepoints=[]): focus_img = util.extract_neighbor(dataset, all_focus, range(len(all_focus)), nb_size=(15, 20)) for i, img in enumerate(focus_img): # focused neighbor image file_name = 'focus_img_%d.png' % (i) file_path = os.path.join(save_path, file_name) imsave(file_path, img) print('saved under', save_path) pos_cnt = np.zeros(dataset.get_shape()[-2:]) cp_mask = np.zeros(len(all_focus), dtype=bool) cp_mask[changepoints] = True save_subpath = util.get_dir(os.path.join(save_path, 'marker')) for i, img in enumerate(dataset.get_frame(0, len(all_focus))): # for i, img in enumerate(util.remove_mean(dataset.get_frame(0, len(all_focus)), all_focus)): # marker file_name = 'marker_%d.png' % (i) marker_file_path = os.path.join(save_subpath, file_name) marker_pos = (all_focus[i] * dataset.get_shape()[2:]).astype(int) img[0, marker_pos[0], :] = 1 img[0, :, marker_pos[1]] = 1 if cp_mask[i]: img = 1 - img pos_cnt[marker_pos[0], marker_pos[1]] += 1 imsave(marker_file_path, img[0]) print('focus by marker saved under', save_subpath) if np.sum(pos_cnt) > 0: pos_cnt = np.sqrt(1.0 - (pos_cnt / np.max(pos_cnt) - 1)**2) pos_cnt_file_path = os.path.join(save_path, 'counter_pos.png') imsave(pos_cnt_file_path, pos_cnt) print('position count saved at', pos_cnt_file_path)
def plot_focus(dataset, indices, all_focus): PX, PY = 2, len(all_focus) focus_img = util.extract_neighbor(dataset, all_focus, indices, nb_size=(15, 20)) for i in range(PY): plt.subplot(PX, PY, i + 1) plt.imshow(dataset.get_frame(indices[i])[0], interpolation=None) plt.subplot(PX, PY, PY + i + 1) plt.imshow(focus_img[i], interpolation=None) plt.show()
def from_focus_model(focus_model_forward, dataset, nb_size, batch_size=100): n_channel = 1 # dataset.get_shape()[-3] # TODO: properly do this match_size = (n_channel, nb_size[0]*2+1, nb_size[1]*2+1) img_stack = np.zeros((dataset.n_state,) + match_size) weight_stack = np.zeros(dataset.n_state) cur_n = 0 for l in range(0, dataset.n_state, batch_size): print(l) # TODO: remove r = min(l + batch_size, dataset.n_state) frames = dataset.get_frame(l, r) focus, extra = focus_model_forward(frames) neighbor = util.extract_neighbor(dataset, focus, np.arange(l, r), nb_size=nb_size) img_stack[l:r, 0] = neighbor # properly channels? weight_stack[l:r] = util.focus_intensity(focus[l:r], extra[l:r]) img_mean = np.average(img_stack, weights=weight_stack, axis=0) img_var = np.average((img_stack - img_mean)**2, weights=weight_stack, axis=0)**0.5 # TODO: this is biased return ModelFocusMeanVar(img_mean, img_var)