import numpy as np
import pickle
from tqdm import tqdm, tqdm_notebook
import random
import time
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import PIL
from PIL import Image
from sklearn.neighbors import NearestNeighbors
import random
from textwrap import wrap
import glob
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib notebook
# Helper function to get the classname
def classname(str):
return str.split('/')[-2]
def display(dic, per_class, neighbors, message):
for key in dic:
print(key, "\tAccuracy: ", per_class[key])
for each_class in dic:
indices_of_class = [
i for i, j in enumerate(filenames) if classname(j) == each_class
]
random_image_index = random.choice(indices_of_class)
distances, indices = neighbors.kneighbors(
[feature_list[random_image_index]])
similar_image_paths = [filenames[random_image_index]] + \
[filenames[indices[0][i]] for i in range(1, 4)]
plot_images(similar_image_paths, distances[0], message)
def get_least_accurate_classes(feature_list):
per_class_acc = {}
num_nearest_neighbors = 5
num_correct_predictions = 0
num_incorrect_predictions = 0
neighbors = NearestNeighbors(n_neighbors=num_nearest_neighbors,
algorithm='brute',
metric='euclidean').fit(feature_list)
for i in tqdm_notebook(range(len(feature_list))):
distances, indices = neighbors.kneighbors([feature_list[i]])
for j in range(1, num_nearest_neighbors):
predicted_class = classname(filenames[i
[视觉工程]以图搜图之提升准确率(模型微调)
最新推荐文章于 2023-08-23 23:41:39 发布