Update README.md
Browse files12 unmodified lines13141516171819202122232425264 unmodified lines313233343536372 unmodified lines4041424344454647484950515253545556575859606162636465666768697071727312 unmodified linesThis repo contains pretrained GPN models (R and RS variants), based on the SimCLR backbone, trained on DeepGaze3 glimpses on COCO scenes.## Install```bashpip install torch torchvision huggingface_hub pillow```## Usage```from huggingface_hub import hf_hub_downloadimport torch4 unmodified linesdevice = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")# setup glimpse encoder - SimCLR-RN50ckpt = hf_hub_download(REPO_ID, filename="SimCLR ResNet50 1x.pt") # from: https://huggingface.co/lightly-ai/simclrv1-imagenet1k-resnet50-1xencoder = resnet50(weights=None)encoder.load_state_dict(torch.load(ckpt, map_location="cpu")['state_dict'])preprocess = transforms.Compose([2 unmodified lines])encoder.to(device)encoder.eval()activation = {} # remember to empty the list after each forward passdef get_activation(name): # setting hooksdef hook(model, input, output):activation[name] = output.detach()return hookencoder.avgpool.register_forward_hook(get_activation('avgpool')) # extracting avgpool activations from the RN50-SimCLR# choose GPN "R" or "RS"variant = "R"ckpt = hf_hub_download(REPO_ID, filename=f"gpn_{variant}.pth")gpn = build_gpn(variant=variant)gpn.load_state_dict(torch.load(ckpt, map_location="cpu"))gpn.eval()# obtain 'glimpse_embeddings' [B,N,2048] for your N input_glimpses per scene [B,N,H,W,C]: numpy arrayinput_glimpses_tensor = preprocess(torch.from_numpy(input_glimpses.reshape(B*N,H,W,C)).permute(0, 3, 1, 2)).to(device)with torch.no_grad():_ = encoder(input_glimpses_tensor)glimpse_embeddings = activation['avgpool'].detach().cpu().numpy().squeeze()glimpse_embeddings = glimpse_embeddings.reshape(B,N,-1)# run the gpn on glimpse_embeddings and saccade_inputs [B,N,2]; set return_all_actvs=True to acquire internal representationsrepresentations, predictions = gpn(glimpse_embeddings, saccade_inputs, set return_all_actvs=True)# predictions: [B,N,2048] - prediction of the T+1 glimpse embeddings at T# representations['lstm_out'], when variant = "R", have the best alignment to ventral and parietal ROI RDMs in NSD.```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.I'd advise you to do similar preprocessing steps on your images to obtain 'input_glimpses', unless you have apriori reasons to do otherwise.12 unmodified lines131415161718192021224 unmodified lines272829303132333435362 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868712 unmodified linesThis repo contains pretrained GPN models (R and RS variants), based on the SimCLR backbone, trained on DeepGaze3 glimpses on COCO scenes.## UsageFirst setup the glimpse encoder backbone and the GPN:```from huggingface_hub import hf_hub_downloadimport torch4 unmodified linesdevice = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")# setup glimpse encoder - SimCLR-RN50# obtained from: https://huggingface.co/lightly-ai/simclrv1-imagenet1k-resnet50-1xckpt = hf_hub_download(REPO_ID, filename="SimCLR ResNet50 1x.pt")encoder = resnet50(weights=None)encoder.load_state_dict(torch.load(ckpt, map_location="cpu")['state_dict'])preprocess = transforms.Compose([2 unmodified lines])encoder.to(device)encoder.eval()# setting up avgpool activation extraction from the RN50-SimCLRactivation = {} # remember to empty the list after each forward passdef get_activation(name): # setting hooksdef hook(model, input, output):activation[name] = output.detach()return hookencoder.avgpool.register_forward_hook(get_activation('avgpool'))# choose GPN "R" or "RS"variant = "R"ckpt = hf_hub_download(REPO_ID, filename=f"gpn_{variant}.pth")gpn = build_gpn(variant=variant)gpn.load_state_dict(torch.load(ckpt, map_location="cpu"))gpn.eval()```Next, gather your N glimpse images for each of the B scenes into a [B,N,H,W,3] uint8 numpy array.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.I'd advise you to do similar preprocessing steps on your images to obtain 'input_glimpses', unless you have apriori reasons to do otherwise.Gather the glimpse embeddings```# obtain 'glimpse_embeddings' [B,N,2048]input_glimpses_tensor = preprocess(torch.from_numpy(input_glimpses.reshape(B*N,H,W,C)).permute(0, 3, 1, 2)).to(device)with torch.no_grad():_ = encoder(input_glimpses_tensor)glimpse_embeddings = activation['avgpool'].detach().cpu().numpy().squeeze()glimpse_embeddings = glimpse_embeddings.reshape(B,N,-1)```Finally, pass the glimpse embeddings into the GPN```# run the gpn on glimpse_embeddings and saccade_inputs [B,N,2]# set return_all_actvs=True to acquire internal representationsrepresentations, predictions = gpn(glimpse_embeddings, saccade_inputs, set return_all_actvs=True)```predictions: [B,N,2048] contain the prediction of the T+1 glimpse embedding at each Trepresentations is a dict; e.g. representations['lstm_out'], when variant = "R", have the best alignment to ventral and parietal ROI RDMs in NSD.
README.md+30-16