def __init__(self, motionType): self.motionType = motionType.replace(" ", "").lower() self.status = 0 self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') self.lb = joblib.load("models/customized/" + self.motionType + "/outputs/lb_" + self.motionType + ".pkl") self.model = cnn_models.CustomCNN(self.lb) self.model.load_state_dict( torch.load("models/customized/" + self.motionType + "/outputs/" + self.motionType + ".pth")) self.aug = albumentations.Compose([albumentations.Resize(224, 224),]) self.model = self.model.eval().to(self.device) self.labels = [] self.isPressed = False
def __init__(self, motionType): self.motionType = motionType self.prepare_data() self.lr = 1e-3 self.batch_size = 32 self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") self.lb = joblib.load('models/customized/' + self.motionType + '/outputs/lb_' + self.motionType + '.pkl') self.model = cnn_models.CustomCNN(self.lb).to(self.device) self.epochs = 75 self.epoch = 0 df = pd.read_csv('models/customized/' + self.motionType + '/input/data_' + self.motionType + '.csv') X = df.image_path.values y = df.target.values (xtrain, xtest, ytrain, ytest) = train_test_split(X, y, test_size=0.10, random_state=42) print(f"Training instances: {len(xtrain)}") print(f"Validation instances: {len(xtest)}") self.train_data = ImageDataset(xtrain, ytrain, tfms=1) self.test_data = ImageDataset(xtest, ytest, tfms=0) self.trainloader = DataLoader(self.train_data, batch_size=self.batch_size, shuffle=True) self.testloader = DataLoader(self.test_data, batch_size=self.batch_size, shuffle=False) total_params = sum(p.numel() for p in self.model.parameters()) print(f"{total_params:,} total parameters.") total_trainable_params = sum( p.numel() for p in self.model.parameters() if p.requires_grad) print(f"{total_trainable_params:,} training parameters.") self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr) self.criterion = nn.CrossEntropyLoss() self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( self.optimizer, mode='min', patience=5, factor=0.5, min_lr=1e-6, verbose=True )
image = np.transpose(image, (2, 0, 1)).astype(np.float32) label = self.y[i] return torch.tensor(image, dtype=torch.float), torch.tensor(label, dtype=torch.long) train_data = ASLImageDataset(xtrain, ytrain) test_data = ASLImageDataset(xtest, ytest) # dataloaders trainloader = DataLoader(train_data, batch_size=32, shuffle=True) testloader = DataLoader(test_data, batch_size=32, shuffle=False) # model = models.MobineNetV2(pretrained=True, requires_grad=False) model = cnn_models.CustomCNN().to(device) print(model) # total parameters and trainable parameters total_params = sum(p.numel() for p in model.parameters()) print(f"{total_params:,} total parameters.") total_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print(f"{total_trainable_params:,} training parameters.") # optimizer optimizer = optim.Adam(model.parameters(), lr=0.001) # loss function criterion = nn.CrossEntropyLoss() # training function
USAGE: python cam_test.py ''' import torch import joblib import torch.nn as nn import numpy as np import cv2 import torch.nn.functional as F import time import cnn_models # load label binarizer lb = joblib.load( 'C:\\Users\\SARAH\\Desktop\\Multivariate Stats\\ASL\\output\\lb.pkl') model = cnn_models.CustomCNN() model.load_state_dict( torch.load( 'C:\\Users\\SARAH\\Desktop\\Multivariate Stats\\ASL\\output\\model.pth' )) print(model) print('Model loaded') # In[ ]: def hand_area(img): hand = img[100:324, 100:324] hand = cv2.resize(hand, (224, 224)) return hand
'--label-bin', required=True, help="path to label binarizer") ap.add_argument('-i', '--input', required=True, help='path to our input video') ap.add_argument('-o', '--output', required=True, type=str, help='path to our output video') args = vars(ap.parse_args()) # load the trained model and label binarizer from disk print('Loading model and label binarizer...') lb = joblib.load(args['label_bin']) model = cnn_models.CustomCNN().cuda() print('Model Loaded...') model.load_state_dict(torch.load(args['model'])) print('Loaded model state_dict...') aug = albumentations.Compose([ albumentations.Resize(224, 224), ]) cap = cv2.VideoCapture(args['input']) if (cap.isOpened() == False): print('Error while trying to read video. Plese check again...') # get the frame width and height