def global_score(image1, image2): # image1 = "static/images/"+image1 # image2 = "static/images/"+image2 """this function uses the methods above to calculate a global_score for a couple images. It returns 1 when we are sure that the two arguments refer to the same image. Otherwise, it returns an average score.""" try: if functions.getSize(data_path + image1) == functions.getSize(data_path + image2): if SSIM(image1, image2).compare_images()[0] > 0.8: return 1 elif image1 != image2: if histogram(image1, image2).correlation() > 0.95: return 1 else: h = histogram(image1, image2).correlation() j = functions.jaccard(image1, image2) return (h + j) / 2 except Exception as e: print(444) print(e) h = histogram(image1, image2).correlation() j = functions.jaccard(image1, image2) return (h + j) / 2 return 0
whole_plot_arr = np.zeros(shape=(len(samples), len(samples))) for i in range(iterations): print '\t' + str( i) + '\t-----------------------------------------------------' subbed_dat = {} for dataset in pep_dat: print '\t' + dataset subbed_dat[dataset] = fxn.subsample_to_number( pep_dat, dataset, sample_to) for x in range(len(samples)): for y in range(len(samples)): sampled_jacc[x][y].append( fxn.jaccard(subbed_dat[samples[x]], subbed_dat[samples[y]])) # Generate Venn plots of one examples of subsamples if i == 0: fxn.save_venn2(plot_dir + 'sub', subbed_dat, samples[x], samples[y], samples[x], samples[y]) sub_plot_arr = np.zeros(shape=(len(samples), len(samples))) for x in range(len(samples)): for y in range(len(samples)): sub_plot_arr[x, y] = np.mean(sampled_jacc[x][y]) # Also calculate Jaccards of whole unsampled peptidomes whole_plot_arr[x, y] = fxn.save_venn2(plot_dir + 'whole', pep_dat, samples[x], samples[y], samples[x], samples[y])
def forward(self, y_pr, y_gt): return 1 - F.jaccard(y_pr, y_gt, eps=self.eps, threshold=None, activation=self.activation)
for i in range(repeats): print str( i) + '\t-----------------------------------------------------' # Subsample each dataset to a fixed number dat = {} for dataset in peptides: print '\t' + dataset sample = fxn.subsample_to_number(peptides, dataset, sample_to) dat[dataset] = sample for x in range(len(pep_samples)): for y in range(len(pep_samples)): sampled_jacc[x][y].append( fxn.jaccard(dat[pep_samples[x]], dat[pep_samples[y]])) plot_arr = np.zeros(shape=(len(pep_samples), len(pep_samples))) for x in range(len(pep_samples)): for y in range(len(pep_samples)): plot_arr[x, y] = np.mean(sampled_jacc[x][y]) fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111) p = ax.pcolor(plot_arr, cmap='gnuplot', vmin=0, vmax=1) ax.set_xticks([x + .5 for x in range(len(pep_samples))]) ax.set_xticklabels(pep_ids, rotation=85) ax.set_yticks([x + .5 for x in range(len(pep_samples))]) ax.set_yticklabels(pep_ids) # , fontsize=4) plt.xlim(0, len(pep_samples)) plt.ylim(0, len(pep_samples))
import matplotlib.pyplot as plt from albumentations.pytorch.transforms import img_to_tensor arrt = np.random.randn(500, 500) tensor = img_to_tensor(arrt) mean=(0.485, 0.456, 0.406) std=(0.229, 0.224, 0.225) #new_img = img - mean /std #old img = model_path = 'pretrained/unet16_instruments_20/model_1.pt' model = get_model(model_path, model_type='UNet16', problem_type='instruments') img_l = load_image('dataset/instrument_dataset_1/left_frames/frame000.png') input_img_l = torch.unsqueeze(img_to_tensor(img_transform(p=1)(image=img_l)['image']), dim=0) mask_l = model(input_img_l) img_r = load_image('dataset/instrument_dataset_1/right_frames/frame000.png') input_img_r = torch.unsqueeze(img_to_tensor(img_transform(p=1)(image=img_r)['image']), dim=0) mask_r = model(input_img_r) mask = jaccard(mask_l, mask_r) im_seg = mask.data[0].cpu().numpy()[0] #mask_array = (im_seg * std) + mean plt.imshow(im_seg > 0)