def predict_image_class(query_folder, target_features, cache_dict, class_minimum): minimum = (float('inf'), 0) for query_image_path in os.listdir(os.path.join(query_path, query_folder)): t = time.time() query = os.path.join(query_path, query_folder, query_image_path) t1 = time.time() - t cache_dict, query_features = memory_cache(cache_dict, model.model, query, os.path.join(cache_path, query_folder, query_image_path + '.pth'), transform) t2 = time.time() - t y = LSHash.euclidean_dist(target_features.cpu().numpy()[0], query_features.cpu().numpy()[0]) t3 = time.time() - t print(t1, t2, t3) if y < minimum[0]: minimum = (y, query_folder) class_minimum[query_folder] = minimum
minimum = (float('inf'), 0) with torch.no_grad(): target_image_ori = Image.fromarray(draw[b[1]:b[3], b[0]:b[2]]) target_image = transform(target_image_ori) x = torch.zeros((1, 3, 224, 224)) x[0] = target_image target_features = model.model._forward_impl(x.cuda()) for query_folder in os.listdir(query_path): for query_image_path in os.listdir( os.path.join(query_path, query_folder)): query = os.path.join(query_path, query_folder, query_image_path) cache_dict, query_features = memory_cache( cache_dict, model.model, query, os.path.join(cache_path, query_folder, query_image_path + '.pth'), transform) y = LSHash.euclidean_dist( target_features.cpu().numpy()[0], query_features.cpu().numpy()[0]) if y < minimum[0]: minimum = (y, query_folder) if minimum[0] > 1: minimum = (minimum[0], 'obj') # print(minimum) obj = ET.SubElement(root, 'object') ET.SubElement(obj, 'name').text = minimum[1] bndbx = ET.SubElement(obj, 'bndbox') ET.SubElement(bndbx, 'xmin').text = str(b[0]) ET.SubElement(bndbx, 'ymin').text = str(b[1])