| 266 | torch.tensor(users), torch.tensor(dates), torch.tensor(keep_inds), torch.tensor(dataids) |
| 267 | |
| 268 | class Inat2018DataModule(pl.LightningDataModule): |
| 269 | def __init__(self, root, batch_size=2000, mode="location", num_workers=0): |
| 270 | super().__init__() |
| 271 | self.root = root |
| 272 | self.batch_size = batch_size |
| 273 | self.mode = mode |
| 274 | self.num_workers = num_workers |
| 275 | |
| 276 | with open(os.path.join(root, "categories.json")) as da: |
| 277 | data = json.load(da) |
| 278 | df = pd.DataFrame(data)[["name", "id"]] |
| 279 | |
| 280 | self.name2id = df.set_index("name").to_dict()["id"] |
| 281 | self.id2name = df.set_index("id").to_dict()["name"] |
| 282 | |
| 283 | |
| 284 | def setup(self, stage=None): |
| 285 | if self.mode == "image" or self.mode == "all": |
| 286 | return_location = self.mode == "all" |
| 287 | image_dataset = INAT_ImageDataset(self.root, os.path.join(self.root, "train2018.json"), |
| 288 | is_train=True, return_location=return_location) |
| 289 | self.train_ds, self.valid_ds = data.random_split(image_dataset, |
| 290 | [VAL_SPLIT_RATIO, 1 - VAL_SPLIT_RATIO]) |
| 291 | self.test_ds = INAT_ImageDataset(self.root, os.path.join(self.root, "val2018.json"), |
| 292 | is_train=False, return_location=return_location, |
| 293 | logits_file=os.path.join(self.root, "val_logits.npy")) |
| 294 | elif self.mode == "location": |
| 295 | locations, classes, *_ = load_inat_location_data(self.root, "train2018_locations.json", "train2018.json") |
| 296 | self.train_ds, self.valid_ds = data.random_split(TensorDataset(locations, classes), |
| 297 | [VAL_SPLIT_RATIO, 1 - VAL_SPLIT_RATIO]) |
| 298 | locations, classes, *_ = load_inat_location_data(self.root, "val2018_locations.json", "val2018.json") |
| 299 | self.test_ds = TensorDataset(locations, classes) |
| 300 | |
| 301 | def train_dataloader(self): |
| 302 | return DataLoader(self.train_ds, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers) |
| 303 | |
| 304 | def val_dataloader(self): |
| 305 | return DataLoader(self.valid_ds, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) |
| 306 | |
| 307 | def test_dataloader(self): |
| 308 | return DataLoader(self.test_ds, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) |