Skip to content

Commit

Permalink
Several improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
olbapmar committed Sep 1, 2018
1 parent 811150e commit 36d2d56
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 8 deletions.
Binary file modified GUI/__pycache__/keras_handler.cpython-36.pyc
Binary file not shown.
Binary file modified GUI/__pycache__/visualize_filter.cpython-36.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions GUI/filters.json
@@ -0,0 +1,18 @@
{
"filtros_bloques": [
{
"2": {
"Horizontal": "block2_conv2:25",
"Vertical": "block2_conv2:12",
"Diagonal 1 (/)": "block2_conv2:105",
"Diagonal 2 (\\)": "block2_conv2:113"
},
"3": {
"h": "",
"v": "",
"d1": "",
"d2": ""
}
}
]
}
16 changes: 12 additions & 4 deletions GUI/keras_handler.py
Expand Up @@ -5,6 +5,7 @@
from vis.utils import utils
from vis.visualization import get_num_filters
from keras import backend as K
import cv2

class KerasHandler:
def __init__(self):
Expand All @@ -21,8 +22,12 @@ def initialize(self,img):
functor = K.function([inp], outputs)
self.middle_layers = functor([img])

self.max_per_layer = [np.amax(layer) for layer in self.middle_layers]

def whole_image(self):
fig, ax = plt.subplots()

plt.rcParams['toolbar'] = 'None'
fig, ax = plt.subplots(num="Clasificacion de imagen")

values = [round(t_uple[2]*100,2) for t_uple in self.predictions]
y_pos = np.arange(len(self.predictions))
Expand All @@ -32,7 +37,6 @@ def whole_image(self):
ax.set_yticks(y_pos)
ax.set_yticklabels(names)
ax.invert_yaxis()

plt.show()

def get_useful_layers_names(self):
Expand All @@ -45,5 +49,9 @@ def get_num_of_channels(self, name):
idx = utils.find_layer_idx(self.model, name)
return get_num_filters(self.model.layers[idx])



def get_img_activations(self, name, filter):
idx = utils.find_layer_idx(self.model, name)
aux = self.middle_layers[idx][0,:,:,filter] * (255.0/np.amax(self.middle_layers[idx][0,:,:,filter]))#self.max_per_layer[idx])
aux = np.expand_dims(aux, axis=2)
aux = aux.astype(np.uint8)
return cv2.resize(aux, (224,224))
5 changes: 3 additions & 2 deletions GUI/main_gui.py
Expand Up @@ -35,8 +35,9 @@ def __init__(self):
def openfile(self):
path = filedialog.askopenfilename(title="Select image")
if path is not "":
img = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2RGB)
self.mainCanvas.new_image(PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2.resize(img, (448,448)))))
img = cv2.imread(path)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.mainCanvas.new_image(PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2.resize(img2, (448,448)))))
self.keras_handler.initialize(cv2.resize(img, (224,224)))

def info(self):
Expand Down
12 changes: 11 additions & 1 deletion GUI/visualize_filter.py
@@ -1,5 +1,8 @@
from tkinter import *
from tkinter import ttk
import numpy as np
import PIL.Image, PIL.ImageTk
import cv2

class VisualizeFilter():
def __init__(self, root):
Expand Down Expand Up @@ -43,6 +46,13 @@ def filtermodified(self, _, __, ___):
self.button['state'] = DISABLED

def ok(self):

img = self.root.keras_handler.get_img_activations(self.combo.get(),int(self.entry_value.get()))
self.canvas_frame = Toplevel()
self.canvas_frame.title(self.combo.get() + ":" + self.entry_value.get())
self.canvas = Canvas(self.canvas_frame, width=224, height=224)
self.canvas.pack(side=TOP)
self.img = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(img))
self.canvas.create_image(0,0,image=self.img,anchor=NW)
self.dialog.destroy()


4 changes: 3 additions & 1 deletion detect_activations.py
Expand Up @@ -13,4 +13,6 @@
img = cv2.imread("features/block2_conv1/8.png", 1)
img = np.expand_dims(img, axis=0)
layer_outs = functor([img])
print(layer_outs[1][0,0,0,1])
print(layer_outs[4][0,:,:,8])
print(np.amax(layer_outs[8]))
print(np.amax(layer_outs[4][0,:,:,:]))
Binary file added features/block1_conv1/Hla.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added prueba.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added prueba2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 36d2d56

Please sign in to comment.