From 98204e0af33d8891992f07429b826ad55d4bb87f Mon Sep 17 00:00:00 2001 From: s Date: 2017年4月16日 23:24:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=97=A0=E8=81=8A=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-3/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-3/run.py b/1-3/run.py index 3dbda68..0a7d92d 100644 --- a/1-3/run.py +++ b/1-3/run.py @@ -2,7 +2,7 @@ # 为了 Python3 的兼容,如果你用的 Python2.7 from __future__ import print_function, division import tensorflow as tf - +#### print('Loaded TF version', tf.__version__, '\n\n') # Tensor 在数学中是"张量" From 8b3ed2a8b932176b365d65fd015d7bec8d674efc Mon Sep 17 00:00:00 2001 From: s Date: 2017年4月16日 23:34:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=97=A0=E8=81=8A=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-3/run.py | 2 +- 4-6/load.py | 116 ---------------------------------------------------- 4-6/run.py | 2 +- 3 files changed, 2 insertions(+), 118 deletions(-) delete mode 100644 4-6/load.py diff --git a/1-3/run.py b/1-3/run.py index 0a7d92d..3dbda68 100644 --- a/1-3/run.py +++ b/1-3/run.py @@ -2,7 +2,7 @@ # 为了 Python3 的兼容,如果你用的 Python2.7 from __future__ import print_function, division import tensorflow as tf -#### + print('Loaded TF version', tf.__version__, '\n\n') # Tensor 在数学中是"张量" diff --git a/4-6/load.py b/4-6/load.py deleted file mode 100644 index 925a2d0..0000000 --- a/4-6/load.py +++ /dev/null @@ -1,116 +0,0 @@ -# encoding:utf-8 -# Python2 兼容 -from __future__ import print_function, division -from scipy.io import loadmat as load -import matplotlib.pyplot as plt -import numpy as np - -def reformat(samples, labels): - # 改变原始数据的形状 - # 0 1 2 3 3 0 1 2 - # (图片高,图片宽,通道数,图片数) -> (图片数,图片高,图片宽,通道数) - new = np.transpose(samples, (3, 0, 1, 2)).astype(np.float32) - - # labels 变成 one-hot encoding, [2] -> [0, 0, 1, 0, 0, 0, 0, 0, 0, 0] - # digit 0 , represented as 10 - # labels 变成 one-hot encoding, [10] -> [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] - labels = np.array([x[0] for x in labels]) # slow code, whatever - one_hot_labels = [] - for num in labels: - one_hot = [0.0] * 10 - if num == 10: - one_hot[0] = 1.0 - else: - one_hot[num] = 1.0 - one_hot_labels.append(one_hot) - labels = np.array(one_hot_labels).astype(np.float32) - return new, labels - -def normalize(samples): - ''' - 并且灰度化: 从三色通道 -> 单色通道 省内存 + 加快训练速度 - (R + G + B) / 3 - 将图片从 0 ~ 255 线性映射到 -1.0 ~ +1.0 - @samples: numpy array - ''' - a = np.add.reduce(samples, keepdims=True, axis=3) # shape (图片数,图片高,图片宽,通道数) - a = a/3.0 - return a/128.0 - 1.0 - - -def distribution(labels, name): - # 查看一下每个label的分布,再画个统计图 - # keys: - # 0 - # 1 - # 2 - # ... - # 9 - count = {} - for label in labels: - key = 0 if label[0] == 10 else label[0] - if key in count: - count[key] += 1 - else: - count[key] = 1 - x = [] - y = [] - for k, v in count.items(): - # print(k, v) - x.append(k) - y.append(v) - - y_pos = np.arange(len(x)) - plt.bar(y_pos, y, align='center', alpha=0.5) - plt.xticks(y_pos, x) - plt.ylabel('Count') - plt.title(name + ' Label Distribution') - plt.show() - -def inspect(dataset, labels, i): - # 显示图片看看 - if dataset.shape[3] == 1: - shape = dataset.shape - dataset = dataset.reshape(shape[0], shape[1], shape[2]) - print(labels[i]) - plt.imshow(dataset[i]) - plt.show() - - -train = load('../data/train_32x32.mat') -test = load('../data/test_32x32.mat') -# extra = load('../data/extra_32x32.mat') - -print('Train Samples Shape:', train['X'].shape) -print('Train Labels Shape:', train['y'].shape) - -print('Train Samples Shape:', test['X'].shape) -print('Train Labels Shape:', test['y'].shape) - -# print('Train Samples Shape:', extra['X'].shape) -# print('Train Labels Shape:', extra['y'].shape) - -train_samples = train['X'] -train_labels = train['y'] -# test_samples = test['X'] -# test_labels = test['y'] -# test_samples = extra['X'] -# test_labels = extra['y'] - -_train_samples, _train_labels = reformat(train_samples, train_labels) -# _test_samples, _test_labels = reformat(test_samples, test_labels) -# -# _train_dataset = normalize(n_train_dataset) -# _test_dataset = normalize(n_test_dataset) - -num_labels = 10 -image_size = 32 - -if __name__ == '__main__': - # 探索数据 - pass - # inspect(_train_samples, _train_labels, 1234) - # _train_samples = normalize(_train_samples) - # inspect(_train_samples, _train_labels, 1234) - # distribution(train_labels, 'Train Labels') - # distribution(test_labels, 'Test Labels') diff --git a/4-6/run.py b/4-6/run.py index 8e55974..3edc260 100644 --- a/4-6/run.py +++ b/4-6/run.py @@ -1,5 +1,5 @@ #《TF Girls 修炼指南》第四期 - +### # 正式开始机器学习 # 首先我们要确定一个目标: 图像识别

AltStyle によって変換されたページ (->オリジナル) /