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

Comparision between ResNet50 and ViT gives error #12

Open
ratom opened this issue Jul 22, 2023 · 0 comments
Open

Comparision between ResNet50 and ViT gives error #12

ratom opened this issue Jul 22, 2023 · 0 comments

Comments

@ratom
Copy link

ratom commented Jul 22, 2023

!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).

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

1 participant