Ejemplo n.º 1
0
 def adapt_params(self,
                  distance_type='eucos',
                  tail_range=range(0, 40),
                  alpha_range=range(0, 10)):
     adaption = np.zeros((len(alpha_range) + 1, len(tail_range) + 1))
     for image in tqdm(self.x_data):
         for alpha in alpha_range:
             for tail in tail_range:
                 self.create_weibull(distance_type,
                                     tailsize=tail,
                                     overwrite=False)
                 if not image.shape == self.input_shape:
                     image = image.reshape(self.input_shape)
                 imgarr = {
                     'scores': self.compute_score(image),
                     self.observations[0]: self.compute_feature(image)
                 }
                 openmax, softmax = recalibrate_scores(
                     self.weibull_model,
                     range(len(self.CLASSES)),
                     imgarr,
                     self.observations[0],
                     distance_type=distance_type,
                     classes=len(self.CLASSES),
                     alpharank=alpha)
                 if np.argmax(openmax) == np.argmax(softmax):
                     adaption[alpha][tail] += 1
     return adaption
Ejemplo n.º 2
0
 def predict(self, image, distance_type='eucos', alpha=10):
     if self.weibull_model is None:
         self.create_weibull(distance_type, self.overwrite)
     if not image.shape == self.input_shape:
         image = image.reshape(self.input_shape)
     imgarr = {
         'scores': self.compute_score(image),
         self.observations[0]: self.compute_feature(image)
     }
     openmax, softmax = recalibrate_scores(self.weibull_model,
                                           range(len(self.CLASSES)),
                                           imgarr,
                                           self.observations[0],
                                           distance_type=distance_type,
                                           classes=len(self.CLASSES),
                                           alpharank=alpha)
     return openmax, softmax
Ejemplo n.º 3
0
def compute_openmax(model, imagearr):
    mean = np.load('mean.npy')
    distance = np.load('distance.npy')
    #Use loop to find the good parameters
    #alpharank_list = [1,2,3,4,5,5,6,7,8,9,10]
    #tail_list = list(range(0,21))

    alpharank_list = [10]
    #tail_list = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    tail_list = [5]
    total = 0
    for alpha in alpharank_list:
        weibull_model = {}
        openmax = None
        softmax = None
        for tail in tail_list:
            #print ('Alpha ',alpha,' Tail ',tail)
            #print ('++++++++++++++++++++++++++++')
            weibull_model = build_weibull(mean, distance, tail)
            openmax, softmax = recalibrate_scores(weibull_model,
                                                  label,
                                                  imagearr,
                                                  alpharank=alpha)

            #print ('Openmax: ',np.argmax(openmax))
            #print ('Softmax: ',np.argmax(softmax))
            #print ('opemax lenght',openmax.shape)
            #print ('openmax',np.argmax(openmax))
            #print ('openmax',openmax)
            #print ('softmax',softmax.shape)
            #print ('softmax',np.argmax(softmax))
            #if np.argmax(openmax) == np.argmax(softmax):
            #if np.argmax(openmax) == 0 and np.argmax(softmax) == 0:
            #print ('########## Parameters found ############')
            #print ('Alpha ',alpha,' Tail ',tail)
            #print ('########## Parameters found ############')
            #    total += 1
            #print ('----------------------------')
    return np.argmax(softmax), np.argmax(openmax)
Ejemplo n.º 4
0
 def adapt_tailsize(self,
                    distance_type='eucos',
                    adaption_range=range(1, 40)):
     tails = np.zeros(len(adaption_range) + 1)
     for image in self.x_data:
         for i in adaption_range:
             self.create_weibull(distance_type, tailsize=i, overwrite=False)
             if not image.shape == self.input_shape:
                 image = image.reshape(self.input_shape)
             imgarr = {
                 'scores': self.compute_score(image),
                 self.observations[0]: self.compute_feature(image)
             }
             openmax, softmax = recalibrate_scores(
                 self.weibull_model,
                 range(len(self.CLASSES)),
                 imgarr,
                 self.observations[0],
                 distance_type=distance_type,
                 classes=len(self.CLASSES))
             if np.argmax(openmax) == np.argmax(softmax):
                 tails[i] += 1
     return tails