In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Pytorch how to achieve feature map visualization, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Does it have to have this effect?
Technical points 1. Select a layer of network and put the tensor of the picture into 2. Plt.imshow the output of the network
The code can be copied and used directly, and what needs to be changed is your picture location.
Import torchfrom torchvision import models, transformsfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as npimport scipy.miscplt.rcParams ['font.sans-serif'] = [' STSong'] import torchvision.models as modelsmodel = models.alexnet (pretrained=True) # 1. Model view # print (model) # shows that there are three layers in the network. Two Sequential () + avgpool# model_features = list (model.children ()) # print (model_features [0] [3]) # take the fourth layer # for index,layer in enumerate (model_features [0]): # print (layer) # 2 in layer 0 Sequential (). Import data # Open the image in RGB format # Pytorch DataLoader uses the image format read by PIL # it is recommended to read the image in this way When reading a grayscale image, convert (') def get_image_info (image_dir): image_info = Image.open (image_dir). Convert ('RGB') # is a picture # data preprocessing method image_transform = transforms.Compose ([transforms.Resize, transforms.CenterCrop, transforms.ToTensor (), transforms.Normalize ([0.485, 0.456, 0.406], [0.229, 0.224] 0.225])) image_info = image_transform (image_info) # torch.Size ([3,224,224]) image_info = image_info.unsqueeze (0) # torch.Size ([1,3,224,224]) because the input requirement of model is 4 dimensions So it becomes 4-D return image_info# and becomes tensor data # 2. Get the feature map of layer k''args:k: define the feature mapx of the extraction layer: the tensormodel_layer of the image is a Sequential () feature layer' 'def get_k_layer_feature_map (model_layer, k, x): with torch.no_grad (): for index, layer in enumerate (model_layer): the first Sequential () of # model has multiple layers So traversing x = layer (x) # torch.Size ([1,64,55,55]) produces 64 channels if k = = index:return x# visual feature graph def show_feature_map (feature_map): # feature_map=torch.Size ([1,64,55,55]), feature_map [0] .shape = torch.Size ([64,55]) 55]) # feature_map [2] .shape out of boundsfeature_map = feature_map.squeeze (0) # compressed into torch.Size ([64,55,55]) feature_map_num = feature_map.shape [0] # number of return channels row_num = np.ceil (np.sqrt (feature_map_num)) # 8plt.figure () for index in range (1, feature_map_num + 1): # by traversal Take out the tensor of 64 channels plt.subplot (row_num, row_num, index) plt.imshow (feature_ map [index-1], cmap='gray') # feature_map [0] .shape = torch.Size ([55,55]) plt.axis ('off') scipy.misc.imsave (' feature_map_save//'+str (index) + ".png" Feature_ map [index-1]) plt.show () if _ name__ = ='_ main__':image_dir = r "car_logol.png" # define the extraction layer feature mapk = 0image_info = get_image_info (image_dir) model = models.alexnet (pretrained=True) model_layer= list (model.children ()) model_layer=model_layer [0] # here select the first Sequential () feature_map of model = get_k_layer_feature_map (model_layer, k) Image_info) show_feature_map (feature_map)
Color picture display
# add a sentence to the show_feature_map function that the tensor data becomes the operation image_PIL=transforms.ToPILImage () of Img (feature_ map [index-1])
If you are not proficient in matplotlib
Matplotlib draws multiple subimages (Chinese character title, XY axis label) & PIL.Image 11 lines to read photos in the folder
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.