novelmartis commited on
Commit
69be934
·
verified ·
1 Parent(s): 2786cbc

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +30 -16
README.md CHANGED
@@ -13,14 +13,10 @@ tags:
13
 
14
  This repo contains pretrained GPN models (R and RS variants), based on the SimCLR backbone, trained on DeepGaze3 glimpses on COCO scenes.
15
 
16
- ## Install
17
-
18
- ```bash
19
- pip install torch torchvision huggingface_hub pillow
20
- ```
21
-
22
  ## Usage
23
 
 
 
24
  ```
25
  from huggingface_hub import hf_hub_download
26
  import torch
@@ -31,7 +27,10 @@ REPO_ID = "novelmartis/GPN"
31
  device = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
32
 
33
  # setup glimpse encoder - SimCLR-RN50
34
- ckpt = hf_hub_download(REPO_ID, filename="SimCLR ResNet50 1x.pt") # from: https://huggingface.co/lightly-ai/simclrv1-imagenet1k-resnet50-1x
 
 
 
35
  encoder = resnet50(weights=None)
36
  encoder.load_state_dict(torch.load(ckpt, map_location="cpu")['state_dict'])
37
  preprocess = transforms.Compose([
@@ -40,34 +39,49 @@ preprocess = transforms.Compose([
40
  ])
41
  encoder.to(device)
42
  encoder.eval()
 
 
 
43
  activation = {} # remember to empty the list after each forward pass
44
  def get_activation(name): # setting hooks
45
  def hook(model, input, output):
46
  activation[name] = output.detach()
47
  return hook
48
- encoder.avgpool.register_forward_hook(get_activation('avgpool')) # extracting avgpool activations from the RN50-SimCLR
 
49
 
50
  # choose GPN "R" or "RS"
 
51
  variant = "R"
52
  ckpt = hf_hub_download(REPO_ID, filename=f"gpn_{variant}.pth")
 
53
  gpn = build_gpn(variant=variant)
54
  gpn.load_state_dict(torch.load(ckpt, map_location="cpu"))
55
  gpn.eval()
 
 
 
 
 
56
 
57
- # obtain 'glimpse_embeddings' [B,N,2048] for your N input_glimpses per scene [B,N,H,W,C]: numpy array
 
 
 
58
  input_glimpses_tensor = preprocess(torch.from_numpy(input_glimpses.reshape(B*N,H,W,C)).permute(0, 3, 1, 2)).to(device)
59
  with torch.no_grad():
60
  _ = encoder(input_glimpses_tensor)
61
  glimpse_embeddings = activation['avgpool'].detach().cpu().numpy().squeeze()
62
  glimpse_embeddings = glimpse_embeddings.reshape(B,N,-1)
 
63
 
64
- # run the gpn on glimpse_embeddings and saccade_inputs [B,N,2]; set return_all_actvs=True to acquire internal representations
65
- representations, predictions = gpn(glimpse_embeddings, saccade_inputs, set return_all_actvs=True)
66
-
67
- # predictions: [B,N,2048] - prediction of the T+1 glimpse embeddings at T
68
- # representations['lstm_out'], when variant = "R", have the best alignment to ventral and parietal ROI RDMs in NSD.
69
 
 
 
 
 
70
  ```
71
 
72
- Note that during training, COCO scene images were scaled on their lower side to 256px and a central 256px crop was taken. DeepGaze3 was fed this image to obtain fixations. 91px crops were taken around the fixations as glimpses.
73
- I'd advise you to do similar preprocessing steps on your images to obtain 'input_glimpses', unless you have apriori reasons to do otherwise.
 
13
 
14
  This repo contains pretrained GPN models (R and RS variants), based on the SimCLR backbone, trained on DeepGaze3 glimpses on COCO scenes.
15
 
 
 
 
 
 
 
16
  ## Usage
17
 
18
+ First setup the glimpse encoder backbone and the GPN:
19
+
20
  ```
21
  from huggingface_hub import hf_hub_download
22
  import torch
 
27
  device = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
28
 
29
  # setup glimpse encoder - SimCLR-RN50
30
+
31
+ # obtained from: https://huggingface.co/lightly-ai/simclrv1-imagenet1k-resnet50-1x
32
+ ckpt = hf_hub_download(REPO_ID, filename="SimCLR ResNet50 1x.pt")
33
+
34
  encoder = resnet50(weights=None)
35
  encoder.load_state_dict(torch.load(ckpt, map_location="cpu")['state_dict'])
36
  preprocess = transforms.Compose([
 
39
  ])
40
  encoder.to(device)
41
  encoder.eval()
42
+
43
+ # setting up avgpool activation extraction from the RN50-SimCLR
44
+
45
  activation = {} # remember to empty the list after each forward pass
46
  def get_activation(name): # setting hooks
47
  def hook(model, input, output):
48
  activation[name] = output.detach()
49
  return hook
50
+
51
+ encoder.avgpool.register_forward_hook(get_activation('avgpool'))
52
 
53
  # choose GPN "R" or "RS"
54
+
55
  variant = "R"
56
  ckpt = hf_hub_download(REPO_ID, filename=f"gpn_{variant}.pth")
57
+
58
  gpn = build_gpn(variant=variant)
59
  gpn.load_state_dict(torch.load(ckpt, map_location="cpu"))
60
  gpn.eval()
61
+ ```
62
+
63
+ Next, gather your N glimpse images for each of the B scenes into a [B,N,H,W,3] uint8 numpy array.
64
+ Note that during training, COCO scene images were scaled on their lower side to 256px and a central 256px crop was taken. DeepGaze3 was fed this image to obtain fixations. 91px crops were taken around the fixations as glimpses.
65
+ I'd advise you to do similar preprocessing steps on your images to obtain 'input_glimpses', unless you have apriori reasons to do otherwise.
66
 
67
+ Gather the glimpse embeddings
68
+
69
+ ```
70
+ # obtain 'glimpse_embeddings' [B,N,2048]
71
  input_glimpses_tensor = preprocess(torch.from_numpy(input_glimpses.reshape(B*N,H,W,C)).permute(0, 3, 1, 2)).to(device)
72
  with torch.no_grad():
73
  _ = encoder(input_glimpses_tensor)
74
  glimpse_embeddings = activation['avgpool'].detach().cpu().numpy().squeeze()
75
  glimpse_embeddings = glimpse_embeddings.reshape(B,N,-1)
76
+ ```
77
 
78
+ Finally, pass the glimpse embeddings into the GPN
 
 
 
 
79
 
80
+ ```
81
+ # run the gpn on glimpse_embeddings and saccade_inputs [B,N,2]
82
+ # set return_all_actvs=True to acquire internal representations
83
+ representations, predictions = gpn(glimpse_embeddings, saccade_inputs, set return_all_actvs=True)
84
  ```
85
 
86
+ predictions: [B,N,2048] contain the prediction of the T+1 glimpse embedding at each T
87
+ representations is a dict; e.g. representations['lstm_out'], when variant = "R", have the best alignment to ventral and parietal ROI RDMs in NSD.