Anthony Miyaguchi commited on
Commit
c10f559
1 Parent(s): 43c4ba2

test for random sizes in images

Browse files
Files changed (2) hide show
  1. generate_dummy_testset.py +4 -1
  2. script.py +15 -21
generate_dummy_testset.py CHANGED
@@ -14,8 +14,11 @@ if __name__ == "__main__":
14
  with tempfile.TemporaryDirectory() as tmpdir:
15
  tmp_path = Path(tmpdir)
16
  for row in metadata.itertuples():
 
 
 
17
  img = PIL.Image.fromarray(
18
- np.random.randint(0, 255, (224, 224, 3), dtype=np.uint8)
19
  )
20
  img.save(tmp_path / row.filename)
21
 
 
14
  with tempfile.TemporaryDirectory() as tmpdir:
15
  tmp_path = Path(tmpdir)
16
  for row in metadata.itertuples():
17
+ # random dimensions
18
+ x = np.random.randint(100, 300)
19
+ y = np.random.randint(100, 300)
20
  img = PIL.Image.fromarray(
21
+ np.random.randint(0, 255, (x, y, 3), dtype=np.uint8)
22
  )
23
  img.save(tmp_path / row.filename)
24
 
script.py CHANGED
@@ -13,10 +13,12 @@ from transformers import AutoImageProcessor, AutoModel
13
 
14
 
15
  class ImageDataset(Dataset):
16
- def __init__(self, metadata_path, images_root_path):
17
  self.metadata_path = metadata_path
18
  self.metadata = pd.read_csv(metadata_path)
19
  self.images_root_path = images_root_path
 
 
20
 
21
  def __len__(self):
22
  return len(self.metadata)
@@ -24,9 +26,18 @@ class ImageDataset(Dataset):
24
  def __getitem__(self, idx):
25
  row = self.metadata.iloc[idx]
26
  image_path = Path(self.images_root_path) / row.filename
27
- img = Image.open(image_path).convert("RGB")
28
- img = torch.from_numpy(np.array(img))
29
- return {"features": img, "observation_id": row.observation_id}
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  class LinearClassifier(nn.Module):
@@ -40,21 +51,6 @@ class LinearClassifier(nn.Module):
40
  return torch.log_softmax(self.model(x), dim=1)
41
 
42
 
43
- class TransformDino:
44
- def __init__(self, model_name="./dinov2"):
45
- self.processor = AutoImageProcessor.from_pretrained(model_name)
46
- self.model = AutoModel.from_pretrained(model_name)
47
-
48
- def forward(self, batch):
49
- model_inputs = self.processor(images=batch["features"], return_tensors="pt")
50
- with torch.no_grad():
51
- outputs = self.model(**model_inputs)
52
- last_hidden_states = outputs.last_hidden_state
53
- # extract the cls token
54
- batch["features"] = last_hidden_states[:, 0]
55
- return batch
56
-
57
-
58
  def make_submission(
59
  test_metadata,
60
  model_path,
@@ -66,13 +62,11 @@ def make_submission(
66
  model = LinearClassifier(hparams["num_features"], hparams["num_classes"])
67
  model.load_state_dict(checkpoint["state_dict"])
68
 
69
- transform = TransformDino()
70
  dataloader = DataLoader(
71
  ImageDataset(test_metadata, images_root_path), batch_size=32, num_workers=4
72
  )
73
  rows = []
74
  for batch in dataloader:
75
- batch = transform.forward(batch)
76
  observation_ids = batch["observation_id"]
77
  logits = model(batch["features"])
78
  class_ids = torch.argmax(logits, dim=1)
 
13
 
14
 
15
  class ImageDataset(Dataset):
16
+ def __init__(self, metadata_path, images_root_path, model_name="./dinov2"):
17
  self.metadata_path = metadata_path
18
  self.metadata = pd.read_csv(metadata_path)
19
  self.images_root_path = images_root_path
20
+ self.processor = AutoImageProcessor.from_pretrained(model_name)
21
+ self.model = AutoModel.from_pretrained(model_name)
22
 
23
  def __len__(self):
24
  return len(self.metadata)
 
26
  def __getitem__(self, idx):
27
  row = self.metadata.iloc[idx]
28
  image_path = Path(self.images_root_path) / row.filename
29
+
30
+ model_inputs = self.processor(
31
+ images=Image.open(image_path), return_tensors="pt"
32
+ )
33
+ with torch.no_grad():
34
+ outputs = self.model(**model_inputs)
35
+ last_hidden_states = outputs.last_hidden_state
36
+ # extract the cls token
37
+ return {
38
+ "features": last_hidden_states[0, 0],
39
+ "observation_id": row.observation_id,
40
+ }
41
 
42
 
43
  class LinearClassifier(nn.Module):
 
51
  return torch.log_softmax(self.model(x), dim=1)
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def make_submission(
55
  test_metadata,
56
  model_path,
 
62
  model = LinearClassifier(hparams["num_features"], hparams["num_classes"])
63
  model.load_state_dict(checkpoint["state_dict"])
64
 
 
65
  dataloader = DataLoader(
66
  ImageDataset(test_metadata, images_root_path), batch_size=32, num_workers=4
67
  )
68
  rows = []
69
  for batch in dataloader:
 
70
  observation_ids = batch["observation_id"]
71
  logits = model(batch["features"])
72
  class_ids = torch.argmax(logits, dim=1)