Beispiel #1
0
def load_prob_cnn(paths, X_test, Y_test, num=100):
    softmax = torch.nn.Softmax(dim=0)
    model1 = VGG16()
    model1.load_state_dict(torch.load(paths))
    print("loaded model!")

    new_size = np.array([])
    test_size = np.array([])
    models_prob = np.zeros((2, num * 77))
    for i in range(len(paths)):
        new_size = np.append(new_size, Y_test)
        test_size = np.append(test_size, np.shape(new_size)[0])
        cur_model = model1
        cur_model.eval()
        print("\n now evaluating")
        for j in range(num):
            A = cur_model(X_test[j * 77:(j + 1) * 77, :, :, :].float())
            B = A.detach().numpy()
            models_prob[:, j * 77:(j + 1) * 77] = B.T
    mp = torch.from_numpy(models_prob)
    out = softmax(mp)
    output = out.numpy()
    return test_size, np.around(output[1, :], decimals=2)
Beispiel #2
0
    w2 = torch.cat((ww1, ww2))
    l1 = labels4[:n4] + labels3[:n3]
    l2 = labels1[n1:] + labels2[n2:]
    wid = window_idx_full1[n1:] + window_idx_full2[n2:]
    train_windows, train_labels = shuffle(w1, l1)
    test_windows, test_labels, test_id = shuffle(w2, l2, wid)
    return train_windows.detach(), test_windows.detach(
    ), train_labels, test_labels, test_id


C = preprossingPipeline(BC_datapath=r"/work3/s173934/Fagprojekt/dataEEG")
path_s = r'/work3/s173934/Fagprojekt/spectograms_rgb'
criterion = nn.CrossEntropyLoss()
X_train, X_valid, Y_train, Y_valid, windows_id = split_dataset_balanced(
    C, path_s, N=120, train_split=80, max_windows=20, num_channels=14)
modelA = VGG16()
freeze_parameters(modelA, feature_extracting=True)
list2 = np.array(list_of_features(modelA))
modelB = VGG16()
freeze_parameters(modelB, feature_extracting=True)
for i in range(2):
    PATH = '/zhome/87/9/127623/FagprojektBALANCEDTESTS'
    if i == 0:
        activation_list = np.array([28, 29, 30, 31])
        grad_parameters(modelA, list(list2[activation_list]))
        optimizer = optim.Adam(modelA.parameters(), lr=0.0005)
        train_acc, train_loss, val_acc, val_loss, wrong_guesses, wrong_predictions, all_pred, modelA = test_CNN(
            modelA,
            X_train,
            Y_train,
            X_valid,
Beispiel #3
0
    train_windows, train_labels = shuffle(w1, l1)
    test_windows, test_labels, test_id = shuffle(w2, l2, wid)
    return train_windows.detach(), test_windows.detach(
    ), train_labels, test_labels, test_id


C = preprossingPipeline(BC_datapath=r"/work3/s173934/Fagprojekt/dataEEG")
path_s = r'/work3/s173934/Fagprojekt/spectograms_rgb'
criterion = nn.CrossEntropyLoss()
X_train, X_valid, Y_train, Y_valid, windows_id = split_dataset(C,
                                                               path_s,
                                                               N=120,
                                                               train_split=80,
                                                               max_windows=20,
                                                               num_channels=14)
modelB = VGG16()
list2 = np.array(list_of_features(modelB))
freeze_parameters(modelB, feature_extracting=True)
PATH = '/zhome/87/9/127623/FagprojektBALANCEDTESTS'

activation_list = np.array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
grad_parameters(modelB, list(list2[activation_list]))
optimizer = optim.Adam(modelB.parameters(), lr=0.0001)
train_acc, train_loss, val_acc, val_loss, wrong_guesses, wrong_predictions, all_pred, modelB = test_CNN(
    modelB,
    X_train,
    Y_train,
    X_valid,
    Y_valid,
    windows_id,
    batch_size=256,
Beispiel #4
0
import pickle
import sys
from CNN.modifyCNN import VGG16


def new_forward(model, x):
    x = model.features(x)
    x = model.avgpool(x)
    x = torch.flatten(x, 1)
    x = model.classifier[0:5](x)
    return x


test_set = torch.load(
    r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_ub\test_set_ub.pt'
)
test_labels = np.load(
    r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_ub\test_labels_ub.npy'
)
test_windows = np.load(
    r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_ub\test_windows_ub.npy'
)
correct_pred = []
model1_b = VGG16()
model1_b.load_state_dict(torch.load(PATH))

feature_vectors = np.array([1234, 4096])

for i in range(np.shape(correct_pred)[0]):
    feature_vectors[i, :] = new_forward(model1_b, test_set[correct_pred])
Beispiel #5
0
from torch.autograd import Variable
from flashtorch.activmax import GradientAscent
import numpy as np

test_set = torch.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_b\test_set_b.pt')
test_labels = np.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_b\test_labels.npy')
test_windows = np.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_b\test_windows.npy')
cpi = np.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Kode\Gruppen\Fagprojekt2020\CNN_HPC\cpi.npy')
marks = np.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Kode\Gruppen\Fagprojekt2020\GMM\marked_windows.npy')
windows = test_windows[cpi]
labels = test_labels[cpi]
data_set = test_set[cpi,:,:,:]
images = data_set[marks,:,:,:]
w = windows[marks]
y = labels[marks]
model = VGG16()
model.load_state_dict(torch.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_b\model1_l5_b.pt'))
model.eval()

def get_image(image,num):
    img1 = image[num, :, :, :].unsqueeze(0).requires_grad_(requires_grad=True)
    return img1
img1 = get_image(images,0)
img2 = get_image(images,1)
img3 = get_image(images,2)
img4 = get_image(images,3)
img5 = get_image(images,4)
img6 = get_image(images,5)
img7 = get_image(images,6)
img8 = get_image(images,7)