3.tensorboard --host=*** --logdir=文件目录
1.save models
model_dir = os.path.join(run_dir, 'model') if not os.path.exists(model_dir): os.mkdir(model_dir) saver = tf.train.Saver() ... if (i+1) % output_model_every_steps == 0: saver.save(sess, os.path.join(model_dir, 'ckp-{}'.format(i+1))) print('model saved to ckp-{}'.format(i+1))2.restore models
model_name = 'ckp-1000' model_path = os.path.join(model_dir, model_name) .... if os.path.exists(model_path + '.index'): saver.restore(sess, model_path) print('model restore from {}'.format(model_path)) else: print('model {} does not exist'.format(model_path)) activation:relu, sigmoid,tanhweight initializer: he,xavier,normaloptimzier:Adam,Momentum,Gradient Descent基本库
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from matplotlib.pyplot import imshowresize api
tf.image.resize_area tf.image.resize_bicubic # 图像缩放,双线性算法插入 tf.image.resize_nearest_neighbor # 图像放大,使用相近的像素点插入放大的像素点 # 图片放大 img_string = tf.read_file(picture1) # img_string2 = tf.read_file(picture2) img_decoded = tf.image.decode_image(img_string) # sess多图片,使用四维矩阵,[index, 宽, 长, 通道数] # resize img_decoded = tf.reshape(img_decoded, [1, 1200, 1400, 3]) # 缩小 resize_img = tf.image.resize_bicubic(img_decoded, [2400, 2800]) sess = tf.Session() img_decoded_val = sess.run(resize_img) img_decoded_val = img_decoded_val.reshape((2400, 2800, 3)) img_decoded_val = np.asarray(img_decoded_val, np.uint8) print(img_decoded_val.shape) imshow(img_decoded_val) # imshow(img_decoded_val2) pylab.show() # crop 裁剪 tf.image.pad_to_bounding_box() tf.image.crop_to_bounding_box() tf.random_crop() img_decoded = tf.image.decode_image(img_string) img_decoded = tf.reshape(img_decoded, [1, 1200, 1400, 3]) # crop padded_img = tf.image.pad_to_bounding_box(img_decoded, 100, 200, 1500, 1600) # arg:[0:2]画布中的位置,[2:4]画布大小 sess = tf.Session() img_decoded_val = sess.run(padded_img) img_decoded_val = img_decoded_val.reshape((1500, 1600, 3)) img_decoded_val = np.asarray(img_decoded_val, np.uint8) print(img_decoded_val.shape) imshow(img_decoded_val) # imshow(img_decoded_val2) pylab.show()tf.image.adjust_brightness() tf.image.random_brightness() tf.image.adjust_contrast() tf.image.random_contrast()
ex: padded_img = tf.image.adjust_brightness(img_decoded, -0.5) # -0.5 为光强减少50%
train 100K :85.6%
def conv_wrapper(inputs, name, is_training, output_channel=32, kernel_size=(3,3), activation=tf.nn.relu, padding = 'same'): """wrapper of tf.laysers.conv2d""" # without bn: conv -> activation # with batch normalization: conv -> bn -> activation with tf.name_scope(name): conv2d = tf.layers.conv2d(inputs, output_channel, kernel_size, padding= padding, activation= None, name= name+'/conv2d') bn = tf.layers.batch_normalization(conv2d, training= is_training) return activation(bn) def pooling_warpper(inputs, name): return tf.layers.max_pooling2d(inputs, (2,2), (2,2), name=name) # conv1: 神经元图, feature_map, 输出图像 conv1_1 = conv_wrapper(normal_data_aug_3, name = 'conv1_1', is_training=True) conv1_2 = conv_wrapper(conv1_1, name = 'conv1_2', is_training=True) conv1_3 = conv_wrapper(conv1_2, name = 'conv1_3', is_training=True) pooling1 = pooling_warpper(conv1_3, name = 'pool1') conv2_1 = conv_wrapper(pooling1, name = 'conv2_1', is_training=True) conv2_2 = conv_wrapper(conv2_1, name = 'conv2_2', is_training=True) conv2_3 = conv_wrapper(conv2_2, name = 'conv2_3', is_training=True) pooling2 = pooling_warpper(conv2_3, name = 'pool2') conv3_1 = conv_wrapper(normal_data_aug_3, name = 'conv3_1', is_training=True) conv3_2 = conv_wrapper(conv3_1, name = 'conv3_2', is_training=True) conv3_3 = conv_wrapper(conv3_2, name = 'conv3_3', is_training=True) pooling3 = pooling_warpper(conv3_3, name = 'pool1')使用resnet train acc 67% after fine-tune -> 94%