How to use get_patch_path method in dbt-osmosis

Best Python code snippet using dbt-osmosis_python

somo_utils.py

Source:somo_utils.py Github

copy

Full Screen

1import os2from concepts.poisson import process3from PIL import Image4import numpy as np5def get_patch_path(target_path: str) -> str:6 """Returns path to a `patch` images in TCAV results folder structure base in concept path"""7 base_dir = "/".join(target_path.split('/')[:-2])8 concept_dir, file_name = target_path.split('/')[-2:]9 patch_dir = concept_dir + '_patches'10 return os.path.join(base_dir, patch_dir, file_name)11def get_img_path(target_path: str) -> str:12 """Returns path to a full image in TCAV results folder structure base in concept path"""13 base_dir = "/".join(target_path.split('/')[:-2])14 img_ind = str(int(target_path.split('/')[-1].split('_')[1][:-4]) + 1)15 file_name = (4 - len(img_ind)) * '0' + img_ind + '.png'16 return os.path.join(base_dir, 'images', file_name)17def load_img(path: str) -> np.ndarray:18 """Loads an image as numpy array"""19 return np.array(Image.open(path))20def get_concept_inds(img: np.ndarray) -> np.ndarray:21 """Returns a 2D array with rows of index pairs of concepts pixels (non-background)"""22 background = (117, 117, 117)23 return np.stack(np.where(np.all(img != background, axis=-1)), axis=1)24def diff(target: np.ndarray, source: np.ndarray) -> int:25 """Calculates the symmetric difference between two concepts based on overlapping non-background pixels"""26 target_ind = get_concept_inds(target)27 source_ind = get_concept_inds(source)28 nrows, ncols = target_ind.shape29 dtype = {'names': ['f{}'.format(i) for i in range(ncols)],30 'formats': ncols * [target_ind.dtype]}31 target_ind = target_ind.view(dtype)32 source_ind = source_ind.view(dtype)33 diff = len(np.setxor1d(target_ind, source_ind))34 return diff35def find_best_source(target: np.ndarray, source_label: str, data_dir: str, max_concepts=None) -> str:36 """37 Based on a diff function (above) searches for best match in all concepts of a given source label38 :param target: target concepts to change39 :param source_label: label from ImageNet, searches for candidates in its results40 :param data_dir: path to main results directory41 :param max_concepts: maximum number of concepts to search through, default None searches through all42 :return: path to best concept image found43 """44 source_concepts_dir = os.path.join(data_dir, f"{source_label}_4c_explained", "concepts")45 sources = []46 paths = []47 concepts_searched = 048 for concept_dir in filter(lambda x: not x.endswith('s'), os.listdir(source_concepts_dir)):49 concept_path = os.path.join(source_concepts_dir, concept_dir)50 for img_name in os.listdir(concept_path):51 img_path = os.path.join(concept_path, img_name)52 source = load_img(path=img_path)53 paths.append(img_path)54 sources.append(source)55 concepts_searched += 156 if max_concepts is not None and concepts_searched == max_concepts:57 break58 sources_arr = np.stack(sources)59 vec_diff = np.vectorize(diff, signature='(224,244,3),(224,224,3)->()')60 scores = vec_diff(target, sources_arr)61 source_path = paths[np.argmin(scores)]62 return source_path63# Poisson blending for Semantic Odd an Out64def blend(image: np.ndarray, target_patch: np.ndarray, source_patch: np.ndarray) -> np.ndarray:65 """Blends source patch into image overwriting the target patch"""66 target_inds = get_concept_inds(target_patch)67 source_inds = get_concept_inds(source_patch)68 target_min = target_inds.min(axis=0)69 target_max = target_inds.max(axis=0)70 target_size = target_max - target_min71 box = np.concatenate([source_inds.min(axis=0)[::-1], source_inds.max(axis=0)[::-1]])72 patch = np.array(Image.fromarray(source_patch).resize(size=target_size[::-1], box=box))73 canvas = 117 * np.ones_like(image)74 canvas[target_min[0]:target_max[0], target_min[1]:target_max[1]] = patch75 mask = np.zeros_like(canvas)[:,:,0]76 mask_inds = get_concept_inds(canvas)77 mask[mask_inds[:,0], mask_inds[:,1]] = 178 result = np.stack([process(canvas[:,:,i], image[:,:,i], mask) for i in range(3)], axis=-1)79 return np.clip(result, 0, 255)80def semantic_odd_man_out(target_concept_path: str, source_label: str, data_dir: str) -> np.ndarray:81 """Searches for a concept from source_label class to overwrite using Poisson blending"""82 target = load_img(target_concept_path)83 target_patch = load_img(get_patch_path(target_concept_path))84 image = load_img(get_img_path(target_concept_path))85 source_path = find_best_source(target=target, source_label=source_label, data_dir=data_dir)86 source_patch = load_img(get_patch_path(source_path))87 result = blend(image, target_patch, source_patch)...

Full Screen

Full Screen

app.py

Source:app.py Github

copy

Full Screen

...32 # Empty T x W x H for each reference tracking point33 X = np.empty((N, 1, 25, 32, 32), np.float32)34 35 for i, point in enumerate(r0):36 im_p, _ = get_patch_path(imt, point, is_scaled=True)37 X[i] = im_p.copy()38 X = X - (X.mean(axis=0) / X.std(axis=0))39 batch_size = 840 N_batches = int(np.ceil(N / batch_size))41 _y1 = []42 with torch.no_grad():43 for i in range(N_batches):44 x = X[i * batch_size:(i + 1) * batch_size]45 x = torch.from_numpy(x).to(device)46 y_pred = model(x)47 _y1.append(y_pred.detach().cpu().numpy())48 y1: np.ndarray = np.vstack(_y1)49 y1 = y1.reshape(-1, 2, 25)50 y1 = y1 + r0[:, :, None]...

Full Screen

Full Screen

predict.py

Source:predict.py Github

copy

Full Screen

...13 # Empty T x W x H for each reference tracking point14 X = np.empty((N, 1, 25, 32, 32), np.float32)15 16 for i, point in enumerate(r0):17 im_p, _ = get_patch_path(imt, point, is_scaled=True)18 X[i] = im_p.copy()19 X = X - (X.mean(axis=0) / X.std(axis=0))20 batch_size = 821 N_batches = int(np.ceil(N / batch_size))22 device = torch.device('cpu')23 model = load_model(MODEL_PATH, device=device)24 y1 = []25 with torch.no_grad():26 for i in tqdm(range(N_batches)):27 x = X[i * batch_size:(i + 1) * batch_size]28 x = torch.from_numpy(x).to(device)29 y_pred = model(x)30 y1.append(y_pred.detach().cpu().numpy())31 y1 = np.vstack(y1)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run dbt-osmosis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful