import tensorflow as tf
import numpy as np
import math
import keras
from keras.layers import Conv2D,Reshape,Input
import numpy as np
import matplotlib.pyplot as plt """ Channel attention module""" if __name__ == '__main__':
file = tf.read_file('img.jpg')
x = tf.image.decode_jpeg(file)
#print("Tensor:", x)
sess = tf.Session()
x1 = sess.run(x)
print("x1:",x1)
gamma = 0.05
sess = tf.Session()
x1 = sess.run(x)
x1 = tf.expand_dims(x1, dim =0)
print("x1.shape:", x1.shape) m_batchsize, height, width, C = x1.shape proj_query = Reshape((width * height, C))(x1)
print("proj_query:", type(proj_query))
print("proj_query:", proj_query.shape)
proj_query = sess.run(proj_query)
print(proj_query)
proj_key = Reshape((width * height, C))(x1)
proj_key = sess.run(proj_key).transpose(0, 2, 1)
print(proj_key)
print("proj_key:", type(proj_key))
print("proj_key:", proj_key.shape) proj_query = proj_query.astype(np.float32)
proj_key = proj_key.astype(np.float32) # N, C, C, bmm 批次矩阵乘法
energy = tf.matmul(proj_key,proj_query)
energy = sess.run(energy)
print("energy:", energy) # 这里实现了softmax用最后一维的最大值减去了原始数据, 获得了一个不是太大的值
# 沿着最后一维的C选择最大值, keepdim保证输出和输入形状一致, 除了指定的dim维度大小为1
energy_new = tf.reduce_max(energy, -1, keep_dims=True)
print("after_softmax_energy:",sess.run(energy_new)) sess = tf.Session()
e = energy_new
print("b:", sess.run(energy_new)) size = energy.shape[1]
for i in range(size - 1):
e = tf.concat([e, energy_new], axis=-1) energy_new = e
print("energy_new2:", sess.run(energy_new))
energy_new = energy_new - energy
print("energy_new3:", sess.run(energy_new)) attention = tf.nn.softmax(energy_new, axis=-1)
print("attention:", sess.run(attention)) proj_value = Reshape((width * height, C))(x1)
proj_value = sess.run(proj_value)
proj_value = proj_value.astype(np.float32)
print("proj_value:", proj_value.shape)
out = tf.matmul(proj_value, attention) out = sess.run(out)
#plt.imshow(out)
print("out1:", out)
out = out.reshape(m_batchsize, width * height, C)
#out1 = out.reshape(m_batchsize, C, height, width)
print("out2:", out.shape) out = gamma * out + x
#out = sess.run(out)
#out = out.astype(np.int16)
print("out3:", out)
import tensorflow as tf
import numpy as np
import math
import keras
from keras.layers import Conv2D,Reshape,Input
from keras.regularizers import l2
from keras.layers.advanced_activations import ELU, LeakyReLU
from keras import Model
import cv2 """
Important: 1、A为CxHxW => Conv+BN+ReLU => B, C 都为CxHxW 2、Reshape B, C to CxN (N=HxW)
3、Transpose B to B’
4、Softmax(Matmul(B’, C)) => spatial attention map S为NxN(HWxHW)
5、如上式1, 其中sji测量了第i个位置在第j位置上的影响
6、也就是第i个位置和第j个位置之间的关联程度/相关性, 越大越相似.
7、A => Covn+BN+ReLU => D 为CxHxW => reshape to CxN
8、Matmul(D, S’) => CxHxW, 这里设置为DS
9、Element-wise sum(scale parameter alpha * DS, A) => the final output E 为 CxHxW (式2)
10、alpha is initialized as 0 and gradually learn to assign more weight.
"""
"""
inputs :
x : input feature maps( N X C X H X W)
returns :
out : attention value + input feature
attention: N X (HxW) X (HxW)
"""
""" Position attention module"""
if __name__ == '__main__':
#x = tf.random_uniform([2, 7, 7, 3],minval=0,maxval=255,dtype=tf.float32)
file = tf.read_file('img.jpg')
x = tf.image.decode_jpeg(file)
#x = cv2.imread('ROIVIA3.jpg')
print(x)
gamma = 0.05
sess = tf.Session()
x1 = sess.run(x)
x1 = tf.expand_dims(x1, axis=0)
print(x1.shape)
in_dim = 3 xlen = x1.shape[1]
ylen = x1.shape[2]
input = Input(shape=(xlen,ylen,3))
query_conv = Conv2D(1, (1,1), activation='relu',kernel_initializer='he_normal')(input)
key_conv = Conv2D(1, (1, 1), activation='relu', kernel_initializer='he_normal')(input)
value_conv = Conv2D(3, (1, 1), activation='relu', kernel_initializer='he_normal')(input)
print(query_conv) batchsize, height, width, C = x1.shape
#print(C, height, width )
# B => N, C, HW
proj_query = Reshape(( width * height ,1))(query_conv)
proj_key = Reshape(( width * height, 1))(key_conv)
proj_value = Reshape((width * height, 3))(value_conv)
print("proj_query:",proj_query)
print("proj_key:", proj_key)
print("proj_value:",proj_value.shape)
model = Model(inputs=[input],outputs=[proj_query])
model.compile(optimizer='adam',loss='binary_crossentropy')
proj_query = model.predict(x1,steps=1)
print("proj_query:",proj_query)
# B' => N, HW, C
proj_query = proj_query.transpose(0, 2, 1)
print("proj_query2:", proj_query.shape)
print("proj_query2:", type(proj_query))
# C => N, C, HW
model1 = Model(inputs=[input], outputs=[proj_key])
model1.compile(optimizer='adam', loss='binary_crossentropy')
proj_key = model1.predict(x1, steps=1)
print("proj_key:", proj_key.shape) print(proj_key)
# B'xC => N, HW, HW
energy = tf.matmul(proj_key, proj_query)
print("energy:",energy.shape) # S = softmax(B'xC) => N, HW, HW
attention = tf.nn.softmax(energy, axis=-1)
print("attention:", attention.shape) # D => N, C, HW
model2 = Model(inputs=[input], outputs=[proj_value])
model2.compile(optimizer='adam', loss='binary_crossentropy')
proj_value = model2.predict(x1, steps=1)
print("proj_value:",proj_value.shape) # DxS' => N, C, HW
out = tf.matmul(proj_value, sess.run(attention).transpose(0, 2, 1))
print("out:", out.shape) # N, C, H, W
out = Reshape((height, width, 3))(out)
print("out1:", out.shape) out = gamma * out + sess.run(x1)
print("out2:", type(out))
随机推荐
-
jsp不能引用js,cs等解决办法
最近项目中使用到Spring3,在感叹Spring3注解配置清爽的同时竟然出现了这个不和谐的事情,实在无法忍受 问题:部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52 ...
-
Nginx平台构架 分类: Nginx 2015-07-13 10:55 205人阅读 评论(0) 收藏
深入理解Nginx模块发开与架构解析读书笔记. nginx在启动后,在unix系统中会以daemon的方式(可以手动关闭 nginx.conf daemon off)在后台运行,后台进程包含一个mas ...
-
C++ string类及其函数的讲解
文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...
-
vs2013 cpu占用100%问题
是由于显卡驱动支持wpf有问题 更新驱动或设置里取消自动调节视觉效果 http://support.microsoft.com/kb/2894215
-
第四节 二维条码与磁卡、IC卡、光卡之比较
二维条码同其他几种自动识别技术的比较可见下表: 比较点 二维条形码 磁卡 IC卡 光卡 抗磁力 强 弱 中等 强 抗静电 强 中等 中等 强 抗损性 强可折叠可局部穿孔可局部切割 弱不可折叠不可穿孔不 ...
-
在LINQ中实现多条件联合主键LEFT JOIN
我昨天遇到一个LINQ下使用多条件比对产生LEFT JOIN的问题,经过深入研究,终于解决了,也让我学到了新的东西,特地拿来分享. 实例:有一张库存异常变更视图KCYD,仓库ID[Ckid]和物品ID ...
-
机器人学 —— 轨迹规划(Artificial Potential)
今天终于完成了机器人轨迹规划的最后一次课了,拜拜自带B - BOX 的 Prof. TJ Taylor. 最后一节课的内容是利用势场来进行轨迹规划.此方法的思路非常清晰,针对Configration ...
-
用Python从零开始实现K近邻算法
KNN算法的定义: KNN通过测量不同样本的特征值之间的距离进行分类.它的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别.K通 ...
-
spring---transaction(6)---事务的配置
1 写在前面 上一篇我们了解到spring的事务的体系.这里我们将结合上篇讲spring事务的配置 2 Spring的三种事务配置形式 2.1 使用TransactionProxyFactoryBea ...
-
Windows7使用无线网卡建立WiFi热点
在Windows7下设置热点需要用到命令netsh wlan,具体的设置步骤如下: 1.配置热点 以管理员身份打开命令行模式,输入命令 netsh wlan set hostednetwork mod ...