<jupyter_start><jupyter_text>Download MNIST data<jupyter_code>transformer = transforms.Compose([
                    transforms.Resize(105),
                    transforms.ToTensor(),
                    transforms.Normalize(mean=0.5,std=0.5)])

# If you run this code first time, make 'download' option True
test_data = dset.MNIST(root='MNIST_data/',train=False,transform=transformer, download=False)
test_image, test_label = test_data[0]

plt.imshow(test_image.squeeze().numpy(), cmap='gray')
plt.title('%i' % test_label)
plt.show()

print(test_image.size())
print('number of test data:', len(test_data))<jupyter_output><empty_output><jupyter_text>Make Dataloader<jupyter_code>class MNISTTest(Dataset):
    def __init__(self, dataset,trial):
        self.dataset = dataset
        self.trial = trial
        if trial > 950:
            self.trial = 950

    def __len__(self):
        return self.trial * 10

    def __getitem__(self, index):
        share, remain = divmod(index,10)
        label = (share//10)%10
        image1 = self.dataset[label][share][0]
        image2 = self.dataset[remain][random.randrange(len(self.dataset[remain]))][0]

        return image1, image2, label
image_by_num = [[],[],[],[],[],[],[],[],[],[]]
for x,y in tqdm(test_data):
    image_by_num[y].append(x)

test_data1 = MNISTTest(image_by_num,trial=950) #MAX trial = 950
test_loader = DataLoader(test_data1, batch_size=10)<jupyter_output><empty_output><jupyter_text>Declare model and configuration<jupyter_code>config = get_config()
config.num_model = "1"
config.logs_dir = "./result/1"
model = SiameseNet()
is_best = False
device = 'cuda' if torch.cuda.is_available() else 'cpu'<jupyter_output>[*] use GPU Quadro RTX 4000<jupyter_text>Load trained model<jupyter_code>if is_best:
    model_path = os.path.join(config.logs_dir, './models/best_model.pt')
else:
    model_path = sorted(glob(config.logs_dir + './models/model_ckpt_*.pt'), key=len)[-1]

ckpt = torch.load(model_path)

model.load_state_dict(ckpt['model_state'])
model.to(device)
print(f"[*] Load model {os.path.basename(model_path)}, best accuracy {ckpt['best_valid_acc']}")<jupyter_output>[*] Load model model_ckpt_50.pt, best accuracy 0.903125<jupyter_text>Test<jupyter_code>correct_sum = 0
num_test = len(test_loader)
print(f"[*] Test on {num_test} pairs.")

pbar = tqdm(enumerate(test_loader), total=num_test, desc="Test")
for i, (x1, x2, y) in pbar:
    # plt.figure(figsize=(20,7))
    # plt.subplot(1,4,1)
    # plt.title("Target")
    # plt.imshow(x1[0].squeeze().numpy(), cmap='gray')
    #
    # s = 2
    # for idx in range(10):
    #     plt.subplot(3,4,s)
    #     plt.title(idx)
    #     plt.imshow(x2[idx].squeeze().numpy(), cmap='gray')
    #     s += 1
    #     if s % 4 == 1:
    #         s += 1
    # plt.show()
    # break

    if torch.cuda.is_available():
        x1, x2, y = x1.to(device), x2.to(device), y.to(device)
    x1, x2 = x1.unsqueeze(1), x2.unsqueeze(1)

    # compute log probabilities
    out = model(x1, x2)
    y_pred = torch.sigmoid(out)
    y_pred = torch.argmax(y_pred)
    if y_pred == y[0].item():
        correct_sum += 1

    pbar.set_postfix_str(f"accuracy: {correct_sum} / {num_test}")

test_acc = (100. * correct_sum) / num_test
print(f"Test Acc: {correct_sum}/{num_test} ({test_acc:.2f}%)")<jupyter_output>[*] Test on 950 pairs.
Test Acc: 296/950 (31.16%)
{'fraction_non_alphanumeric': 0.16274217585692996, 'fraction_numerical': 0.031594634873323396, 'mean_word_length': 3.856729377713459, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '2530', 'license': 'MIT', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1281, 'n_words': 225, 'path': 'siamese/MNIST_Test.ipynb', 'repo_name': 'SoongE/MetaLearning-pytorch', 'size': '3355'}