利用Caffe做回归 预测随机噪声的频率
2018-08-08 16:59
211 查看
1. 生成70000个样本:随机噪声
[code]import os import sys import datetime import cv2 from multiprocessing import Process, cpu_count import numpy as np import matplotlib.pyplot as plt H_IMG, W_IMG = 100, 100 SAMPLES_SIZE = 70000 SAMPLES_DIR = 'samples' def make_noise(index): h = np.random.randint(1, H_IMG) w = np.random.randint(1, W_IMG) noise = np.random.random((h, w)) noisy_img = cv2.resize(noise, (H_IMG, W_IMG), interpolation= cv2.INTER_CUBIC) fx = float(w) / float(W_IMG) fy = float(h) / float(H_IMG) filename = '{}/{:0>5d}_{}_{}.jpg'.format(SAMPLES_DIR, index, fx, fy) plt.imsave(filename, noisy_img, cmap='gray') def make_noises(i0, i1): np.random.seed(datetime.datetime.now().microsecond) for i in xrange(i0, i1): make_noise(i) print ('noises from {} to {} are made!'.format(i0+1, i1)) sys.stdout.flush() def main(): cmd = 'mkdir -p {}'.format(SAMPLES_DIR) os.system(cmd) n_procs = cpu_count() print ('making noises with {} processes ...'.format(n_procs)) length = float(SAMPLES_SIZE)/float(n_procs) indices = [int(round(i * length)) for i in range(n_procs + 1)] processes = [Process(target=make_noises, args=(indices[i], indices[i+1])) for i in range(n_procs)] for p in processes: p.start() for p in processes: p.join() print ('done!') if __name__ == '__main__': main()
2.制作多标签HFD5数据
[code]import os filename2score = lambda x: x[:x.rfind('.')].split('_')[-2:] filenames = os.listdir('samples') with open('tain.txt', 'w') as f_train_txt: for filename in filenames[:50000]: fx, fy = filename2score(filename) line = 'samples/{} {} {}\n'.format(filename, fx, fy) f_train_txt.write(line) with open('val.txt', 'w') as f_val_txt: for filename in filenames[50000:60000]: fx, fy = filename2score(filename) line = 'samples/{} {} {}\n'.format(filename, fx, fy) f_val_txt.write(line) with open('test.txt', 'w') as f_test_txt: for filename in filenames[60000:]: fx, fy = filename2score(filename) line = 'samples/{}\n'.format(filename) f_test_txt.write(line)
3.产生相应的hdf5文件
[code]import sys import numpy as np import matplotlib.pyplot as plt import h5py IMAGE_SIZE = (100, 100) MEAN_VALUE = 128 filename = sys.argv[1] setname, ext = filename.split('.') with open(filename, 'r') as f: lines = f.readlines() np.random.shuffle(lines) sample_size = len(lines) imgs = np.zeros((sample_size, 1,) + IMAGE_SIZE, dtype=np.float32) freqs= np.zeros((sample_size, 2), dtype=np.float32) h5_filename = '{}.h5'.format(setname) with h5py.File(h5_filename, 'w') as h: for i, line in enumerate(lines): image_name, fx, fy = line[:-1].split() img = plt.imread(image_name)[:, :, 0].astype(np.float32) img = img.reshape((1,) + img.shape) img -= MEAN_VALUE imgs[i] = img freqs[i] = [float(fx), float(fy)] if (i+1) % 1000 == 0: print ('processed {} images!'.format(i+1)) h.create_dataset('data', data=imgs) h.create_dataset('freq', data=freqs) with open('{}_h5.txt'.format(setname), 'w') as f: f.write(h5_filename)
4.训练网络
[code]name: "RegressionExample" layer { name: "data" type: "HDF5Data" top: "data" top: "freq" include { phase: TRAIN } hdf5_data_param { source: "train_h5.txt" batch_size: 50 } } layer { name: "data" type: "HDF5Data" top: "data" top: "freq" include { phase: TEST } hdf5_data_param { source: "val_h5.txt" batch_size: 50 } } layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } convolution_param { num_output: 96 kernel_size: 5 stride: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu1" type: "ReLU" bottom: "conv1" top: "conv1" } layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "conv2" type: "Convolution" bottom: "pool1" top: "conv2" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } convolution_param { num_output: 96 pad: 2 kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu2" type: "ReLU" bottom: "conv2" top: "conv2" } layer { name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "conv3" type: "Convolution" bottom: "pool2" top: "conv3" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } convolution_param { num_output: 128 pad: 1 kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu3" type: "ReLU" bottom: "conv3" top: "conv3" } layer { name: "pool3" type: "Pooling" bottom: "conv3" top: "pool3" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "fc4" type: "InnerProduct" bottom: "pool3" top: "fc4" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } inner_product_param { num_output: 192 weight_filler { type: "gaussian" std: 0.005 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu4" type: "ReLU" bottom: "fc4" top: "fc4" } layer { name: "drop4" type: "Dropout" bottom: "fc4" top: "fc4" dropout_param { dropout_ratio: 0.35 } } layer { name: "fc5" type: "InnerProduct" bottom: "fc4" top: "fc5" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } inner_product_param { num_output: 2 weight_filler { type: "gaussian" std: 0.005 } bias_filler { type: "constant" value: 0 } } } layer { name: "sigmoid5" type: "Sigmoid" bottom: "fc5" top: "pred" } layer { name: "loss" type: "EuclideanLoss" bottom: "pred" bottom: "freq" top: "loss" }
[code]net: "./train_val.prototxt" test_iter: 200 test_interval: 1000 base_lr: 0.01 lr_policy: "step" gamma: 0.707 stepsize: 2000 display: 100 max_iter: 10000 momentum: 0.9 weight_decay: 0.00001 snapshot_prefix: "./freq_regression" solver_mode: GPU type: "Nesterov"
运行下面的命令训练模型
[code]> /path/to/caffe/build/tools/caffe train -solver solver.prototxt
5.批量装载图片并预测
[code]name: "RegressionExample" layer { name: "data" type: "Input" top: "data" input_param { shape: { dim: 100 dim: 1 dim: 100 dim: 100 } } } layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } convolution_param { num_output: 96 kernel_size: 5 stride: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu1" type: "ReLU" bottom: "conv1" top: "conv1" } layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "conv2" type: "Convolution" bottom: "pool1" top: "conv2" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } convolution_param { num_output: 96 pad: 2 kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu2" type: "ReLU" bottom: "conv2" top: "conv2" } layer { name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "conv3" type: "Convolution" bottom: "pool2" top: "conv3" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } convolution_param { num_output: 128 pad: 1 kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu3" type: "ReLU" 1e92c bottom: "conv3" top: "conv3" } layer { name: "pool3" type: "Pooling" bottom: "conv3" top: "pool3" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "fc4" type: "InnerProduct" bottom: "pool3" top: "fc4" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } inner_product_param { num_output: 192 weight_filler { type: "gaussian" std: 0.005 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu4" type: "ReLU" bottom: "fc4" top: "fc4" } layer { name: "drop4" type: "Dropout" bottom: "fc4" top: "fc4" dropout_param { dropout_ratio: 0.35 } } layer { name: "fc5" type: "InnerProduct" bottom: "fc4" top: "fc5" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 1 decay_mult: 0 } inner_product_param { num_output: 2 weight_filler { type: "gaussian" std: 0.005 } bias_filler { type: "constant" value: 0 } } } layer { name: "sigmoid5" type: "Sigmoid" bottom: "fc5" top: "pred" }
[code]import sys import numpy as np sys.path.append('/path/to/caffe/python') import caffe WEIGHTS_FILE = 'freq_regression_iter_10000.caffemodel' DEPLOY_FILE = 'deploy.prototxt' MEAN_VALUE = 128 #caffe.set_mode_cpu() net = caffe.Net(DEPLOY_FILE, WEIGHTS_FILE, caffe.TEST) transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2,0,1)) transformer.set_mean('data', np.array([MEAN_VALUE])) transformer.set_raw_scale('data', 255) image_list = sys.argv[1] batch_size = net.blobs['data'].data.shape[0] with open(image_list, 'r') as f: i = 0 filenames = [] for line in f.readlines(): filename = line[:-1] filenames.append(filename) image = caffe.io.load_image(filename, False) transformed_image = transformer.preprocess('data', image) net.blobs['data'].data[i, ...] = transformed_image i += 1 if i == batch_size: output = net.forward() freqs = output['pred'] for filename, (fx, fy) in zip(filenames, freqs): print('Predicted frequencies for {} is {:.2f} and {:.2f}'.format(filename, fx, fy)) i = 0 filenames = []
[code]> python predict.py test.txt
6.测试结果
阅读更多
相关文章推荐
- Caffe 用于解决预测(回归)问题
- 使用随机逻辑回归进行特征筛选,并利用筛选后的特征建立逻辑回归模型
- caffe下py-faster-rcnn利用vgg16训练及预测
- 【自然语言处理入门】03:利用线性回归对数据集进行分析预测(上)
- 利用caffemodel进行预测
- 利用spark的随机森林做票房预测
- 《机器学习实战》笔记(第二部分 利用回归预测数值型数据)
- 利用K近邻(回归)KNeighborsRegressor进行回归预测(复习9)
- 利用随机噪声生成纹理 Texture Generation using Random Noise A D V E R T I S E M E N T Introduction In n
- TF之NN之回归预测:利用NN算法(RelU)实现根据三个自变量预测一个因变量的回归问题
- 利用Caffe做回归(regression)
- 利用Caffe做回归(regression)
- R语言利用逻辑回归模型预测用户活跃
- 利用回归树DecisionTreeRegressor进行回归预测(复习10)
- 利用随机森林,xgboost,logistic回归,预测泰坦尼克号上面的乘客的获救概率
- caffe 利用Python API做分类预测,以及特征的可视化
- Caffe - 利用classify.py完成分类任务,输出预测类别和各类概率
- 利用线性回归器LinearRegression/SGDRegressor进行回归预测(复习7)
- 机器学习实战------利用logistics回归预测病马死亡率
- 【自然语言处理入门】03:利用线性回归对数据集进行分析预测(下)