Tensorflow代码学习-8-3下载google图像识别网络inception-v3并查看结构

下载google图像识别网络inception-v3并查看结构

import tensorflow as tf
import os
import tarfile
import requests
# inception-v3 是googlenet的第三个版本
#inception模型下载地址
inception_pretrain_model_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
#这里采用手动下载后直接放入下述模型存放地址中

#模型存放地址,
inception_pretrain_model_dir = "inception_model"   #此文件夹如果不存在会自动创建
if not os.path.exists(inception_pretrain_model_dir):
    os.makedirs(inception_pretrain_model_dir)
    
#获取文件名,以及文件路径
filename = inception_pretrain_
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Inception-v4 是 Google 在 2016 年提出的一种深度卷积神经网络模型,是 Inception 系列模型的最新版本,其网络结构更深,性能更优。下面是使用 TensorFlow 搭建 Inception-v4 图像识别模型的示例代码: ```python import tensorflow as tf import numpy as np def conv2d(inputs, filters, kernel_size, strides=1, padding='same', activation=tf.nn.relu, name=None): ''' 定义卷积层函数 inputs: 输入张量 filters: 卷积核个数 kernel_size: 卷积核大小 strides: 步长 padding: 边界填充方式 activation: 激活函数 name: 层的名称 ''' return tf.layers.conv2d(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, activation=activation, name=name) def inception_v4(inputs, num_classes, is_training=True): ''' 定义 Inception-v4 模型 inputs: 输入张量 num_classes: 分类数 is_training: 是否训练 ''' # Stem net = conv2d(inputs, 32, 3, strides=2, padding='valid', name='Conv2d_1a_3x3') net = conv2d(net, 32, 3, padding='valid', name='Conv2d_2a_3x3') net = conv2d(net, 64, 3, name='Conv2d_2b_3x3') net = tf.layers.max_pooling2d(net, pool_size=3, strides=2, padding='valid', name='MaxPool_3a_3x3') net = conv2d(net, 80, 1, padding='valid', name='Conv2d_3b_1x1') net = conv2d(net, 192, 3, padding='valid', name='Conv2d_4a_3x3') net = tf.layers.max_pooling2d(net, pool_size=3, strides=2, padding='valid', name='MaxPool_5a_3x3') # Inception-A net = inception_a(net, 96, name='Mixed_5b') net = inception_a(net, 96, name='Mixed_5c') net = inception_a(net, 96, name='Mixed_5d') # Reduction-A net = reduction_a(net, name="Mixed_6a") # Inception-B net = inception_b(net, 128, name='Mixed_6b') net = inception_b(net, 160, name='Mixed_6c') net = inception_b(net, 160, name='Mixed_6d') net = inception_b(net, 192, name='Mixed_6e') # Reduction-B net = reduction_b(net, name="Mixed_7a") # Inception-C net = inception_c(net, 192, name='Mixed_7b') net = inception_c(net, 224, name='Mixed_7c') # Average Pooling net = tf.layers.average_pooling2d(net, pool_size=8, strides=1, padding='valid', name='AvgPool_1a_8x8') # Dropout net = tf.layers.dropout(net, rate=0.2, training=is_training, name='Dropout_1b') # Flatten net = tf.layers.flatten(net, name='Flatten_1c') # Output logits = tf.layers.dense(net, num_classes, name='Logits') return logits def inception_a(inputs, filters, name=None): ''' 定义 Inception-A 模块 inputs: 输入张量 filters: 卷积核个数 name: 模块名称 ''' with tf.variable_scope(name): with tf.variable_scope('Branch_0'): branch_0 = conv2d(inputs, filters, 1, name='Conv2d_0a_1x1') with tf.variable_scope('Branch_1'): branch_1 = conv2d(inputs, filters, 1, name='Conv2d_1a_1x1') branch_1 = conv2d(branch_1, filters, 3, name='Conv2d_1b_3x3') with tf.variable_scope('Branch_2'): branch_2 = conv2d(inputs, filters, 1, name='Conv2d_2a_1x1') branch_2 = conv2d(branch_2, filters, 3, name='Conv2d_2b_3x3') branch_2 = conv2d(branch_2, filters, 3, name='Conv2d_2c_3x3') with tf.variable_scope('Branch_3'): branch_3 = tf.layers.average_pooling2d(inputs, pool_size=3, strides=1, padding='same', name='AvgPool_3a_3x3') branch_3 = conv2d(branch_3, filters, 1, name='Conv2d_3b_1x1') output = tf.concat([branch_0, branch_1, branch_2, branch_3], axis=-1, name='Concatenate') return output def reduction_a(inputs, name=None): ''' 定义 Reduction-A 模块 inputs: 输入张量 name: 模块名称 ''' with tf.variable_scope(name): with tf.variable_scope('Branch_0'): branch_0 = conv2d(inputs, 384, 3, strides=2, padding='valid', name='Conv2d_0a_3x3') with tf.variable_scope('Branch_1'): branch_1 = conv2d(inputs, 192, 1, name='Conv2d_1a_1x1') branch_1 = conv2d(branch_1, 224, 3, name='Conv2d_1b_3x3') branch_1 = conv2d(branch_1, 256, 3, strides=2, padding='valid', name='Conv2d_1c_3x3') with tf.variable_scope('Branch_2'): branch_2 = tf.layers.max_pooling2d(inputs, pool_size=3, strides=2, padding='valid', name='MaxPool_2a_3x3') output = tf.concat([branch_0, branch_1, branch_2], axis=-1, name='Concatenate') return output def inception_b(inputs, filters, name=None): ''' 定义 Inception-B 模块 inputs: 输入张量 filters: 卷积核个数 name: 模块名称 ''' with tf.variable_scope(name): with tf.variable_scope('Branch_0'): branch_0 = conv2d(inputs, filters, 1, name='Conv2d_0a_1x1') with tf.variable_scope('Branch_1'): branch_1 = conv2d(inputs, filters, 1, name='Conv2d_1a_1x1') branch_1 = conv2d(branch_1, filters, [1, 7], name='Conv2d_1b_1x7') branch_1 = conv2d(branch_1, filters, [7, 1], name='Conv2d_1c_7x1') with tf.variable_scope('Branch_2'): branch_2 = conv2d(inputs, filters, 1, name='Conv2d_2a_1x1') branch_2 = conv2d(branch_2, filters, [7, 1], name='Conv2d_2b_7x1') branch_2 = conv2d(branch_2, filters, [1, 7], name='Conv2d_2c_1x7') branch_2 = conv2d(branch_2, filters, [7, 1], name='Conv2d_2d_7x1') branch_2 = conv2d(branch_2, filters, [1, 7], name='Conv2d_2e_1x7') with tf.variable_scope('Branch_3'): branch_3 = tf.layers.average_pooling2d(inputs, pool_size=3, strides=1, padding='same', name='AvgPool_3a_3x3') branch_3 = conv2d(branch_3, filters, 1, name='Conv2d_3b_1x1') output = tf.concat([branch_0, branch_1, branch_2, branch_3], axis=-1, name='Concatenate') return output def reduction_b(inputs, name=None): ''' 定义 Reduction-B 模块 inputs: 输入张量 name: 模块名称 ''' with tf.variable_scope(name): with tf.variable_scope('Branch_0'): branch_0 = conv2d(inputs, 192, 1, name='Conv2d_0a_1x1') branch_0 = conv2d(branch_0, 192, 3, strides=2, padding='valid', name='Conv2d_0b_3x3') with tf.variable_scope('Branch_1'): branch_1 = conv2d(inputs, 256, 1, name='Conv2d_1a_1x1') branch_1 = conv2d(branch_1, 256, [1, 7], name='Conv2d_1b_1x7') branch_1 = conv2d(branch_1, 320, [7, 1], name='Conv2d_1c_7x1') branch_1 = conv2d(branch_1, 320, 3, strides=2, padding='valid', name='Conv2d_1d_3x3') with tf.variable_scope('Branch_2'): branch_2 = tf.layers.max_pooling2d(inputs, pool_size=3, strides=2, padding='valid', name='MaxPool_2a_3x3') output = tf.concat([branch_0, branch_1, branch_2], axis=-1, name='Concatenate') return output ``` 以上就是使用 TensorFlow 搭建 Inception-v4 图像识别模型的示例代码,其中包含了模型的定义以及各个模块的实现。你可以根据自己的需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值