最近研究了一下如何用tensorflow搭建alexnet并进行测试,使用tensorboard查看训练过程,我没有使用tensorflow自带的minist数据集,而是通过加载keras的minist数据集得到numpy array类型的数据,再自己处理之后“喂”给网络的,使用了tqdm显示训练进度条。我把整个过程写了一个完整的代码供大家参考。
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 3 15:10:40 2018
@author: leex
"""
import tensorflow as tf
#from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
#from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
from keras.datasets import mnist
from keras.utils import to_categorical
import os
import cv2
import time
from tqdm import tqdm
import random
os.environ['CUDA_VISIBLE_DEVICES']='0'
log_dir = '/opt/Data/lixiang/alexnet/log'
model_path = '/opt/Data/lixiang/alexnet/model'
model_id = np.int64(time.strftime('%Y%m%d%H%M', time.localtime(time.time())))
#n_input = 784
n_output = 10
lr = 0.00001
dropout_rate = 0.75
epochs = 20
test_step = 10
batch_size = 32
image_size = 224
def load_mnist(image_size):
(x_train,y_train),(x_test,y_test) = mnist.load_data()
train_image = [cv2.cvtColor(cv2.resize(img,(image_size,image_size)),cv2.COLOR_GRAY2BGR) for img in x_train]
test_image = [cv2.cvtColor(cv2.resize(img,(image_size,image_size)),cv2.COLOR_GRAY2BGR) for img in x_test]
train_image = np.asarray(train_image)
test_image = np.asarray(test_image)
train_label = to_categorical(y_train)
test_label = to_categorical(y_test)
print('finish loading data!')
return train_image, train_label, test_image, test_label
def get_batch(image, label, batch_size, now_batch, total_batch):
if now_batch < total_batch-1:
image_batch = image[now_batch*batch_size:(now_batch+1)*batch_size]
label_batch = label[now_batch*batch_size:(now_batch+1)*batch_size]
else:
image_batch = image[now_batch*batch_size:]
label_batch = label[now_batch*batch_size:]
# image_batch = tf.convert_to_tensor(image_batch)
# label_batch = tf.convert_to_tensor(label_batch)
return image_batch, label_batch
def shuffle_set(train_image, train_label, test_image, test_label):
train_row = range(len(train_label))
random.shuffle(train_row)
train_image = train_image[train_row]
train_label = train_label[train_row]
test_row = range(len(test_label))
random.shuffle(test_row)
test_image = test_image[test_row]
test_label = test_label[test_row]
return train_image, train_label, test_image, test_label
def print_layer(layer):
print(layer.op.name + ':' + str(layer.get_shape().as_list()))
# define layers
def conv(x, kernel, strides, b):
return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, kernel, strides, padding = 'SAME'), b))
def max_pooling(x, kernel, strides):
return tf.nn.max_pool(x, kernel, strides, padding = 'VALID')
def fc(x, w, b):
return tf.nn.relu(tf.add(tf.matmul(