This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
add visual search #25
Open
shashikg
wants to merge
14
commits into
brain-score:master
Choose a base branch
from
shashikg:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fce9862
add visual search model for object array
shashikg a9b3caf
Chnages to ModelCommitment to add visual search brain model
shashikg b15f42f
show visual search status
shashikg 13f1bb0
Merge branch 'master' into master
shashikg b73ac40
removed candidate_model dependencies and some minor changes
shashikg c0c14c9
Merge branch 'master' of https://github.com/shashikg/model-tools
shashikg 3a81068
Merge branch 'master' into master
mschrimpf 4e583fa
simplify behavior arbitration
mschrimpf 1f9b095
auto-format
mschrimpf 0a40eb3
add vs
shashikg 9958deb
Merge branch 'master' of https://github.com/brain-score/model-tools i…
shashikg 9d1cf90
Merge branch 'brain-score-master'
shashikg d5f29ac
remove redundant import
shashikg f4cf9b3
visual search - waldo and natural design
shashikg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| __pycache__/* | ||
| .ipynb_checkpoints | ||
| build/* | ||
| dist/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import os | ||
| from collections import OrderedDict | ||
|
|
||
| from brainio_base.assemblies import BehavioralAssembly | ||
| from brainscore.model_interface import BrainModel | ||
| from candidate_models.base_models import BaseModelPool | ||
| from candidate_models.model_commitments.vs_layer import visual_search_layer | ||
|
shashikg marked this conversation as resolved.
Outdated
|
||
|
|
||
| import cv2 | ||
|
shashikg marked this conversation as resolved.
Outdated
|
||
| import numpy as np | ||
| from tqdm import tqdm | ||
|
|
||
| class VisualSearchObjArray(BrainModel): | ||
| def __init__(self, identifier, target_layer, stimulus_layer): | ||
| self.current_task = None | ||
| self.eye_res = 224 | ||
| self.arr_size = 6 | ||
| self.data_len = 300 | ||
|
shashikg marked this conversation as resolved.
Outdated
|
||
| self.identifier = identifier | ||
|
|
||
| self.fix = [[640, 512], | ||
| [365, 988], | ||
| [90, 512], | ||
| [365, 36], | ||
| [915, 36], | ||
| [1190, 512], | ||
| [915, 988]] | ||
|
shashikg marked this conversation as resolved.
Outdated
|
||
|
|
||
| target_model_pool = BaseModelPool(input_size=28) | ||
| stimulus_model_pool = BaseModelPool(input_size=224) | ||
| self.target_model = target_model_pool[identifier] | ||
| self.stimuli_model = stimulus_model_pool[identifier] | ||
|
shashikg marked this conversation as resolved.
Outdated
|
||
|
|
||
| if target_layer==None: | ||
| self.target_layer = visual_search_layer[identifier][0] | ||
| self.stimuli_layer = visual_search_layer[identifier][0] | ||
| else: | ||
| self.target_layer = target_layer | ||
| self.stimuli_layer = stimulus_layer | ||
|
|
||
|
|
||
| def start_task(self, task: BrainModel.Task): | ||
| self.current_task = task | ||
| print(task, "started") | ||
|
shashikg marked this conversation as resolved.
Outdated
|
||
|
|
||
| def look_at(self, stimuli_set): | ||
| self.gt_array = [] | ||
| gt = stimuli_set[stimuli_set['image_label'] == 'mask'] | ||
| gt_paths = list(gt.image_paths.values())[int(gt.index.values[0]):int(gt.index.values[-1]+1)] | ||
|
|
||
| for i in range(6): | ||
| imagename_gt = gt_paths[i] | ||
|
|
||
| gt = cv2.imread(imagename_gt, 0) | ||
| gt = cv2.resize(gt, (self.eye_res, self.eye_res), interpolation = cv2.INTER_AREA) | ||
| retval, gt = cv2.threshold(gt, 125, 255, cv2.THRESH_BINARY) | ||
| temp_stim = np.uint8(np.zeros((3*self.eye_res, 3*self.eye_res))) | ||
| temp_stim[self.eye_res:2*self.eye_res, self.eye_res:2*self.eye_res] = np.copy(gt) | ||
| gt = np.copy(temp_stim) | ||
| gt = gt/255 | ||
|
|
||
| self.gt_array.append(gt) | ||
|
|
||
| self.gt_total = np.copy(self.gt_array[0]) | ||
| for i in range(1,6): | ||
| self.gt_total += self.gt_array[i] | ||
|
|
||
| self.score = np.zeros((self.data_len, self.arr_size+1)) | ||
| self.data = np.zeros((self.data_len, self.arr_size+2, 2), dtype=int) | ||
| S_data = np.zeros((300, 7, 2), dtype=int) | ||
| I_data = np.zeros((300, 1), dtype=int) | ||
|
|
||
| data_cnt = 0 | ||
|
|
||
| target = stimuli_set[stimuli_set['image_label'] == 'target'] | ||
| target_features = self.target_model(target, layers=[self.target_layer], stimuli_identifier=False) | ||
| if target_features.shape[0] == target_features['neuroid_num'].shape[0]: | ||
| target_features = target_features.T | ||
|
|
||
| stimuli = stimuli_set[stimuli_set['image_label'] == 'stimuli'] | ||
| stimuli_features = self.stimuli_model(stimuli, layers=[self.stimuli_layer], stimuli_identifier=False) | ||
| if stimuli_features.shape[0] == stimuli_features['neuroid_num'].shape[0]: | ||
| stimuli_features = stimuli_features.T | ||
|
|
||
| print(stimuli_features.shape, self.stimuli_layer, target_features.shape, self.target_layer) | ||
|
|
||
| import torch | ||
|
|
||
| for i in tqdm(range(self.data_len)): | ||
| op_target = self.unflat(target_features[i:i+1]) | ||
| MMconv = torch.nn.Conv2d(op_target.shape[1], 1, kernel_size=op_target.shape[2], stride=1, bias=False) | ||
| MMconv.weight = torch.nn.Parameter(torch.Tensor(op_target)) | ||
|
|
||
| gt_idx = target_features.tar_obj_pos.values[i] | ||
| gt = self.gt_array[gt_idx] | ||
|
|
||
| op_stimuli = self.unflat(stimuli_features[i:i+1]) | ||
| out = MMconv(torch.Tensor(op_stimuli)).detach().numpy() | ||
| out = out.reshape(out.shape[2:]) | ||
|
|
||
| out = out - np.min(out) | ||
| out = out/np.max(out) | ||
| out *= 255 | ||
| out = np.uint8(out) | ||
| out = cv2.resize(out, (self.eye_res, self.eye_res), interpolation = cv2.INTER_AREA) | ||
| out = cv2.GaussianBlur(out,(7,7),3) | ||
|
|
||
| temp_stim = np.uint8(np.zeros((3*self.eye_res, 3*self.eye_res))) | ||
| temp_stim[self.eye_res:2*self.eye_res, self.eye_res:2*self.eye_res] = np.copy(out) | ||
| attn = np.copy(temp_stim*self.gt_total) | ||
|
|
||
| saccade = [] | ||
| (x, y) = int(attn.shape[0]/2), int(attn.shape[1]/2) | ||
| saccade.append((x, y)) | ||
|
|
||
| for k in range(self.arr_size): | ||
| (x, y) = np.unravel_index(np.argmax(attn), attn.shape) | ||
|
|
||
| fxn_x, fxn_y = x, y | ||
|
|
||
| fxn_x, fxn_y = max(fxn_x, self.eye_res), max(fxn_y, self.eye_res) | ||
| fxn_x, fxn_y = min(fxn_x, (attn.shape[0]-self.eye_res)), min(fxn_y, (attn.shape[1]-self.eye_res)) | ||
|
|
||
| saccade.append((fxn_x, fxn_y)) | ||
|
|
||
| attn, t = self.remove_attn(attn, saccade[-1][0], saccade[-1][1]) | ||
|
|
||
| if(t==gt_idx): | ||
| self.score[data_cnt, k+1] = 1 | ||
| data_cnt += 1 | ||
| break | ||
|
|
||
| saccade = np.asarray(saccade) | ||
| j = saccade.shape[0] | ||
|
|
||
| for k in range(j): | ||
| tar_id = self.get_pos(saccade[k, 0], saccade[k, 1], 0) | ||
| saccade[k, 0] = self.fix[tar_id][0] | ||
| saccade[k, 1] = self.fix[tar_id][1] | ||
|
|
||
| I_data[i, 0] = min(7, j) | ||
| S_data[i, :j, 0] = saccade[:, 0].reshape((-1,))[:7] | ||
| S_data[i, :j, 1] = saccade[:, 1].reshape((-1,))[:7] | ||
|
|
||
| self.data[:,:7,:] = S_data | ||
| self.data[:,7,:] = I_data | ||
|
|
||
| return (self.score, self.data) | ||
|
|
||
| def remove_attn(self, img, x, y): | ||
| t = -1 | ||
| for i in range(5, -1, -1): | ||
| fxt_place = self.gt_array[i][x, y] | ||
| if (fxt_place>0): | ||
| t = i | ||
| break | ||
|
|
||
| if(t>-1): | ||
| img[self.gt_array[t] == 1] = 0 | ||
|
|
||
| return img, t | ||
|
|
||
| def get_pos(self, x, y, t): | ||
| for i in range(5, -1, -1): | ||
| fxt_place = self.gt_array[i][int(x), int(y)] | ||
| if (fxt_place>0): | ||
| t = i + 1 | ||
| break | ||
| return t | ||
|
|
||
| def unflat(self, X): | ||
| channel_names = ['channel', 'channel_x', 'channel_y'] | ||
| assert all(hasattr(X, coord) for coord in channel_names) | ||
| shapes = [len(set(X[channel].values)) for channel in channel_names] | ||
| X = np.reshape(X.values, [X.shape[0]] + shapes) | ||
| X = np.transpose(X, axes=[0, 3, 1, 2]) | ||
| return X | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.