import numpy as np
import os
import matplotlib.pyplot as plt
from keras_preprocessing import image
import cv2
import re
def resize_images(src_path, sizeX, sizeY):#对图像进行resize
images = []
imagesresize = []
width, height = sizeX, sizeY
for filename in os.listdir(src_path):
img = cv2.imread(src_path + filename)
if img is not None:
images.append(img)
cnt = 1
for img in images:
im2 = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
ext = ".png"
imagesresize.append(im2)
cnt += 1
print(len(imagesresize))
return imagesresize
def gaussian_noise(x_train, mean, sigma):#添加高斯噪声
x_train_noisy = []
for i in range(x_train.shape[0]):
image = x_train[i]
noise = np.random.normal(mean, sigma/255.0, x_train[0].shape)
x_train_noisy.append(image + noise)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
return np.array(x_train_noisy)
x = resize_images('data/Test/Set12/', 256,256)
image_data = []
for file in x:#读取文件夹下的图片并保存到列表中
gray_img = cv2.cvtColor(file, cv2.COLOR_BGR2GRAY)
img_array = image.img_to_array(gray_img)
image_data.append(img_array)
img_data = np.array(image_data, dtype='float32') / 255.0
print(img_data.shape)
noisy_set = gaussian_noise(img_data,0,25)
'''
for i in range(noisy_set.shape[0]):
cv2.imwrite("./noise/" + str(i) + ".jpg", noisy_set[i] * 255.)
'''
print(noisy_set.shape)
from keras.layers import *
from keras.layers import Lambda
from keras.models import Model
from keras import backend as K
def get_cnn_9conv(model_path=None):
input_img = Input(shape=((None, None, 1))) # adapt this if using `channels_first` image data format
x = Conv2D(56, (5, 5), activation='relu', padding='same')(input_img)
x = Conv2D(12, (1, 1), activation='relu', padding='same')(x)
x = Conv2D(12, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(12, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(12, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(12, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(56, (1, 1), activation='relu', padding='same')(x)
x = Conv2D(28, (9, 9), activation='relu', padding='same')(x)
decoded = Conv2D(1, (5, 5), activation='relu', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='sgd', loss='mean_squared_error', metrics=['accuracy', 'mean_squared_error'])
# print(autoencoder.summary())#
return autoencoder
def get_gated_connections(gatePercentageFactor, inputLayer):
gateFactor = Input(tensor=K.variable([gatePercentageFactor]))
fractionG = Lambda(lambda x: x[0] * x[1])([inputLayer, gateFactor])
complement = Lambda(lambda x: x[0] - x[1])([inputLayer, fractionG])
return gateFactor, fractionG, complement
def get_cnn_architecture(weights_path=None):
input_img = Input(shape=(None, None, 1)) # adapt this if using `channels_first` image data format
x1 = Conv2D(64, (3, 3), activation='relu', padding='same')(input_img)#180 180 64
gf1, fg1, c1 = get_gated_connections(0.1, x1)
x = MaxPooling2D((2, 2), padding='same')(fg1)#90 90 64
x2 = Conv2D(64, (3, 3), activation='relu', padding='same')(x) #90 90 64
gf2, fg2, c2 = get_gated_connections(0.2, x2)
x = MaxPooling2D((2, 2), padding='same')(fg2) #45 45 64
x3 = Conv2D(128, (3, 3), activation='relu', padding='same')(x)#45 45 128
gf3, fg3, c3 = get_gated_connections(0.3, x3)
x = MaxPooling2D((2, 2), padding='same')(x3)#22.5 22.5 128
x4 = Conv2D(256, (3, 3), activation='relu', padding='same')(x)#22.5 22.5 256
gf4, fg4, c4 = get_gated_connections(0.4, x4)
x = MaxPooling2D((2, 2), padding='same')(x4)
x5 = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
gf5, fg5, c5 = get_gated_connections(0.5, x5)
x = MaxPooling2D((2, 2), padding='same')(x5)
x6 = Conv2D(1024, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x6)
y1 = Conv2DTranspose(512, (3, 3), activation='relu', padding='same')(x)
jt4 = Add()([y1, c5])
x = UpSampling2D((2, 2))(jt4)
y2 = Conv2DTranspose(256, (3, 3), activation='relu', padding='same')(x)
jt3 = Add()([y2, c4])
x = UpSampling2D((2, 2))(jt3)
y3 = Conv2DTranspose(128, (3, 3), activation='relu', padding='same')(x)
jt2 = Add()([y3, c3])
x = UpSampling2D((2, 2))(jt2)#90 90 128
y4 = Conv2DTranspose(64, (3, 3), activation='relu', padding='same')(x)#90 90 64
jt1 = Add()([y4, c2])
x = UpSampling2D((2, 2))(jt1)#180 180 64
jt0 = Add()([x, c1])
y5 = Conv2DTranspose(64, (3, 3), activation='relu', padding='same')(jt0)#180 180 64
y6 = Conv2DTranspose(1, (3, 3), activation='relu', padding='same')(y5)#180 180 3
layers = y6
#model = Model([input_img,gf1,gf2], layers)
model = Model([input_img, gf1, gf2, gf3, gf4, gf5], layers)
model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['accuracy'])#编译
#model.summary()
return model
def DnCNN(depth, filters=64, image_channels=1, use_bnorm=True):
layer_count = 0
inpt = Input(shape=(None, None, image_channels), name='input' + str(layer_count))
# 1st layer, Conv+relu
layer_count += 1
x = Conv2D(filters=filters, kernel_size=(3, 3), strides=(1, 1), kernel_initializer='Orthogonal', padding='same',
name='conv' + str(layer_count))(inpt)
layer_count += 1
x = Activation('relu', name='relu' + str(layer_count))(x)
# depth-2 layers, Conv+BN+relu
for i in range(depth - 2):
layer_count += 1
x = Conv2D(filters=filters, kernel_size=(3, 3), strides=(1, 1), kernel_initializer='Orthogonal', padding='same',
use_bias=False, name='conv' + str(layer_count))(x)
if use_bnorm:
layer_count += 1
# x = BatchNormalization(axis=3, momentum=0.1,epsilon=0.0001, name = 'bn'+str(layer_count))(x)
x = BatchNormalization(axis=3, momentum=0.0, epsilon=0.0001, name='bn' + str(layer_count))(x)
layer_count += 1
x = Activation('relu', name='relu' + str(layer_count))(x)
# last layer, Conv
layer_count += 1
x = Conv2D(filters=image_channels, kernel_size=(3, 3), strides=(1, 1), kernel_initializer='Orthogonal',
padding='same', use_bias=False, name='conv' + str(layer_count))(x)
layer_count += 1
x = Subtract(name='subtract' + str(layer_count))([inpt, x]) # input - noise
model = Model(inputs=inpt, outputs=x)
return model
if __name__ == '__main__':
#bmodel = get_cnn_9conv()
#bmodel = get_cnn_architecture()
bmodel = DnCNN(depth=17,filters=64,image_channels=1,use_bnorm=True)
#bmodel.load_weights('./save_weights/get_cnn_9conv25.h5')
#bmodel.load_weights('./save_weights/get_cnn_architecture25.h5')
bmodel.load_weights('./save_weights/DnCNN25.h5')
pic = bmodel.predict(noisy_set)
from skimage.measure import compare_psnr
psnrs = []
psnr1 = []
print(img_data[1].shape,pic[1].shape)
for x in range(len(noisy_set)):
'''
psnr_x = skimage.metrics.peak_signal_noise_ratio(img_data[x] * 255., pic[x] * 255.,data_range = 255)
psnr_y = skimage.metrics.peak_signal_noise_ratio(img_data[x] * 255., noisy_set[x] * 255.,data_range = 255)
'''
psnr_x = compare_psnr(img_data[x] * 255., pic[x] * 255., data_range=255)
psnr_y = compare_psnr(img_data[x] * 255., noisy_set[x] * 255., data_range=255)
psnrs.append(psnr_x)
psnr1.append(psnr_y)
psnr_avg = np.mean(psnrs)
psnr_avg1 = np.mean(psnr1)
for y in psnrs:
print(y, end=' ')
print(psnr_avg)
for z in psnr1:
prin
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
基于卷积神经网络的图像去噪(基础篇) (427个子文件)
.gitignore 50B
get_cnn_architecture25.h5 48.18MB
DnCNN25.h5 2.24MB
DnCNN15.h5 2.24MB
DnCNN10.h5 2.24MB
get_cnn_9conv25.h5 563KB
ImageDenoiseCnn.iml 499B
11.png 205KB
12.png 189KB
09.png 181KB
10.png 174KB
08.png 148KB
04.png 42KB
06.png 40KB
05.png 40KB
03.png 39KB
07.png 39KB
01.png 37KB
02.png 34KB
7.png 30KB
379.png 29KB
270.png 29KB
361.png 29KB
108.png 29KB
263.png 29KB
176.png 29KB
350.png 29KB
357.png 29KB
245.png 29KB
367.png 29KB
373.png 29KB
248.png 28KB
143.png 28KB
118.png 28KB
274.png 28KB
171.png 28KB
100.png 28KB
148.png 28KB
388.png 28KB
337.png 28KB
174.png 28KB
121.png 28KB
125.png 28KB
97.png 28KB
124.png 28KB
332.png 28KB
194.png 28KB
185.png 28KB
244.png 28KB
114.png 28KB
47.png 28KB
223.png 27KB
208.png 27KB
228.png 27KB
370.png 27KB
179.png 27KB
196.png 27KB
336.png 27KB
283.png 27KB
107.png 27KB
146.png 27KB
378.png 27KB
284.png 27KB
327.png 27KB
51.png 27KB
227.png 27KB
333.png 27KB
302.png 27KB
15.png 27KB
126.png 27KB
99.png 27KB
144.png 27KB
209.png 27KB
72.png 27KB
261.png 27KB
276.png 27KB
363.png 27KB
62.png 27KB
224.png 27KB
325.png 27KB
61.png 27KB
155.png 26KB
210.png 26KB
83.png 26KB
142.png 26KB
190.png 26KB
253.png 26KB
191.png 26KB
113.png 26KB
182.png 26KB
112.png 26KB
154.png 26KB
273.png 26KB
360.png 26KB
389.png 26KB
318.png 26KB
141.png 26KB
33.png 26KB
220.png 26KB
358.png 26KB
共 427 条
- 1
- 2
- 3
- 4
- 5
luasihuerdd
- 粉丝: 28
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1