def ranking(types, model, distfn): lt = th.from_numpy(model.embedding()) embedding = Variable(lt, requires_grad=True) with torch.no_grad(): ranks = [] ap_scores = [] for s, s_types in types.items(): s_e = Variable(lt[s].expand_as(embedding), requires_grad=True) with torch.no_grad(): _dists = model.dist()(s_e, embedding).data.cpu().numpy().flatten() _dists[s] = 1e+12 _labels = np.zeros(embedding.size(0)) _dists_masked = _dists.copy() _ranks = [] for o in s_types: _dists_masked[o] = np.Inf _labels[o] = 1 ap_scores.append(average_precision_score(_labels, -_dists)) for o in s_types: d = _dists_masked.copy() d[o] = _dists[o] r = np.argsort(d) _ranks.append(np.where(r == o)[0][0] + 1) ranks += _ranks return np.mean(ranks), np.mean(ap_scores)
def plot_emb(types, model, epoch, objects, ap_scores, loss, best_rank, best_mAP): # TODO : SB fig = plt.figure(figsize=(10, 10)) ax = plt.gca() ax.cla() fig.patch.set_visible(False) ax.axis('off') ax.set_xlim((-1.1, 1.1)) ax.set_ylim((-1.1, 1.1)) ax.add_artist(plt.Circle((0, 0), 1., color='lightgrey', fill=False)) emb = model.embedding() # visible_ents = np.unique(np.random.randint(low=0, high=len(ap_scores)-1, size=(int(.05*len(ap_scores))))) # visible_ents = [th.LongTensor([5])] printed_ents = [] prob_u = np.random.uniform(0, 1, len(ap_scores)) i = 0 for s, s_types in tqdm(types.items()): # if s in visible_ents and s not in printed_ents: # printed_ents.append(s) if prob_u[i] < .01: plt.plot(emb[s][0], emb[s][1], marker='b', color='black') # plot the dot ax.text( emb[s][0] + .01, emb[s][1] + .01, objects[s].split('.')[0], color='g', bbox=dict( facecolor='none', edgecolor='black', boxstyle='round,pad=1')) # add text with box dodgerblue # m = 0 for o in s_types: # if o not in visible_ents and m <= 4: # visible_ents.append(o) plt.plot([emb[s][0], emb[o][0]], [emb[s][1], emb[o][1]], c='cornflowerblue', linewidth=ap_scores[i] / max(ap_scores) * 5) # m += 1 i += 1 plt.text(-1, 1, 'epoch = ' + str(epoch + 1) + '\nloss = ' + str(loss)[:5], bbox=dict(facecolor='red', alpha=0.5, edgecolor='black', boxstyle='round,pad=1')) plt.text(-1, -1, 'best_rank = ' + str(best_rank)[:5] + '\nbest_mAP = ' + str(best_mAP)[:5], bbox=dict(facecolor='red', alpha=0.5, edgecolor='black', boxstyle='round,pad=1')) plt.show(block=False) fig.savefig(model.model_name + '_e' + str(epoch + 1) + '.png', dpi=fig.dpi)
left_images_path = os.path.join('C:/Users/Hazem/', 'left') right_images_path = os.path.join('C:/Users/Hazem/', 'right') img_shape = (245, 200, 3) dataset = SiameseDatasetGenerator(left_images_path, right_images_path) siamese_model.fit(dataset, epochs=2) #here we just load from the dataset an example #we should NOT test the performace of the model #using training data but here we are just see how did it learn example_prediction = dataset[9] anchor_example = image.array_to_img(example_prediction[:, 0][0]) positive_example = image.array_to_img(example_prediction[:, 1][0]) negative_example = image.array_to_img(example_prediction[:, 2][0]) #we add an extra dimension (batch_size dimension) in the first axis by using expand dims. anchor_tensor = np.expand_dims(example_prediction[:, 0][0], axis=0) positive_tensor = np.expand_dims(example_prediction[:, 1][0], axis=0) negative_tensor = np.expand_dims(example_prediction[:, 2][0], axis=0) anchor_embedding, positive_embedding = embedding(anchor_tensor), embedding(positive_tensor) positive_similarity = CosineDistance()(anchor_embedding, positive_embedding) print("Similarity:", positive_similarity) anchor_embedding, negative_embedding = embedding(anchor_tensor), embedding(negative_tensor) negative_similarity = CosineDistance()(anchor_embedding, negative_embedding) print("Similarity:", negative_similarity)