Spaces:
Sleeping
Sleeping
File size: 4,746 Bytes
40243b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import torch
import torch.nn as nn
import torchvision.models as models
import sys
import os
import pickle
import re
from collections import Counter
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
EMBED_DIM = 512
HIDDEN_DIM = 512
MAX_LEN = 25
# Vocabulary class
class Vocabulary:
def __init__(self, freq_threshold=5):
self.freq_threshold = freq_threshold
self.itos = {0: "pad", 1: "startofseq", 2: "endofseq", 3: "unk"}
self.stoi = {v: k for k, v in self.itos.items()}
self.index = 4
def __len__(self):
return len(self.itos)
def tokenizer(self, text):
text = text.lower()
tokens = re.findall(r"\w+", text)
return tokens
def build_vocabulary(self, sentence_list):
frequencies = Counter()
for sentence in sentence_list:
tokens = self.tokenizer(sentence)
frequencies.update(tokens)
for word, freq in frequencies.items():
if freq >= self.freq_threshold:
self.stoi[word] = self.index
self.itos[self.index] = word
self.index += 1
def numericalize(self, text):
tokens = self.tokenizer(text)
numericalized = []
for token in tokens:
if token in self.stoi:
numericalized.append(self.stoi[token])
else:
numericalized.append(self.stoi["unk"])
return numericalized
class Encoder(nn.Module):
def __init__(self, embed_dim):
super().__init__()
resnet = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
self.backbone = nn.Sequential(*list(resnet.children())[:-1])
self.fc = nn.Linear(resnet.fc.in_features, embed_dim)
self.bn = nn.BatchNorm1d(embed_dim)
def forward(self, x):
with torch.no_grad():
features = self.backbone(x)
features = features.reshape(features.size(0), -1)
features = self.bn(self.fc(features))
return features
class Decoder(nn.Module):
def __init__(self, embed_dim, hidden_dim, vocab_size):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(
embed_dim,
hidden_dim,
batch_first=True
)
self.fc = nn.Linear(hidden_dim, vocab_size)
def forward(self, x, states=None):
emb = self.embedding(x)
outputs, states = self.lstm(emb, states)
logits = self.fc(outputs)
return logits, states
class CaptionModel(nn.Module):
def __init__(self, embed_dim, hidden_dim, vocab_size):
super().__init__()
self.encoder = Encoder(embed_dim)
self.decoder = Decoder(embed_dim, hidden_dim, vocab_size)
# Main debug
script_dir = os.path.dirname(os.path.abspath(__file__))
CHECKPOINT_PATH = os.path.join(script_dir, "best_checkpoint.pth")
VOCAB_PATH = os.path.join(script_dir, "vocab.pkl")
print("=" * 80)
print("LOADING CHECKPOINT")
print("=" * 80)
checkpoint = torch.load(CHECKPOINT_PATH, map_location=DEVICE)
print(f"\nCheckpoint keys: {list(checkpoint.keys())}")
print("\nCheckpoint model_state_dict keys:")
checkpoint_keys = set(checkpoint["model_state_dict"].keys())
for key in sorted(checkpoint_keys):
shape = checkpoint["model_state_dict"][key].shape
print(f" {key}: {shape}")
# Load vocab
with open(VOCAB_PATH, "rb") as f:
vocab = pickle.load(f)
vocab_size = len(vocab)
print(f"\nVocab size: {vocab_size}")
# Create model
model = CaptionModel(
EMBED_DIM,
HIDDEN_DIM,
vocab_size
).to(DEVICE)
print("\n" + "=" * 80)
print("MODEL STATE DICT KEYS")
print("=" * 80)
model_keys = set(model.state_dict().keys())
for key in sorted(model_keys):
shape = model.state_dict()[key].shape
print(f" {key}: {shape}")
# Check differences
print("\n" + "=" * 80)
print("COMPARISON")
print("=" * 80)
print("\nKeys in checkpoint but NOT in model:")
for key in sorted(checkpoint_keys - model_keys):
print(f" {key}")
print("\nKeys in model but NOT in checkpoint:")
for key in sorted(model_keys - checkpoint_keys):
print(f" {key}")
print("\nKeys in both but with different shapes:")
for key in sorted(checkpoint_keys & model_keys):
cp_shape = checkpoint["model_state_dict"][key].shape
model_shape = model.state_dict()[key].shape
if cp_shape != model_shape:
print(f" {key}")
print(f" Checkpoint: {cp_shape}")
print(f" Model: {model_shape}")
print("\n" + "=" * 80)
print("ATTEMPTING TO LOAD WEIGHTS")
print("=" * 80)
try:
model.load_state_dict(checkpoint["model_state_dict"])
print("SUCCESS: Weights loaded successfully!")
except Exception as e:
print(f"ERROR: {e}")
|