CoolFace
Modelpublic

fernandoperlar/preprocessing_image

sourceHugging Faceupdated 5y agoView on Hugging Face
0likes
Join.py49 linesDownload Raw Back to scripts
1import matplotlib.pyplot as plt2import cv2 as cv3import numpy as np4from keras import preprocessing5 6class Join():7	def __init__(self, *models):8		self.models = models9 10	def visualize_heatmap(self, image):11		fig, model_rows = plt.subplots(nrows=len(self.models), ncols=1, constrained_layout=True)12		fig.suptitle(f"Image: {image.image}, Class: {image.category}")13 14		for model_row in model_rows:15			model_row.remove()16 17		gridspec = model_rows[0].get_subplotspec().get_gridspec()18		model_rows = [fig.add_subfigure(gs) for gs in gridspec]19 20		for row, model_row in enumerate(model_rows):21			img = cv.cvtColor(cv.imread(image.image), cv.COLOR_BGR2RGB).astype("float32") * 1./25522			img = np.expand_dims(img, axis=0)23			24			heatmap = self.models[row].compute_heatmap(img)25			jet_heatmap, superimposed_img = self.models[row].get_heatmap(image.image, heatmap)26			predicted = self.models[row].model.predict(img)[0]27 28			model_row.suptitle(f"Model: {self.models[row].name.title()}")29 30			ax = model_row.subplots(nrows=1, ncols=4)31 32			ax[0].imshow(img[0])33			ax[0].set_title("Original")34			ax[0].axis("off")35 36			ax[1].imshow(self.models[row].filter(img[0][np.newaxis, ...])[0])37			ax[1].set_title("Filter")38			ax[1].axis("off")39 40			ax[2].imshow(preprocessing.image.array_to_img(jet_heatmap))41			ax[2].set_title(np.round(predicted, 2))42			ax[2].axis("off")43			44			ax[3].imshow(superimposed_img)45			ax[3].set_title(f"Predicted: {np.argmax(predicted)}", color="g" if np.argmax(predicted) == int(image.category) else "r")46			ax[3].axis("off")47 48		plt.show()49