import argparse
import numpy as np
import sys
import time
import images
from PIL import Image
from keras.preprocessing import image
from keras.applications import VGG19
import keras.backend as K
from keras.applications.vgg19 import preprocess_input
from keras.layers import (
Input,
InputLayer,
Flatten,
Activation,
Dense)
from keras.layers.convolutional import (
Convolution2D,
MaxPooling2D)
from keras.activations import *
from keras.models import Model
class DInput(object):
'''
A class to define forward and backward operation on Input
'''
def __init__(self, layer):
'''
# Arguments
layer: an instance of Input layer, whose configuration
will be used to initiate DInput(input_shape,
output_shape, weights)
'''
self.layer = layer
# input and output of Input layer are the same
def up(self, data):
'''
function to operate input in forward pass, the input and output
are the same
# Arguments
data: Data to be operated in forward pass
# Returns
data
'''
self.up_data = data
return self.up_data
def down(self, data):
'''
function to operate input in backward pass, the input and output
are the same
# Arguments
data: Data to be operated in backward pass
# Returns
data
'''
self.down_data = data
return self.down_data
class DConvolution2D(object):
'''
A class to define forward and backward operation on Convolution2D
'''
def __init__(self, layer):
'''
# Arguments
layer: an instance of Convolution2D layer, whose configuration
will be used to initiate DConvolution2D(input_shape,
output_shape, weights)
'''
self.layer = layer
weights = layer.get_weights()
W, b = weights
config = layer.get_config()
# Set up_func for DConvolution2D
input = Input(shape=layer.input_shape[1:])
output = Convolution2D.from_config(config)(input)
up_func = Model(input, output)
up_func.layers[1].set_weights(weights)
self.up_func = up_func
# Flip W horizontally and vertically,
# and set down_func for DConvolution2D
W = np.transpose(W, (0, 1, 3, 2))
W = W[::-1, ::-1, :, :]
config['filters'] = W.shape[3]
config['kernel_size'] = (W.shape[0], W.shape[1])
b = np.zeros(config['filters'])
input = Input(shape=layer.output_shape[1:])
output = Convolution2D.from_config(config)(input)
down_func = Model(input, output)
down_func.layers[1].set_weights((W, b))
self.down_func = down_func
def up(self, data):
'''
function to compute Convolution output in forward pass
# Arguments
data: Data to be operated in forward pass
# Returns
Convolved result
'''
self.up_data = self.up_func.predict(data)
return self.up_data
def down(self, data):
'''
function to compute Deconvolution output in backward pass
# Arguments
data: Data to be operated in backward pass
# Returns
Deconvolved result
'''
self.down_data = self.down_func.predict(data)
return self.down_data
class DPooling(object):
'''
A class to define forward and backward operation on Pooling
'''
def __init__(self, layer):
'''
# Arguments
layer: an instance of Pooling layer, whose configuration
will be used to initiate DPooling(input_shape,
output_shape, weights)
'''
self.layer = layer
self.poolsize = layer.pool_size
def up(self, data):
'''
function to compute pooling output in forward pass
# Arguments
data: Data to be operated in forward pass
# Returns
Pooled result
'''
[self.up_data, self.switch] = \
self.__max_pooling_with_switch(data, self.poolsize)
return self.up_data
def down(self, data):
'''
function to compute unpooling output in backward pass
# Arguments
data: Data to be operated in forward pass
# Returns
Unpooled result
'''
self.down_data = self.__max_unpooling_with_switch(data, self.switch)
return self.down_data
def __max_pooling_with_switch(self, input, poolsize):
'''
Compute pooling output and switch in forward pass, switch stores
location of the maximum value in each poolsize * poolsize block
# Arguments
input: data to be pooled
poolsize: size of pooling operation
# Returns
Pooled result and Switch
'''
switch = np.zeros(input.shape)
out_shape = list(input.shape)
row_poolsize = int(poolsize[0])
col_poolsize = int(poolsize[1])
out_shape[1] = out_shape[1] // poolsize[0]
out_shape[2] = out_shape[2] // poolsize[1]
pooled = np.zeros(out_shape)
for sample in range(input.shape[0]):
for dim in range(input.shape[3]):
for row in range(out_shape[1]):
for col in range(out_shape[2]):
patch = input[sample,
row * row_poolsize: (row + 1) * row_poolsize,
col * col_poolsize: (col + 1) * col_poolsize,
dim]
max_value = patch.max()
pooled[sample, row, col, dim] = max_value
max_col_index = patch.argmax(axis=-1)
max_cols = patch.max(axis=-1)
max_row = max_cols.argmax()
max_col = max_col_index[max_row]
switch[sample,
row * row_poolsize + max_row,
col * col_poolsize + max_col,
dim] = 1
return [pooled, switch]
# Compute unpooled output using pooled data and switch
def __max_unpooling_with_switch(self, input, switch):
'''
Compute unpooled output using pooled data and switch
# Arguments
input: data to be pooled
poolsize: size of pooling operation
switch: switch storing location of each elements
# Returns
Unpooled result
'''
out_shape = switch.shape
unpooled = np.zeros(out_shape)
for sample in range(input.shape[0]):
for dim in range(input.shape[3]):
tile = np.ones((switch.shape[1] // input.shape[1],
switch.shape[2] // input.shape[2]))
out = np.kron(input[sample, :, :, dim], tile)
unpooled[sample, :, :, dim] = out * switch[sample, :, :, dim]
return unpooled
class DActivation(object):
'''
A class to define forward and backward operation on Activation
'''
def __init__(self, layer, linear=False):
'''
# Arguments
layer: an instance of Activation layer, whose configuration
will be used to initiate DActivation(input_shape,
output_shape, weights)
'''
self.layer = layer
self.linear = linear
self.activation = layer.activation
input = K.placeholder(shape=layer.output_shape)
output = self.activation(inp
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于反卷积的卷积网路特征可视化.zip (50个子文件)
基于反卷积的卷积网路特征可视化
vis
block5_conv1_7.jpg 14KB
block5_conv4_3.jpg 11KB
block1_conv1_4.jpg 3KB
block4_conv1_3.jpg 15KB
block1_conv1_0.jpg 4KB
block5_conv4_5.jpg 12KB
block3_conv1_5.jpg 11KB
block5_conv1_5.jpg 11KB
block3_conv1_7.jpg 14KB
block5_conv4_4.jpg 12KB
block1_conv1_2.jpg 4KB
block1_conv1_7.jpg 3KB
block1_conv1_1.jpg 1KB
block5_conv1_2.jpg 10KB
block5_conv1_6.jpg 11KB
block5_conv4_6.jpg 9KB
block4_conv1_2.jpg 14KB
block4_conv1_4.jpg 14KB
block5_conv1_3.jpg 12KB
block2_conv1_5.jpg 13KB
block3_conv1_4.jpg 13KB
block3_conv1_1.jpg 11KB
block3_conv1_6.jpg 9KB
block1_conv1_5.jpg 3KB
block4_conv1_5.jpg 13KB
block1_conv1_3.jpg 2KB
block2_conv1_4.jpg 9KB
block4_conv1_0.jpg 15KB
block2_conv1_0.jpg 13KB
block5_conv4_1.jpg 14KB
block3_conv1_3.jpg 12KB
block5_conv4_0.jpg 12KB
block5_conv4_7.jpg 9KB
block2_conv1_1.jpg 13KB
block2_conv1_7.jpg 10KB
block4_conv1_6.jpg 14KB
block5_conv1_1.jpg 11KB
block5_conv1_4.jpg 12KB
block2_conv1_6.jpg 14KB
block5_conv4_2.jpg 14KB
block2_conv1_2.jpg 14KB
block5_conv1_0.jpg 12KB
block3_conv1_0.jpg 11KB
block1_conv1_6.jpg 3KB
block3_conv1_2.jpg 10KB
block4_conv1_1.jpg 15KB
block2_conv1_3.jpg 13KB
block4_conv1_7.jpg 13KB
Visualizing.py 18KB
cat.jpg 105KB
共 50 条
- 1
资源评论
RYAN-LZ
- 粉丝: 48
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于springboot的医院门诊管理系统源码.zip
- 基于python的垃圾分类系统源码(高分优质项目).zip
- 场景视点偏移改善激光雷达点云分割项目源码(下载即用).zip
- 纸板缺陷检测数据集VOC+YOLO格式1055张1类别.7z
- 基于CarNet实现裂缝检测python源码+文档说明+数据+图片(课程设计)
- 课程设计-基于耐火材料裂缝剥落检测python源码+课件
- 基于OpenCV的视频道路车道检测python源码+文档说明+实验演示+图片+使用方法(高分毕业设计)
- 基于OpenCV的案例:图像边缘、角点和轮廓检测,图像分割,图像增强;图片拼接;运动目标检测,颜色直方图比较,三帧帧差法,抠图
- SmartPlug-html大一笔记
- SmartPlug-proteusdemo
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功