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))

随机推荐

  1. 使用docker加载已有镜像安装Hyperledger Fabric v1.1.0

    背景 每次在新的服务器上安装Hyperledger Fabric网络时,通过fabric官方提供的脚本安装时,需要从网络上down下近10G的fabric相关镜像,这个过程是漫长及痛苦的,有时因网络问 ...

  2. List<Map<String, Object>>集合中获取某个key并转换为List<Integer>集合

    package com.codyy.sso.controller.yuanqu; import java.util.ArrayList; import java.util.HashMap; impor ...

  3. 关闭Cadence Orcad Capture CIS原理图弹出startpage页面的方法

    打开原理图工具 Orcad Capture CIS 时,总是会弹出startpage 页面,有时候感觉这个东西挺碍事的,还是关了感觉好.解决方法如下:(1) View---Toolbar----Com ...

  4. 网络通信协议tcp,udp区别

    1 网络通信协议 Tcp udp的区别 重点(*****) Tcp三次握手四次挥手(******) udp客户端多人聊天 import socket udp_client = socket.socke ...

  5. hyper-v下的ubuntu虚拟机分辨率修改

    修改/etc/default/grub sudo vim /etc/default/grub 改变前: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash&qu ...

  6. Oracle根据主键获取对应表,Oracle根据外键获取相关表

    Oracle根据主键获取对应表 select * from user_constraints a, USER_CONS_COLUMNS b where a.CONSTRAINT_TYPE = 'P' ...

  7. C# 使用NPOI 操作Excel

    首先 Nuget 引入NPOI 1.读取Excel /// <summary> /// 读取Excel数据 /// </summary> public static void ...

  8. vue项目中icon图标的完美引入

    第一步: 进入阿里矢量图标库并登录 地址:https://www.iconfont.cn 第二步: 选择项目需要的图标添加到库 第三步: 选完之后点击右上角的购物车,打开后点击添加到项目,没有就自己建 ...

  9. Golang 包管理简介

    Golang 包管理 在一个项目里,如果想引用本地包,经常会把新手搞的莫名其妙.这里通俗记录一下. 首先先要知道几个默认的规则 必须定义环境变量GOPATH,GOPATH可以定义多个目录 所有项目代码 ...

  10. [python]global与nonlocal关键字

    在Python中,当引用一个变量的时候,对这个变量的搜索是按找本地作用域(Local).嵌套作用域(Enclosing function locals).全局作用域(Global).内置作用域(bui ...