Commit 3b197eb1 authored by Liuyuxinict's avatar Liuyuxinict

提交文件

parents
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.6 (pytorch1.7)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="pytest" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6 (pytorch1.7)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PMG-Progressive-Multi-Granularity-Training-master.iml" filepath="$PROJECT_DIR$/.idea/PMG-Progressive-Multi-Granularity-Training-master.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="1de14600-5bec-46d2-972f-11687490a303" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="ProjectId" id="1hp1mgftlnNubdit1AM27vnxPWs" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showExcludedFiles" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\PMG" />
</key>
</component>
<component name="RunManager" selected="Python.train">
<configuration name="1" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="PMG-Progressive-Multi-Granularity-Training-master" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/1.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="train" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="PMG-Progressive-Multi-Granularity-Training-master" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/train.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="visualization" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="PMG-Progressive-Multi-Granularity-Training-master" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/visualization.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.train" />
<item itemvalue="Python.visualization" />
<item itemvalue="Python.1" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="1de14600-5bec-46d2-972f-11687490a303" name="Default Changelist" comment="" />
<created>1600693379983</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1600693379983</updated>
</task>
<servers />
</component>
<component name="WindowStateProjectService">
<state x="549" y="171" key="FileChooserDialogImpl" timestamp="1658372379078">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="549" y="171" key="FileChooserDialogImpl/0.0.1536.824@0.0.1536.824" timestamp="1658372379078" />
<state width="1515" height="290" key="GridCell.Tab.0.bottom" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.0.center" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.0.left" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.0.right" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.1.bottom" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.1.bottom/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.1.center" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.1.center/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.1.left" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.1.left/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state width="1515" height="290" key="GridCell.Tab.1.right" timestamp="1658494944285">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1515" height="290" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1658494944285" />
<state x="272" y="58" key="SettingsEditor" timestamp="1658493360383">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="272" y="58" key="SettingsEditor/0.0.1536.824@0.0.1536.824" timestamp="1658493360383" />
<state x="461" y="241" key="com.intellij.ide.util.TipDialog" timestamp="1658372307680">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="461" y="241" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1658372307680" />
</component>
</project>
\ No newline at end of file
import numpy as np
'''
A= np.array([[1,1,1,1,1,1,1,1,1,1,1],
[-5,-4,-3,-2,-1,0,1,2,3,4,5],
]).transpose()
b= np.array([2,7,9,12,13,14,14,13,10,8,4])
AA = np.dot(A.transpose(),A)
print(AA)
AB = np.dot(A.transpose(),b)
print(AB)
x = np.dot(np.linalg.inv(AA),AB)
print(x)
print(np.dot(b-np.dot(A,x),(b-np.dot(A,x).transpose())))
#[25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25]
A = np.array([[1,3,1,-4],
[-1,-3,1,0],
[2,6,2,-8]])
AA = np.dot(A.transpose(),A)
print(AA)
print(np.linalg.matrix_rank(A))
'''
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.random.rand(10000)
t = np.arange(len(x))
# plt.plot(t, x, 'g.', label=u'均匀分布') # 散点图
plt.hist(x, 1000, color='m', alpha=0.6, label=u'均匀分布', normed=True)
plt.legend(loc='upper right')
plt.grid(True, ls=':')
plt.show()
MIT License
Copyright (c) 2020 PRIS-CV: Computer Vision Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Progressive Region Enhancement Network (PRENet)
Code release for Large Scale Visual Food Recognition
### Requirement
python 3.6
PyTorch >= 1.3.1
torchvision >= 0.4.2
dropblock
### Training
1.
2. Train from scratch with ``train.py``.
### Inference
1. Download the pretrained model on Food2k [here](https://pan.baidu.com/s/1HMvBf0F-FpMIMPtuQtUE8Q)(Code: o0nj)
### Pretrained model
| CNN | link |
| ---- | ---- |
| resnet50 | [here](https://pan.baidu.com/s/1WY7VsCBTJt2mL9n3Gdl8Mg)(Code: 5eay) |
| resnet101 | [here](https://pan.baidu.com/s/1mEO7KyJFHrkpB5G0Aj6oWw)(Code: yv1o) |
| resnet152 | [here](https://pan.baidu.com/s/1-3LikXkDEvbxQur6n-FUJw)(Code: 22zw) |
| densenet161 | [here](https://pan.baidu.com/s/1UllqjTJMAQEnGFVgzf6-nQ)(Code: bew5) |
| inception_resnet_v2 | [here](https://pan.baidu.com/s/1_974E4eZRzKubemLIQlOHA)(Code: xa8r) |
| senet154 | [here](https://pan.baidu.com/s/1tHpFFSm2AySRjDZ4BTtboQ)(Code: kwzf) |
## Contact
This diff is collapsed.
import torch.nn as nn
import torch
import torch.nn.functional as F
class layer_self_attention(nn.Module):
def __init__(self, in_channels, out_channels, dk, dq, dv, Nh):
super(layer_self_attention, self).__init__()
self.Cin = in_channels
self.Cout = out_channels
self.dq = dq
self.dk = dk
self.dv = dv
self.Nh = Nh
self.k = int(self.dk * self.Cin)
self.q = int(self.dq * self.Cin)
self.v = int(self.dv * self.Cin)
self.q_conv = nn.Sequential(
nn.Conv2d(self.Cin, self.q, kernel_size=1, stride=1, padding=0),
#nn.BatchNorm2d(self.q,self.q)
)
self.kv_conv = nn.Sequential(
nn.Conv2d(self.Cin, self.k + self.v, kernel_size=1, stride=1, padding=0),
#nn.BatchNorm2d(self.k + self.v, self.k + self.v)
)
self.attn = nn.Conv2d(self.v, self.Cout, kernel_size=1, stride=1)
def split_heads_2d(self, x, Nh):
batch, channels, height, width = x.size()
ret_shape = (batch, Nh, channels // Nh, height, width)
split = torch.reshape(x, ret_shape)
return split
#shape of flat_q: (N, Nh, dq//Nh, H*W)
#shape of q: (N, Nh, dq//Nh, H, W)
def layer_compute_flat_qkv(self, x, x_st, dq, dk, dv, Nh):
q = self.q_conv(x)
N, _, Hq, Wq = q.shape
kv = self.kv_conv(x_st)
N, _, H, W = kv.shape
k,v = torch.split(kv, [dk, dv], dim=1)
q = self.split_heads_2d(q, Nh)
k = self.split_heads_2d(k, Nh)
v = self.split_heads_2d(v, Nh)
dkh = dk // Nh
q *= dkh ** -0.5
flat_q = torch.reshape(q, (N, Nh, dq // Nh, Hq * Wq))
flat_k = torch.reshape(k, (N, Nh, dk // Nh, H * W))
flat_v = torch.reshape(v, (N, Nh, dv // Nh, H * W))
return flat_q, flat_k, flat_v, q, k, v
#use inputs_st(k,v) to strength inputs(q)
def forward(self, inputs, inputs_st):
batch, N, H, W = inputs.shape
#print(inputs.shape)
flat_q, flat_k, flat_v, q, k, v = self.layer_compute_flat_qkv(inputs, inputs_st,self.q, self.k,self.v,self.Nh)
#print(flat_q.shape)
logits = torch.matmul(flat_q.transpose(2, 3), flat_k)
weights = F.softmax(logits, dim=1)
#print(weights.shape)
attn_out = torch.matmul(weights, flat_v.transpose(2, 3))
attn_out = torch.reshape(attn_out, (batch, self.Nh, self.v // self.Nh, H, W))
#print(attn_out.shape)
attn_out = torch.reshape(attn_out, (batch, self.Nh * (self.v // self.Nh), H, W))
#print(attn_out.shape)
attn_out = self.attn(attn_out)
#print(attn_out.shape)
return attn_out
import torch.nn as nn
import torch
import torch.nn.functional as F
from self_attention import self_attention
from layer_self_attention import layer_self_attention
from dropblock import DropBlock2D
import numpy as np
class PMG(nn.Module):
def __init__(self, model, feature_size, classes_num):
super(PMG, self).__init__()
self.features = model
self.num_ftrs = 2048 * 1 * 1
self.elu = nn.ELU(inplace=True)
self.dk = 0.5
self.dq = 0.5
self.dv = 0.5
self.Nh = 8
self.classifier_concat = nn.Sequential(
nn.BatchNorm1d(1024 * 5),
nn.Linear(1024 * 5, feature_size),
nn.BatchNorm1d(feature_size),
nn.ELU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.conv_block0 = nn.Sequential(
BasicConv(self.num_ftrs // 8, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs // 2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier0 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs // 2),
nn.Linear(self.num_ftrs // 2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ELU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.conv_block1 = nn.Sequential(
BasicConv(self.num_ftrs//4, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs//2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier1 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs//2),
nn.Linear(self.num_ftrs//2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ELU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.conv_block2 = nn.Sequential(
BasicConv(self.num_ftrs//2, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs//2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier2 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs//2),
nn.Linear(self.num_ftrs//2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ELU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.conv_block3 = nn.Sequential(
BasicConv(self.num_ftrs, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs//2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier3 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs//2),
nn.Linear(self.num_ftrs//2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ELU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.Avgmax = nn.AdaptiveMaxPool2d(output_size=(1,1))
self.attn1_1 = self_attention(self.num_ftrs // 2,self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn2_2 = self_attention(self.num_ftrs // 2,self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn3_3 = self_attention(self.num_ftrs // 2,self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
'''
self.attn1_2 = layer_self_attention(self.num_ftrs // 2,self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn1_3 = layer_self_attention(self.num_ftrs // 2,self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn2_3 = layer_self_attention(self.num_ftrs // 2,self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn2_1 = layer_self_attention(self.num_ftrs // 2, self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn3_1 = layer_self_attention(self.num_ftrs // 2, self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
self.attn3_2 = layer_self_attention(self.num_ftrs // 2, self.num_ftrs // 2, self.dk, self.dq, self.dv, self.Nh)
'''
self.sconv1 = nn.Conv2d((self.num_ftrs // 2), self.num_ftrs // 2, kernel_size= 3, padding= 1)
self.sconv2 = nn.Conv2d((self.num_ftrs // 2), self.num_ftrs // 2, kernel_size= 3, padding= 1)
self.sconv3 = nn.Conv2d((self.num_ftrs // 2), self.num_ftrs // 2, kernel_size= 3, padding= 1)
self.drop_block = DropBlock2D(block_size=3, drop_prob=0.5)
def forward(self, x, label):
xf1, xf2, xf3, xf4, xf5, xn = self.features(x)
batch_size, _, _, _ = x.shape
#get feature pyramid
xl1 = self.conv_block1(xf3)
xl2 = self.conv_block2(xf4)
xl3 = self.conv_block3(xf5)
xk1 = self.Avgmax(xl1)
xk1 = xk1.view(xk1.size(0), -1)
xc1 = self.classifier1(xk1)
xk2 = self.Avgmax(xl2)
xk2 = xk2.view(xk2.size(0), -1)
xc2 = self.classifier2(xk2)
xk3 = self.Avgmax(xl3)
xk3 = xk3.view(xk3.size(0), -1)
xc3 = self.classifier3(xk3)
if label:
# xs1_2 means that using x2 to strength x1
#(batch, 1024, 56, 56)
xs1 = self.attn1_1(xl1)
#xs1_2 = self.attn1_2(xl1, xl2)
#xs1_3 = self.attn1_3(xl1, xl3)
# (batch, 1024, 28, 28)
xs2 = self.attn1_1(xl2)
#xs2_3 = self.attn2_3(xl2, xl3)
#xs2_1 = self.attn2_1(xl2, xl1)
# (batch, 1024, 14, 14)
xs3 = self.attn1_1(xl3)
#xs3_1 = self.attn2_1(xl3, xl1)
#xs3_2 = self.attn2_1(xl3, xl2)
#xr1 = self.drop_block(self.sconv1(torch.cat([xs1,xs1_2,xs1_3], dim=1)))
#xr2 = self.drop_block(self.sconv2(torch.cat([xs2,xs2_3,xs2_1], dim=1)))
#xr3 = self.drop_block(self.sconv3(torch.cat([xs3,xs3_1,xs3_2], dim=1)))
xr1 = self.drop_block(self.sconv1(xs1))
xr2 = self.drop_block(self.sconv2(xs2))
xr3 = self.drop_block(self.sconv3(xs3))
xm1 = self.Avgmax(xr1)
xm1 = xm1.view(xm1.size(0), -1)
#print(np.argmax(F.softmax(xm1, dim=1).cpu().detach().numpy(),axis=1))
#input()
xm2 = self.Avgmax(xr2)
xm2 = xm2.view(xm2.size(0), -1)
#print(np.argmax(F.softmax(xm2, dim=1).cpu().detach().numpy(),axis=1))
#input()
xm3 = self.Avgmax(xr3)
xm3 = xm3.view(xm3.size(0), -1)
#print(np.argmax(F.softmax(xm3, dim=1).cpu().detach().numpy(),axis=1))
#input()
x_concat = torch.cat((xm1, xm2, xm3, xn), dim=1)
x_concat = self.classifier_concat(x_concat)
else:
x_concat = torch.cat((xk1, xk2, xk3, xn), dim=1)
x_concat = self.classifier_concat(x_concat)
#get origal feature vector
return xk1, xk2, xk3, x_concat, xc1, xc2, xc3
class BasicConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True, bias=False):
super(BasicConv, self).__init__()
self.out_channels = out_planes
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size,
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
self.bn = nn.BatchNorm2d(out_planes, eps=1e-5,
momentum=0.01, affine=True) if bn else None
self.relu = nn.ReLU() if relu else None
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu is not None:
x = self.relu(x)
return x
import torch.nn as nn
import torch
import torch.nn.functional as F
import numpy as np
class self_attention(nn.Module):
def __init__(self, in_channels, out_channels, dk, dq, dv, Nh):
super(self_attention, self).__init__()
self.Cin = in_channels
self.Cout = out_channels
self.dq = dq
self.dk = dk
self.dv = dv
self.Nh = Nh
self.k = int(self.dk * self.Cin)
self.q = int(self.dq * self.Cin)
self.v = int(self.dv * self.Cin)
self.kqv_conv = nn.Sequential(
nn.Conv2d(self.Cin, self.k+self.q+self.v, kernel_size=1, stride=1, padding=0),
#nn.BatchNorm2d(self.k+self.q+self.v,self.k+self.q+self.v)
)
self.attn = nn.Conv2d(self.v, self.Cout, kernel_size=1, stride=1)
def split_heads_2d(self, x, Nh):
batch, channels, height, width = x.size()
ret_shape = (batch, Nh, channels // Nh, height, width)
split = torch.reshape(x, ret_shape)
return split
#shape of flat_q: (N, Nh, dq//Nh, H*W)
#shape of q: (N, Nh, dq//Nh, H, W)
def compute_flat_qkv(self, x, dq, dk, dv, Nh):
qkv = self.kqv_conv(x)
N, _, H, W = qkv.size()
q, k, v = torch.split(qkv, [dq, dk, dv], dim=1)
q = self.split_heads_2d(q, Nh)
k = self.split_heads_2d(k, Nh)
v = self.split_heads_2d(v, Nh)
dkh = dk // Nh
q *= dkh ** -0.5
flat_q = torch.reshape(q, (N, Nh, dq // Nh, H * W))
flat_k = torch.reshape(k, (N, Nh, dk // Nh, H * W))
flat_v = torch.reshape(v, (N, Nh, dv // Nh, H * W))
return flat_q, flat_k, flat_v, q, k, v
def forward(self, inputs):
batch, N, H, W = inputs.shape
#print(inputs.shape)
flat_q, flat_k, flat_v, q, k, v = self.compute_flat_qkv(inputs, self.q, self.k,self.v,self.Nh)
#print(flat_q.shape)
logits = torch.matmul(flat_q.transpose(2, 3), flat_k)
weights = F.softmax(logits, dim=1)
#print(weights.shape)
#result = weights.cpu().detach().numpy()
#np.save("visual/matrix"+str(H), result)
#print(weights.shape)
attn_out = torch.matmul(weights, flat_v.transpose(2, 3))
attn_out = torch.reshape(attn_out, (batch, self.Nh, self.v // self.Nh, H, W))
#print(attn_out.shape)
attn_out = torch.reshape(attn_out, (batch, self.Nh * (self.v // self.Nh), H, W))
#print(attn_out.shape)
attn_out = self.attn(attn_out)
#print(attn_out.shape)
return attn_out
This diff is collapsed.
This diff is collapsed.
import numpy as np
import random
import torch
import torchvision
from torch.autograd import Variable
from torchvision import transforms, models
import torch.nn.functional as F
from tqdm import tqdm
from model import *
from Resnet import *
def load_model(model_name, pretrain=True, require_grad=True, num_class=1000, pretrained_model=None):
print('==> Building model..')
if model_name == 'resnet50_pmg':
net = resnet50(pretrained=pretrain, path=pretrained_model)
#for param in net.parameters():
#param.requires_grad = require_grad
net = PMG(net, 512, num_class)
return net
def jigsaw_generator(images, n):
l = []
for a in range(n):
for b in range(n):
l.append([a, b])
block_size = 448 // n
rounds = n ** 2
random.shuffle(l)
jigsaws = images.clone()
for i in range(rounds):
x, y = l[i]
temp = jigsaws[..., 0:block_size, 0:block_size].clone()
jigsaws[..., 0:block_size, 0:block_size] = jigsaws[..., x * block_size:(x + 1) * block_size,
y * block_size:(y + 1) * block_size].clone()
jigsaws[..., x * block_size:(x + 1) * block_size, y * block_size:(y + 1) * block_size] = temp
return jigsaws
def test(net, criterion, batch_size, testloader,useattn):
net.eval()
test_loss = 0
correct = 0
correct_com = 0
total = 0
idx = 0
val_corrects1 = 0
val_corrects2 = 0
val_corrects5 = 0
val_en_corrects1 = 0
val_en_corrects2 = 0
val_en_corrects5 = 0
batch_idx = 0
for (inputs, targets) in tqdm(testloader):
idx = batch_idx
with torch.no_grad():
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs), Variable(targets)
_, _, _, output_concat, output1, output2, output3 = net(inputs,useattn)
#print(np.argmax(F.softmax(output_concat, dim=1).cpu().numpy(),axis=1))
#input()
#continue
outputs_com = output1 + output2 + output3 + output_concat
#print(np.argmax(F.softmax(output1, dim=1).cpu().numpy(),axis=1))
#input()
loss = criterion(output_concat, targets)
test_loss += loss.item()
_, top3_pos = torch.topk(output_concat.data, 5)
_, top3_pos_en = torch.topk(outputs_com.data, 5)
total += targets.size(0)
batch_corrects1 = torch.sum((top3_pos[:, 0] == targets)).data.item()
val_corrects1 += batch_corrects1
batch_corrects2 = torch.sum((top3_pos[:, 1] == targets)).data.item()
val_corrects2 += (batch_corrects2 + batch_corrects1)
batch_corrects3 = torch.sum((top3_pos[:, 2] == targets)).data.item()
batch_corrects4 = torch.sum((top3_pos[:, 3] == targets)).data.item()
batch_corrects5 = torch.sum((top3_pos[:, 4] == targets)).data.item()
val_corrects5 += (batch_corrects5 + batch_corrects4 + batch_corrects3 + batch_corrects2 + batch_corrects1)
batch_corrects1 = torch.sum((top3_pos_en[:, 0] == targets)).data.item()
val_en_corrects1 += batch_corrects1
batch_corrects2 = torch.sum((top3_pos_en[:, 1] == targets)).data.item()
val_en_corrects2+= (batch_corrects2 + batch_corrects1)
batch_corrects3 = torch.sum((top3_pos_en[:, 2] == targets)).data.item()
batch_corrects4 = torch.sum((top3_pos_en[:, 3] == targets)).data.item()
batch_corrects5 = torch.sum((top3_pos_en[:, 4] == targets)).data.item()
val_en_corrects5 += (batch_corrects5 + batch_corrects4 + batch_corrects3 + batch_corrects2 + batch_corrects1)
batch_idx += 1
test_acc = val_corrects1 / total
test5_acc = val_corrects5 / total
test_acc_en = val_en_corrects1 / total
test5_acc_en = val_en_corrects5 / total
test_loss = test_loss / (idx + 1)
return test_acc, test5_acc, test_acc_en, test5_acc_en, test_loss
#return test_acc, test5_acc, test_loss
import torch
import torch.nn as nn
import torch.optim as optim
import cv2
import numpy as np
import torchvision
from torchvision import datasets
# coding: utf-8
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
from utils import load_model
check = False
load_pretrained = False
#model = ResNet_aac(fb=64, n_label = 11, model_size=152,kernel_size=3,stride=2,dk=1,dv=1, Nh=8,shape=224,relative = False)
model = load_model('resnet50_pmg',pretrain=False,require_grad=True,num_class=101)
model.cuda()
model = nn.DataParallel(model)
#model = resnet152()
pointlist=[]
if load_pretrained:
model_dict = model.state_dict()
#pretrained_dict = torch.load(args.pretrainedmodel)
pretrained_dict = torch.load("D:/resnet50.pth")
# pretrained_dict = model_zoo.load_url('https://download.pytorch.org/models/resnet50-19c8e357.pth')
state_dict = {}
for k, v in model_dict.items():
# if k in dict1.keys():
if k in pretrained_dict.keys() and "fc" not in k:
#state_dict[k] = pretrained_dict[dict1[k]]
state_dict[k] = pretrained_dict[k]
else:
state_dict[k] = v
print(k)
if check:
#filename = "best_model_"
#checkpoint = torch.load('./checkpoint/' + filename + 'ckpt.t7')
#checkpoint = torch.load('unprebest.t7')
model.module.load_state_dict(torch.load("./food101/model.pth").module.state_dict())
model.eval()
test_img = "G:/images/apple_pie/116697.jpg"
img = Image.open(test_img).convert('RGB')
transform_test = transforms.Compose([
transforms.Resize(size=(299, 299)),
transforms.CenterCrop((224,224)),
transforms.ToTensor(),
transforms.Normalize((0.5457954,0.44430383,0.34424934), (0.23273608,0.24383051,0.24237761))
])
img1 = transform_test(img).reshape([1,3,224,224])
model(img1,True)
#print(model(img1,True).shape)
def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
print (xy)
pointlist.append((x,y))
cv2.circle(img, (x, y), 1, (255, 0, 0), thickness = -1)
cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (0,0,0), thickness = 1)
cv2.imshow("Image", img)
return
test_img = "G:/images/apple_pie/116697.jpg"
#显示原图
img = cv2.imread(test_img)
img = cv2.resize(img,(224,224))
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", on_EVENT_LBUTTONDOWN)
img_raw=img.copy()
cv2.imshow("Image", img)
cv2.waitKey(0)
#显示normalize之后的图
img = Image.open(test_img).convert('RGB')
transform_test = transforms.Compose([
transforms.Resize(size=(224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5457954,0.44430383,0.34424934), (0.23273608,0.24383051,0.24237761))
])
img1 = transform_test(img)
img = img1.transpose(0,1).transpose(1,2)
img = img.numpy()
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow("Image", img)
cv2.waitKey (0)
#显示热力图
AAC_mat = np.load(r"D:\PMG\visual\matrix14.npy")
depth = AAC_mat.shape[-1]
height = int(np.sqrt(depth))
AAC_mat = np.reshape(AAC_mat,[8,depth,depth])
#print(AAC_mat.shape)
isfirst1 = True
imgs = None
imgs1 = None
for item in pointlist:
isfirst = True
x,y = item
x/=224/height
y/=224/height
mat = AAC_mat[:,int(x*height+y),:]
#print(mat.shape)
#for i in range(0,8):
result = mat
result = result.reshape([8,height,height])
for i in range(0,8):
result = mat[i:]
img = result
#img = cv2.resize(result, (224, 224))
#img = img*255
heatmap = img / np.max(img)
heatmap = np.uint8(255 * heatmap)
w=heatmap
w = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) # 转化为jet 的colormap
#w = heatmap
w = cv2.resize(w,(224,224))
#x = img_raw*0.5+w*0.5 # 权重自己定
x =w
x = x.astype(np.uint8)
#print(x.shape)
if isfirst:
imgs = x
isfirst = False
else:
print(imgs.shape)
print(x.shape)
imgs = np.hstack([imgs, x])
#print(imgs.shape)
if isfirst1:
imgs1 = imgs
isfirst1 = False
else:
imgs1 = np.vstack([imgs1, imgs])
print(imgs1.shape)
cv2.imshow("mutil_pic", imgs1)
cv2.waitKey(0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment