ckscr = cohen_kappa_score(y_true, y_pred) f1scr = f1_score(y_true, y_pred, average="macro") print("Cohen Kappa Score:", ckscr) print("F1 Score: ", f1scr) return (ckscr + f1scr) / 2 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") alphabet = preprocess.labels() # Training data X_train, X_te, y_train, y_te, train_ws = preprocess.load_data() X_train = np.vstack((X_train, X_te)) y_train = np.hstack((y_train, y_te)) train_data = MyDataset(X_train, y_train, preprocess.make_transform(mode="eval")) train_loader = DataLoader(train_data, batch_size=512, shuffle=False, num_workers=8, pin_memory=True) # Testing data X_test, y_test, test_ws = preprocess.load_test_data() test_data = MyDataset(X_test, y_test, preprocess.make_transform(mode="eval")) test_loader = DataLoader(test_data, batch_size=512, shuffle=False, num_workers=8, pin_memory=True)
from modelling import MyDataset, get_model from torch import nn from torch import optim from torch.utils.data import DataLoader device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Optimizer learning rate learning_rate = 1e-2 # Epochs epochs = 100 # Validation loss early stopping patience # Number of epochs patience = 10 X_train, X_val, y_train, y_val, ws = preprocess.load_data() train_data = MyDataset(X_train, y_train, preprocess.make_transform("train")) train_loader = DataLoader( train_data, batch_size=128, shuffle=True, num_workers=8, pin_memory=torch.cuda.is_available() ) test_data = MyDataset(X_val, y_val, preprocess.make_transform("eval")) test_loader = DataLoader( test_data, batch_size=512, shuffle=False, num_workers=8, pin_memory=torch.cuda.is_available() ) base_dir = os.getcwd() model_dir = os.path.join(base_dir, "Code", "model") model_name = "ResNet" model_path = os.path.join(model_dir, "sign_model.pth") with open(os.path.join(model_dir, "model_specification"), "w") as ms: ms.write(model_name) my_classifier = get_model(model_name)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") labels = preprocess.labels() # Load model base_dir = os.getcwd() model_dir = os.path.join(base_dir, "Code", "best_models", "model_09") model_path = os.path.join(model_dir, "sign_model.pth") with open(os.path.join(model_dir, "model_specification"), "r") as ms: model_name = ms.readline().strip() my_classifier = get_model(model_name) my_classifier.load_state_dict(torch.load(model_path)) my_classifier.to(device) my_classifier.eval() transform = preprocess.make_transform(mode="predict") plt.ion() camera_url = "http://*****:*****@ip:8080/photo.jpg" while True: r = requests.get(camera_url) f = io.BytesIO(r.content) im = imread(f) im = rotate(im, -90, resize=True) im = im[420:-420, :] im = transform(np.float32(im)) im = im.view(1, 3, 224, 224) show_im = np.clip(im[0].numpy().transpose(1, 2, 0), 0, 1) plt.clf() plt.imshow(show_im)