This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dataset, vocab, max_len = [], set(), 0 | |
| # Preprocess the dataset by the specific IAM_Words dataset file structure | |
| words = open(os.path.join(dataset_path, "words.txt"), "r").readlines() | |
| for line in tqdm(words): | |
| if line.startswith("#"): | |
| continue | |
| line_split = line.split(" ") | |
| if line_split[1] == "err": |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def download_and_unzip(url, extract_to='Datasets', chunk_size=1024*1024): | |
| http_response = urlopen(url) | |
| data = b'' | |
| iterations = http_response.length // chunk_size + 1 | |
| for _ in tqdm(range(iterations)): | |
| data += http_response.read(chunk_size) | |
| zipfile = ZipFile(BytesIO(data)) | |
| zipfile.extractall(path=extract_to) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import tarfile | |
| from tqdm import tqdm | |
| from io import BytesIO | |
| from zipfile import ZipFile | |
| from urllib.request import urlopen | |
| import torch | |
| import torch.optim as optim | |
| from torchsummaryX import summary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # loop over test images | |
| for test_image, test_target in zip(test_data, test_targets): | |
| # normalize image and convert to tensor | |
| inference_image = torch.from_numpy(test_image).float() / 255.0 | |
| inference_image = inference_image.unsqueeze(0).unsqueeze(0) | |
| # predict | |
| output = network(inference_image) | |
| pred = output.argmax(dim=1, keepdim=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import cv2 | |
| import numpy as np | |
| import requests, gzip, os, hashlib | |
| from model import Net | |
| path='Datasets/mnist' # Path where to save the downloaded mnist dataset | |
| def fetch(url): | |
| if os.path.exists(path) is False: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| from model import Net | |
| # output path | |
| model_path = 'Model/07_pytorch_wrapper' | |
| # construct network and load weights | |
| network = Net() | |
| network.load_state_dict(torch.load("Models/07_pytorch_wrapper/model.pt")) | |
| network.eval() # set to evaluation mode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Epoch 1 - loss: 0.6189 - accuracy: 0.8059: 100%|ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 938/938 [00:12<00:00, 74.08it/s] | |
| val_loss: 0.1308 - val_accuracy: 0.9584: 100%|βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 157/157 [00:00<00:00, 239.33it/s] | |
| 2023-03-08 21:31:31,494 INFO ModelCheckpoint: Epoch 1: val_accuracy improved from -inf to 0.95840, saving model to Models/07_pytorch_wrapper/model.pt | |
| Epoch 2 - loss: 0.2956 - accuracy: 0.9151: 100%|βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 938/938 [00:06<00:00, 138.14it/s] | |
| val_loss: 0.0915 - val_accuracy: 0.9720: 100%|βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 157/157 [00:00<00:00, 225.51it/s] | |
| 2023-03-08 21:31:38,996 INFO ModelCheckpoint: Epoch 2: val_accuracy improved from 0.95840 to 0.97200, saving model to Models/07_pytorch_wrapper/m |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.fit( | |
| train_dataProvider, | |
| test_dataProvider, | |
| epochs=100, | |
| callbacks=[earlyStopping, modelCheckpoint] | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create model object that will handle training and testing of the network | |
| model = Model(network, optimizer, loss, metrics=[Accuracy()]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create callbacks | |
| earlyStopping = EarlyStopping( | |
| monitor='val_accuracy', | |
| patience=3, | |
| mode="max", | |
| verbose=1 | |
| ) | |
| modelCheckpoint = ModelCheckpoint( | |
| 'Models/07_pytorch_wrapper/model.pt', | |
| monitor='val_accuracy', |