├── .gitignore ├── README.md ├── gym ├── CartPole-v0-nn │ ├── CartPole-v0-nn.ipynb │ ├── predict.py │ ├── train.py │ └── try_gym.py ├── CartPole-v0-policy-gradient │ ├── policy_gradient.py │ └── test_policy_gradient.py ├── MountainCar-v0-dqn │ ├── dqn.py │ └── test_dqn.py └── MountainCar-v0-q-learning │ ├── q_learning.py │ ├── test_q_learning.py │ └── try_gym.py ├── make_data_set ├── gen_200_images.py ├── images │ ├── 0_108.png │ ├── 0_11.png │ ├── 0_120.png │ ├── 0_132.png │ ├── 0_141.png │ ├── 0_15.png │ ├── 0_17.png │ ├── 0_18.png │ ├── 0_20.png │ ├── 0_21.png │ ├── 0_26.png │ ├── 0_28.png │ ├── 0_57.png │ ├── 0_7.png │ ├── 0_71.png │ ├── 0_80.png │ ├── 0_93.png │ ├── 1_103.png │ ├── 1_114.png │ ├── 1_119.png │ ├── 1_13.png │ ├── 1_14.png │ ├── 1_140.png │ ├── 1_150.png │ ├── 1_158.png │ ├── 1_161.png │ ├── 1_164.png │ ├── 1_173.png │ ├── 1_177.png │ ├── 1_178.png │ ├── 1_186.png │ ├── 1_189.png │ ├── 1_19.png │ ├── 1_192.png │ ├── 1_32.png │ ├── 1_4.png │ ├── 1_48.png │ ├── 1_5.png │ ├── 1_73.png │ ├── 1_75.png │ ├── 1_77.png │ ├── 1_79.png │ ├── 1_8.png │ ├── 1_92.png │ ├── 1_95.png │ ├── 2_0.png │ ├── 2_101.png │ ├── 2_106.png │ ├── 2_126.png │ ├── 2_146.png │ ├── 2_149.png │ ├── 2_162.png │ ├── 2_171.png │ ├── 2_185.png │ ├── 2_188.png │ ├── 2_191.png │ ├── 2_22.png │ ├── 2_27.png │ ├── 2_30.png │ ├── 2_39.png │ ├── 2_55.png │ ├── 2_58.png │ ├── 2_63.png │ ├── 2_66.png │ ├── 2_82.png │ ├── 2_89.png │ ├── 3_105.png │ ├── 3_124.png │ ├── 3_129.png │ ├── 3_133.png │ ├── 3_135.png │ ├── 3_138.png │ ├── 3_139.png │ ├── 3_145.png │ ├── 3_151.png │ ├── 3_152.png │ ├── 3_169.png │ ├── 3_174.png │ ├── 3_31.png │ ├── 3_40.png │ ├── 3_45.png │ ├── 3_47.png │ ├── 3_49.png │ ├── 3_50.png │ ├── 3_51.png │ ├── 3_59.png │ ├── 3_67.png │ ├── 3_72.png │ ├── 3_74.png │ ├── 3_94.png │ ├── 3_98.png │ ├── 4_109.png │ ├── 4_118.png │ ├── 4_127.png │ ├── 4_137.png │ ├── 4_165.png │ ├── 4_182.png │ ├── 4_194.png │ ├── 4_29.png │ ├── 4_81.png │ ├── 4_86.png │ ├── 4_99.png │ ├── 5_1.png │ ├── 5_113.png │ ├── 5_116.png │ ├── 5_128.png │ ├── 5_134.png │ ├── 5_144.png │ ├── 5_148.png │ ├── 5_16.png │ ├── 5_166.png │ ├── 5_167.png │ ├── 5_172.png │ ├── 5_176.png │ ├── 5_179.png │ ├── 5_181.png │ ├── 5_184.png │ ├── 5_195.png │ ├── 5_196.png │ ├── 5_24.png │ ├── 5_25.png │ ├── 5_33.png │ ├── 5_34.png │ ├── 5_37.png │ ├── 5_56.png │ ├── 5_60.png │ ├── 5_64.png │ ├── 5_76.png │ ├── 5_84.png │ ├── 5_87.png │ ├── 5_9.png │ ├── 6_110.png │ ├── 6_122.png │ ├── 6_125.png │ ├── 6_136.png │ ├── 6_142.png │ ├── 6_175.png │ ├── 6_199.png │ ├── 6_36.png │ ├── 6_41.png │ ├── 6_42.png │ ├── 6_44.png │ ├── 6_52.png │ ├── 6_53.png │ ├── 6_54.png │ ├── 6_62.png │ ├── 6_68.png │ ├── 6_69.png │ ├── 7_10.png │ ├── 7_112.png │ ├── 7_12.png │ ├── 7_130.png │ ├── 7_147.png │ ├── 7_154.png │ ├── 7_168.png │ ├── 7_170.png │ ├── 7_3.png │ ├── 7_61.png │ ├── 7_65.png │ ├── 7_70.png │ ├── 7_78.png │ ├── 7_83.png │ ├── 8_102.png │ ├── 8_104.png │ ├── 8_117.png │ ├── 8_123.png │ ├── 8_131.png │ ├── 8_143.png │ ├── 8_153.png │ ├── 8_155.png │ ├── 8_156.png │ ├── 8_157.png │ ├── 8_159.png │ ├── 8_160.png │ ├── 8_163.png │ ├── 8_190.png │ ├── 8_193.png │ ├── 8_2.png │ ├── 8_23.png │ ├── 8_43.png │ ├── 8_6.png │ ├── 8_88.png │ ├── 8_97.png │ ├── 9_100.png │ ├── 9_107.png │ ├── 9_111.png │ ├── 9_115.png │ ├── 9_121.png │ ├── 9_180.png │ ├── 9_183.png │ ├── 9_187.png │ ├── 9_197.png │ ├── 9_198.png │ ├── 9_35.png │ ├── 9_38.png │ ├── 9_46.png │ ├── 9_85.png │ ├── 9_90.png │ ├── 9_91.png │ └── 9_96.png ├── make_hdf5_data_set.py └── make_npy_data_set.py └── mnist ├── data_set ├── t10k-images-idx3-ubyte.gz ├── t10k-labels-idx1-ubyte.gz ├── train-images-idx3-ubyte.gz └── train-labels-idx1-ubyte.gz ├── data_set_tf2 └── mnist.npz ├── test_images ├── 0.png ├── 1.png └── 4.png ├── v1 ├── model.py └── train.py ├── v2 ├── model.py ├── predict.py └── train.py ├── v3 ├── model.py ├── predict.py └── train.py └── v4_cnn ├── predict.py └── train.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode 3 | .idea 4 | ckpt 5 | mnist/v3/log 6 | make_data_set/data_set 7 | .ipynb_checkpoints 8 | *.h5 9 | *.pickle 10 | __pycache__ 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow 2.0 Tutorial 入门教程实战案例 2 | 3 | > 用最白话的语言,讲解机器学习、神经网络与深度学习 4 | > 示例基于 TensorFlow 1.4 和 TensorFlow 2.0 实现 5 | 6 | ## 相关链接 7 | 8 | - [Python 简明教程](https://geektutu.com/post/quick-python.html) 9 | - [机器学习笔试面试题](https://geektutu.com/post/qa-ml-1.html),[Github](https://github.com/geektutu/interview-questions) 10 | - [TensorFlow 2.0 中文文档](https://geektutu.com/post/tf2doc.html),[Github](https://github.com/geektutu/tensorflow2-docs-zh) 11 | - [TensorFlow 2.0 图像识别&强化学习实战](https://geektutu.com/post/tensorflow2-mnist-cnn.html),[Github](https://github.com/geektutu/tensorflow-tutorial-samples) 12 | 13 | ## OpenAI gym 14 | 15 | - [TensorFlow 2.0 (九) - 强化学习70行代码实战 Policy Gradient](https://geektutu.com/post/tensorflow2-gym-pg.html) 16 | - [Github - gym/CartPole-v0-policy-gradient](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/gym/CartPole-v0-policy-gradient) 17 | - 介绍了策略梯度算法(Policy Gradient)来玩 CartPole-v0 18 | 19 | - [TensorFlow 2.0 (八) - 强化学习 DQN 玩转 gym Mountain Car](https://geektutu.com/post/tensorflow2-gym-dqn.html) 20 | - [Github - gym/MountainCar-v0-dqn](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/gym/MountainCar-v0-dqn) 21 | - 介绍了DQN(Deep Q-Learning)来玩MountainCar-v0游戏 22 | - Q-Table用神经网络来代替。 23 | 24 | - [TensorFlow 2.0 (七) - 强化学习 Q-Learning 玩转 OpenAI gym](https://geektutu.com/post/tensorflow2-gym-q-learning.html) 25 | - [Github - gym/MountainCar-v0-q-learning](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/gym/MountainCar-v0-q-learning) 26 | - 介绍了使用Q-Learning(创建Q-Table)来玩MountainCar-v0游戏 27 | - 将连续的状态离散化。 28 | 29 | - [TensorFlow 2.0 (六) - 监督学习玩转 OpenAI gym game ](https://geektutu.com/post/tensorflow2-gym-nn.html) 30 | - [Github - gym/CartPole-v0-nn](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/gym/CartPole-v0-nn) 31 | - 介绍了使用纯监督学习(神经网络)来玩CartPole-v0游戏 32 | - 使用TensorFlow 2.0 33 | 34 | ## mnist 35 | 36 | - [TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络)](https://geektutu.com/post/tensorflow2-mnist-cnn.html) 37 | - [Github - v4_cnn](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/mnist/v4_cnn) 38 | - 介绍了如何搭建CNN网络,准确率达到0.99 39 | - 使用TensorFlow 2.0 40 | 41 | - [TensorFlow入门(四) - mnist手写数字识别(制作h5py训练集)](https://geektutu.com/post/tensorflow-make-npy-hdf5-data-set.html) 42 | - [Github - make_data_set](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/make_data_set) 43 | - 介绍了如何使用 numpy 制作 npy 格式的数据集 44 | - 介绍了如何使用 h5py 制作 HDF5 格式的数据集 45 | 46 | - [TensorFlow入门(三) - mnist手写数字识别(可视化训练)](https://geektutu.com/post/tensorflow-mnist-tensorboard-training.html) 47 | - [Github - mnist/v3](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/mnist/v3) 48 | - 介绍了tensorboard的简单用法,包括标量图、直方图以及网络结构图 49 | 50 | - [TensorFlow入门(二) - mnist手写数字识别(模型保存加载)](https://geektutu.com/post/tensorflow-mnist-save-ckpt.html) 51 | - [Github - mnist/v2](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/mnist/v2) 52 | - 介绍了 TensorFlow 中如何保存训练好的模型 53 | - 介绍了如何从某一个模型为起点继续训练 54 | - 介绍了模型如何加载使用,传入真实的图片如何识别 55 | 56 | - [TensorFlow入门(一) - mnist手写数字识别(网络搭建)](https://geektutu.com/post/tensorflow-mnist-simplest.html) 57 | - [Github - mnist/v1](https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/mnist/v1) 58 | - 这篇博客介绍了使用 TensorFlow 搭建最简单的神经网络。 59 | - 包括输入输出、独热编码与损失函数,以及正确率的验证。 60 | 61 | -------------------------------------------------------------------------------- /gym/CartPole-v0-nn/CartPole-v0-nn.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 试一试 gym" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "score: 25.0\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "# try_gym.py\n", 25 | "# https://geektutu.com\n", 26 | "import gym # 0.12.5\n", 27 | "import random\n", 28 | "import time\n", 29 | "\n", 30 | "env = gym.make(\"CartPole-v0\") # 加载游戏环境\n", 31 | "\n", 32 | "state = env.reset()\n", 33 | "score = 0\n", 34 | "while True:\n", 35 | " time.sleep(0.1)\n", 36 | " env.render() # 显示画面\n", 37 | " action = random.randint(0, 1) # 随机选择一个动作 0 或 1\n", 38 | " state, reward, done, _ = env.step(action) # 执行这个动作\n", 39 | " score += reward # 每回合的得分\n", 40 | " if done: # 游戏结束\n", 41 | " print('score: ', score) # 打印分数\n", 42 | " break\n", 43 | "env.close()" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "## 搭建模型" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "Model: \"sequential\"\n", 63 | "_________________________________________________________________\n", 64 | "Layer (type) Output Shape Param # \n", 65 | "=================================================================\n", 66 | "dense (Dense) (None, 64) 320 \n", 67 | "_________________________________________________________________\n", 68 | "dense_1 (Dense) (None, 20) 1300 \n", 69 | "_________________________________________________________________\n", 70 | "dense_2 (Dense) (None, 2) 42 \n", 71 | "=================================================================\n", 72 | "Total params: 1,662\n", 73 | "Trainable params: 1,662\n", 74 | "Non-trainable params: 0\n", 75 | "_________________________________________________________________\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "# train.py\n", 81 | "# https://geektutu.com\n", 82 | "import random\n", 83 | "import gym\n", 84 | "import numpy as np\n", 85 | "from tensorflow.keras import models, layers\n", 86 | "\n", 87 | "env = gym.make(\"CartPole-v0\") # 加载游戏环境\n", 88 | "\n", 89 | "STATE_DIM, ACTION_DIM = 4, 2 # State 维度 4, Action 维度 2\n", 90 | "model = models.Sequential([\n", 91 | " layers.Dense(64, input_dim=STATE_DIM, activation='relu'),\n", 92 | " layers.Dense(20, activation='relu'),\n", 93 | " layers.Dense(ACTION_DIM, activation='linear')\n", 94 | "])\n", 95 | "model.summary() # 打印神经网络信息" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "## 生成训练数据" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 3, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "def generate_data_one_episode():\n", 112 | " '''生成单次游戏的训练数据'''\n", 113 | " x, y, score = [], [], 0\n", 114 | " state = env.reset()\n", 115 | " while True:\n", 116 | " action = random.randrange(0, 2)\n", 117 | " x.append(state)\n", 118 | " y.append([1, 0] if action == 0 else [0, 1]) # 记录数据\n", 119 | " state, reward, done, _ = env.step(action) # 执行动作\n", 120 | " score += reward\n", 121 | " if done:\n", 122 | " break\n", 123 | " return x, y, score\n", 124 | "\n", 125 | "\n", 126 | "def generate_training_data(expected_score=100):\n", 127 | " '''# 生成N次游戏的训练数据,并进行筛选,选择 > 100 的数据作为训练集'''\n", 128 | " data_X, data_Y, scores = [], [], []\n", 129 | " for i in range(10000):\n", 130 | " x, y, score = generate_data_one_episode()\n", 131 | " if score > expected_score:\n", 132 | " data_X += x\n", 133 | " data_Y += y\n", 134 | " scores.append(score)\n", 135 | " print('dataset size: {}, max score: {}'.format(len(data_X), max(scores)))\n", 136 | " return np.array(data_X), np.array(data_Y)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "## 训练并保存模型" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 4, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "dataset size: 749, max score: 120.0\n", 156 | "Train on 749 samples\n", 157 | "Epoch 1/5\n", 158 | "749/749 [==============================] - 0s 210us/sample - loss: 0.3735\n", 159 | "Epoch 2/5\n", 160 | "749/749 [==============================] - 0s 37us/sample - loss: 0.2777\n", 161 | "Epoch 3/5\n", 162 | "749/749 [==============================] - 0s 37us/sample - loss: 0.2523\n", 163 | "Epoch 4/5\n", 164 | "749/749 [==============================] - 0s 40us/sample - loss: 0.2403\n", 165 | "Epoch 5/5\n", 166 | "749/749 [==============================] - 0s 36us/sample - loss: 0.2355\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "data_X, data_Y = generate_training_data()\n", 172 | "model.compile(loss='mse', optimizer='adam')\n", 173 | "model.fit(data_X, data_Y, epochs=5)\n", 174 | "model.save('CartPole-v0-nn.h5') # 保存模型" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "## 测试/预测模型" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 5, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "using nn, score: 196.0\n", 194 | "using nn, score: 200.0\n", 195 | "using nn, score: 200.0\n", 196 | "using nn, score: 200.0\n", 197 | "using nn, score: 200.0\n" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "# predict.py\n", 203 | "# https://geektutu.com\n", 204 | "import time\n", 205 | "import numpy as np\n", 206 | "import gym\n", 207 | "from tensorflow.keras import models\n", 208 | "\n", 209 | "\n", 210 | "saved_model = models.load_model('CartPole-v0-nn.h5') # 加载模型\n", 211 | "env = gym.make(\"CartPole-v0\") # 加载游戏环境\n", 212 | "\n", 213 | "for i in range(5):\n", 214 | " state = env.reset()\n", 215 | " score = 0\n", 216 | " while True:\n", 217 | " time.sleep(0.01)\n", 218 | " env.render() # 显示画面\n", 219 | " action = np.argmax(saved_model.predict(np.array([state]))[0]) # 预测动作\n", 220 | " state, reward, done, _ = env.step(action) # 执行这个动作\n", 221 | " score += reward # 每回合的得分\n", 222 | " if done: # 游戏结束\n", 223 | " print('using nn, score: ', score) # 打印分数\n", 224 | " break\n", 225 | "env.close()" 226 | ] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 3", 232 | "language": "python", 233 | "name": "python3" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 3 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython3", 245 | "version": "3.7.0" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 2 250 | } 251 | -------------------------------------------------------------------------------- /gym/CartPole-v0-nn/predict.py: -------------------------------------------------------------------------------- 1 | # predict.py 2 | # https://geektutu.com 3 | import time 4 | import numpy as np 5 | import gym 6 | from tensorflow.keras import models 7 | 8 | 9 | saved_model = models.load_model('CartPole-v0-nn.h5') # 加载模型 10 | env = gym.make("CartPole-v0") # 加载游戏环境 11 | 12 | for i in range(5): 13 | state = env.reset() 14 | score = 0 15 | while True: 16 | time.sleep(0.01) 17 | env.render() # 显示画面 18 | action = np.argmax(saved_model.predict(np.array([state]))[0]) # 预测动作 19 | state, reward, done, _ = env.step(action) # 执行这个动作 20 | score += reward # 每回合的得分 21 | if done: # 游戏结束 22 | print('using nn, score: ', score) # 打印分数 23 | break 24 | env.close() 25 | -------------------------------------------------------------------------------- /gym/CartPole-v0-nn/train.py: -------------------------------------------------------------------------------- 1 | # train.py 2 | # https://geektutu.com 3 | import random 4 | import gym 5 | import numpy as np 6 | from tensorflow.keras import models, layers 7 | 8 | env = gym.make("CartPole-v0") # 加载游戏环境 9 | 10 | STATE_DIM, ACTION_DIM = 4, 2 # State 维度 4, Action 维度 2 11 | model = models.Sequential([ 12 | layers.Dense(64, input_dim=STATE_DIM, activation='relu'), 13 | layers.Dense(20, activation='relu'), 14 | layers.Dense(ACTION_DIM, activation='linear') 15 | ]) 16 | model.summary() # 打印神经网络信息 17 | 18 | 19 | def generate_data_one_episode(): 20 | '''生成单次游戏的训练数据''' 21 | x, y, score = [], [], 0 22 | state = env.reset() 23 | while True: 24 | action = random.randrange(0, 2) 25 | x.append(state) 26 | y.append([1, 0] if action == 0 else [0, 1]) # 记录数据 27 | state, reward, done, _ = env.step(action) # 执行动作 28 | score += reward 29 | if done: 30 | break 31 | return x, y, score 32 | 33 | 34 | def generate_training_data(expected_score=100): 35 | '''# 生成N次游戏的训练数据,并进行筛选,选择 > 100 的数据作为训练集''' 36 | data_X, data_Y, scores = [], [], [] 37 | for i in range(10000): 38 | x, y, score = generate_data_one_episode() 39 | if score > expected_score: 40 | data_X += x 41 | data_Y += y 42 | scores.append(score) 43 | print('dataset size: {}, max score: {}'.format(len(data_X), max(scores))) 44 | return np.array(data_X), np.array(data_Y) 45 | 46 | 47 | data_X, data_Y = generate_training_data() 48 | model.compile(loss='mse', optimizer='adam') 49 | model.fit(data_X, data_Y, epochs=5) 50 | model.save('CartPole-v0-nn.h5') # 保存模型 51 | -------------------------------------------------------------------------------- /gym/CartPole-v0-nn/try_gym.py: -------------------------------------------------------------------------------- 1 | # try_gym.py 2 | # https://geektutu.com 3 | import gym # 0.12.5 4 | import random 5 | import time 6 | 7 | env = gym.make("CartPole-v0") # 加载游戏环境 8 | 9 | state = env.reset() 10 | score = 0 11 | while True: 12 | time.sleep(0.1) 13 | env.render() # 显示画面 14 | action = random.randint(0, 1) # 随机选择一个动作 0 或 1 15 | state, reward, done, _ = env.step(action) # 执行这个动作 16 | score += reward # 每回合的得分 17 | if done: # 游戏结束 18 | print('score: ', score) # 打印分数 19 | break 20 | env.close() 21 | -------------------------------------------------------------------------------- /gym/CartPole-v0-policy-gradient/policy_gradient.py: -------------------------------------------------------------------------------- 1 | # policy_gradient.py 2 | # https://geektutu.com 3 | import matplotlib.pyplot as plt 4 | import gym 5 | import numpy as np 6 | from tensorflow.keras import models, layers, optimizers 7 | 8 | env = gym.make('CartPole-v0') 9 | 10 | STATE_DIM, ACTION_DIM = 4, 2 11 | model = models.Sequential([ 12 | layers.Dense(100, input_dim=STATE_DIM, activation='relu'), 13 | layers.Dropout(0.1), 14 | layers.Dense(ACTION_DIM, activation="softmax") 15 | ]) 16 | model.compile(loss='mean_squared_error', 17 | optimizer=optimizers.Adam(0.001)) 18 | 19 | 20 | def choose_action(s): 21 | """预测动作""" 22 | prob = model.predict(np.array([s]))[0] 23 | return np.random.choice(len(prob), p=prob) 24 | 25 | 26 | def discount_rewards(rewards, gamma=0.95): 27 | """计算衰减reward的累加期望,并中心化和标准化处理""" 28 | prior = 0 29 | out = np.zeros_like(rewards) 30 | for i in reversed(range(len(rewards))): 31 | prior = prior * gamma + rewards[i] 32 | out[i] = prior 33 | return out / np.std(out - np.mean(out)) 34 | 35 | 36 | def train(records): 37 | s_batch = np.array([record[0] for record in records]) 38 | # action 独热编码处理,方便求动作概率,即 prob_batch 39 | a_batch = np.array([[1 if record[1] == i else 0 for i in range(ACTION_DIM)] 40 | for record in records]) 41 | # 假设predict的概率是 [0.3, 0.7],选择的动作是 [0, 1] 42 | # 则动作[0, 1]的概率等于 [0, 0.7] = [0.3, 0.7] * [0, 1] 43 | prob_batch = model.predict(s_batch) * a_batch 44 | r_batch = discount_rewards([record[2] for record in records]) 45 | 46 | model.fit(s_batch, prob_batch, sample_weight=r_batch, verbose=0) 47 | 48 | 49 | episodes = 2000 # 至多2000次 50 | score_list = [] # 记录所有分数 51 | for i in range(episodes): 52 | s = env.reset() 53 | score = 0 54 | replay_records = [] 55 | while True: 56 | a = choose_action(s) 57 | next_s, r, done, _ = env.step(a) 58 | replay_records.append((s, a, r)) 59 | 60 | score += r 61 | s = next_s 62 | if done: 63 | train(replay_records) 64 | score_list.append(score) 65 | print('episode:', i, 'score:', score, 'max:', max(score_list)) 66 | break 67 | # 最后10次的平均分大于 195 时,停止并保存模型 68 | if np.mean(score_list[-10:]) > 195: 69 | model.save('CartPole-v0-pg.h5') 70 | break 71 | env.close() 72 | 73 | 74 | # 画图 75 | plt.plot(score_list) 76 | x = np.array(range(len(score_list))) 77 | smooth_func = np.poly1d(np.polyfit(x, score_list, 3)) 78 | plt.plot(x, smooth_func(x), label='Mean', linestyle='--') 79 | plt.show() 80 | -------------------------------------------------------------------------------- /gym/CartPole-v0-policy-gradient/test_policy_gradient.py: -------------------------------------------------------------------------------- 1 | # test_policy_gradient.py 2 | # https://geektutu.com 3 | import time 4 | import numpy as np 5 | import gym 6 | from tensorflow.keras import models 7 | 8 | 9 | saved_model = models.load_model('CartPole-v0-pg.h5') 10 | env = gym.make("CartPole-v0") 11 | 12 | for i in range(5): 13 | s = env.reset() 14 | score = 0 15 | while True: 16 | time.sleep(0.01) 17 | env.render() 18 | prob = saved_model.predict(np.array([s]))[0] 19 | a = np.random.choice(len(prob), p=prob) 20 | s, r, done, _ = env.step(a) 21 | score += r 22 | if done: 23 | print('using policy gradient, score: ', score) # 打印分数 24 | break 25 | env.close() 26 | -------------------------------------------------------------------------------- /gym/MountainCar-v0-dqn/dqn.py: -------------------------------------------------------------------------------- 1 | # dqn.py 2 | # https://geektutu.com 3 | from collections import deque 4 | import random 5 | import gym 6 | import numpy as np 7 | from tensorflow.keras import models, layers, optimizers 8 | 9 | 10 | class DQN(object): 11 | def __init__(self): 12 | self.step = 0 13 | self.update_freq = 200 # 模型更新频率 14 | self.replay_size = 2000 # 训练集大小 15 | self.replay_queue = deque(maxlen=self.replay_size) 16 | self.model = self.create_model() 17 | self.target_model = self.create_model() 18 | 19 | def create_model(self): 20 | """创建一个隐藏层为100的神经网络""" 21 | STATE_DIM, ACTION_DIM = 2, 3 22 | model = models.Sequential([ 23 | layers.Dense(100, input_dim=STATE_DIM, activation='relu'), 24 | layers.Dense(ACTION_DIM, activation="linear") 25 | ]) 26 | model.compile(loss='mean_squared_error', 27 | optimizer=optimizers.Adam(0.001)) 28 | return model 29 | 30 | def act(self, s, epsilon=0.1): 31 | """预测动作""" 32 | # 刚开始时,加一点随机成分,产生更多的状态 33 | if np.random.uniform() < epsilon - self.step * 0.0002: 34 | return np.random.choice([0, 1, 2]) 35 | return np.argmax(self.model.predict(np.array([s]))[0]) 36 | 37 | def save_model(self, file_path='MountainCar-v0-dqn.h5'): 38 | print('model saved') 39 | self.model.save(file_path) 40 | 41 | def remember(self, s, a, next_s, reward): 42 | """历史记录,position >= 0.4时给额外的reward,快速收敛""" 43 | if next_s[0] >= 0.4: 44 | reward += 1 45 | self.replay_queue.append((s, a, next_s, reward)) 46 | 47 | def train(self, batch_size=64, lr=1, factor=0.95): 48 | if len(self.replay_queue) < self.replay_size: 49 | return 50 | self.step += 1 51 | # 每 update_freq 步,将 model 的权重赋值给 target_model 52 | if self.step % self.update_freq == 0: 53 | self.target_model.set_weights(self.model.get_weights()) 54 | 55 | replay_batch = random.sample(self.replay_queue, batch_size) 56 | s_batch = np.array([replay[0] for replay in replay_batch]) 57 | next_s_batch = np.array([replay[2] for replay in replay_batch]) 58 | 59 | Q = self.model.predict(s_batch) 60 | Q_next = self.target_model.predict(next_s_batch) 61 | 62 | # 使用公式更新训练集中的Q值 63 | for i, replay in enumerate(replay_batch): 64 | _, a, _, reward = replay 65 | Q[i][a] = (1 - lr) * Q[i][a] + lr * (reward + factor * np.amax(Q_next[i])) 66 | 67 | # 传入网络进行训练 68 | self.model.fit(s_batch, Q, verbose=0) 69 | 70 | 71 | env = gym.make('MountainCar-v0') 72 | episodes = 1000 # 训练1000次 73 | score_list = [] # 记录所有分数 74 | agent = DQN() 75 | for i in range(episodes): 76 | s = env.reset() 77 | score = 0 78 | while True: 79 | a = agent.act(s) 80 | next_s, reward, done, _ = env.step(a) 81 | agent.remember(s, a, next_s, reward) 82 | agent.train() 83 | score += reward 84 | s = next_s 85 | if done: 86 | score_list.append(score) 87 | print('episode:', i, 'score:', score, 'max:', max(score_list)) 88 | break 89 | # 最后10次的平均分大于 -160 时,停止并保存模型 90 | if np.mean(score_list[-10:]) > -160: 91 | agent.save_model() 92 | break 93 | env.close() 94 | 95 | import matplotlib.pyplot as plt 96 | 97 | plt.plot(score_list, color='green') 98 | plt.show() -------------------------------------------------------------------------------- /gym/MountainCar-v0-dqn/test_dqn.py: -------------------------------------------------------------------------------- 1 | # test_dqn.py 2 | # https://geektutu.com 3 | import time 4 | import gym 5 | import numpy as np 6 | from tensorflow.keras import models 7 | env = gym.make('MountainCar-v0') 8 | model = models.load_model('MountainCar-v0-dqn.h5') 9 | s = env.reset() 10 | score = 0 11 | while True: 12 | env.render() 13 | time.sleep(0.01) 14 | a = np.argmax(model.predict(np.array([s]))[0]) 15 | s, reward, done, _ = env.step(a) 16 | score += reward 17 | if done: 18 | print('score:', score) 19 | break 20 | env.close() -------------------------------------------------------------------------------- /gym/MountainCar-v0-q-learning/q_learning.py: -------------------------------------------------------------------------------- 1 | # q_learning.py 2 | # https://geektutu.com 3 | import pickle 4 | from collections import defaultdict 5 | import gym # 0.12.5 6 | import numpy as np 7 | 8 | # 默认将Action 0,1,2的价值初始化为0 9 | Q = defaultdict(lambda: [0, 0, 0]) 10 | 11 | env = gym.make('MountainCar-v0') 12 | 13 | 14 | def transform_state(state): 15 | """将 position, velocity 通过线性转换映射到 [0, 40] 范围内""" 16 | pos, v = state 17 | pos_low, v_low = env.observation_space.low 18 | pos_high, v_high = env.observation_space.high 19 | 20 | a = 40 * (pos - pos_low) / (pos_high - pos_low) 21 | b = 40 * (v - v_low) / (v_high - v_low) 22 | 23 | return int(a), int(b) 24 | 25 | # print(transform_state([-1.0, 0.01])) 26 | # eg: (4, 22) 27 | 28 | 29 | lr, factor = 0.7, 0.95 30 | episodes = 10000 # 训练10000次 31 | score_list = [] # 记录所有分数 32 | for i in range(episodes): 33 | s = transform_state(env.reset()) 34 | score = 0 35 | while True: 36 | a = np.argmax(Q[s]) 37 | # 训练刚开始,多一点随机性,以便有更多的状态 38 | if np.random.random() > i * 3 / episodes: 39 | a = np.random.choice([0, 1, 2]) 40 | # 执行动作 41 | next_s, reward, done, _ = env.step(a) 42 | next_s = transform_state(next_s) 43 | # 根据上面的公式更新Q-Table 44 | Q[s][a] = (1 - lr) * Q[s][a] + lr * (reward + factor * max(Q[next_s])) 45 | score += reward 46 | s = next_s 47 | if done: 48 | score_list.append(score) 49 | print('episode:', i, 'score:', score, 'max:', max(score_list)) 50 | break 51 | env.close() 52 | 53 | # 保存模型 54 | with open('MountainCar-v0-q-learning.pickle', 'wb') as f: 55 | pickle.dump(dict(Q), f) 56 | print('model saved') 57 | -------------------------------------------------------------------------------- /gym/MountainCar-v0-q-learning/test_q_learning.py: -------------------------------------------------------------------------------- 1 | # test_q_learning.py 2 | # https://geektutu.com 3 | import time 4 | import pickle 5 | import gym 6 | import numpy as np 7 | 8 | 9 | def transform_state(state): 10 | """将 position, velocity 通过线性转换映射到 [0, 40] 范围内""" 11 | pos, v = state 12 | pos_low, v_low = env.observation_space.low 13 | pos_high, v_high = env.observation_space.high 14 | 15 | a = 40 * (pos - pos_low) / (pos_high - pos_low) 16 | b = 40 * (v - v_low) / (v_high - v_low) 17 | 18 | return int(a), int(b) 19 | 20 | 21 | # 加载模型 22 | with open('MountainCar-v0-q-learning.pickle', 'rb') as f: 23 | Q = pickle.load(f) 24 | print('model loaded') 25 | 26 | env = gym.make('MountainCar-v0') 27 | s = env.reset() 28 | score = 0 29 | while True: 30 | env.render() 31 | time.sleep(0.01) 32 | # transform_state函数 与 训练时的一致 33 | s = transform_state(s) 34 | a = np.argmax(Q[s]) if s in Q else 0 35 | s, reward, done, _ = env.step(a) 36 | score += reward 37 | if done: 38 | print('score:', score) 39 | break 40 | env.close() 41 | -------------------------------------------------------------------------------- /gym/MountainCar-v0-q-learning/try_gym.py: -------------------------------------------------------------------------------- 1 | import time 2 | import random 3 | import gym # 0.12.5 4 | 5 | env = gym.make('MountainCar-v0') 6 | env.reset() 7 | score = 0 8 | for i in range(2): 9 | while True: 10 | env.render() 11 | time.sleep(0.01) 12 | a = random.randint(0, 2) 13 | s, reward, done, _ = env.step(a) 14 | score += reward 15 | if done: 16 | print('score:', score) 17 | break 18 | env.close() -------------------------------------------------------------------------------- /make_data_set/gen_200_images.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | from PIL import Image 4 | from tensorflow.examples.tutorials.mnist import input_data 5 | 6 | """ 7 | tensorflow 1.4 8 | PIL 4.3.0 (pip install pillow) 9 | numpy 1.13.1 10 | """ 11 | 12 | if not os.path.exists('./images'): 13 | os.mkdir('./images') 14 | 15 | 16 | def gen_image(arr, index, label): 17 | # 直接保存 arr,是黑底图片,1.0 - arr 是白底图片 18 | matrix = (np.reshape(1.0 - arr, (28, 28)) * 255).astype(np.uint8) 19 | img = Image.fromarray(matrix, 'L') 20 | # 存储图片时,label_index的格式,方便在制作数据集时,从文件名即可知道label 21 | img.save("./images/{}_{}.png".format(label, index)) 22 | 23 | 24 | data = input_data.read_data_sets('../mnist/data_set') 25 | x, y = data.train.next_batch(200) 26 | for i, (arr, label) in enumerate(zip(x, y)): 27 | print(i, label) 28 | gen_image(arr, i, label) 29 | -------------------------------------------------------------------------------- /make_data_set/images/0_108.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_108.png -------------------------------------------------------------------------------- /make_data_set/images/0_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_11.png -------------------------------------------------------------------------------- /make_data_set/images/0_120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_120.png -------------------------------------------------------------------------------- /make_data_set/images/0_132.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_132.png -------------------------------------------------------------------------------- /make_data_set/images/0_141.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_141.png -------------------------------------------------------------------------------- /make_data_set/images/0_15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_15.png -------------------------------------------------------------------------------- /make_data_set/images/0_17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_17.png -------------------------------------------------------------------------------- /make_data_set/images/0_18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_18.png -------------------------------------------------------------------------------- /make_data_set/images/0_20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_20.png -------------------------------------------------------------------------------- /make_data_set/images/0_21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_21.png -------------------------------------------------------------------------------- /make_data_set/images/0_26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_26.png -------------------------------------------------------------------------------- /make_data_set/images/0_28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_28.png -------------------------------------------------------------------------------- /make_data_set/images/0_57.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_57.png -------------------------------------------------------------------------------- /make_data_set/images/0_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_7.png -------------------------------------------------------------------------------- /make_data_set/images/0_71.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_71.png -------------------------------------------------------------------------------- /make_data_set/images/0_80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_80.png -------------------------------------------------------------------------------- /make_data_set/images/0_93.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/0_93.png -------------------------------------------------------------------------------- /make_data_set/images/1_103.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_103.png -------------------------------------------------------------------------------- /make_data_set/images/1_114.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_114.png -------------------------------------------------------------------------------- /make_data_set/images/1_119.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_119.png -------------------------------------------------------------------------------- /make_data_set/images/1_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_13.png -------------------------------------------------------------------------------- /make_data_set/images/1_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_14.png -------------------------------------------------------------------------------- /make_data_set/images/1_140.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_140.png -------------------------------------------------------------------------------- /make_data_set/images/1_150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_150.png -------------------------------------------------------------------------------- /make_data_set/images/1_158.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_158.png -------------------------------------------------------------------------------- /make_data_set/images/1_161.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_161.png -------------------------------------------------------------------------------- /make_data_set/images/1_164.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_164.png -------------------------------------------------------------------------------- /make_data_set/images/1_173.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_173.png -------------------------------------------------------------------------------- /make_data_set/images/1_177.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_177.png -------------------------------------------------------------------------------- /make_data_set/images/1_178.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_178.png -------------------------------------------------------------------------------- /make_data_set/images/1_186.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_186.png -------------------------------------------------------------------------------- /make_data_set/images/1_189.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_189.png -------------------------------------------------------------------------------- /make_data_set/images/1_19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_19.png -------------------------------------------------------------------------------- /make_data_set/images/1_192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_192.png -------------------------------------------------------------------------------- /make_data_set/images/1_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_32.png -------------------------------------------------------------------------------- /make_data_set/images/1_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_4.png -------------------------------------------------------------------------------- /make_data_set/images/1_48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_48.png -------------------------------------------------------------------------------- /make_data_set/images/1_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_5.png -------------------------------------------------------------------------------- /make_data_set/images/1_73.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_73.png -------------------------------------------------------------------------------- /make_data_set/images/1_75.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_75.png -------------------------------------------------------------------------------- /make_data_set/images/1_77.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_77.png -------------------------------------------------------------------------------- /make_data_set/images/1_79.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_79.png -------------------------------------------------------------------------------- /make_data_set/images/1_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_8.png -------------------------------------------------------------------------------- /make_data_set/images/1_92.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_92.png -------------------------------------------------------------------------------- /make_data_set/images/1_95.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/1_95.png -------------------------------------------------------------------------------- /make_data_set/images/2_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_0.png -------------------------------------------------------------------------------- /make_data_set/images/2_101.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_101.png -------------------------------------------------------------------------------- /make_data_set/images/2_106.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_106.png -------------------------------------------------------------------------------- /make_data_set/images/2_126.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_126.png -------------------------------------------------------------------------------- /make_data_set/images/2_146.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_146.png -------------------------------------------------------------------------------- /make_data_set/images/2_149.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_149.png -------------------------------------------------------------------------------- /make_data_set/images/2_162.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_162.png -------------------------------------------------------------------------------- /make_data_set/images/2_171.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_171.png -------------------------------------------------------------------------------- /make_data_set/images/2_185.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_185.png -------------------------------------------------------------------------------- /make_data_set/images/2_188.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_188.png -------------------------------------------------------------------------------- /make_data_set/images/2_191.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_191.png -------------------------------------------------------------------------------- /make_data_set/images/2_22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_22.png -------------------------------------------------------------------------------- /make_data_set/images/2_27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_27.png -------------------------------------------------------------------------------- /make_data_set/images/2_30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_30.png -------------------------------------------------------------------------------- /make_data_set/images/2_39.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_39.png -------------------------------------------------------------------------------- /make_data_set/images/2_55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_55.png -------------------------------------------------------------------------------- /make_data_set/images/2_58.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_58.png -------------------------------------------------------------------------------- /make_data_set/images/2_63.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_63.png -------------------------------------------------------------------------------- /make_data_set/images/2_66.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_66.png -------------------------------------------------------------------------------- /make_data_set/images/2_82.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_82.png -------------------------------------------------------------------------------- /make_data_set/images/2_89.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/2_89.png -------------------------------------------------------------------------------- /make_data_set/images/3_105.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_105.png -------------------------------------------------------------------------------- /make_data_set/images/3_124.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_124.png -------------------------------------------------------------------------------- /make_data_set/images/3_129.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_129.png -------------------------------------------------------------------------------- /make_data_set/images/3_133.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_133.png -------------------------------------------------------------------------------- /make_data_set/images/3_135.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_135.png -------------------------------------------------------------------------------- /make_data_set/images/3_138.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_138.png -------------------------------------------------------------------------------- /make_data_set/images/3_139.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_139.png -------------------------------------------------------------------------------- /make_data_set/images/3_145.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_145.png -------------------------------------------------------------------------------- /make_data_set/images/3_151.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_151.png -------------------------------------------------------------------------------- /make_data_set/images/3_152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_152.png -------------------------------------------------------------------------------- /make_data_set/images/3_169.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_169.png -------------------------------------------------------------------------------- /make_data_set/images/3_174.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_174.png -------------------------------------------------------------------------------- /make_data_set/images/3_31.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_31.png -------------------------------------------------------------------------------- /make_data_set/images/3_40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_40.png -------------------------------------------------------------------------------- /make_data_set/images/3_45.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_45.png -------------------------------------------------------------------------------- /make_data_set/images/3_47.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_47.png -------------------------------------------------------------------------------- /make_data_set/images/3_49.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_49.png -------------------------------------------------------------------------------- /make_data_set/images/3_50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_50.png -------------------------------------------------------------------------------- /make_data_set/images/3_51.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_51.png -------------------------------------------------------------------------------- /make_data_set/images/3_59.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_59.png -------------------------------------------------------------------------------- /make_data_set/images/3_67.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_67.png -------------------------------------------------------------------------------- /make_data_set/images/3_72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_72.png -------------------------------------------------------------------------------- /make_data_set/images/3_74.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_74.png -------------------------------------------------------------------------------- /make_data_set/images/3_94.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_94.png -------------------------------------------------------------------------------- /make_data_set/images/3_98.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/3_98.png -------------------------------------------------------------------------------- /make_data_set/images/4_109.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_109.png -------------------------------------------------------------------------------- /make_data_set/images/4_118.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_118.png -------------------------------------------------------------------------------- /make_data_set/images/4_127.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_127.png -------------------------------------------------------------------------------- /make_data_set/images/4_137.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_137.png -------------------------------------------------------------------------------- /make_data_set/images/4_165.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_165.png -------------------------------------------------------------------------------- /make_data_set/images/4_182.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_182.png -------------------------------------------------------------------------------- /make_data_set/images/4_194.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_194.png -------------------------------------------------------------------------------- /make_data_set/images/4_29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_29.png -------------------------------------------------------------------------------- /make_data_set/images/4_81.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_81.png -------------------------------------------------------------------------------- /make_data_set/images/4_86.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_86.png -------------------------------------------------------------------------------- /make_data_set/images/4_99.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/4_99.png -------------------------------------------------------------------------------- /make_data_set/images/5_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_1.png -------------------------------------------------------------------------------- /make_data_set/images/5_113.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_113.png -------------------------------------------------------------------------------- /make_data_set/images/5_116.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_116.png -------------------------------------------------------------------------------- /make_data_set/images/5_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_128.png -------------------------------------------------------------------------------- /make_data_set/images/5_134.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_134.png -------------------------------------------------------------------------------- /make_data_set/images/5_144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_144.png -------------------------------------------------------------------------------- /make_data_set/images/5_148.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_148.png -------------------------------------------------------------------------------- /make_data_set/images/5_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_16.png -------------------------------------------------------------------------------- /make_data_set/images/5_166.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_166.png -------------------------------------------------------------------------------- /make_data_set/images/5_167.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_167.png -------------------------------------------------------------------------------- /make_data_set/images/5_172.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_172.png -------------------------------------------------------------------------------- /make_data_set/images/5_176.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_176.png -------------------------------------------------------------------------------- /make_data_set/images/5_179.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_179.png -------------------------------------------------------------------------------- /make_data_set/images/5_181.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_181.png -------------------------------------------------------------------------------- /make_data_set/images/5_184.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_184.png -------------------------------------------------------------------------------- /make_data_set/images/5_195.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_195.png -------------------------------------------------------------------------------- /make_data_set/images/5_196.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_196.png -------------------------------------------------------------------------------- /make_data_set/images/5_24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_24.png -------------------------------------------------------------------------------- /make_data_set/images/5_25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_25.png -------------------------------------------------------------------------------- /make_data_set/images/5_33.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_33.png -------------------------------------------------------------------------------- /make_data_set/images/5_34.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_34.png -------------------------------------------------------------------------------- /make_data_set/images/5_37.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_37.png -------------------------------------------------------------------------------- /make_data_set/images/5_56.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_56.png -------------------------------------------------------------------------------- /make_data_set/images/5_60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_60.png -------------------------------------------------------------------------------- /make_data_set/images/5_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_64.png -------------------------------------------------------------------------------- /make_data_set/images/5_76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_76.png -------------------------------------------------------------------------------- /make_data_set/images/5_84.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_84.png -------------------------------------------------------------------------------- /make_data_set/images/5_87.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_87.png -------------------------------------------------------------------------------- /make_data_set/images/5_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/5_9.png -------------------------------------------------------------------------------- /make_data_set/images/6_110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_110.png -------------------------------------------------------------------------------- /make_data_set/images/6_122.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_122.png -------------------------------------------------------------------------------- /make_data_set/images/6_125.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_125.png -------------------------------------------------------------------------------- /make_data_set/images/6_136.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_136.png -------------------------------------------------------------------------------- /make_data_set/images/6_142.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_142.png -------------------------------------------------------------------------------- /make_data_set/images/6_175.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_175.png -------------------------------------------------------------------------------- /make_data_set/images/6_199.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_199.png -------------------------------------------------------------------------------- /make_data_set/images/6_36.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_36.png -------------------------------------------------------------------------------- /make_data_set/images/6_41.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_41.png -------------------------------------------------------------------------------- /make_data_set/images/6_42.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_42.png -------------------------------------------------------------------------------- /make_data_set/images/6_44.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_44.png -------------------------------------------------------------------------------- /make_data_set/images/6_52.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_52.png -------------------------------------------------------------------------------- /make_data_set/images/6_53.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_53.png -------------------------------------------------------------------------------- /make_data_set/images/6_54.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_54.png -------------------------------------------------------------------------------- /make_data_set/images/6_62.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_62.png -------------------------------------------------------------------------------- /make_data_set/images/6_68.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_68.png -------------------------------------------------------------------------------- /make_data_set/images/6_69.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/6_69.png -------------------------------------------------------------------------------- /make_data_set/images/7_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_10.png -------------------------------------------------------------------------------- /make_data_set/images/7_112.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_112.png -------------------------------------------------------------------------------- /make_data_set/images/7_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_12.png -------------------------------------------------------------------------------- /make_data_set/images/7_130.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_130.png -------------------------------------------------------------------------------- /make_data_set/images/7_147.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_147.png -------------------------------------------------------------------------------- /make_data_set/images/7_154.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_154.png -------------------------------------------------------------------------------- /make_data_set/images/7_168.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_168.png -------------------------------------------------------------------------------- /make_data_set/images/7_170.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_170.png -------------------------------------------------------------------------------- /make_data_set/images/7_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_3.png -------------------------------------------------------------------------------- /make_data_set/images/7_61.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_61.png -------------------------------------------------------------------------------- /make_data_set/images/7_65.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_65.png -------------------------------------------------------------------------------- /make_data_set/images/7_70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_70.png -------------------------------------------------------------------------------- /make_data_set/images/7_78.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_78.png -------------------------------------------------------------------------------- /make_data_set/images/7_83.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/7_83.png -------------------------------------------------------------------------------- /make_data_set/images/8_102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_102.png -------------------------------------------------------------------------------- /make_data_set/images/8_104.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_104.png -------------------------------------------------------------------------------- /make_data_set/images/8_117.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_117.png -------------------------------------------------------------------------------- /make_data_set/images/8_123.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_123.png -------------------------------------------------------------------------------- /make_data_set/images/8_131.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_131.png -------------------------------------------------------------------------------- /make_data_set/images/8_143.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_143.png -------------------------------------------------------------------------------- /make_data_set/images/8_153.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_153.png -------------------------------------------------------------------------------- /make_data_set/images/8_155.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_155.png -------------------------------------------------------------------------------- /make_data_set/images/8_156.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_156.png -------------------------------------------------------------------------------- /make_data_set/images/8_157.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_157.png -------------------------------------------------------------------------------- /make_data_set/images/8_159.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_159.png -------------------------------------------------------------------------------- /make_data_set/images/8_160.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_160.png -------------------------------------------------------------------------------- /make_data_set/images/8_163.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_163.png -------------------------------------------------------------------------------- /make_data_set/images/8_190.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_190.png -------------------------------------------------------------------------------- /make_data_set/images/8_193.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_193.png -------------------------------------------------------------------------------- /make_data_set/images/8_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_2.png -------------------------------------------------------------------------------- /make_data_set/images/8_23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_23.png -------------------------------------------------------------------------------- /make_data_set/images/8_43.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_43.png -------------------------------------------------------------------------------- /make_data_set/images/8_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_6.png -------------------------------------------------------------------------------- /make_data_set/images/8_88.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_88.png -------------------------------------------------------------------------------- /make_data_set/images/8_97.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/8_97.png -------------------------------------------------------------------------------- /make_data_set/images/9_100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_100.png -------------------------------------------------------------------------------- /make_data_set/images/9_107.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_107.png -------------------------------------------------------------------------------- /make_data_set/images/9_111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_111.png -------------------------------------------------------------------------------- /make_data_set/images/9_115.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_115.png -------------------------------------------------------------------------------- /make_data_set/images/9_121.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_121.png -------------------------------------------------------------------------------- /make_data_set/images/9_180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_180.png -------------------------------------------------------------------------------- /make_data_set/images/9_183.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_183.png -------------------------------------------------------------------------------- /make_data_set/images/9_187.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_187.png -------------------------------------------------------------------------------- /make_data_set/images/9_197.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_197.png -------------------------------------------------------------------------------- /make_data_set/images/9_198.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_198.png -------------------------------------------------------------------------------- /make_data_set/images/9_35.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_35.png -------------------------------------------------------------------------------- /make_data_set/images/9_38.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_38.png -------------------------------------------------------------------------------- /make_data_set/images/9_46.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_46.png -------------------------------------------------------------------------------- /make_data_set/images/9_85.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_85.png -------------------------------------------------------------------------------- /make_data_set/images/9_90.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_90.png -------------------------------------------------------------------------------- /make_data_set/images/9_91.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_91.png -------------------------------------------------------------------------------- /make_data_set/images/9_96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/make_data_set/images/9_96.png -------------------------------------------------------------------------------- /make_data_set/make_hdf5_data_set.py: -------------------------------------------------------------------------------- 1 | import os 2 | import h5py 3 | import numpy as np 4 | from PIL import Image 5 | from sklearn.model_selection import train_test_split 6 | 7 | """ 8 | h5py: 2.7.1 9 | PIL 4.3.0 (pip install pillow) 10 | numpy 1.13.1 11 | sklearn: 0.19.1 12 | """ 13 | if not os.path.exists('./data_set'): 14 | os.mkdir('./data_set') 15 | 16 | 17 | def make_hdf5_data_set(): 18 | x, y = [], [] 19 | 20 | for i, image_path in enumerate(os.listdir('./images')): 21 | # label转为独热编码后再保存 22 | label = int(image_path.split('_')[0]) 23 | label_one_hot = [0 if i != label else 1 for i in range(10)] 24 | y.append(label_one_hot) 25 | 26 | # 图片像素值映射到 0 - 1之间 27 | image = Image.open('./images/{}'.format(image_path)).convert('L') 28 | image_arr = 1 - np.reshape(image, 784) / 255.0 29 | x.append(image_arr) 30 | 31 | with h5py.File('./data_set/data.h5', 'w') as f: 32 | f.create_dataset('x_data', data=np.array(x)) 33 | f.create_dataset('y_data', data=np.array(y)) 34 | 35 | 36 | class DataSet: 37 | def __init__(self): 38 | with h5py.File('./data_set/data.h5', 'r') as f: 39 | x, y = f['x_data'].value, f['y_data'].value 40 | 41 | self.train_x, self.test_x, self.train_y, self.test_y = \ 42 | train_test_split(x, y, test_size=0.2, random_state=0) 43 | 44 | self.train_size = len(self.train_x) 45 | 46 | def get_train_batch(self, batch_size=64): 47 | # 随机获取batch_size个训练数据 48 | choice = np.random.randint(self.train_size, size=batch_size) 49 | batch_x = self.train_x[choice, :] 50 | batch_y = self.train_y[choice, :] 51 | 52 | return batch_x, batch_y 53 | 54 | def get_test_set(self): 55 | return self.test_x, self.test_y 56 | 57 | 58 | if __name__ == '__main__': 59 | make_hdf5_data_set() 60 | import time 61 | 62 | s = time.time() 63 | for i in range(1000): 64 | data_set = DataSet() 65 | train_x, train_y = data_set.get_train_batch() 66 | test_x, test_y = data_set.get_test_set() 67 | 68 | print(time.time() - s) 69 | -------------------------------------------------------------------------------- /make_data_set/make_npy_data_set.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | from PIL import Image 4 | from sklearn.model_selection import train_test_split 5 | 6 | """ 7 | PIL 4.3.0 (pip install pillow) 8 | numpy 1.13.1 9 | sklearn: 0.19.1 10 | """ 11 | if not os.path.exists('./data_set'): 12 | os.mkdir('./data_set') 13 | 14 | 15 | def make_npy_data_set(): 16 | x, y = [], [] 17 | 18 | for i, image_path in enumerate(os.listdir('./images')): 19 | # label转为独热编码后再保存 20 | label = int(image_path.split('_')[0]) 21 | label_one_hot = [0 if i != label else 1 for i in range(10)] 22 | y.append(label_one_hot) 23 | 24 | # 图片像素值映射到 0 - 1之间 25 | image = Image.open('./images/{}'.format(image_path)).convert('L') 26 | image_arr = 1 - np.reshape(image, 784) / 255.0 27 | x.append(image_arr) 28 | 29 | np.save('data_set/X.npy', np.array(x)) 30 | np.save('data_set/Y.npy', np.array(y)) 31 | 32 | 33 | class DataSet: 34 | def __init__(self): 35 | x, y = np.load('data_set/X.npy'), np.load('data_set/Y.npy') 36 | self.train_x, self.test_x, self.train_y, self.test_y = \ 37 | train_test_split(x, y, test_size=0.2, random_state=0) 38 | 39 | self.train_size = len(self.train_x) 40 | 41 | def get_train_batch(self, batch_size=64): 42 | # 随机获取batch_size个训练数据 43 | choice = np.random.randint(self.train_size, size=batch_size) 44 | batch_x = self.train_x[choice, :] 45 | batch_y = self.train_y[choice, :] 46 | 47 | return batch_x, batch_y 48 | 49 | def get_test_set(self): 50 | return self.test_x, self.test_y 51 | 52 | 53 | if __name__ == '__main__': 54 | make_npy_data_set() 55 | 56 | import time 57 | 58 | s = time.time() 59 | for i in range(1000): 60 | data_set = DataSet() 61 | train_x, train_y = data_set.get_train_batch() 62 | test_x, test_y = data_set.get_test_set() 63 | 64 | print(time.time() - s) 65 | -------------------------------------------------------------------------------- /mnist/data_set/t10k-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/data_set/t10k-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /mnist/data_set/t10k-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/data_set/t10k-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /mnist/data_set/train-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/data_set/train-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /mnist/data_set/train-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/data_set/train-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /mnist/data_set_tf2/mnist.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/data_set_tf2/mnist.npz -------------------------------------------------------------------------------- /mnist/test_images/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/test_images/0.png -------------------------------------------------------------------------------- /mnist/test_images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/test_images/1.png -------------------------------------------------------------------------------- /mnist/test_images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geektutu/tensorflow-tutorial-samples/0a16bfc12111da1da1ace0ccfa9895ac0d974be7/mnist/test_images/4.png -------------------------------------------------------------------------------- /mnist/v1/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | class Network: 5 | def __init__(self): 6 | # 学习速率,一般在 0.00001 - 0.5 之间 7 | self.learning_rate = 0.001 8 | 9 | # 输入张量 28 * 28 = 784个像素的图片一维向量 10 | self.x = tf.placeholder(tf.float32, [None, 784]) 11 | 12 | # 标签值,即图像对应的结果,如果对应数字是8,则对应label是 [0,0,0,0,0,0,0,0,1,0] 13 | # 这种方式称为 one-hot编码 14 | # 标签是一个长度为10的一维向量,值最大的下标即图片上写的数字 15 | self.label = tf.placeholder(tf.float32, [None, 10]) 16 | 17 | # 权重,初始化全 0 18 | self.w = tf.Variable(tf.zeros([784, 10])) 19 | # 偏置 bias, 初始化全 0 20 | self.b = tf.Variable(tf.zeros([10])) 21 | # 输出 y = softmax(X * w + b) 22 | self.y = tf.nn.softmax(tf.matmul(self.x, self.w) + self.b) 23 | 24 | # 损失,即交叉熵,最常用的计算标签(label)与输出(y)之间差别的方法 25 | self.loss = -tf.reduce_sum(self.label * tf.log(self.y + 1e-10)) 26 | 27 | # 反向传播,采用梯度下降的方法。调整w与b,使得损失(loss)最小 28 | # loss越小,那么计算出来的y值与 标签(label)值越接近,准确率越高 29 | self.train = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(self.loss) 30 | 31 | # 以下代码验证正确率时使用 32 | # argmax 返回最大值的下标,最大值的下标即答案 33 | # 例如 [0,0,0,0.9,0,0.1,0,0,0,0] 代表数字3 34 | predict = tf.equal(tf.argmax(self.label, 1), tf.argmax(self.y, 1)) 35 | 36 | # predict -> [true, true, true, false, false, true] 37 | # reduce_mean即求predict的平均数 即 正确个数 / 总数,即正确率 38 | self.accuracy = tf.reduce_mean(tf.cast(predict, "float")) 39 | -------------------------------------------------------------------------------- /mnist/v1/train.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.examples.tutorials.mnist import input_data 3 | from model import Network 4 | 5 | ''' 6 | python 3.6 7 | tensorflow 1.4 8 | ''' 9 | 10 | 11 | class Train: 12 | def __init__(self): 13 | self.net = Network() 14 | 15 | # 初始化 session 16 | # Network() 只是构造了一张计算图,计算需要放到会话(session)中 17 | self.sess = tf.Session() 18 | # 初始化变量 19 | self.sess.run(tf.global_variables_initializer()) 20 | 21 | # 读取训练和测试数据,这是tensorflow库自带的,不存在训练集会自动下载 22 | # 项目目录下已经下载好,删掉后,重新运行代码会自动下载 23 | # data_set/train-images-idx3-ubyte.gz 24 | # data_set/train-labels-idx1-ubyte.gz 25 | # data_set/t10k-images-idx3-ubyte.gz 26 | # data_set/t10k-labels-idx1-ubyte.gz 27 | self.data = input_data.read_data_sets('../data_set', one_hot=True) 28 | 29 | def train(self): 30 | # batch_size 是指每次迭代训练,传入训练的图片张数。 31 | # 数据集小,可以使用全数据集,数据大的情况下, 32 | # 为了提高训练速度,用随机抽取的n张图片来训练,效果与全数据集相近 33 | # https://www.zhihu.com/question/32673260 34 | batch_size = 64 35 | 36 | # 总的训练次数 37 | train_step = 2000 38 | 39 | # 开始训练 40 | for i in range(train_step): 41 | # 从数据集中获取 输入和标签(也就是答案) 42 | x, label = self.data.train.next_batch(batch_size) 43 | # 每次计算train,更新整个网络 44 | # loss只是为了看到损失的大小,方便打印 45 | _, loss = self.sess.run([self.net.train, self.net.loss], 46 | feed_dict={self.net.x: x, self.net.label: label}) 47 | 48 | # 打印 loss,训练过程中将会看到,loss有变小的趋势 49 | # 代表随着训练的进行,网络识别图像的能力提高 50 | # 但是由于网络规模较小,后期没有明显下降,而是有明显波动 51 | if (i + 1) % 10 == 0: 52 | print('第%5d步,当前loss:%.2f' % (i + 1, loss)) 53 | 54 | def calculate_accuracy(self): 55 | test_x = self.data.test.images 56 | test_label = self.data.test.labels 57 | # 注意:与训练不同的是,并没有计算 self.net.train 58 | # 只计算了accuracy这个张量,所以不会更新网络 59 | # 最终准确率约为0.91 60 | accuracy = self.sess.run(self.net.accuracy, 61 | feed_dict={self.net.x: test_x, self.net.label: test_label}) 62 | print("准确率: %.2f,共测试了%d张图片 " % (accuracy, len(test_label))) 63 | 64 | 65 | if __name__ == "__main__": 66 | app = Train() 67 | app.train() 68 | app.calculate_accuracy() 69 | -------------------------------------------------------------------------------- /mnist/v2/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | class Network: 5 | def __init__(self): 6 | self.learning_rate = 0.001 7 | # 记录已经训练的次数 8 | self.global_step = tf.Variable(0, trainable=False) 9 | 10 | self.x = tf.placeholder(tf.float32, [None, 784]) 11 | self.label = tf.placeholder(tf.float32, [None, 10]) 12 | 13 | self.w = tf.Variable(tf.zeros([784, 10])) 14 | self.b = tf.Variable(tf.zeros([10])) 15 | self.y = tf.nn.softmax(tf.matmul(self.x, self.w) + self.b) 16 | 17 | self.loss = -tf.reduce_sum(self.label * tf.log(self.y + 1e-10)) 18 | 19 | # minimize 可传入参数 global_step, 每次训练 global_step的值会增加1 20 | # 因此,可以通过计算self.global_step这个张量的值,知道当前训练了多少步 21 | self.train = tf.train.GradientDescentOptimizer(self.learning_rate).minimize( 22 | self.loss, global_step=self.global_step) 23 | 24 | predict = tf.equal(tf.argmax(self.label, 1), tf.argmax(self.y, 1)) 25 | self.accuracy = tf.reduce_mean(tf.cast(predict, "float")) 26 | -------------------------------------------------------------------------------- /mnist/v2/predict.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | from PIL import Image 4 | 5 | from model import Network 6 | 7 | ''' 8 | python 3.6 9 | tensorflow 1.4 10 | pillow(PIL) 4.3.0 11 | 使用tensorflow的模型来预测手写数字 12 | 输入是28 * 28像素的图片,输出是个具体的数字 13 | ''' 14 | 15 | CKPT_DIR = 'ckpt' 16 | 17 | 18 | class Predict: 19 | def __init__(self): 20 | self.net = Network() 21 | self.sess = tf.Session() 22 | self.sess.run(tf.global_variables_initializer()) 23 | 24 | # 加载模型到sess中 25 | self.restore() 26 | 27 | def restore(self): 28 | saver = tf.train.Saver() 29 | ckpt = tf.train.get_checkpoint_state(CKPT_DIR) 30 | if ckpt and ckpt.model_checkpoint_path: 31 | saver.restore(self.sess, ckpt.model_checkpoint_path) 32 | else: 33 | raise FileNotFoundError("未保存任何模型") 34 | 35 | def predict(self, image_path): 36 | # 读图片并转为黑白的 37 | img = Image.open(image_path).convert('L') 38 | flatten_img = np.reshape(img, 784) 39 | x = np.array([1 - flatten_img]) 40 | y = self.sess.run(self.net.y, feed_dict={self.net.x: x}) 41 | 42 | # 因为x只传入了一张图片,取y[0]即可 43 | # np.argmax()取得独热编码最大值的下标,即代表的数字 44 | print(image_path) 45 | print(' -> Predict digit', np.argmax(y[0])) 46 | 47 | 48 | if __name__ == "__main__": 49 | app = Predict() 50 | app.predict('../test_images/0.png') 51 | app.predict('../test_images/1.png') 52 | app.predict('../test_images/4.png') 53 | -------------------------------------------------------------------------------- /mnist/v2/train.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.examples.tutorials.mnist import input_data 3 | from model import Network 4 | 5 | ''' 6 | python 3.6 7 | tensorflow 1.4 8 | 9 | 重点对保存模型的部分添加了注释 10 | 如果想看其他代码的注释,请移步 v1 11 | v2 版本比 v1 版本增加了模型的保存和继续训练 12 | ''' 13 | 14 | CKPT_DIR = 'ckpt' 15 | 16 | 17 | class Train: 18 | def __init__(self): 19 | self.net = Network() 20 | self.sess = tf.Session() 21 | self.sess.run(tf.global_variables_initializer()) 22 | self.data = input_data.read_data_sets('../data_set', one_hot=True) 23 | 24 | def train(self): 25 | batch_size = 64 26 | train_step = 10000 27 | # 记录训练次数, 初始化为0 28 | step = 0 29 | 30 | # 每隔1000步保存模型 31 | save_interval = 1000 32 | 33 | # tf.train.Saver是用来保存训练结果的。 34 | # max_to_keep 用来设置最多保存多少个模型,默认是5 35 | # 如果保存的模型超过这个值,最旧的模型将被删除 36 | saver = tf.train.Saver(max_to_keep=10) 37 | 38 | ckpt = tf.train.get_checkpoint_state(CKPT_DIR) 39 | if ckpt and ckpt.model_checkpoint_path: 40 | saver.restore(self.sess, ckpt.model_checkpoint_path) 41 | # 读取网络中的global_step的值,即当前已经训练的次数 42 | step = self.sess.run(self.net.global_step) 43 | print('Continue from') 44 | print(' -> Minibatch update : ', step) 45 | 46 | while step < train_step: 47 | x, label = self.data.train.next_batch(batch_size) 48 | _, loss = self.sess.run([self.net.train, self.net.loss], 49 | feed_dict={self.net.x: x, self.net.label: label}) 50 | step = self.sess.run(self.net.global_step) 51 | if step % 1000 == 0: 52 | print('第%5d步,当前loss:%.2f' % (step, loss)) 53 | 54 | # 模型保存在ckpt文件夹下 55 | # 模型文件名最后会增加global_step的值,比如1000的模型文件名为 model-1000 56 | if step % save_interval == 0: 57 | saver.save(self.sess, CKPT_DIR + '/model', global_step=step) 58 | 59 | def calculate_accuracy(self): 60 | test_x = self.data.test.images 61 | test_label = self.data.test.labels 62 | accuracy = self.sess.run(self.net.accuracy, 63 | feed_dict={self.net.x: test_x, self.net.label: test_label}) 64 | print("准确率: %.2f,共测试了%d张图片 " % (accuracy, len(test_label))) 65 | 66 | 67 | if __name__ == "__main__": 68 | app = Train() 69 | app.train() 70 | app.calculate_accuracy() 71 | -------------------------------------------------------------------------------- /mnist/v3/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | class Network: 5 | def __init__(self): 6 | self.learning_rate = 0.001 7 | self.global_step = tf.Variable(0, trainable=False, name="global_step") 8 | 9 | self.x = tf.placeholder(tf.float32, [None, 784], name="x") 10 | self.label = tf.placeholder(tf.float32, [None, 10], name="label") 11 | 12 | self.w = tf.Variable(tf.zeros([784, 10]), name="fc/weight") 13 | self.b = tf.Variable(tf.zeros([10]), name="fc/bias") 14 | self.y = tf.nn.softmax(tf.matmul(self.x, self.w) + self.b, name="y") 15 | 16 | self.loss = -tf.reduce_sum(self.label * tf.log(self.y + 1e-10)) 17 | self.train = tf.train.GradientDescentOptimizer(self.learning_rate).minimize( 18 | self.loss, global_step=self.global_step) 19 | 20 | predict = tf.equal(tf.argmax(self.label, 1), tf.argmax(self.y, 1)) 21 | self.accuracy = tf.reduce_mean(tf.cast(predict, "float")) 22 | 23 | # 创建 summary node 24 | # w, b 画直方图 25 | # loss, accuracy画标量图 26 | tf.summary.histogram('weight', self.w) 27 | tf.summary.histogram('bias', self.b) 28 | tf.summary.scalar('loss', self.loss) 29 | tf.summary.scalar('accuracy', self.accuracy) 30 | -------------------------------------------------------------------------------- /mnist/v3/predict.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | from PIL import Image 4 | 5 | from model import Network 6 | 7 | ''' 8 | python 3.6 9 | tensorflow 1.4 10 | pillow(PIL) 4.3.0 11 | 使用tensorflow的模型来预测手写数字 12 | 输入是28 * 28像素的图片,输出是个具体的数字 13 | ''' 14 | 15 | CKPT_DIR = 'ckpt' 16 | 17 | 18 | class Predict: 19 | def __init__(self): 20 | self.net = Network() 21 | self.sess = tf.Session() 22 | self.sess.run(tf.global_variables_initializer()) 23 | 24 | # 加载模型到sess中 25 | self.restore() 26 | 27 | def restore(self): 28 | saver = tf.train.Saver() 29 | ckpt = tf.train.get_checkpoint_state(CKPT_DIR) 30 | if ckpt and ckpt.model_checkpoint_path: 31 | saver.restore(self.sess, ckpt.model_checkpoint_path) 32 | else: 33 | raise FileNotFoundError("未保存任何模型") 34 | 35 | def predict(self, image_path): 36 | # 读图片并转为黑白的 37 | img = Image.open(image_path).convert('L') 38 | flatten_img = np.reshape(img, 784) 39 | x = np.array([1 - flatten_img]) 40 | y = self.sess.run(self.net.y, feed_dict={self.net.x: x}) 41 | 42 | # 因为x只传入了一张图片,取y[0]即可 43 | # np.argmax()取得独热编码最大值的下标,即代表的数字 44 | print(image_path) 45 | print(' -> Predict digit', np.argmax(y[0])) 46 | 47 | 48 | if __name__ == "__main__": 49 | app = Predict() 50 | app.predict('../test_images/0.png') 51 | app.predict('../test_images/1.png') 52 | app.predict('../test_images/4.png') 53 | -------------------------------------------------------------------------------- /mnist/v3/train.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.examples.tutorials.mnist import input_data 3 | from model import Network 4 | 5 | ''' 6 | python 3.6 7 | tensorflow 1.4 8 | 9 | 重点对训练可视化的部分添加了注释 10 | 如果想看其他代码的注释,请移步 v1, v2 11 | v3比v2增加了loss和accuracy的可视化 12 | ''' 13 | 14 | CKPT_DIR = 'ckpt' 15 | 16 | 17 | class Train: 18 | def __init__(self): 19 | self.net = Network() 20 | self.sess = tf.Session() 21 | self.sess.run(tf.global_variables_initializer()) 22 | self.data = input_data.read_data_sets('../data_set', one_hot=True) 23 | 24 | def train(self): 25 | batch_size = 64 26 | train_step = 20000 27 | step = 0 28 | save_interval = 1000 29 | saver = tf.train.Saver(max_to_keep=5) 30 | 31 | # merge所有的summary node 32 | merged_summary_op = tf.summary.merge_all() 33 | # 可视化存储目录为当前文件夹下的 log 34 | merged_writer = tf.summary.FileWriter("./log", self.sess.graph) 35 | 36 | ckpt = tf.train.get_checkpoint_state(CKPT_DIR) 37 | if ckpt and ckpt.model_checkpoint_path: 38 | saver.restore(self.sess, ckpt.model_checkpoint_path) 39 | # 读取网络中的global_step的值,即当前已经训练的次数 40 | step = self.sess.run(self.net.global_step) 41 | print('Continue from') 42 | print(' -> Minibatch update : ', step) 43 | 44 | while step < train_step: 45 | x, label = self.data.train.next_batch(batch_size) 46 | _, loss, merged_summary = self.sess.run( 47 | [self.net.train, self.net.loss, merged_summary_op], 48 | feed_dict={self.net.x: x, self.net.label: label} 49 | ) 50 | step = self.sess.run(self.net.global_step) 51 | 52 | if step % 100 == 0: 53 | merged_writer.add_summary(merged_summary, step) 54 | 55 | if step % save_interval == 0: 56 | saver.save(self.sess, CKPT_DIR + '/model', global_step=step) 57 | print('%s/model-%d saved' % (CKPT_DIR, step)) 58 | 59 | def calculate_accuracy(self): 60 | test_x = self.data.test.images 61 | test_label = self.data.test.labels 62 | accuracy = self.sess.run(self.net.accuracy, 63 | feed_dict={self.net.x: test_x, self.net.label: test_label}) 64 | print("准确率: %.2f,共测试了%d张图片 " % (accuracy, len(test_label))) 65 | 66 | 67 | if __name__ == "__main__": 68 | app = Train() 69 | app.train() 70 | app.calculate_accuracy() 71 | -------------------------------------------------------------------------------- /mnist/v4_cnn/predict.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from PIL import Image 3 | import numpy as np 4 | 5 | from train import CNN 6 | 7 | ''' 8 | python 3.7 9 | tensorflow 2.0.0b0 10 | pillow(PIL) 4.3.0 11 | ''' 12 | 13 | 14 | class Predict(object): 15 | def __init__(self): 16 | latest = tf.train.latest_checkpoint('./ckpt') 17 | self.cnn = CNN() 18 | # 恢复网络权重 19 | self.cnn.model.load_weights(latest) 20 | 21 | def predict(self, image_path): 22 | # 以黑白方式读取图片 23 | img = Image.open(image_path).convert('L') 24 | img = np.reshape(img, (28, 28, 1)) / 255. 25 | x = np.array([1 - img]) 26 | 27 | # API refer: https://keras.io/models/model/ 28 | y = self.cnn.model.predict(x) 29 | 30 | # 因为x只传入了一张图片,取y[0]即可 31 | # np.argmax()取得最大值的下标,即代表的数字 32 | print(image_path) 33 | print(y[0]) 34 | print(' -> Predict digit', np.argmax(y[0])) 35 | 36 | 37 | if __name__ == "__main__": 38 | app = Predict() 39 | app.predict('../test_images/0.png') 40 | app.predict('../test_images/1.png') 41 | app.predict('../test_images/4.png') 42 | -------------------------------------------------------------------------------- /mnist/v4_cnn/train.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tensorflow as tf 3 | from tensorflow.keras import datasets, layers, models 4 | 5 | ''' 6 | python 3.7 7 | tensorflow 2.0.0b0 8 | ''' 9 | 10 | 11 | class CNN(object): 12 | def __init__(self): 13 | model = models.Sequential() 14 | # 第1层卷积,卷积核大小为3*3,32个,28*28为待训练图片的大小 15 | model.add(layers.Conv2D( 16 | 32, (3, 3), activation='relu', input_shape=(28, 28, 1))) 17 | model.add(layers.MaxPooling2D((2, 2))) 18 | # 第2层卷积,卷积核大小为3*3,64个 19 | model.add(layers.Conv2D(64, (3, 3), activation='relu')) 20 | model.add(layers.MaxPooling2D((2, 2))) 21 | # 第3层卷积,卷积核大小为3*3,64个 22 | model.add(layers.Conv2D(64, (3, 3), activation='relu')) 23 | 24 | model.add(layers.Flatten()) 25 | model.add(layers.Dense(64, activation='relu')) 26 | model.add(layers.Dense(10, activation='softmax')) 27 | 28 | model.summary() 29 | 30 | self.model = model 31 | 32 | 33 | class DataSource(object): 34 | def __init__(self): 35 | # mnist数据集存储的位置,如何不存在将自动下载 36 | data_path = os.path.abspath(os.path.dirname( 37 | __file__)) + '/../data_set_tf2/mnist.npz' 38 | (train_images, train_labels), (test_images, 39 | test_labels) = datasets.mnist.load_data(path=data_path) 40 | # 6万张训练图片,1万张测试图片 41 | train_images = train_images.reshape((60000, 28, 28, 1)) 42 | test_images = test_images.reshape((10000, 28, 28, 1)) 43 | # 像素值映射到 0 - 1 之间 44 | train_images, test_images = train_images / 255.0, test_images / 255.0 45 | 46 | self.train_images, self.train_labels = train_images, train_labels 47 | self.test_images, self.test_labels = test_images, test_labels 48 | 49 | class Train: 50 | def __init__(self): 51 | self.cnn = CNN() 52 | self.data = DataSource() 53 | 54 | def train(self): 55 | check_path = './ckpt/cp-{epoch:04d}.ckpt' 56 | # period 每隔5epoch保存一次 57 | save_model_cb = tf.keras.callbacks.ModelCheckpoint( 58 | check_path, save_weights_only=True, verbose=1, period=5) 59 | 60 | self.cnn.model.compile(optimizer='adam', 61 | loss='sparse_categorical_crossentropy', 62 | metrics=['accuracy']) 63 | self.cnn.model.fit(self.data.train_images, self.data.train_labels, 64 | epochs=5, callbacks=[save_model_cb]) 65 | 66 | test_loss, test_acc = self.cnn.model.evaluate( 67 | self.data.test_images, self.data.test_labels) 68 | print("准确率: %.4f,共测试了%d张图片 " % (test_acc, len(self.data.test_labels))) 69 | 70 | 71 | if __name__ == "__main__": 72 | app = Train() 73 | app.train() 74 | --------------------------------------------------------------------------------