-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from johnzhang1999/multi-image
Multi image
- Loading branch information
Showing
7 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,31 @@ | ||
import numpy as np | ||
import torch | ||
__all__ = ['combine_by_id'] | ||
|
||
def combine_by_id(gf, g_pids, method): | ||
""" | ||
transforms features of same bag to a bag embedding | ||
""" | ||
if method == "none": | ||
print("Does not combine by id") | ||
return gf, g_pids | ||
elif method == "mean": | ||
print("Calculating mean by id ...") | ||
gf = gf.numpy() | ||
unique_ids = set(g_pids) | ||
new_g_pids = [] | ||
gf_by_id = np.empty((len(unique_ids), gf.shape[-1])) | ||
for i, gid in enumerate(unique_ids): | ||
gf_by_id[i] = np.mean(gf[np.asarray(g_pids) == gid], axis=0) | ||
new_g_pids.append(gid) | ||
gf = torch.tensor(gf_by_id, dtype=torch.float) | ||
g_pids = np.array(new_g_pids) | ||
return gf, g_pids | ||
elif method == "self_attention": | ||
# TODO: self attention | ||
return distmat | ||
elif method == "multi_head_attention": | ||
# TODO: multi-headed attention | ||
return distmat | ||
else: | ||
raise ValueError('Must be valid combine-method') |