예제 #1
0
def classifythis():
    #loading model
    print("classifythis")
    if model_num == 0:
        net = model.SqueezeNet()
        weights = torch.load('bsqueezenet_onfulldatatest.pth')
    if model_num == 1:
        net = model2.AlexNet()
        weights = torch.load('tryAlexNet.pth')

    print("model loded")
    net.load_state_dict(weights)
    print("CHECK WEIGHTS")
    #print(weights)
    print("weights loded")
    # some layer react differently when they are in training and in production
    # net.train() is to let know the model he is in train mode
    # net.eval() when we need the result of the model
    net.eval()
    #creating var that will contain the result
    df = pd.DataFrame(columns=[
        "file", "prediction", "proba_class0", "proba_class1", "proba_class2",
        "proba_class4", "proba_class5", "proba_class6", "proba_class7",
        "proba_class8"
    ])
    i = 0
    number_of_file = len(os.listdir(to_classify_data))
    for file in os.listdir(to_classify_data):
        if file.lower().endswith(".jpg"):
            result, output = classifyOneImage(file, net, weights, model_num,
                                              "", None)
            print("the class with the highest proba :")
            print(result)

            #capture the value of predicted class output[0][i], 0<i<8, scores[i]
            print("testing the printing of probabilities")
            for u in range(8):
                print(output[0][u])

            df = df.append(
                {
                    "file": file,
                    "prediction": result,
                    "proba_class0": output[0][0],
                    "proba_class1": output[0][1],
                    "proba_class2": output[0][2],
                    "proba_class4": output[0][3],
                    "proba_class5": output[0][4],
                    "proba_class6": output[0][5],
                    "proba_class7": output[0][6],
                    "proba_class8": output[0][7]
                },
                ignore_index=True)
            i = i + 1
            print(str(i) + " of " + str(number_of_file) + " done")
    df.to_csv("result.csv", header=True, index=None, sep=",")
예제 #2
0
def show_Weights(summary):
    print("charging weights")
    if model_num == 0:
        net = model.SqueezeNet()
        weights = torch.load('bsqueezenet_onfulldatatest.pth')
    if model_num == 1:
        net = model2.AlexNet()
        weights = torch.load('tryAlexNet.pth')
    print("number of layer = ")
    print(len(weights)/2)
    for layer_name in (weights):
        print(layer_name)
        if summary == "yes":
            print(len(weights[layer_name]))
        else:
            print(weights[layer_name])
예제 #3
0
def classifythis():
    #loading model 
    print("classifythis")
    if model_num == 0:
        net  = model.SqueezeNet()
        weights = torch.load('bsqueezenet_onfulldatatest.pth')
    if model_num == 1:
        net = model2.AlexNet()
        weights = torch.load('tryAlexNet.pth')
    
    print("model loded")
    net.load_state_dict(weights)
    print("weights loded")
    # some layer react differently when they are in training and in production
    # net.train() is to let know the model he is in train mode
    # net.eval() when we need the result of the model
    net.eval()
    #loading classes
    target = ['BottomCenter','BottomLeft','BottomRight','MiddleCenter','MiddleLeft','MiddleRight','TopCenter','TopLeft', 'TopRight']
    #creating var that will contain the result
    df = pd.DataFrame(columns=["file", "prediction","proba_class0","proba_class1","proba_class2","proba_class3","proba_class4","proba_class5","proba_class6","proba_class7","proba_class8"])
    i = 0
    number_of_file = len(os.listdir(to_classify_data))
    for file in os.listdir(to_classify_data):
        if file.lower().endswith(".jpg"):
            image = Image.open(to_classify_data+ "/" + str(file))
            data_transform = transforms.Compose([
            transforms.CenterCrop([256,256]),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])
            image_tensor = data_transform(image).float()
            image_tensor = image_tensor.unsqueeze_(0)
            input = Variable(image_tensor)
            output = net.forward(input)
            output = output.detach().numpy()
            print("probabilitie of the image for each class :")
            print(output)
            result = target[output.argmax()]
            print("the class with the highest proba :")
            print (result)
            df = df.append({"file": file,"prediction":  result,"proba_class0": output[0][0],"proba_class1": output[0][1],"proba_class2": output[0][2],"proba_class3": output[0][3],"proba_class4": output[0][4],"proba_class5": output[0][5],"proba_class6": output[0][6],"proba_class7": output[0][7],"proba_class8": output[0][8]}, ignore_index=True)
            print(str(i) + " of " + str(number_of_file) + " done")
            i = i+1
    df.to_csv("result.csv",header=True,index=None,sep=",")
예제 #4
0
def classifyOneImage(file, net, weights, model_num, relativePathtoDNN,
                     relativePathToImage):
    if model_num == 0:
        net = model.SqueezeNet()
        weights = torch.load('bsqueezenet_onfulldatatest.pth')
    if model_num == 1:
        net = model2.AlexNet()
        weights = torch.load('tryAlexNet.pth')

    print("model loded")
    net.load_state_dict(weights)
    print("weights loded")
    # some layer react differently when they are in training and in production
    # net.train() is to let know the model he is in train mode
    # net.eval() when we need the result of the model
    net.eval()

    target = [
        'BottomCenter', 'BottomLeft', 'BottomRight', 'MiddleLeft',
        'MiddleRight', 'TopCenter', 'TopLeft', 'TopRight'
    ]

    if file.lower().endswith(".jpg"):
        if (relativePathToImage is None):
            image = Image.open(to_classify_data + "/" + str(file))
        else:
            image = Image.open(relativePathToImage + "/" + str(file))
        data_transform = transforms.Compose([
            transforms.CenterCrop([256, 256]),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
        image_tensor = data_transform(image).float()
        image_tensor = image_tensor.unsqueeze_(0)
        input = Variable(image_tensor)
        output = net.forward(input)
        output = output.detach().numpy()
        print("probabilitie of the image for each class :")
        print(output)
        result = target[output.argmax()]
        print("the class with the highest proba :")
        return result, output
예제 #5
0
def show_Weights(model_num, x):
    print("Loading weights")
    if model_num == 0:
        net = model.SqueezeNet()
        weights = torch.load('bsqueezenet_onfulldatatest.pth')
    if model_num == 1:
        net = model2.AlexNet()
        weights = torch.load('tryAlexNet.pth')
    open('weights.txt', 'w').close()
    f = open('weights.txt', 'r+')
    f.truncate(0)
    print(type(weights))
    if (str(x).strip().lower() != "s"):
        printAndAddtoFile(f, "You have selected a full display")
    else:
        printAndAddtoFile(f, "You have selected a truncated display")
    ch = "Total number of layers (each layer is composed of weights and biases) = " + str(
        len(weights) / 2)
    printAndAddtoFile(f, ch)
    printAndAddtoFile(f, "Display all modules in net")
    for idx, m in enumerate(net.modules()):
        printAndAddtoFile(f, str(idx) + '->' + str(m))
        printAndAddtoFile(f, "----Associated named parameters---")
        for name, param in m.named_parameters():
            printAndAddtoFile(f, name + str(param.size()))
        printAndAddtoFile(f, "----Fin---")
    i = 0

    printAndAddtoFile(f, "Info from weights associated to net")
    for layer_name in (weights):
        i = i + 1
        printAndAddtoFile(
            f,
            "******************* Layer number  " + str(i) + " = " + layer_name)
        values = weights[layer_name]
        printAndAddtoFile(f, "# of nodes " + str(len(values)))
        if (str(x).strip().lower() != "s"):
            printAndAddtoFile(f, "values==>")
            printAndAddtoFile(f, str(values))
    f.close()
예제 #6
0
def classifythis():
    #loading model 
    net  = model.SqueezeNet()
    weights = torch.load('bsqueezenet_onfulldata.pth')
    net.load_state_dict(weights)
    net.eval()
    #loading classes    
    target = ['BottomCenter','BottomLeft','BottomRight','MiddleCenter','MiddleLeft','MiddleRight','TopCenter','TopLeft', 'TopRight']
    #creating var that will contain the result
    df = pd.DataFrame(columns=["file", "prediction"])
    for file in os.listdir('data/toClassify'):
        if file.endswith(".jpg"):
            image = Image.open("data/toClassify/" + str(file))
            data_transform = transforms.Compose([
            transforms.CenterCrop([256,256]),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])
            image_tensor = data_transform(image).float()
            image_tensor = image_tensor.unsqueeze_(0)
            input = Variable(image_tensor)
            output = net.forward(input)
            result = target[output.data.numpy().argmax()]
            df = df.append({"file": file,"prediction":  result}, ignore_index=True)
    df.to_csv("result.csv",header=True,index=None,sep=",")
예제 #7
0
def classifyOneImage(file, net, weights, model_num, relativePathtoDNN,
                     relativePathToImage):
    if model_num == 0:
        net = model.SqueezeNet()
        weights = torch.load('bsqueezenet_onfulldatatest.pth')
    if model_num == 1:
        net = model2.AlexNet()
        weights = torch.load('tryAlexNet.pth')

    print("model loded")
    net.load_state_dict(weights)
    print("weights loded")
    # some layer react differently when they are in training and in production
    # net.train() is to let know the model he is in train mode
    # net.eval() when we need the result of the model
    net.eval()

    target = [
        'BottomCenter', 'BottomLeft', 'BottomRight', 'MiddleLeft',
        'MiddleRight', 'TopCenter', 'TopLeft', 'TopRight'
    ]

    if file.lower().endswith(".jpg"):
        if (relativePathToImage is None):
            image = Image.open(to_classify_data + "/" + str(file))
        else:
            image = Image.open(relativePathToImage + "/" + str(file))
        data_transform = transforms.Compose([
            transforms.CenterCrop([256, 256]),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
        image_tensor = data_transform(image).float()
        image_tensor = image_tensor.unsqueeze_(0)

        # forward hook method for retrieving intermediate results
        for i in range(0, len(net.features)):
            net.features[i].register_forward_hook(forward_hook)

        for i in range(0, len(net.classifier)):
            net.classifier[i].register_forward_hook(forward_hook)

        net.eval()
        input = Variable(image_tensor)
        output = net.forward(input)

        for i in range(0, len(net.features)):
            print("Activations for layer " + str(i) + " : ")
            print(net.features[i].Y)

        #for i in range(0, len(net.features)):
        #    net.features[i].register_forward_hook(forward_hook)

        #T_Alex = output.squeeze().cpu().numpy()
        #T_Alex = (T_Alex[:,np.newaxis] == np.arange(1000))*1.0
        #T_Alex = torch.from_numpy(T_Alex).type(torch.FloatTensor)
        #T_Alex = Variable(T_Alex).cuda()

        #LRP_Alex = net.relprop(output_Alex * T_Alex)

        output = output.detach().numpy()
        print("probabilitie of the image for each class :")
        print(output)
        result = target[output.argmax()]

        best = output.argmax()
        outputProb = output[0][best]

        for i in range(0, best):
            output[0][i] = 0

        for i in range(best + 1, len(output[0])):
            output[0][i] = 0

        print("probability : " + str(outputProb))

        #print(output[0])
        tAF = torch.from_numpy(output[0]).type(torch.FloatTensor)
        tAF = Variable(tAF).cpu()
        LRP_Alex = net.relprop(tAF)

        #print("the class with the highest proba :")
        #for i in range(0, len(net.features)):
        #    print ( net.features[i])
        return result, output
예제 #8
0
    # the same with test data
    transformed_eyes2 = datasets.ImageFolder(root=test_data,
                                             transform=data_transform)

    # In[7]:

    test_loader = torch.utils.data.DataLoader(transformed_eyes2,
                                              batch_size=batch_size,
                                              shuffle=True,
                                              num_workers=num_workers)

    # In[8]:
    print("--> Configuration can be done via the file Config.ini")
    if model_num == 0:
        print("****** model architechture type: Squeezenet ****** ")
        net = model.SqueezeNet()
    if model_num == 1:
        print("****** model architechture type: AlexNet *******")
        net = model2.AlexNet()
    else:
        net = model2.AlexNet()

    if model_name is not None:
        print("loading pre trained weights")
        pretrained_weights = torch.load(model_name)
        net.load_state_dict(pretrained_weights)

    if no_cuda:
        net.cuda()

    avg_loss = list()