PyTorch-Model-Compare icon indicating copy to clipboard operation
PyTorch-Model-Compare copied to clipboard

Comparision between ResNet50 and ViT gives error

Open ratom opened this issue 2 years ago • 0 comments

!pip install transformers import torch from torchvision import transforms from torch.utils.data import Dataset, DataLoader import torch.nn as nn import torch.nn.functional as F from transformers import ViTFeatureExtractor, ViTModel, ViTConfig, AutoConfig

Modify the model - ResNet

model_Res = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)

Remove the last layer of the model Res

layers_Res = list(model_Res.children()) model_Res = nn.Sequential(*layers_Res[:-1])

Set the top layers to be not trainable

count = 0 for child in model_Res.children(): count += 1 if count < 8: for param in child.parameters(): param.requires_grad = False

Modify the model - ViT model

model_trans = ViTModel.from_pretrained('google/vit-base-patch16-224-in21k') count = 0 for child in model_trans.children(): count += 1 if count >= 4: for param in child.parameters(): param.requires_grad = False

layers_trans = list(model_trans.children()) model_trans_top = nn.Sequential(*layers_trans[:-2])

model1 = model_Res model2 = model_trans_top

cka = CKA(model1, model2, model1_name="ResNet50", model2_name="ViT", device='cuda')

cka.compare(dataloader)

cka.plot_results(save_path="/content/drive/MyDrive/resnet-ViTcompare.png")

i got this error ValueError: Input image size (3232) doesn't match model (224224).

ratom avatar Jul 22 '23 16:07 ratom