Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file modified lessonOne/imgClassifierWeb/__pycache__/cnnModel.cpython-36.pyc
Binary file not shown.
Binary file modified lessonOne/imgClassifierWeb/__pycache__/execute.cpython-36.pyc
Binary file not shown.
Binary file modified lessonOne/imgClassifierWeb/__pycache__/getConfig.cpython-36.pyc
Binary file not shown.
Binary file added lessonOne/imgClassifierWeb/airplane.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 25 additions & 50 deletions lessonOne/imgClassifierWeb/app.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
"""
Good and quick.
Deeper in ML and DL.
Overfitting.
Github readme instructions should tell only how to run the code.
"""

import flask
import werkzeug
import os
import scipy.misc
import tensorflow as tf
import getConfig
import execute
gConfig={}
gConfig=getConfig.get_config(config_file='config.ini')

#Creating a new Flask Web application. It accepts the package name.
gConfig = {}
gConfig = getConfig.get_config(config_file='config.ini')

# Creating a new Flask Web application. It accepts the package name.
app = flask.Flask("imgClassifierWeb")

def CNN_predict():

def CNN_predict():
global sess
global model
global graph
Expand All @@ -28,13 +22,13 @@ def CNN_predict():
global:
"""
global secure_filename
#从本地目录读取需要分类的图片
# 从本地目录读取需要分类的图片
img = scipy.misc.imread(os.path.join(app.root_path, secure_filename))

"""
校验图片格式
"""
if(img.ndim) == 3:
if (img.ndim) == 3:
"""
是否为32*32
"""
Expand All @@ -44,112 +38,93 @@ def CNN_predict():
"""
if img.shape[-1] == 3:



predicted_class = execute.predict_line(sess,model,img,graph)
predicted_class = execute.predict_line(sess, model, img, graph)
"""
将返回的结果用页面模板给渲染出来
"""
return flask.render_template(template_name_or_list="prediction_result.html", predicted_class=predicted_class)
return flask.render_template(template_name_or_list="prediction_result.html",
predicted_class=predicted_class)
else:
""" 如果检测出图片格式不符合要求,则返回错误并返回上传图片的格式"""
return flask.render_template(template_name_or_list="error.html", img_shape=img.shape)
else:
""" 如果检测出图片格式不符合要求,则返回错误并返回上传图片的格式"""
return flask.render_template(template_name_or_list="error.html", img_shape=img.shape)
return "遇到非图片格式的未知错误,请联系技术人员解决"


"""
flask路由系统:

1、使用flask.Flask.route() 修饰器。
2、使用flask.Flask.add_url_rule()函数。

3、直接访问基于werkzeug路由系统的flask.Flask.url_map.

参考知识链接:https://www.jianshu.com/p/e69016bd8f08

1、@app.route('/index.html')
def index():
return "Hello World!"

2、def index():
return "Hello World!"
index = app.route('/index.html')(index)



app.add_url_rule:app.add_url_rule(rule,endpoint,view_func)

关于rule、ednpoint、view_func以及函数注册路由的原理可以参考:https://www.cnblogs.com/eric-nirnava/p/endpoint.html

"""
app.add_url_rule(rule="/predict/", endpoint="predict", view_func=CNN_predict)
"""
知识点:
flask.request属性

form:
一个从POST和PUT请求解析的 MultiDict(一键多值字典)。

args:
MultiDict,要操作 URL (如 ?key=value )中提交的参数可以使用 args 属性:

searchword = request.args.get('key', '')

values:
CombinedMultiDict,内容是form和args。
可以使用values替代form和args。

cookies:
顾名思义,请求的cookies,类型是dict。

stream:
在可知的mimetype下,如果进来的表单数据无法解码,会没有任何改动的保存到这个·stream·以供使用。很多时候,当请求的数据转换为string时,使用data是最好的方式。这个stream只返回数据一次。

headers:
请求头,字典类型。

data:
包含了请求的数据,并转换为字符串,除非是一个Flask无法处理的mimetype。

files:
MultiDict,带有通过POST或PUT请求上传的文件。

method:
请求方法,比如POST、GET

知识点参考链接:https://blog.csdn.net/yannanxiu/article/details/53116652



werkzeug

"""


def upload_image():
global secure_filename
if flask.request.method == "POST":#设置request的模式为POST
img_file = flask.request.files["image_file"]#获取需要分类的图片
secure_filename = werkzeug.secure_filename(img_file.filename)#生成一个没有乱码的文件名
img_path = os.path.join(app.root_path, secure_filename)#获取图片的保存路径
img_file.save(img_path)#将图片保存在应用的根目录下
if flask.request.method == "POST": # 设置request的模式为POST
img_file = flask.request.files["image_file"] # 获取需要分类的图片
secure_filename = werkzeug.secure_filename(img_file.filename) # 生成一个没有乱码的文件名
img_path = os.path.join(app.root_path, secure_filename) # 获取图片的保存路径
img_file.save(img_path) # 将图片保存在应用的根目录下
print("图片上传成功.")
"""

"""
return flask.redirect(flask.url_for(endpoint="predict"))
return "图片上传失败"


"""
"""
app.add_url_rule(rule="/upload/", endpoint="upload", view_func=upload_image, methods=["POST"])

def redirect_upload():

def redirect_upload():
return flask.render_template(template_name_or_list="upload_image.html")


"""
"""
app.add_url_rule(rule="/", endpoint="homepage", view_func=redirect_upload)
sess = tf.Session()
sess, model,graph = execute.init_session(sess, conf='config.ini')
sess, model, graph = execute.init_session(sess, conf='config.ini')
if __name__ == "__main__":
app.run(host="localhost", port=7777, debug=False)
19 changes: 16 additions & 3 deletions lessonOne/imgClassifierWeb/cnnModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import pickle
import getConfig
from collections import Counter


gConfig={}
Expand Down Expand Up @@ -33,6 +34,7 @@ def __init__(self,percent,learning_rate,learning_rate_decay_factor):
self.percent=percent
self.learning_rate=tf.Variable(float(learning_rate), trainable=False)
self.learning_rate_decay_op = self.learning_rate.assign(self.learning_rate * learning_rate_decay_factor)
self.global_step = tf.Variable(0, trainable=False)

def create_conv_layer(input_data, filter_size, num_filters):
filters = tf.Variable(tf.truncated_normal(shape=(
Expand Down Expand Up @@ -119,8 +121,10 @@ def fc_layer(flattened_layer, num_inputs, num_outputs):
labels=self.label_tensor)
cost=tf.reduce_mean(cross_entropy)

self.ops=tf.train.GradientDescentOptimizer(self.learning_rate).minimize(cost)
#保存所有变量的值
self.ops=tf.train.GradientDescentOptimizer(self.learning_rate).minimize(cost,global_step=self.global_step)
#保存所有变量的
sess=tf.Session()
sess.run(tf.global_variables_initializer())
self.saver = tf.train.Saver(tf.all_variables())

def step(self,sess,shuffled_data,shuffled_labels,graph,forward_only=None):
Expand All @@ -134,6 +138,9 @@ def step(self,sess,shuffled_data,shuffled_labels,graph,forward_only=None):
k_size=gConfig['percent']*gConfig['dataset_size']/100
dataset_array = np.random.rand(int(k_size), 32, 32, 3)
dataset_array[0,:,:,:] = shuffled_data
print(shuffled_data)
print(dataset_array[0])
print(dataset_array)
feed_dict_test={data_tensor:dataset_array,keep_prop:1.0
}
softmax_propabilities_, softmax_predictions_ = sess.run([self.softmax_propabilities, self.softmax_predictions],
Expand All @@ -143,10 +150,16 @@ def step(self,sess,shuffled_data,shuffled_labels,graph,forward_only=None):
patch_bin_file = open(file, 'rb')
label_names_dict = pickle.load(patch_bin_file)
print(label_names_dict)
print(softmax_predictions_[0])
print(softmax_predictions_)
print(Counter(softmax_predictions_).most_common(1))

#k=Counter(softmax_predictions_).most_common(1)
#print(k)
dataset_label_names = label_names_dict["label_names"]
return dataset_label_names[softmax_predictions_[0]]
else:

cnn_feed_dict = {self.data_tensor: shuffled_data, self.label_tensor: shuffled_labels, keep_prop: gConfig['keeps']}
softmax_predictions_, _ = sess.run([self.softmax_predictions, self.ops],feed_dict=cnn_feed_dict)
# 统计预测争取的数量
Expand Down
3 changes: 2 additions & 1 deletion lessonOne/imgClassifierWeb/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Mode : train, test, serve
mode = train
working_directory = model/
dataset_path=/Users/zhaoyingjun/Learning/TensorFlow-Coding/lessionOne/imgClassifierWeb/train_data/
dataset_path=/Users/zhaoyingjun/Learning/TensorFlow_code/lessonOne/imgClassifierWeb/train_data/
dataset_test=/Users/zhaoyingjun/Learning/TensorFlow_code/lessonOne/imgClassifierWeb/test_data/

[ints]
steps_per_checkpoint = 10
Expand Down
Binary file added lessonOne/imgClassifierWeb/deer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessonOne/imgClassifierWeb/dog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 45 additions & 8 deletions lessonOne/imgClassifierWeb/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
gConfig = {}

def read_data(dataset_path, im_dim, num_channels,num_files,images_per_file):
files_names = os.listdir(dataset_path) # 获取训练集中训练文件的名称
files_names = os.listdir(dataset_path)
print(files_names)
# 获取训练集中训练文件的名称
"""
在CIFAR10中已经为我们标注和准备好了数据,一时找不到合适的高质量的标注训练集,我们就是使用CIFAR10的来作为我们的训练集。
在训练集中一共有50000个训练样本,放到5个二进制文件中心,每个样本有3072个像素点,是32*3维度的
Expand All @@ -22,7 +24,7 @@ def read_data(dataset_path, im_dim, num_channels,num_files,images_per_file):
#从训练集中读取二进制数据并将其维度转换成32*32*3
for file_name in files_names:

if file_name[0:len(file_name) - 1] == "data_batch_":
if file_name[0:len(file_name)-1] == "data_batch_":
print("正在处理数据 : ", file_name)
data_dict = unpickle_patch(dataset_path + file_name)
images_data = data_dict[b"data"]
Expand Down Expand Up @@ -71,6 +73,7 @@ def get_batch(data,labels,percent):
np.random.shuffle(shuffled_labels)
return data[shuffled_labels[:num_elements], :, :, :], shuffled_labels[:num_elements]


#定义训练函数
def train():
"""使用BFC内存管理管理算法,tf.ConfigProto()用于GPU的管理,可以控制GPU的使用率
Expand All @@ -92,17 +95,28 @@ def train():


dataset_array, dataset_labels = read_data(dataset_path=gConfig['dataset_path'], im_dim=gConfig['im_dim'],
num_channels=gConfig['num_channels'],num_files=gConfig['num_files'],images_per_file=gConfig['images_per_file'])
num_channels=gConfig['num_channels'],num_files=gConfig['num_files'],images_per_file=gConfig['images_per_file'])


dataset_array_test, dataset_labels_test = read_data(dataset_path=gConfig['dataset_test'], im_dim=gConfig['im_dim'], num_channels=gConfig['num_channels'],num_files=1,images_per_file=gConfig['images_per_file'])
print("Size of data : ", dataset_array.shape)
with tf.Session(config=config) as sess:
model,_=create_model(sess,False)
# 开始训练循环,这里没有设置结束条件,知道最终我们手动结束为止,不过大家可以思考一下该如何设置合适的结束条件以及如何设置?
step_time, accuracy = 0.0, 0.0
current_step = 0
previous_correct = []
shuffled_data, shuffled_labels = get_batch(data=dataset_array, labels=dataset_labels,
percent=gConfig['percent'])

while model.learning_rate.eval()>gConfig['end_learning_rate']:

shuffled_data, shuffled_labels = get_batch(data=dataset_array, labels=dataset_labels,
percent=gConfig['percent'])
#print(shuffled_data)

shuffled_data_test, shuffled_labels_test = get_batch(data=dataset_array_test, labels=dataset_labels_test,
percent=5*gConfig['percent'])


start_time = time.time()
step_correct=model.step(sess,shuffled_data,shuffled_labels,False)
step_time += (time.time() - start_time) / gConfig['steps_per_checkpoint']
Expand All @@ -116,13 +130,36 @@ def train():
sess.run(model.learning_rate_decay_op)
previous_correct.append(accuracy)
checkpoint_path = os.path.join(gConfig['working_directory'], "cnn.ckpt")
model.saver.save(sess, checkpoint_path)
print("在", str(gConfig['percent'] *gConfig['dataset_size']/100),"个样本集上训练的准确率", ' : ', accuracy)
#saver=tf.train.Saver()
model.saver.save(sess, checkpoint_path,global_step=model.global_step)

#sess.run(tf.global_variables_initializer())
#以下为增加模型在测试集上的准确率测试
graph = tf.get_default_graph()

softmax_propabilities = graph.get_tensor_by_name(name="softmax_probs:0")
softmax_predictions = tf.argmax(softmax_propabilities, axis=1)
data_tensor = graph.get_tensor_by_name(name="data_tensor:0")
label_tensor = graph.get_tensor_by_name(name="label_tensor:0")
keep_prop = graph.get_tensor_by_name(name="keep_prop:0")

feed_dict_testing = {data_tensor: shuffled_data_test,
label_tensor: shuffled_labels_test,
keep_prop: 1.0}

softmax_propabilities_, softmax_predictions_ = sess.run([softmax_propabilities, softmax_predictions],
feed_dict=feed_dict_testing)

correct = np.array(np.where(softmax_predictions_ == shuffled_labels_test))
correct = correct.size
print("模型在测试集上的准确率为 : ", correct/(gConfig['percent']*gConfig['dataset_size']/100))


print("在", str(gConfig['percent'] *gConfig['dataset_size']/100),"个训练集上训练的准确率", ' : ', accuracy)
print("学习率 %.4f 每步耗时 %.2f " % ( model.learning_rate.eval(),step_time))
step_time, accuracy = 0.0,0.0
sys.stdout.flush()


def init_session(sess,conf='config.ini'):
global gConfig
gConfig=getConfig.get_config(conf)
Expand Down
Binary file added lessonOne/imgClassifierWeb/im.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions lessonOne/imgClassifierWeb/model/checkpoint

This file was deleted.

Binary file not shown.
Binary file removed lessonOne/imgClassifierWeb/model/cnn.ckpt.index
Binary file not shown.
Binary file removed lessonOne/imgClassifierWeb/model/cnn.ckpt.meta
Binary file not shown.
Loading