基于pytorch开发CNN提取全连接层作为特征
生活随笔
收集整理的這篇文章主要介紹了
基于pytorch开发CNN提取全连接层作为特征
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景:利用CNN網(wǎng)絡(luò)的全連接層作為圖像的特征。
代碼:
import sys import os import math import random import heapq import time import copy import numpy as np import pandas as pd from functools import reduce from scipy.spatial.distance import pdist from PIL import Image import matplotlib.pyplot as plt import cv2 #import faiss import torch import torch.nn as nn import torch.nn.functional as F torch.cuda.set_device(5) print (torch.cuda.current_device()) #2. define CNN network with pytorch class CNN_FCL_Net(nn.Module): def __init__(self,inChannels=3):super(CNN_FCL_Net, self).__init__()#(channels, Height, Width)#layer1: Convolution, (3,1024,1024)->(16,512,512)self.conv1 = nn.Conv2d(in_channels=inChannels, out_channels=16, kernel_size=3, padding=1, stride=2)self.bn1 = nn.BatchNorm2d(16)self.relu1 = nn.ReLU(inplace=True)#layer2: max pooling,(16,512,512)->(16,256,256)self.maxpool = nn.MaxPool2d(kernel_size=3, padding=1, stride=2)self.bn2 = nn.BatchNorm2d(16)#layer3: Convolution, (16,256,256)->(8,128,128)self.conv2 = nn.Conv2d(in_channels=16, out_channels=8, kernel_size=3, padding=1, stride=2)self.bn3 = nn.BatchNorm2d(8)self.relu2 = nn.ReLU(inplace=True)#layer4: mean pooling, (8,128,128)->(8,64,64)self.avgpool1 = nn.AvgPool2d(kernel_size=3, padding=1, stride=2)self.bn4 = nn.BatchNorm2d(8)#layer5: Convolution, (8,64,64)->(4*32*32)self.conv3 = nn.Conv2d(in_channels=8, out_channels=4, kernel_size=3, padding=1, stride=2)self.bn5 = nn.BatchNorm2d(4)self.relu3 = nn.ReLU(inplace=True)#layer6: mean pooling, (4,32,32)->(4,16,16)self.avgpool2 = nn.AvgPool2d(kernel_size=3, padding=1, stride=2)self.bn6 = nn.BatchNorm2d(4)#layer7: fully connected, 4*16*16->512self.fcl1 = nn.Linear(4*16*16,512)self.relu4 = nn.ReLU(inplace=True)#layer8: Hashing layer, 512->16self.fcl2 = nn.Linear(512,16)#self.tanh = nn.Tanh()#layer9: fully connected, 16->5self.fcl3 = nn.Linear(16,5)#type:5def forward(self,x):#input: (batch_size, in_channels, Height, Width)#output: (batch_size, out_channels, Height, Width)#layer1: convolutionx = self.conv1(x)x = self.bn1(x)x = self.relu1(x)#layer2: max poolingx = self.maxpool(x)x = self.bn2(x)#layer3: Convolutionx = self.conv2(x)x = self.bn3(x)x = self.relu2(x)#layer4: mean poolingx = self.avgpool1(x)x = self.bn4(x)#layer5: Convolutionx = self.conv3(x)x = self.bn5(x)x = self.relu3(x)#layer6: mean poolingx = self.avgpool2(x)x = self.bn6(x)#layer7:fully connectedx = x.view(x.size(0),-1) #transfer three dims to one dimx = self.fcl1(x)x = self.relu4(x)#layer8: fully connectedx = self.fcl2(x)x = self.tanh(x)#[-1,1]#layer9: fully connectedout = self.fcl3(x)return x,out #test network: valid x = torch.rand(10,3,1024,1024) y = torch.LongTensor([0,1,2,3,4,3,2,4,0,1]) model = CNN_FCL_Net() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) #define optimizer for epoch in range(10):optimizer.zero_grad()_,out = model(x)out = F.log_softmax(out)loss = F.nll_loss(out, y)print (loss.item())loss.backward()optimizer.step()#observe the variant of model.parametersfor i in model.named_parameters():print(i[0])print(i[1][0][0][0])break #output x2 = torch.rand(10,3,1024,1024)#.cuda() x2,_ = model(x2) print (x2) print (x2.size())結(jié)果:提取了16維的作為圖像特征。訓(xùn)練時(shí),我這里的類型是5類,計(jì)算softmax損失;并用倒第二全連接層作為特征。
1.597808599472046 conv1.weight tensor([-0.1370, 0.0693, 0.0283]) 1.0785161256790161 conv1.weight tensor([-0.1373, 0.0698, 0.0279]) 0.7979228496551514 conv1.weight tensor([-0.1372, 0.0705, 0.0274]) 0.6365795135498047 conv1.weight tensor([-0.1370, 0.0707, 0.0268]) 0.5375972390174866 conv1.weight tensor([-0.1369, 0.0708, 0.0261]) 0.4756961762905121 conv1.weight tensor([-0.1368, 0.0707, 0.0255]) 0.4359434247016907 conv1.weight tensor([-0.1367, 0.0705, 0.0249]) 0.40972647070884705 conv1.weight tensor([-0.1366, 0.0703, 0.0243]) 0.39210301637649536 conv1.weight tensor([-0.1365, 0.0700, 0.0238]) 0.37974879145622253 conv1.weight tensor([-0.1365, 0.0697, 0.0233]) tensor([[ 0.0126, -0.4354, 0.7358, -0.3279, 0.0964, -0.1032, 0.0251,0.0671, -0.3541, 0.1048, -0.2245, -0.0713, 0.0981, -0.3019,-0.0763, -0.3924],[-0.2796, -0.4190, 0.6042, -0.1088, 0.2098, -0.0519, -0.1614,-0.2900, -0.5231, 0.6286, -0.5180, -0.5717, -0.1499, -0.0641,-0.2040, -0.2051],[-0.3552, -0.6642, 0.6478, -0.0942, 0.3250, 0.0988, -0.1476,-0.2584, -0.1124, 0.3132, -0.5809, -0.2650, 0.3680, -0.6628,-0.1631, -0.0010],[ 0.5833, -0.1066, 0.4511, -0.3111, 0.0538, -0.3561, -0.2830,-0.5321, -0.3872, 0.6228, -0.2672, -0.4205, -0.2053, 0.5105,-0.2763, -0.0691],[-0.0379, -0.2094, 0.4713, -0.0013, 0.2720, -0.3556, 0.0795,-0.0534, -0.0985, 0.2867, -0.2555, -0.0439, 0.1377, -0.3558,-0.4235, 0.2471],[ 0.4115, -0.1686, 0.3313, 0.0857, -0.1116, -0.3676, -0.0543,0.2222, 0.4960, -0.0238, 0.1978, 0.4767, 0.1434, -0.2598,-0.1566, -0.3695],[ 0.2363, -0.5129, 0.3948, 0.2537, 0.2340, -0.0543, -0.0141,0.3067, 0.5632, -0.0250, -0.2869, 0.2674, 0.3395, -0.0649,0.0442, -0.5803],[ 0.4465, 0.3422, -0.0216, 0.0579, 0.0054, -0.7552, 0.0600,0.0594, 0.3528, 0.2613, 0.0207, 0.4569, 0.6297, -0.4662,-0.7167, 0.2272],[-0.3499, -0.4729, 0.6180, 0.4714, -0.0566, -0.0809, -0.3741,0.0748, -0.3641, 0.5802, -0.2637, -0.0513, 0.1439, -0.5016,0.0724, -0.1476],[ 0.3509, -0.1694, 0.3861, 0.2594, -0.1662, -0.4163, -0.0885,0.3407, 0.6411, -0.0377, -0.2181, 0.4241, 0.6128, -0.3431,-0.2390, -0.0309]]) torch.Size([10, 16])?
?
?
總結(jié)
以上是生活随笔為你收集整理的基于pytorch开发CNN提取全连接层作为特征的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 贝叶斯神经网络的辩论
- 下一篇: Feature Map of Pytor