def process_patch_features(model, patch, cuda): patch = np.moveaxis(patch, -1, 0) patch = np.expand_dims(patch, axis=0) patch = clip_and_scale_image(patch, 'rgb') # Embed tile patch = torch.from_numpy(patch).float() patch = Variable(patch) if cuda: patch = patch.cuda() z = model.encode(patch) if cuda: z = z.cpu() z = z.data.numpy() return z
def get_emax_features(img_names, model, z_dim, cuda, bands=5, patch_size=50, patch_per_img=36, save=True, verbose=False, npy=True): model.eval() X = np.zeros((len(img_names), z_dim)) for k in range(len(img_names)): img_name = img_names[k] if verbose: print("Getting features for: " + img_name) img = load_landsat(img_name, bands, bands_only=True, is_npy=npy) img_shape = img.shape offset_row = (img_shape[0] - 300) // 2 offset_col = (img_shape[1] - 300) // 2 output = np.zeros((36, z_dim)) for i in range(6): for j in range(6): start_row = i * 50 + offset_row end_row = (i + 1) * 50 + offset_row start_col = j * 50 + offset_col end_col = (j + 1) * 50 + offset_col # print("Rows: " + str((start_row,end_row))) # print("Cols: " + str((start_col,end_col))) patch = img[start_row:end_row, start_col:end_col, :] patch = np.moveaxis(patch, -1, 0) patch = np.expand_dims(patch, axis=0) patch = clip_and_scale_image(patch, 'landsat') # Embed tile patch = torch.from_numpy(patch).float() patch = Variable(patch) if cuda: patch = patch.cuda() z = model.encode(patch) if cuda: z = z.cpu() z = z.data.numpy() output[(i + 1) * (j + 1) - 1, :] += z.reshape(-1) output_emax = np.zeros((1, z_dim)) for i in range(36): output_emax[0][i] = np.amax(output[:, i]) X[k] = output_emax return X
def __getitem__(self, index): # Get tile path = self.list_ids.iloc[index].patch_dir name = os.path.basename(path) tile = load_patch(path) # randomly sample 50x50 section of the tile # x_idx = np.random.randint(low=0, high=120-50) # y_idx = np.random.randint(low=0, high=120-50) # tile = tile[x_idx:x_idx+50,y_idx:y_idx+50,:] tile = tile[35:85, 35:85, :] # Scale tile = clip_and_scale_image(tile, img_type='landsat') # Rearrange to PyTorch order tile = np.moveaxis(tile, -1, 0) tile = np.expand_dims(tile, axis=0) # Load data and get label y = multihot(self.y[name], self.labs) return tile, y
def __call__(self, sample): a, n, d = (clip_and_scale_image(sample['anchor'], self.img_type), clip_and_scale_image(sample['neighbor'], self.img_type), clip_and_scale_image(sample['distant'], self.img_type)) sample = {'anchor': a, 'neighbor': n, 'distant': d} return sample