torchreid/engine/engine.py
def visactmap(self, testloader, save_dir, width, height, print_freq):
        """Visualizes CNN activation maps to see where the CNN focuses on to extract features.

        This function takes as input the query images of target datasets

        Reference:
            - Zagoruyko and Komodakis. Paying more attention to attention: Improving the
              performance of convolutional neural networks via attention transfer. ICLR, 2017
            - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019.
        """
        self.model.eval()
        
        imagenet_mean = [0.485, 0.456, 0.406]
        imagenet_std = [0.229, 0.224, 0.225]

        for target in list(testloader.keys()):
            queryloader = testloader[target]['query']
            # original images and activation maps are saved individually
            actmap_dir = osp.join(save_dir, 'actmap_'+target)
            mkdir_if_missing(actmap_dir)
            print('Visualizing activation maps for {} ...'.format(target))

            for batch_idx, data in enumerate(queryloader):
                imgs, paths = data[0], data[3]
                if self.use_gpu:
                    imgs = imgs.cuda()
                
                # forward to get convolutional feature maps
                try:
                    outputs = self.model(imgs, return_featuremaps=True)
                except TypeError:
                    raise TypeError('forward() got unexpected keyword argument "return_featuremaps". '
                                    'Please add return_featuremaps as an input argument to forward(). When '
                                    'return_featuremaps=True, return feature maps only.')
                
                if outputs.dim() != 4:
                    raise ValueError('The model output is supposed to have '
                                     'shape of (b, c, h, w), i.e. 4 dimensions, but got {} dimensions. '
                                     'Please make sure you set the model output at eval mode '
                                     'to be the last convolutional feature maps'.format(outputs.dim()))
                
                # compute activation maps
                outputs = (outputs**2).sum(1)
                b, h, w = outputs.size()
                outputs = outputs.view(b, h*w)
                outputs = F.normalize(outputs, p=2, dim=1)
                outputs = outputs.view(b, h, w)
                
                if self.use_gpu:
                    imgs, outputs = imgs.cpu(), outputs.cpu()

                for j in range(outputs.size(0)):
                    # get image name
                    path = paths[j]
                    imname = osp.basename(osp.splitext(path)[0])
                    
                    # RGB image
                    img = imgs[j, ...]
                    for t, m, s in zip(img, imagenet_mean, imagenet_std):
                        t.mul_(s).add_(m).clamp_(0, 1)
                    img_np = np.uint8(np.floor(img.numpy() * 255))
                    img_np = img_np.transpose((1, 2, 0)) # (c, h, w) -> (h, w, c)
                    
                    # activation map
                    am = outputs[j, ...].numpy()
                    am = cv2.resize(am, (width, height))
                    am = 255 * (am - np.max(am)) / (np.max(am) - np.min(am) + 1e-12)
                    am = np.uint8(np.floor(am))
                    am = cv2.applyColorMap(am, cv2.COLORMAP_JET)
                    
                    # overlapped
                    overlapped = img_np * 0.4 + am * 0.6
                    overlapped[overlapped>255] = 255
                    overlapped = overlapped.astype(np.uint8)

                    # save images in a single figure (add white spacing between images)
                    # from left to right: original image, activation map, overlapped image
                    grid_img = 255 * np.ones((height, 3*width+2*GRID_SPACING, 3), dtype=np.uint8)
                    grid_img[:, :width, :] = img_np[:, :, ::-1]
                    grid_img[:, width+GRID_SPACING: 2*width+GRID_SPACING, :] = am
                    grid_img[:, 2*width+2*GRID_SPACING:, :] = overlapped
                    cv2.imwrite(osp.join(actmap_dir, imname+'.jpg'), grid_img)

                if (batch_idx+1) % print_freq == 0:
                    print('- done batch {}/{}'.format(batch_idx+1, len(queryloader)))
Visualizes CNN activation maps to see where the CNN focuses on to extract features.

        This function takes as input the query images of target datasets

        Reference:
            - Zagoruyko and Komodakis. Paying more attention to attention: Improving the
              performance of convolutional neural networks via attention transfer. ICLR, 2017
            - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019.
        
Visualizes CNN activation maps to see where the CNN focuses on to extract features.
This function takes as input the query images of target datasets

Zagoruyko and Komodakis. Paying more attention to attention: Improving the
performance of convolutional neural networks via attention transfer. ICLR, 2017
Zhou et al. Omni-Scale Feature Learning for Person Re-Identification.
Visualizes CNN activation maps to see where the CNN focuses on to extract features . This function takes as input the query images of target datasets Zagoruyko and Komodakis . Paying more attention to attention : Improving the performance of convolutional neural networks via attention transfer . ICLR 2017 Zhou et al . Omni - Scale Feature Learning for Person Re - Identification .
def visactmap(self, testloader, save_dir, width, height, print_freq):
        self.model.eval()
        imagenet_mean = [0.485, 0.456, 0.406]
        imagenet_std = [0.229, 0.224, 0.225]
        for target in list(testloader.keys()):
            queryloader = testloader[target]['query']
            actmap_dir = osp.join(save_dir, 'actmap_'+target)
            mkdir_if_missing(actmap_dir)
            print('Visualizing activation maps for {} ...'.format(target))
            for batch_idx, data in enumerate(queryloader):
                imgs, paths = data[0], data[3]
                if self.use_gpu:
                    imgs = imgs.cuda()
                try:
                    outputs = self.model(imgs, return_featuremaps=True)
                except TypeError:
                    raise TypeError('forward() got unexpected keyword argument "return_featuremaps". '
                                    'Please add return_featuremaps as an input argument to forward(). When '
                                    'return_featuremaps=True, return feature maps only.')
                if outputs.dim() != 4:
                    raise ValueError('The model output is supposed to have '
                                     'shape of (b, c, h, w), i.e. 4 dimensions, but got {} dimensions. '
                                     'Please make sure you set the model output at eval mode '
                                     'to be the last convolutional feature maps'.format(outputs.dim()))
                outputs = (outputs**2).sum(1)
                b, h, w = outputs.size()
                outputs = outputs.view(b, h*w)
                outputs = F.normalize(outputs, p=2, dim=1)
                outputs = outputs.view(b, h, w)
                if self.use_gpu:
                    imgs, outputs = imgs.cpu(), outputs.cpu()
                for j in range(outputs.size(0)):
                    path = paths[j]
                    imname = osp.basename(osp.splitext(path)[0])
                    img = imgs[j, ...]
                    for t, m, s in zip(img, imagenet_mean, imagenet_std):
                        t.mul_(s).add_(m).clamp_(0, 1)
                    img_np = np.uint8(np.floor(img.numpy() * 255))
                    img_np = img_np.transpose((1, 2, 0)) 
                    am = outputs[j, ...].numpy()
                    am = cv2.resize(am, (width, height))
                    am = 255 * (am - np.max(am)) / (np.max(am) - np.min(am) + 1e-12)
                    am = np.uint8(np.floor(am))
                    am = cv2.applyColorMap(am, cv2.COLORMAP_JET)
                    overlapped = img_np * 0.4 + am * 0.6
                    overlapped[overlapped>255] = 255
                    overlapped = overlapped.astype(np.uint8)
                    grid_img = 255 * np.ones((height, 3*width+2*GRID_SPACING, 3), dtype=np.uint8)
                    grid_img[:, :width, :] = img_np[:, :, ::-1]
                    grid_img[:, width+GRID_SPACING: 2*width+GRID_SPACING, :] = am
                    grid_img[:, 2*width+2*GRID_SPACING:, :] = overlapped
                    cv2.imwrite(osp.join(actmap_dir, imname+'.jpg'), grid_img)
                if (batch_idx+1) % print_freq == 0:
                    print('- done batch {}/{}'.format(batch_idx+1, len(queryloader)))
def visactmap ( self , testloader , save_dir , width , height , print_freq ) : self . model . eval ( ) imagenet_mean = [ 0.485 , 0.456 , 0.406 ] imagenet_std = [ 0.229 , 0.224 , 0.225 ] for target in list ( testloader . keys ( ) ) : queryloader = testloader [ target ] [ 'query' ] actmap_dir = osp . join ( save_dir , 'actmap_' + target ) mkdir_if_missing ( actmap_dir ) print ( 'Visualizing activation maps for {} ...' . format ( target ) ) for batch_idx , data in enumerate ( queryloader ) : imgs , paths = data [ 0 ] , data [ 3 ] if self . use_gpu : imgs = imgs . cuda ( ) try : outputs = self . model ( imgs , return_featuremaps = True ) except TypeError : raise TypeError ( 'forward() got unexpected keyword argument "return_featuremaps". ' 'Please add return_featuremaps as an input argument to forward(). When ' 'return_featuremaps=True, return feature maps only.' ) if outputs . dim ( ) != 4 : raise ValueError ( 'The model output is supposed to have ' 'shape of (b, c, h, w), i.e. 4 dimensions, but got {} dimensions. ' 'Please make sure you set the model output at eval mode ' 'to be the last convolutional feature maps' . format ( outputs . dim ( ) ) ) outputs = ( outputs ** 2 ) . sum ( 1 ) b , h , w = outputs . size ( ) outputs = outputs . view ( b , h * w ) outputs = F . normalize ( outputs , p = 2 , dim = 1 ) outputs = outputs . view ( b , h , w ) if self . use_gpu : imgs , outputs = imgs . cpu ( ) , outputs . cpu ( ) for j in range ( outputs . size ( 0 ) ) : path = paths [ j ] imname = osp . basename ( osp . splitext ( path ) [ 0 ] ) img = imgs [ j , ... ] for t , m , s in zip ( img , imagenet_mean , imagenet_std ) : t . mul_ ( s ) . add_ ( m ) . clamp_ ( 0 , 1 ) img_np = np . uint8 ( np . floor ( img . numpy ( ) * 255 ) ) img_np = img_np . transpose ( ( 1 , 2 , 0 ) ) am = outputs [ j , ... ] . numpy ( ) am = cv2 . resize ( am , ( width , height ) ) am = 255 * ( am - np . max ( am ) ) / ( np . max ( am ) - np . min ( am ) + 1e-12 ) am = np . uint8 ( np . floor ( am ) ) am = cv2 . applyColorMap ( am , cv2 . COLORMAP_JET ) overlapped = img_np * 0.4 + am * 0.6 overlapped [ overlapped > 255 ] = 255 overlapped = overlapped . astype ( np . uint8 ) grid_img = 255 * np . ones ( ( height , 3 * width + 2 * GRID_SPACING , 3 ) , dtype = np . uint8 ) grid_img [ : , : width , : ] = img_np [ : , : , : : - 1 ] grid_img [ : , width + GRID_SPACING : 2 * width + GRID_SPACING , : ] = am grid_img [ : , 2 * width + 2 * GRID_SPACING : , : ] = overlapped cv2 . imwrite ( osp . join ( actmap_dir , imname + '.jpg' ) , grid_img ) if ( batch_idx + 1 ) % print_freq == 0 : print ( '- done batch {}/{}' . format ( batch_idx + 1 , len ( queryloader ) ) )
Visualizes CNN activation maps to see where the CNN focuses on to extract features.
Visualizes CNN activation maps to see where the CNN focuses on to extract features .
"""Visualizes CNN activation maps to see where the CNN focuses on to extract features.

        This function takes as input the query images of target datasets

        Reference:
            - Zagoruyko and Komodakis. Paying more attention to attention: Improving the
              performance of convolutional neural networks via attention transfer. ICLR, 2017
            - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019.
        """ # original images and activation maps are saved individually # forward to get convolutional feature maps # compute activation maps # get image name # RGB image # (c, h, w) -> (h, w, c) # activation map # overlapped # save images in a single figure (add white spacing between images) # from left to right: original image, activation map, overlapped image
self
testloader
save_dir
width
height
print_freq