Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to initialize the category center #3

Closed
dongdongtong opened this issue Jul 17, 2022 · 3 comments
Closed

How to initialize the category center #3

dongdongtong opened this issue Jul 17, 2022 · 3 comments

Comments

@dongdongtong
Copy link

Hi, it's me again.

I want to try your code on another dataset, the abdominal dataset, which was reported in the SIFA[TMI] paper. But I cannot find the initialization code for calculating category centers. Could you plz release the corresponding code?

Thanks!

@TFboys-lzz
Copy link
Owner

TFboys-lzz commented Jul 25, 2022

Sorry I didn't see it until now.
For calculating category centers, you first need pretrain the model on the source dataset,
and then you can calculate the category center by

def update_class_center_iter(cla_src_feas,batch_src_labels,class_center_feas,m):
'''
batch_src_feas : n*c*h*w
barch_src_labels: n*h*w
'''
batch_src_feas = cla_src_feas.detach()
batch_src_labels = batch_src_labels.cuda()
n,c,fea_h,fea_w = batch_src_feas.size()
batch_y_downsample = label_downsample(batch_src_labels, fea_h, fea_w) # n*fea_h*fea_w
batch_y_downsample = batch_y_downsample.unsqueeze(1) # n*1*fea_h*fea_w
batch_class_center_fea_list = []
for i in range(5):
fea_mask = torch.eq(batch_y_downsample,i).float().cuda() #n*1*fea_h*fea_w
class_feas = batch_src_feas * fea_mask # n*c*fea_h*fea_w
class_fea_sum = torch.sum(class_feas, [0, 2, 3]) # c
class_num = torch.sum(fea_mask, [0, 1, 2, 3])
if class_num == 0:
batch_class_center_fea = class_center_feas[i,:].detach()
else:
batch_class_center_fea = class_fea_sum/class_num
batch_class_center_fea = batch_class_center_fea.unsqueeze(0) # 1 * c
batch_class_center_fea_list.append(batch_class_center_fea)
batch_class_center_feas = torch.cat(batch_class_center_fea_list,dim=0) # n_class * c
class_center_feas = m * class_center_feas + (1-m) * batch_class_center_feas
return class_center_feas

@dongdongtong
Copy link
Author

Sovled. Thanks for your kindly reply.

@cqjtuhxf
Copy link

May I ask if you have solved it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants