MarekKowalski / DeepAlignmentNetwork

A deep neural network for face alignment
MIT License
510 stars 136 forks source link

how can I visualize heatmap ? #7

Closed dedoogong closed 7 years ago

dedoogong commented 7 years ago

I'm trying to see how the heatmap is generated and focusing on draw_landmarks_helper. def draw_landmarks_helper(self, landmark): img = T.zeros((1, self.img_shape[0], self.img_shape[1]))

    intLandmark = landmark.astype('int32')
    locations = self.offsets + intLandmark
    dxdy = landmark - intLandmark

    offsetsSubPix = self.offsets - dxdy
    vals = 1 / (1 + T.sqrt(T.sum(offsetsSubPix * offsetsSubPix, axis=1) + 1e-6))

    img = T.set_subtensor(img[0, locations[:, 1], locations[:, 0]], vals)

    img2=T.cast(img,'uint8') 
    vals2=img2.eval()  << error!
    imshow("heatmap",vals2)
    return img

can you give me an advice on how to see the array or tensor?

thank you.

MarekKowalski commented 7 years ago

Unfortunately in theano you can't just visualize things like that. Try the code example below:

from FaceAlignment import FaceAlignment
from ImageServer import ImageServer
import theano
import lasagne
import numpy as np
from matplotlib import pyplot as plt

faceAlignment = FaceAlignment(112, 112, 1, 2)
faceAlignment.loadNetwork('../DAN.npz')

datasetDir ="../data/"
challengingSet = ImageServer.Load(datasetDir + "challengingSet.npz")

inputImg, transform = faceAlignment.CropResizeRotate(challengingSet.imgs[1], challengingSet.initLandmarks[1])
inputImg = inputImg - faceAlignment.meanImg
inputImg = inputImg / faceAlignment.stdDevImg

theanoHeatmap = lasagne.layers.get_output(faceAlignment.layers["s1_img_landmarks"], deterministic=True)
heatmapFunction = theano.function([faceAlignment.data], [theanoHeatmap])
heatmap = heatmapFunction([inputImg])[0][0][0]

plt.imshow(heatmap)
plt.show()

In order to run this you need to have the "challengingSet.npz" file, the instructions for its creation are in the readme file. You can also subsitute the image from "challengingSet.npz" with any other image.

dedoogong commented 7 years ago

wow really thank you! As I'm little bit lazy guy;;; and it's first time to handle theano, rather than creating the npz file, I just applied your guide to CameraDemo.py and FaceAlignment.py

def initializeNetwork(self):
    self.layers = self.createCNN()
    self.network = self.layers['output']

    self.prediction = lasagne.layers.get_output(self.network, deterministic=True)
    self.generate_network_output = theano.function([self.data], [self.prediction])

    self.theanoHeatmap = lasagne.layers.get_output(self.layers["s1_img_landmarks"], deterministic=True)
    self.heatmapFunction = theano.function([self.data], [self.theanoHeatmap])

def processImg(self, img, inputLandmarks):
    inputImg, transform = self.CropResizeRotate(img, inputLandmarks)
    inputImg = inputImg - self.meanImg
    inputImg = inputImg / self.stdDevImg  
    heatmap = self.heatmapFunction([inputImg])[0][0][0] 
    return heatmap

from FaceAlignment import FaceAlignment import numpy as np import cv2 import utils from matplotlib import pyplot as plt

model = FaceAlignment(112, 112, 1, 2) model.loadNetwork("../DAN-Menpo.npz")

cascade = cv2.CascadeClassifier("../data/haarcascade_frontalface_alt.xml")

reset = True landmarks = None

print ("Press space to detect the face, press escape to exit") vis =cv2.imread("test.jpg") if len(vis.shape) > 2: img = np.mean(vis, axis=2).astype(np.uint8) else: img = vis.astype(np.uint8)

rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=3, minSize=(50, 50)) if len(rects) > 0: minX = rects[0][0] maxX = rects[0][0] + rects[0][2] minY = rects[0][1] maxY = rects[0][1] + rects[0][3] cv2.rectangle(vis, (minX, minY), (maxX, maxY), (255, 0, 0)) initLandmarks = utils.bestFitRect(None, model.initLandmarks, [minX, minY, maxX, maxY])

landmarks = model.processImg(img[np.newaxis], initLandmarks) 

plt.imshow(landmarks) plt.show()

and I finally succeeded in seeing the heatmap!

really thank you again!