Tensorflow之VGG网络模型

Tensorflow之VGG网络模型

模型资源,后期补上

!pip install pillow imageio
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pillow in d:\progra~2\python\virtua~1\py37_x64\lib\site-packages (6.0.0)
Requirement already satisfied: imageio in d:\progra~2\python\virtua~1\py37_x64\lib\site-packages (2.8.0)
Requirement already satisfied: numpy in d:\progra~2\python\virtua~1\py37_x64\lib\site-packages (from imageio) (1.16.2)
import scipy.io
import numpy as np 
import os 
import scipy.misc 
import matplotlib.pyplot as plt 
import tensorflow as tf
import imageio
tf.compat.v1.disable_eager_execution()

AttributeError: module ‘scipy.misc’ has no attribute ‘imread’

  • scipy.misc.imread(path);
  • SciPy1.0.0不赞成使用imread,在1.2中已经弃用;

方案1

import imageio 
content_image=imageio.imread()

imageio.imwrite('imageio:astronaut-gray.jpg', im[:, :, 0])

方案2

from matplotlib.pyplot import imread

方案3

import cv2
im = cv2.imread('astronaut.png') # im will be of type : <class 'numpy.ndarray'>
def _conv_layer(_input, weights, bias):
    conv = tf.nn.conv2d(_input, tf.constant(weights), strides=(1, 1, 1, 1), padding='SAME')
    return tf.nn.bias_add(conv, bias)

def _pool_layer(_input):
    return tf.nn.max_pool(_input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding='SAME')

def preprocess(image, mean_pixel):
    return image - mean_pixel

def unprocess(image, mean_pixel):
    return image + mean_pixel

def imread(path):
    # return scipy.misc.imread(path).astype(np.float)
    return imageio.imread(path).astype(np.float)

def imsave(path, img):
    img = np.clip(img, 0, 255).astype(np.uint8)
    scipy.misc.imsave(path, img)
    
print ("Functions for VGG ready")
Functions for VGG ready
def net(data_path, input_image):
    # 19层结构名
    layers = (
        'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',
        'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
        'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
        'relu3_3', 'conv3_4', 'relu3_4', 'pool3',
        'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
        'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
        'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
        'relu5_3', 'conv5_4', 'relu5_4'
    )
    data = scipy.io.loadmat(data_path)
    mean = data['normalization'][0][0][0]
    mean_pixel = np.mean(mean, axis=(0, 1))
    weights = data['layers'][0]
    net = {}
    current = input_image
    for i, name in enumerate(layers):
        kind = name[:4]
        if kind == 'conv':
            kernels, bias = weights[i][0][0][0][0]
            # matconvnet: weights are [width, height, in_channels, out_channels]
            # tensorflow: weights are [height, width, in_channels, out_channels]
            kernels = np.transpose(kernels, (1, 0, 2, 3))
            bias = bias.reshape(-1)
            current = _conv_layer(current, kernels, bias)
        elif kind == 'relu':
            current = tf.nn.relu(current)
        elif kind == 'pool':
            current = _pool_layer(current)
        net[name] = current
    assert len(net) == len(layers)
    return net, mean_pixel, layers
print ("Network for VGG ready")
Network for VGG ready

Depth of input (32) is not a multiple of input depth of filter (3) for ‘conv1_1/Conv2D’ (op: ‘Conv2D’) with input shapes: [1,3,24,32], [3,3,3,64].

  • tf.nn.conv2d(_input_r, _w[‘wc1’], strides=[1, 1, 1, 1], padding=‘SAME’)
  • IMG_PATH = “/vgg_data/cat.png”
  • IMG_PATH = “/vgg_data/cat.jpg”
  • 注意图片格式:jpg
cwd  = os.getcwd()
VGG_PATH = cwd + "/vgg_data/imagenet-vgg-verydeep-19.mat"
# 注意图片格式:jpg
IMG_PATH = cwd + "/vgg_data/cat.jpg"

# 显示前5个特征图
show_count = 5

input_image = imread(IMG_PATH)
shape = (1,input_image.shape[0],input_image.shape[1],input_image.shape[2]) 

with tf.compat.v1.Session() as sess:
    image = tf.compat.v1.placeholder('float', shape=shape)
    nets, mean_pixel, all_layers = net(VGG_PATH, image)
    
    input_image_pre = np.array([preprocess(input_image, mean_pixel)])
    layers = all_layers # For all layers 
    
    # layers = ('relu2_1', 'relu3_1', 'relu4_1')
    for i, layer in enumerate(layers):
        print ("[%d/%d] %s" % (i+1, len(layers), layer))
        features = nets[layer].eval(feed_dict={image: input_image_pre})
        
        print (" Type of 'features' is ", type(features))
        print (" Shape of 'features' is %s" % (features.shape,))
        
        # Plot response 
        if show_count > 0:
            show_count -= 1
            
            plt.figure(i+1, figsize=(10, 5))
            plt.matshow(features[0, :, :, 0], cmap=plt.cm.gray, fignum=i+1)
            plt.title("" + layer)
            plt.colorbar()
            plt.show()
[1/36] conv1_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 579, 772, 64)

在这里插入图片描述

[2/36] relu1_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 579, 772, 64)

在这里插入图片描述

[3/36] conv1_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 579, 772, 64)

在这里插入图片描述

[4/36] relu1_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 579, 772, 64)

在这里插入图片描述

[5/36] pool1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 290, 386, 64)

在这里插入图片描述

[6/36] conv2_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 290, 386, 128)
[7/36] relu2_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 290, 386, 128)
[8/36] conv2_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 290, 386, 128)
[9/36] relu2_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 290, 386, 128)
[10/36] pool2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 128)
[11/36] conv3_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[12/36] relu3_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[13/36] conv3_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[14/36] relu3_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[15/36] conv3_3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[16/36] relu3_3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[17/36] conv3_4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[18/36] relu3_4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 145, 193, 256)
[19/36] pool3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 256)
[20/36] conv4_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[21/36] relu4_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[22/36] conv4_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[23/36] relu4_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[24/36] conv4_3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[25/36] relu4_3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[26/36] conv4_4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[27/36] relu4_4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 73, 97, 512)
[28/36] pool4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[29/36] conv5_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[30/36] relu5_1
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[31/36] conv5_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[32/36] relu5_2
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[33/36] conv5_3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[34/36] relu5_3
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[35/36] conv5_4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
[36/36] relu5_4
 Type of 'features' is  <class 'numpy.ndarray'>
 Shape of 'features' is (1, 37, 49, 512)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
下面是使用TensorFlow搭建VGG16模型的示例代码: ```python import tensorflow as tf def vgg16(input_tensor, num_classes): # 定义卷积层和池化层的函数,方便重复使用 def conv_block(inputs, filters, kernel_size, strides=(1,1), padding='same', activation=tf.nn.relu): conv = tf.layers.conv2d(inputs, filters, kernel_size, strides, padding, activation=activation) return tf.layers.batch_normalization(conv) def max_pool(inputs, pool_size=(2,2), strides=(2,2), padding='same'): return tf.layers.max_pooling2d(inputs, pool_size, strides, padding) # 定义VGG16的卷积层和池化层结构 net = conv_block(input_tensor, 64, (3,3)) net = conv_block(net, 64, (3,3)) net = max_pool(net) net = conv_block(net, 128, (3,3)) net = conv_block(net, 128, (3,3)) net = max_pool(net) net = conv_block(net, 256, (3,3)) net = conv_block(net, 256, (3,3)) net = conv_block(net, 256, (3,3)) net = max_pool(net) net = conv_block(net, 512, (3,3)) net = conv_block(net, 512, (3,3)) net = conv_block(net, 512, (3,3)) net = max_pool(net) net = conv_block(net, 512, (3,3)) net = conv_block(net, 512, (3,3)) net = conv_block(net, 512, (3,3)) net = max_pool(net) # 定义全连接层结构 net = tf.layers.flatten(net) net = tf.layers.dense(net, 4096, activation=tf.nn.relu) net = tf.layers.dropout(net, rate=0.5) net = tf.layers.dense(net, 4096, activation=tf.nn.relu) net = tf.layers.dropout(net, rate=0.5) net = tf.layers.dense(net, num_classes) return net ``` 在上面的代码中,我们使用了TensorFlow中的tf.layers模块,这个模块可以方便地创建各种神经网络层,包括卷积层、池化层、全连接层等。 VGG16模型的具体结构可以参考下图: ![VGG16结构图](https://miro.medium.com/max/2000/1*7S266GKv9KsK1Tehn3Km7g.png) 上面的代码实现了VGG16模型的卷积层和全连接层结构。你可以通过调用这个函数来创建一个VGG16模型,并且可以通过传入不同的输入张量和输出类别数来创建不同的模型

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值