执行环境,可能出现
AttributeError: module 'tensorflow.python.ops.image_ops' has no attribute 'per_image_whitening'
需要将cifar10_input.py
的182行改为per_image_whitening 改为 per_image_standardization
cifar10_input.py
和cifar10.py
下载地址
#coding=utf-8
import cifar10,cifar10_input
from matplotlib import pyplot as plt
import tensorflow as tf
import numpy as np
import time
import math
max_steps = 4500
batch_size = 128
data_dir = './cifar10_data/cifar-10-batches-bin'
#下载好的数据集所在的文件夹
def variable_with_weight_loss(shape, stddev, wl):
var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
if wl is not None:
weight_loss = tf.multiply(tf.nn.l2_loss(var), wl, name='weight_loss')
## wl 控制l2损失的比重
tf.add_to_collection('losses', weight_loss)
## 参数的l2损失加入到损失集合中
return var
def loss(logits, labels):
labels = tf.cast(labels, tf.int64)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=labels, name='cross_entropy_per_example')
# logits为[batch_size,num_classes]
# labels为[batch_size,]的一维向量,其中每一个元素代表对应样本的类别
# 先对网络的输出 Logits 进行 Softmax 概率化
# Cross-Entropy 每个样本的交叉熵(损失)
cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
# 一个 batch 内样本求平均损失
tf.add_to_collection('losses', cross_entropy_mean)
return tf.add_n(tf.get_collection('losses'), name='total_loss')
# get 损失集合中所有损失,并相加后返回损失总和
# cifar10.maybe_download_and_extract()
# 如果没有下载,则需要将该行注释取消,
# 当然检查到 data_dir 目录下已经下载好的,则自动取消下载
images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir,
batch_size=batch_size)
images_test, labels_test = cifar10_input.inputs(eval_data=True,
data_dir=data_dir,
batch_size=batch_size)
#images_train, labels_train = cifar10.distorted_inputs()
#images_test, labels_test = cifar10.inputs(eval_data=True)
image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
label_holder = tf.placeholder(tf.int32, [batch_size])
#原始图像是32*32*3,distorted_inputs函数随机裁剪,旋转成24*24*3的尺寸
#inputs用于测试集,只在正中间裁剪成24*24*3的尺寸
#logits = inference(image_holder)
#############################第一层卷积层###################################
weight1 = variable_with_weight_loss(shape=[5, 5, 3, 64], stddev=5e-2, wl=0.0)
kernel1 = tf.nn.conv2d(image_holder, weight1, [1, 1, 1, 1], padding='SAME')
bias1 = tf.Variable(tf.constant(0.0, shape=[64]))
conv1 = tf