Example #1
0
def predict(X):
    nn = NN([6, 8, 4, 1])
    nn.load_state_dict(torch.load('model_state/state'))
    nn.eval()
    result = nn.forward(X).detach().numpy().reshape(-1, 1)
    result_transformed = scaler.inverse_transform(result)
    return result_transformed
Example #2
0
 def train(self, epochs=10):
     Model = NN(batch_size=30)
     opt = optim.Adam(Model.parameters(), lr=0.005)
     criterion = nn.BCELoss()
     softmax = nn.Softmax(dim=0)
     loss = 0
     print(self.labels.shape)
     for i in range(epochs):
         item_loss = 0
         for i, (feat, lab) in enumerate(self.dataloader):
             feat = feat[0][:, :1, :, :]
             output = Model.forward(feat)
             loss = criterion(output, lab.view((30)))
             loss.backward()
             opt.step()
             item_loss += loss.item()
         print("LOSS ---->", item_loss / len(self.dataloader))
     torch.save(Model.state_dict(), "1")
Example #3
0
import cv2
from model import NN
import numpy as np
import torch

cap = cv2.VideoCapture(0)
i = 0 
classify = 1 
labels =[] 
Model = NN(batch_size = 1)
Model.load_state_dict(torch.load("1"))
Model.eval()
tardict = {1 : 'Face Detected' , 0 : 'Undetected'  }

while True:
    i += 1
    ret  , frame = cap.read()
    gray = cv2.cvtColor(frame , cv2.COLOR_RGB2GRAY)
    gray = cv2.GaussianBlur(gray, (15,15), 0)
    cv2.imshow('feed' , frame)
    gray = torch.from_numpy(gray).view(1 , 1, 480 , 640).float()
    output = torch.round(Model.forward(gray))
    output = output.item()
    print (tardict[output])
    if output != 0:
        input()
    if cv2.waitKey(1) & 0xFF == ord('q') :
        break 
   
Example #4
0
            print('Early stopping')
            break
        last_loss_test = loss_test
        print('epoch {}, loss - {}'.format(epoch, loss))
        print('test loss - {}'.format(loss_test))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


if __name__ == '__main__':
    data = pd.read_csv('datasets/anchor-weed.csv')
    scaler.fit_transform(data[['price']])

    np.random.seed(100)
    nr_classes = 6
    hidden_size = 12
    data = pd.read_csv('datasets/anchor-weed-prepared.csv')
    data = data.drop('Unnamed: 0', axis=1)
    y = data['price+1'].values
    X = data.drop('price+1', axis=1).values
    model = NN([nr_classes, 8, 4, 1])
    train(model, X, y, 30000, 0.01)
    torch.save(model.state_dict(), 'model_state/state')

    y_pred = model.forward(torch.from_numpy(X[275, :]).float())
    y_pred = np.exp(y_pred.detach().numpy().reshape(-1, 1))
    y_ = np.exp(y[275])

    plot_results(model, X, y)