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

Same output for every image input #169

Open
sulakshgupta988 opened this issue Jan 26, 2022 · 2 comments
Open

Same output for every image input #169

sulakshgupta988 opened this issue Jan 26, 2022 · 2 comments

Comments

@sulakshgupta988
Copy link

I am trying to use SWIN Transformers for a problem that maps every image to a score of 1 to 100
When in initialize the model, everything works fine. But after training 1 epoch. the model gives the same output for every image.
I tried changing a few parameters.
Any suggestion what could be the reason behind this?

Thanks

@ancientmooner
Copy link
Contributor

Can you please provide more information, for example, your loss function, training configuration, loss curves, etc?

@sulakshgupta988
Copy link
Author

This is the model creation
model = swin_transformer.SwinTransformer(img_size=224, patch_size=4, in_chans=3, num_classes=0, embed_dim=96, depths=[2,2,6,2], num_heads=[3,6,12,24], window_size=7, mlp_ratio=4., qkv_bias=True, qk_scale=None, drop_rate=0.1, drop_path_rate=0.1, ape=False, patch_norm=True, use_checkpoint=False)

Below is the model configuration:
optimizer = build_optimizer(model)
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"number of params: {n_parameters}")
num_steps = int(20 * len(train_set))
warmup_steps = int(20 * len(train_set))
decay_steps = int(3 *len(train_set))
lr_scheduler = StepLRScheduler( optimizer, decay_t=decay_steps, decay_rate=0.1, warmup_lr_init=0.001, warmup_t=warmup_steps, t_in_epochs=False, )
criterion = nn.MSELoss()
model.float().cuda()
num_epochs = 50

Training Loop:

for epoch in range(num_epochs):
    n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print(n_parameters)
    for i, (samples, score) in enumerate(train_set):
      optimizer.zero_grad()
      samples = samples.cuda()
      score = score.cuda()
      outputs = model(samples)
      loss = criterion(outputs.squeeze(), score)
      loss.backward()
      optimizer.step()
      lr_scheduler.step_update(epoch * num_steps + i)
      torch.cuda.synchronize()
      lr = optimizer.param_groups[0]['lr']
    #train_one_epoch(model, criterion, train_set, optimizer, epoch, lr_scheduler, num_epochs)
    acc1, acc5, loss, corr, spear = validate(val_set, model)
    print(f"Correlation on {len(val_set)} test images: {corr:.7f}%" + "     " +str(spear))
    f = open("temp.txt", "a+")
    f.write("\n"+str(corr)+"\n")
    f.close()

Loss Function used is mean squared error.
build_optimizer function is taken from the build_optimizer.py file in the repo.
len(train_set) is around 500.
len(val_set) is around 150.

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

2 participants