(features_path, output_path, show_num_identities_for_each_domain, if_reduce_dim_before_select=False, if_from_numpy='')
| 542 | |
| 543 | |
| 544 | def visualize_gcd(features_path, output_path, show_num_identities_for_each_domain, if_reduce_dim_before_select=False, if_from_numpy=''): |
| 545 | from collections import defaultdict |
| 546 | datasets = defaultdict(dict) |
| 547 | if not osp.exists(features_path[0]): |
| 548 | assert False, f'features_path ({features_path}) is not existing!' |
| 549 | query_dict_list, gallery_dict_list = [], [] |
| 550 | masks = None |
| 551 | if if_from_numpy: |
| 552 | print(f'Time: {time_now} \n Load {if_from_numpy} begin ...') |
| 553 | path = osp.join(if_from_numpy, 'embedding_2D.npy') |
| 554 | # saved_target_path = osp.join(if_from_numpy, 'target.npy') |
| 555 | # saved_mark_path = osp.join(if_from_numpy, 'mark.npy') |
| 556 | X_embedded = np.load(path) |
| 557 | targets = np.load(features_path[1]) |
| 558 | masks = np.load(features_path[2]) |
| 559 | else: |
| 560 | print(f'Time: {time_now} \n Dimentional Reduction begin ...') |
| 561 | tsne = TSNE(n_jobs=32) |
| 562 | l2_features = np.load(features_path[0]) |
| 563 | targets = np.load(features_path[1]) |
| 564 | masks = np.load(features_path[2]) |
| 565 | if if_reduce_dim_before_select is False: |
| 566 | targets = targets.astype(np.int64) |
| 567 | masks = masks.astype(np.bool) |
| 568 | old_classes = np.unique(targets[masks]) |
| 569 | new_classes = np.unique(targets[~masks]) |
| 570 | if show_num_identities_for_each_domain == 0: |
| 571 | selected_old_classes = old_classes |
| 572 | selected_new_classes = new_classes |
| 573 | else: |
| 574 | selected_old_classes = np.random.choice(old_classes, show_num_identities_for_each_domain) |
| 575 | selected_new_classes = np.random.choice(new_classes, show_num_identities_for_each_domain) |
| 576 | targets_, masks_ = np.array([]), np.array([]) |
| 577 | selected_feature = None |
| 578 | for _f, _l, _t in zip(l2_features, targets, masks): |
| 579 | if _l in selected_old_classes: |
| 580 | if selected_feature is None: |
| 581 | selected_feature = np.expand_dims(_f, 0) |
| 582 | else: |
| 583 | selected_feature = np.concatenate((selected_feature, np.expand_dims(_f, 0)), axis=0) |
| 584 | targets_ = np.append(targets_, _l) |
| 585 | masks_ = np.append(masks_, _t) |
| 586 | elif _l in selected_new_classes: |
| 587 | if selected_feature is None: |
| 588 | selected_feature = np.expand_dims(_f, 0) |
| 589 | else: |
| 590 | selected_feature = np.concatenate((selected_feature, np.expand_dims(_f, 0)), axis=0) |
| 591 | targets_ = np.append(targets_, _l) |
| 592 | masks_ = np.append(masks_, _t) |
| 593 | else: |
| 594 | selected_feature = l2_features |
| 595 | targets = targets_ |
| 596 | masks = masks_ |
| 597 | X_embedded = tsne.fit_transform(selected_feature) |
| 598 | # X_embedded = TSNE(n_components=2, perplexity=15, learning_rate=10).fit_transform(concated_features) |
| 599 | print(f'Time: {time_now} \n Dimentional Reduction end ...') |
| 600 | output_embedding_path = osp.join(output_path, 'embedding_2D.npy') |
| 601 | # output_paths_path = osp.join(output_path, 'concated_paths.npy') |
nothing calls this directly
no test coverage detected