cap_img = ImageCapture()
    frame = cap_img.capture_image()

    if frame is not None:
        im_conv = ImageConverter()
        temp_img = im_conv.crop_frame_to_square(
            im_conv.convert_frame_to_grey_scale(frame))
        img_48 = im_conv.rescale(temp_img, size=48)
        img_48 = im_conv.normalize_frame(img_48)
        img_array = im_conv.reshape_frame_to_array(img_48)

        dtype = torch.float
        device = torch.device("cpu")

        model_name = f'best'
        m_importer = ModelImporter('best_model')
        model = m_importer.load_nn_model(model_name, )
        model.eval()

        x = model.reshape_data(
            torch.tensor([img_48], device=device, dtype=dtype))
        predicted_emotion = None
        predicted_emotion2 = None
        with torch.no_grad():
            results = model(x).squeeze().detach().numpy()
            print(results)
            sort = np.sort(results, axis=0)
            idx1 = np.where(results == sort[6])
            idx2 = np.where(results == sort[5])
            print(idx1[0][0])
            print(idx2[0][0])
    pre.load_data(filename='test_public_norm.csv', name='test')

    X_df = pre.get(name='test').drop(columns=['emotion'])
    y_df = pre.get(name='test')['emotion']

    dtype = torch.float
    device = torch.device("cpu")

    n_classes = 7
    n_totalepochs = 100
    learning_rate = 0.0001
    batch_size = 32
    epoch_n = 50

    model_name = f'cnn_simple_reduced_{learning_rate}_{batch_size}_{n_totalepochs}_{n_classes}_epoch{epoch_n}'
    m_importer = ModelImporter('temp')
    model = m_importer.load_nn_model(model_name)
    model.eval()

    X_test = model.reshape_data(
        torch.tensor(X_df.values, device=device, dtype=dtype))
    y_test = torch.tensor(y_df.values, device=device, dtype=torch.long)

    y_pred = model(X_test).argmax(1)
    print(y_pred)
    print(y_test)

    accuracy_soft = (y_pred == y_test).float().mean()

    print(f'test accuracy {accuracy_soft.item()}')
    pre.load_data(filename='test_public_norm.csv', name='test')

    X_df = pre.get(name='test').drop(columns=['emotion'])
    y_df = pre.get(name='test')['emotion']

    n_classes = 7
    n_totalepochs = 200
    learning_rate = 0.00005
    batch_size = 32
    epoch_n = 40#for the temp folder

    dtype = torch.float
    device = torch.device("cpu")

    model_name = f'cnn_double_layer_reduced_{learning_rate}_{batch_size}_{n_totalepochs}_{n_classes}'
    model_name_bestvalloss = f'{model_name}_epoch150'
    m_importer = ModelImporter('fer2013_DatasetA')
    model = m_importer.load_nn_model(model_name_bestvalloss)
    model.eval()

    X_test = model.reshape_data(torch.tensor(X_df.values, device=device, dtype=dtype))
    y_test = torch.tensor(y_df.values, device=device, dtype=torch.long)

    y_pred = model(X_test).argmax(1)
    print(y_pred)
    print(y_test)

    accuracy_soft = (y_pred == y_test).float().mean()

    print(f'test accuracy {accuracy_soft.item()}')
Esempio n. 4
0
    print(file)
    frame = cv2.imread(file, 0)

    if frame is not None:
        im_conv = ImageConverter()
        temp_img = im_conv.crop_frame_to_square(
            im_conv.convert_frame_to_grey_scale(frame))
        img_48 = im_conv.rescale(temp_img, size=48)
        img_48 = im_conv.normalize_frame(img_48)
        img_array = im_conv.reshape_frame_to_array(img_48)

        dtype = torch.float
        device = torch.device("cpu")

        model_name = f'best_model'
        m_importer = ModelImporter('best')
        model = m_importer.load_nn_model(model_name)
        model.eval()

        x = model.reshape_data(
            torch.tensor([img_48], device=device, dtype=dtype))
        predicted_emotion = None
        predicted_emotion2 = None
        with torch.no_grad():
            results = model(x).squeeze().detach().numpy()
            sort = np.sort(results, axis=0)
            idx1 = np.where(results == sort[6])
            idx2 = np.where(results == sort[5])
            print(results)
            predicted_emotion = emotion_dict[idx1[0][0]]
            predicted_emotion2 = emotion_dict[idx2[0][0]]