def reset_end(self): """ Must call after reset """ self.w_map = None self.density = None if self.config["reward"] == "density": self.density = density_map(self.gt_lbl) elif self.config["reward"] == "seg" and self.type == "train": self.gt_lbl = relabel(reorder_label(self.gt_lbl)) idx_list = np.unique(self.gt_lbl) self.segs = [self.gt_lbl == idx for idx in np.unique(self.gt_lbl)] self.bdrs = [] self.inrs = [] for radius in self.config["out_radius"]: self.bdrs += [[ seg ^ budget_binary_dilation( seg, radius, fac=self.config["dilate_fac"]) for seg in self.segs ]] for radius in self.config["in_radius"]: self.inrs += [[ budget_binary_erosion(seg, radius, minsize=self.config["minsize"]) for seg in self.segs ]] # self.inrs = [seg for seg in self.segs] else: self.density = np.ones(self.size, dtype=np.float32) self.random_init_lbl()
def step(self, action): self.new_lbl = self.lbl * 2 + action done = False if self.config["reward"] == "gaussian": w_map = guassian_weight_map((self.r * 2 + 1, self.r * 2 + 1)) reward = self.split_penalty_step(w_map=w_map) reward += self.split_reward_step(w_map=w_map) elif self.config["reward"] == "density": density = density_map(self.gt_lbl) reward = self.split_penalty_step(density=density) reward += self.split_reward_step(density=density) else: reward = self.split_penalty_step() reward += self.split_reward_step() self.lbl = self.new_lbl self.mask[self.step_cnt:self.step_cnt + 1] += (2 * action - 1) * 255 self.step_cnt += 1 if self.step_cnt >= self.T: done = True info = {} self.rewards.append(reward) self.sum_reward += reward return self.observation(), reward, done, info
def load_example(self, img_f): # load the image and the binary mask X = io.imread(os.path.join(self.path, 'images', img_f)) mask = scipy.io.loadmat( os.path.join(self.path, 'images', img_f.replace('.jpg', 'mask.mat')))['BW'] mask = mask[:, :, np.newaxis].astype('float32') img_centers = self.centers[img_f] # reduce the size of image and mask by the given amount H_orig, W_orig = X.shape[0], X.shape[1] if H_orig != self.out_shape[0] or W_orig != self.out_shape[1]: X = SkT.resize(X, self.out_shape, preserve_range=True).astype('uint8') mask = SkT.resize(mask, self.out_shape, preserve_range=True).astype('float32') # compute the density map density = density_map((H_orig, W_orig), img_centers, self.gamma * np.ones((len(img_centers), 2)), out_shape=self.out_shape) density = density[:, :, np.newaxis].astype('float32') return X, mask, density
def load_example(self, img_f): X = io.imread(os.path.join(self.path, img_f)) mask_f = os.path.join(img_f.split(os.sep)[0], img_f.split(os.sep)[1]) + '-msk.npy' mask = np.load(os.path.join(self.path, mask_f)) mask = mask[:, :, np.newaxis].astype('float32') bndboxes = self.bndboxes[img_f] H_orig, W_orig = X.shape[0], X.shape[1] # reduce the size of image and mask by the given amount H_orig, W_orig = X.shape[0], X.shape[1] if H_orig != self.out_shape[0] or W_orig != self.out_shape[1]: X = SkT.resize(X, self.out_shape, preserve_range=True).astype('uint8') mask = SkT.resize(mask, self.out_shape, preserve_range=True).astype('float32') # compute the density map img_centers = [(int((xmin + xmax) / 2.), int((ymin + ymax) / 2.)) for xmin, ymin, xmax, ymax in bndboxes] gammas = self.gamma * np.array( [[1. / np.absolute(xmax - xmin), 1. / np.absolute(ymax - ymin)] for xmin, ymin, xmax, ymax in bndboxes]) # gammas = self.gamma*np.ones((len(bndboxes), 2)) density = density_map((H_orig, W_orig), img_centers, gammas, out_shape=self.out_shape) density = density[:, :, np.newaxis].astype('float32') return X, mask, density
def draw_gaussian(self, zoom_shape = settings.WEBCAMT_NEW_SHAPE, mask = None): centers = [] sigmas = [] for vehicle in self.vehicles: centers.append(vehicle.calculate_center()) sigmas.append(vehicle.calculate_sigma()) self.density = utils.density_map((self.original_shape[0], self.original_shape[1]), centers, sigmas, zoom_shape, mask)
def compute_densities(domain_path, data_domain): subfiles = [f for f in os.listdir(domain_path)] for file in subfiles: if file[-15:] == '_frame_full.mat': vid_number, clip_number = parse_gt_file_name(file) file_path = os.path.join(domain_path, file) data_matlab = sio.loadmat(file_path) for frame_number in range(len(data_matlab['fgt'][0][0][0][0])): if (frame_number - 1) % FRAMES_SPACING == 0: centers = [] sigmas = [] real_frame_number = (clip_number * 200 + frame_number - 1) / FRAMES_SPACING for person in data_matlab['fgt'][0][0][0][0][frame_number][ 0][0][0]: centers.append([person[0], person[1]]) sigmas.append([SIGMAX, SIGMAY]) shape = data_domain[vid_number].frames[ real_frame_number].frame.shape data_domain[vid_number].frames[ real_frame_number].density = utils.density_map( (shape[1], shape[2]), centers, sigmas, mask=None).reshape((1, shape[1], shape[2])) data_domain[vid_number].frames[ real_frame_number].count = len( data_matlab['fgt'][0][0][0][0][frame_number][0][0] [0]) if settings.LOAD_DATA_AUGMENTATION: density_s90 = utils.transformations.transform_matrix_channels( data_domain[vid_number].frames[real_frame_number]. density, utils.transformations.symmetric, 90) data_domain[vid_number].frames[ real_frame_number].density_augmentation = { 's90': density_s90 } for vid_number in data_domain.keys(): keys = list(data_domain[vid_number].frames.keys()) for frame_number in keys: if data_domain[vid_number].frames[frame_number].density is None: data_domain[vid_number].frames.pop(frame_number)
def load_example(self, img_f): # load the image X = io.imread(os.path.join(self.path, 'images', img_f)) img_centers = self.centers[img_f] # reduce the size of image by the given amount H_orig, W_orig = X.shape[0], X.shape[1] if H_orig != self.out_shape[0] or W_orig != self.out_shape[1]: X = SkT.resize(X, self.out_shape, preserve_range=True).astype('uint8') # compute the density map density = density_map( (H_orig, W_orig), img_centers, self.gamma*np.ones((len(img_centers), 2)), out_shape=self.out_shape) density = density[:, :, np.newaxis].astype('float32') return X, density
binned = pd.cut(data.train.DURATION, bins, labels=bins[:-1] / 60, include_lowest=True) sns.countplot(binned, color='royalblue') plt.gca().xaxis.set_major_locator(MultipleLocator(5)) plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d')) plt.xlim(-1, 40) plt.xticks(fontsize=9) plt.xlabel('Duration (in minutes)') plt.show() plt.savefig('TAXI_trip_duration.png') # visual representation of the dataset's spatial distribution (or density all_coords = np.concatenate(data.train['POLYLINE_FULL'].as_matrix()) density_map(all_coords[:, 0], all_coords[:, 1]) plt.savefig('TAXI_heatmap_train.png') all_coords = np.concatenate(data.test['POLYLINE_FULL'].as_matrix()) density_map(all_coords[:, 0], all_coords[:, 1]) plt.savefig('TAXI_heatmap_test.png') # model that won the competition is in fact pretty simple. It is a neural network with # a single hidden layer of 500 neurons and a Rectifier Linear Unit (ReLU) activation function. # The input layer is comprised of embedding vectors learned for each key feature (quarter hour of the day, # day of the week, week of the year, the client IDs, the taxi IDs and the stand IDs), # as well as the first 5 and latest 5 recorded GPS coordinates for each taxi trip # 1) Before training the model, do a bit of pre-processing by estimating the most popular destination points # (a few thousands of them) using a mean-shift clustering algorithm.