tensorflow 1.0用CNN进行图像分类

作者:denny402 时间:2022-08-17 17:32:29 

tensorflow升级到1.0之后,增加了一些高级模块: 如tf.layers, tf.metrics, 和tf.losses,使得代码稍微有些简化。

任务:花卉分类

版本:tensorflow 1.0

数据:flower-photos

花总共有五类,分别放在5个文件夹下。

闲话不多说,直接上代码,希望大家能看懂:)

复制代码


# -*- coding: utf-8 -*-

from skimage import io,transform
import glob
import os
import tensorflow as tf
import numpy as np
import time

path='e:/flower/'

#将所有的图片resize成100*100
w=100
h=100
c=3

#读取图片
def read_img(path):
cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
imgs=[]
labels=[]
for idx,folder in enumerate(cate):
 for im in glob.glob(folder+'/*.jpg'):
  print('reading the images:%s'%(im))
  img=io.imread(im)
  img=transform.resize(img,(w,h))
  imgs.append(img)
  labels.append(idx)
return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
data,label=read_img(path)

#打乱顺序
num_example=data.shape[0]
arr=np.arange(num_example)
np.random.shuffle(arr)
data=data[arr]
label=label[arr]

#将所有数据分为训练集和验证集
ratio=0.8
s=np.int(num_example*ratio)
x_train=data[:s]
y_train=label[:s]
x_val=data[s:]
y_val=label[s:]

#-----------------构建网络----------------------
#占位符
x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')

#第一个卷积层(100——>50)
conv1=tf.layers.conv2d(
 inputs=x,
 filters=32,
 kernel_size=[5, 5],
 padding="same",
 activation=tf.nn.relu,
 kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool1=tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

#第二个卷积层(50->25)
conv2=tf.layers.conv2d(
 inputs=pool1,
 filters=64,
 kernel_size=[5, 5],
 padding="same",
 activation=tf.nn.relu,
 kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool2=tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

#第三个卷积层(25->12)
conv3=tf.layers.conv2d(
 inputs=pool2,
 filters=128,
 kernel_size=[3, 3],
 padding="same",
 activation=tf.nn.relu,
 kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool3=tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)

#第四个卷积层(12->6)
conv4=tf.layers.conv2d(
 inputs=pool3,
 filters=128,
 kernel_size=[3, 3],
 padding="same",
 activation=tf.nn.relu,
 kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool4=tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)

re1 = tf.reshape(pool4, [-1, 6 * 6 * 128])

#全连接层
dense1 = tf.layers.dense(inputs=re1,
     units=1024,
     activation=tf.nn.relu,
     kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
     kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003))
dense2= tf.layers.dense(inputs=dense1,
     units=512,
     activation=tf.nn.relu,
     kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
     kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003))
logits= tf.layers.dense(inputs=dense2,
     units=5,
     activation=None,
     kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
     kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003))
#---------------------------网络结束---------------------------

loss=tf.losses.sparse_softmax_cross_entropy(labels=y_,logits=logits)
train_op=tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_)
acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#定义一个函数,按批次取数据
def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
assert len(inputs) == len(targets)
if shuffle:
 indices = np.arange(len(inputs))
 np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
 if shuffle:
  excerpt = indices[start_idx:start_idx + batch_size]
 else:
  excerpt = slice(start_idx, start_idx + batch_size)
 yield inputs[excerpt], targets[excerpt]

#训练和测试数据,可将n_epoch设置更大一些

n_epoch=10
batch_size=64
sess=tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for epoch in range(n_epoch):
start_time = time.time()

#training
train_loss, train_acc, n_batch = 0, 0, 0
for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True):
 _,err,ac=sess.run([train_op,loss,acc], feed_dict={x: x_train_a, y_: y_train_a})
 train_loss += err; train_acc += ac; n_batch += 1
print(" train loss: %f" % (train_loss/ n_batch))
print(" train acc: %f" % (train_acc/ n_batch))

#validation
val_loss, val_acc, n_batch = 0, 0, 0
for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False):
 err, ac = sess.run([loss,acc], feed_dict={x: x_val_a, y_: y_val_a})
 val_loss += err; val_acc += ac; n_batch += 1
print(" validation loss: %f" % (val_loss/ n_batch))
print(" validation acc: %f" % (val_acc/ n_batch))

sess.close()

来源:http://www.cnblogs.com/denny402/p/6931338.html

标签:tensorflow,CNN,图像分类
0
投稿

猜你喜欢

  • js神秘的电报密码 哈弗曼编码实现

    2024-04-16 09:13:58
  • Python 编程速成(推荐)

    2021-04-03 04:30:10
  • 超简单的Python HTTP服务

    2023-01-22 06:19:15
  • Python依赖管理及打包工具Poetry使用规范

    2021-06-02 13:21:43
  • python 实现在tkinter中动态显示label图片的方法

    2022-07-17 11:10:15
  • MySQL慢查询日志的作用和开启

    2024-01-21 20:23:33
  • 纯JavaScript 实现flappy bird小游戏实例代码

    2024-05-11 09:05:57
  • javascript实现九宫格相加数值相等

    2024-04-17 10:32:53
  • python实现整数序列求和

    2023-12-14 06:53:10
  • php中json 序列化为 [] 的弊端

    2023-05-25 00:14:30
  • python实现的登录与提交表单数据功能示例

    2021-04-28 21:16:38
  • Python基于yield遍历多个可迭代对象

    2023-01-05 23:01:32
  • Python计算多幅图像栅格值的平均值

    2021-03-28 01:41:05
  • Pytorch 数据加载与数据预处理方式

    2021-06-12 11:07:05
  • 解决Python pandas df 写入excel 出现的问题

    2021-07-27 15:37:21
  • asp 分页函数,可以显示 1,2,3,4,5... 前十页,后十页,下一页,上一页

    2009-07-05 18:34:00
  • python Web应用程序测试selenium库使用用法详解

    2023-10-05 22:15:00
  • 使用Python为中秋节绘制一块美味的月饼

    2023-06-30 17:36:10
  • MS-SQL Server 中单引号的两种处理方法

    2024-01-24 11:17:13
  • Laravel框架实现发送短信验证功能代码

    2024-05-03 15:29:08
  • asp之家 网络编程 m.aspxhome.com