Ejemplo n.º 1
0
def test():
    resnet50.eval()
    epoch_loss = 0
    gt = []
    pre = []
    with tqdm(total=data_size - n_train, desc=f'evaluating',
              unit='img') as pbar:
        for i_batch, batch_data in enumerate(val_loader):
            img = batch_data['img'].to(device=device)
            #print(vin.shape)
            label = batch_data['label'].cpu()
            gt += label.numpy().tolist()

            pred = resnet50(img)
            pred = pred.squeeze().detach().cpu()

            loss = criterion(pred, label)
            epoch_loss += loss.item()

            #print(pred.shape)
            if len(list(pred.size())) == 1:
                pre.append(int(argmax(pred, 0)))
            else:
                pre += argmax(pred, 1).tolist()

            pbar.update(img.shape[0])
    #print(np.array(gt))
    #print(np.array(pre))
    ac = float(sum(np.array(gt) == np.array(pre))) / float(len(gt))
    print('accuracy on test set is: ', ac)

    return gt, pre, epoch_loss, ac
def test():
    resnet50.eval()
    pre = []
    with tqdm(total=data_size, desc=f'evaluating', unit='img') as pbar:
        for i_batch, batch_data in enumerate(val_loader):
            img = batch_data['img'].to(device=device)
            pred = resnet50(img)
            pred = pred.squeeze().detach().cpu()
            if len(list(pred.size())) == 1:
                pre.append(int(argmax(pred, 0)))
            else:
                pre += argmax(pred, 1).tolist()
            pbar.update(img.shape[0])
    return np.array(pre)
Ejemplo n.º 3
0
def drawline(data):  # 画平行线
    global dots
    length = 0  # 画线总长
    times = 0  # 平行线数量
    for i in range(len(data)):
        line = np.array([0, 0])
        area = data[i]
        maxy = round(max(area[:, 1]), 1)
        miny = round(min(area[:, 1]), 1)
        j = miny
        while j <= maxy:
            index = (np.where(area == j))[0]
            temp = area[index, 0]
            if round(j / abs(d)) % 2:
                line = np.row_stack((line, [j, min(temp)]))
                temp = np.delete(temp, argmin(temp))
                line = np.row_stack((line, [j, min(temp)]))
            else:
                line = np.row_stack((line, [j, max(temp)]))
                temp = np.delete(temp, argmax(temp))
                line = np.row_stack((line, [j, max(temp)]))
            j = round(j + abs(d), 1)
        line = np.delete(line, 0, axis=0)
        line = np.column_stack((line[:, 1], line[:, 0]))
        writecsv(line, i)
        plt.plot(line[:, 0], line[:, 1], '-', color='r')
        times = times + int(line.shape[0] / 2)
        for j in range(line.shape[0] - 1):
            length = length + \
                math.sqrt((line[j+1, 0]-line[j, 0])**2 +
                          (line[j+1, 1]-line[j, 1])**2)
            dots += 1
        i += 1
    return ([length, times])
Ejemplo n.º 4
0
    def run(self):
        ages = [(1, 0.1), (1.5, 0.4), (1.7, 0.1), (1.8, 0.2), (2.1, 0.5)]
        pool = Pool(processes=10)
        n = 1e5

        #         st = time.time()
        #         aa = ((ai, ages) for ai in arange(n))
        #         print 'a"', time.time() - st
        age_gen = age_generator(ages, n)
        st = time.time()
        results = pool.map(_monte_carlo_step, age_gen)
        print 'a', time.time() - st

        st = time.time()
        results = vstack((ri for ri in results if ri is not None))
        print 'b', time.time() - st

        for xx, (ai, ei) in zip(results.T, ages):
            #             print 'dev ', abs(xx.mean() - ai) / ai * 100, abs(xx.std() - ei) / ei * 100
            #             print xx

            f, v = histogram(xx, 40)
            lp = plot(v[:-1], f)[0]
            c = lp.get_color()

            nf = smooth(f, window='flat')
            plot(v[:-1], nf, c=c, ls='--', lw=2)

            axvline(ai, c=c)
            #             print f, v
            idx = argmax(nf)
            #             axvline(xx.mean(), c=c, ls='--')
            axvline(v[idx], c=c, ls='--')

        show()
Ejemplo n.º 5
0
def detectCommunityForNode(net,alpha,node,useStrength=True):
    com=[node]
    cont=True
    while cont==True and len(com)>0:
        neighbors=getNeighborNodes(net,com)
        if len(neighbors)==0:
            break
        fitness=map(lambda x:getNodeFitness(net,com,x,alpha,useStrength),neighbors)
        best=argmax(fitness)
        if fitness[best]<0:
            cont=False
        else:
            com.append(neighbors[best])
            remove=True
            while remove==True:
                remove=False
                newcom=[]
                fitness=getNodeFitnessForAll(net,com,alpha,useStrength)
                for i,n in enumerate(com):
                    #if getNodeFitness(net,com,n,alpha,useStrength)>=0:
                    if fitness[i]>=0:
                        newcom.append(n)
                    else:
                        remove=True
                com=newcom
    return com
Ejemplo n.º 6
0
def monteCarloHeuristic(gs, moves=None, maxDepth=None, verbose=False):
    """Determines the best move using a Monte-Carlo estimation
       of the final scores"""
    if moves==None:
        moves = gs.legalMoves()
    nextStates = map(lambda move : gs.clone().playMove(move), moves)
    relativeScoreGrid = [[] for _ in xrange(len(moves))]
    for trial in xrange(1000000):
        for m in xrange(len(moves)):
            scores = nextStates[m].monteCarloScores(\
                        gs,maxDepth=maxDepth)
            if verbose:
                print "trial %d, move %d/%d, scores : %s" \
                      % (trial+1, m+1, len(moves), scores)
            relativeScoreGrid[m].append(scores)
        # for each move, compute the margin of each winning trial
        winningMargin = [ map(lambda z : z[1][-1]-z[1][-2], \
            ifilter(lambda y: y[0][-1]==gs.nextPlayer, \
            izip(imap(argsort,x),x))) \
            for x in relativeScoreGrid ]
        choice = argmax(map(mean,winningMargin))
        if verbose:
            print "best move so far is %d, mean margin = %f (wins %d/%d)" \
                  % (choice+1, mean(winningMargin[choice]), \
                     len(winningMargin[choice]), trial+1)
        yield moves[choice]
def predict_on_patches(val_loader):
    pre = []
    global model
    global DEVICE

    # with tqdm(total = len(val_loader), desc = f'evaluating',unit = 'img') as pbar:
    for i_batch, batch_data in enumerate(val_loader):
        img = batch_data['img'].to(device=DEVICE)
        pred = model(img)
        pred = pred.squeeze().detach().cpu()
        if len(list(pred.size())) == 1:
            pre.append(int(argmax(pred, 0)))
        else:
            pre += argmax(pred, 1).tolist()
            # pbar.update(img.shape[0])
    return np.array(pre)
Ejemplo n.º 8
0
def top_docs(x):
    num_topics = 11
    mgp = pickle.load(open(path / ('Models/GSDMMModel.pkl'), 'rb'))
    top_docs = [{} for _ in range(num_topics)]
    doc_set = set()
    ind = -1
    for doc in data_words:
        ind += 1
        doc_joined = ' '.join(doc)
        if doc_joined in doc_set:
            continue
        doc_set.add(doc_joined)
        probabilities = mgp.score(doc)
        top_docs[argmax(probabilities)][ind] = max(probabilities)
    for ind in range(len(top_docs)):
        topic = top_docs[ind]
        sorted_topic = sorted(topic.items(),
                              key=lambda kv: kv[1],
                              reverse=True)
        top_docs[ind] = sorted_topic
    topic_ind = 1
    for topic in top_docs:
        print('Topic %s' % topic_ind)
        topic_ind += 1
        num = 0
        for doc in topic:
            if num >= x:
                break
            print(orig_data[doc[0]])
            print('-------')
            num += 1
        print('--------------------------------')
 def predict(self, X):
     neighbors = self.kneighbors(X, return_distance=False)
     predicted = []
     for ns in neighbors:
         votes = [0.0, 0.0]
         for n in ns:
             vote = int(self.Y[n])
             votes[vote] += self.class_weights[vote]
         predicted.append(argmax(votes))
     return numpy.array(predicted)
def test():
    net.eval()
    pre = []
    with torch.no_grad():
        probs = t.Tensor(proba_results).to(device=device)
        print(probs.shape)
        pred = net(probs)
        pred = pred.squeeze().detach().cpu()
        pre += argmax(pred, 1).tolist()
    return pre
Ejemplo n.º 11
0
def retrieve_input(texto):
    if (texto == ""): return ""
    texto_procesado = str(preprocess(texto))

    loaded_tfidf = pickle.load(open("tf-idf_model.sav", 'rb'))
    transformed_text = loaded_tfidf.transform([texto_procesado])

    loaded_model = pickle.load(open("modelo_datos.sav", 'rb'))
    prediction = loaded_model.predict_proba(transformed_text)

    lista_ods = [
        "Fin de la pobreza", "Hambre cero", "Salud y bienestar",
        "Educacion de calidad", "Igualdad de genero",
        "Agua limpia y saneamiento", "Energia asequible y no contaminante",
        "Trabajo decente y crecimiento economico",
        "Industria, Innovacion e Infraestructura",
        "Reduccion de las desigualdades", "Ciudades y comunidades sostenibles",
        "Produccion y consumo responsables", "Accion por el clima",
        "Vida submarina", "Vida de ecosistemas terrestres",
        "Paz, Justicia e Instituciones solidas",
        "Alianzas para lograr los objetivos"
    ]

    print("----- RESULTADOS OBTENIDOS -----")
    print("")
    result = argmax(prediction)
    print(
        f"  El texto ha sido clasificado como ODS {result+1} -> {lista_ods[result]}"
    )
    print("")
    print("Las probabilidades obtenidas para cada ods son las siguientes:")

    #cambiar lambda por 0 o 1 para tenerlo ordenado o no
    lista = sorted(list(enumerate(prediction[0])),
                   key=lambda prediction: prediction[1],
                   reverse=True)

    for index, val in lista:
        value = prediction[0][index]
        print(
            f"  ODS {(index+1):<2} - {value:>8.2%}   ->   {lista_ods[index]:<40}"
        )
Ejemplo n.º 12
0
    def run(self):
        ages = [(1, 0.1), (1.5, 0.4), (1.7, 0.1), (1.8, 0.2), (2.1, 0.5)]
        pool = Pool(processes=10)
        n = 1e5


#         st = time.time()
#         aa = ((ai, ages) for ai in arange(n))
#         print 'a"', time.time() - st
        age_gen = age_generator(ages, n)
        st = time.time()
        results = pool.map(_monte_carlo_step, age_gen)
        print 'a', time.time() - st

        st = time.time()
        results = vstack((ri for ri in results if ri is not None))
        print 'b', time.time() - st

        for xx, (ai, ei)in zip(results.T, ages):
#             print 'dev ', abs(xx.mean() - ai) / ai * 100, abs(xx.std() - ei) / ei * 100
#             print xx

            f, v = histogram(xx, 40)
            lp = plot(v[:-1], f)[0]
            c = lp.get_color()

            nf = smooth(f, window='flat')
            plot(v[:-1], nf, c=c, ls='--', lw=2)

            axvline(ai, c=c)
#             print f, v
            idx = argmax(nf)
#             axvline(xx.mean(), c=c, ls='--')
            axvline(v[idx], c=c, ls='--')

        show()
Ejemplo n.º 13
0
def net_feature(image, distance_to_center_filter_pixels, n):
    img = image

    #Find the center of the image
    height, width, col = img.shape
    coordinate = [height / 2, width / 2]
    y = int(coordinate[0])
    x = int(coordinate[1])

    #Set up the detector
    params = cv2.SimpleBlobDetector_Params()
    params.filterByInertia = False
    params.filterByConvexity = False
    params.minThreshold = 50
    params.maxThreshold = 255
    params.filterByColor = True
    params.blobColor = 255
    params.filterByArea = False
    params.minArea = 1
    detector = cv2.SimpleBlobDetector_create(params)

    #Detect stars
    keypoints = detector.detect(img)
    coord = []
    for index, keypoint in enumerate(keypoints):
        x_centralstar = int(round(keypoints[index].pt[0]))
        y_centralstar = int(round(keypoints[index].pt[1]))
        distance_to_center = sqrt(((x_centralstar - x)**2) +
                                  ((y_centralstar - y)**2))
        coord.append([x_centralstar, y_centralstar, distance_to_center])
        # cv2.circle(img,center=(x_centralstar,y_centralstar),radius=2,color=(255,0,0),thickness=2)

    #Find stars within radius
    stars_within_radius = []
    for coordinate in coord:
        dist_to_center = coordinate[2]
        if (dist_to_center < distance_to_center_filter_pixels):
            stars_within_radius.append(coordinate)
    if len(stars_within_radius) == 0:
        return img

    #Find magnitude of star within radius
    magnitude_of_star_within_radius = []
    for star in stars_within_radius:
        x = star[0]
        y = star[1]
        brightness_value = img[y, x][0]
        magnitude_of_star_within_radius.append(brightness_value)

    #Find index of brightest star and coordinate
    max_index = argmax(magnitude_of_star_within_radius)
    pivot_coord = stars_within_radius[max_index]
    cv2.circle(img,
               center=(pivot_coord[0], pivot_coord[1]),
               radius=2,
               color=(255, 0, 0),
               thickness=2)

    #Find nearest stars
    x_pivot = pivot_coord[0]
    y_pivot = pivot_coord[1]
    distances = []
    for coordinate in coord:
        x_relate = coordinate[0]
        y_relate = coordinate[1]
        delta_x = abs(x_pivot - x_relate)
        delta_y = abs(y_pivot - y_relate)
        distance = sqrt((delta_x * delta_x) + (delta_y * delta_y))
        if (distance == 0):
            continue
        distances.append([distance, coordinate])

    distances = sorted(distances, key=lambda row: row[0])
    neighbor_stars = []
    for distance in distances:
        neighbor_stars.append(distance[1])

    #Draw circle and line
    for i, star in enumerate(neighbor_stars):
        cv2.circle(img,
                   center=(star[0], star[1]),
                   radius=2,
                   color=(255, 0, 0),
                   thickness=2)
        cv2.line(img, (pivot_coord[0], pivot_coord[1]), (star[0], star[1]),
                 (255, 0, 0), 2)
        if i == n - 1:
            break

    return img
Ejemplo n.º 14
0
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import matplotlib.pyplot as plt
import cv2
from numpy.core.fromnumeric import argmax

# read own image
#image = cv2.imread('pix/mercedes280.jpg', cv2.IMREAD_UNCHANGED)
image = cv2.imread('pix/audiropika.jpg', cv2.IMREAD_UNCHANGED)

# convert color space
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# resize image
image=cv2.resize(image,(32,32))
# normalize image
image = image.astype('float32')
image = image / 255.0

# get the compiled model
model = load_model('scnn.h5')

# add 0st dimenstion (nb_samples) for input of Convolution2D
reshaped_image = image.reshape( (1,) + image.shape )  

# predict
prediction = argmax(model.predict(reshaped_image))

# show image
plt.imshow(image)
plt.title('This is a '+['car', 'cat'][prediction])
plt.show()
Ejemplo n.º 15
0
if __name__ == "__main__":
    #Get user data
    usersdF = pd.read_csv("data/users.csv", index_col="Name")
    user_name = input("Name:")
    user_taste = usersdF.loc[user_name]
    
    #Prepare plot
    fig = plt.figure(figsize=(10,5))
    ax = fig.add_subplot(111)
    
    #Get best recommendations
    best_recommendations = []
    for i in range(10):
        mu = np.random.random()
        pop,log = algorithms.eaMuCommaLambda(pop,toolbox,10,10,mu,1-mu,100,stats = stats,verbose=False)
        best_recommendations.append(pop[argmax(map(evaluation,pop))])
        df = pd.DataFrame(log)
        data= data.append(df)
    
    #Get data for plot
    data = data.reset_index(drop=True)
    dfAverages = data.groupby(['gen']).agg({'max': ['mean', 'std']})
    data_by_gen = data['gen'].unique()
    averages = dfAverages['max']['mean'].values
    desviacion = dfAverages['max']['std'].values
    
    #Prepare plot
    ax.set_title("Evolution of model")
    ax.set_ylabel("Taste match")
    ax.set_xlabel("Generation")
    ax.plot(data_by_gen, averages, color='r')