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 modified lessonOne/imgClassifierWeb/__pycache__/cnnModel.cpython-36.pyc
Binary file not shown.
61 changes: 47 additions & 14 deletions lessonOne/imgClassifierWeb/cnnModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,85 @@ def create_conv_layer(input_data, filter_size, num_filters):
conv_layer = tf.nn.conv2d(input=input_data,
filter=filters,
strides=[1, 1, 1, 1],
padding="VALID")
padding="SAME")
print("Size of conv result : ", conv_layer.shape)

return filters, conv_layer

def create_CNN(input_data, num_classes, keep_prop):
filters1, conv_layer1 = create_conv_layer(input_data=input_data, filter_size=5, num_filters=4)
filters1, conv_layer1 = create_conv_layer(input_data=input_data, filter_size=3, num_filters=64)
relu_layer1 = tf.nn.relu(conv_layer1)
print("Size of relu1 result : ", relu_layer1.shape)
max_pooling_layer1 = tf.nn.max_pool(value=relu_layer1,
ksize=[1, 2, 2, 1],
strides=[1, 1, 1, 1],
padding="VALID")
padding="SAME")
print("Size of maxpool1 result : ", max_pooling_layer1.shape)

filters2, conv_layer2 = create_conv_layer(input_data=max_pooling_layer1, filter_size=7, num_filters=3)
filters2, conv_layer2 = create_conv_layer(input_data=max_pooling_layer1, filter_size=3, num_filters=64)
relu_layer2 = tf.nn.relu(conv_layer2)
print("Size of relu2 result : ", relu_layer2.shape)
max_pooling_layer2 = tf.nn.max_pool(value=relu_layer2,
ksize=[1, 2, 2, 1],
strides=[1, 1, 1, 1],
padding="VALID")
strides=[1, 2, 2, 1],
padding="SAME")
print("Size of maxpool2 result : ", max_pooling_layer2.shape)

# Conv layer with 2 filters and a filter sisze of 5x5.
filters3, conv_layer3 = create_conv_layer(input_data=max_pooling_layer2, filter_size=5, num_filters=2)

filters3, conv_layer3 = create_conv_layer(input_data=max_pooling_layer2, filter_size=3, num_filters=64)
relu_layer3 = tf.nn.relu(conv_layer3)
print("Size of relu3 result : ", relu_layer3.shape)
max_pooling_layer3 = tf.nn.max_pool(value=relu_layer3,
ksize=[1, 2, 2, 1],
strides=[1, 1, 1, 1],
strides=[1, 2, 2, 1],
padding="VALID")
print("Size of maxpool3 result : ", max_pooling_layer3.shape)

# Adding dropout layer before the fully connected layers to avoid overfitting.
flattened_layer = dropout_flatten_layer(previous_layer=max_pooling_layer3, keep_prop=keep_prop)

# First fully connected (FC) layer. It accepts the result of the dropout layer after being flattened (1D).

filters4, conv_layer4 = create_conv_layer(input_data=max_pooling_layer3, filter_size=3, num_filters=128)
relu_layer4 = tf.nn.relu(conv_layer4)
print("Size of relu4 result : ", relu_layer4.shape)
max_pooling_layer4 = tf.nn.max_pool(value=relu_layer4,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding="SAME")
print("Size of maxpool4 result : ", max_pooling_layer4.shape)



filters5, conv_layer5 = create_conv_layer(input_data=max_pooling_layer4, filter_size=3, num_filters=128)
relu_layer5 = tf.nn.relu(conv_layer5)
print("Size of relu5 result : ", relu_layer5.shape)
max_pooling_layer5 = tf.nn.max_pool(value=relu_layer5,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding="SAME")
print("Size of maxpool5 result : ", max_pooling_layer5.shape)



filters6, conv_layer6 = create_conv_layer(input_data=max_pooling_layer5, filter_size=3, num_filters=128)
relu_layer6 = tf.nn.relu(conv_layer6)
print("Size of relu6 result : ", relu_layer6.shape)
max_pooling_layer6 = tf.nn.max_pool(value=relu_layer6,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding="SAME")
print("Size of maxpool6 result : ", max_pooling_layer6.shape)

# 将输出维度降为一维,并增加dropout防止过拟合
flattened_layer = dropout_flatten_layer(previous_layer=max_pooling_layer6, keep_prop=keep_prop)

# 全连接网络+dropout
fc_resultl = fc_layer(flattened_layer=flattened_layer,
num_inputs=flattened_layer.get_shape()[1:].num_elements(),
num_outputs=200)
# Second fully connected layer accepting the output of the previous fully connected layer. Number of outputs is equal to the number of dataset classes.
# 全连接网络+dropout,这里的网络输出的数量要和标注的数量一致
fc_result2 = fc_layer(flattened_layer=fc_resultl, num_inputs=fc_resultl.get_shape()[1:].num_elements(),
num_outputs=num_classes)
print("Fully connected layer results : ", fc_result2)
return fc_result2 # Returning the result of the last FC layer.
return fc_result2

def dropout_flatten_layer(previous_layer, keep_prop):

Expand Down
4 changes: 2 additions & 2 deletions lessonOne/imgClassifierWeb/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ num_channels = 3
num_files=5
images_per_file=10000
max_gradient_norm=5
percent=1

[floats]
learning_rate = 0.01
keeps=0.5
learning_rate_decay_factor = 0.9
percent=1
learning_rate_decay_factor = 0.5
end_learning_rate=0.0

7 changes: 2 additions & 5 deletions lessonOne/imgClassifierWeb/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ def create_model(session,forward_only):
if ckpt and ckpt.model_checkpoint_path:
print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
model.saver.restore(session, ckpt.model_checkpoint_path)
session.run(tf.global_variables_initializer())
graph = tf.get_default_graph()

return model,graph

else:
print("Created model with fresh parameters.")
session.run(tf.global_variables_initializer())
Expand Down Expand Up @@ -130,8 +127,8 @@ def train():

# 达到一个训练模型保存点后,将模型保存下来,并打印出这个保存点的平均准确率
if current_step % gConfig['steps_per_checkpoint'] == 0:
#如果超过三次预测正确率没有升高则改变学习率
if len(previous_correct) > 2 and accuracy < min(previous_correct[-3:]):
#如果超过5次预测正确率没有升高则改变学习率
if len(previous_correct) > 2 and accuracy < min(previous_correct[-5:]):
sess.run(model.learning_rate_decay_op)
previous_correct.append(accuracy)
checkpoint_path = os.path.join(gConfig['working_directory'], "cnn.ckpt")
Expand Down
65 changes: 49 additions & 16 deletions lessonThree/Anti-Fraud-App/VAE.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
import getConfig
import sys

import os
gConfig={}
gConfig=getConfig.get_config(config_file='config.ini')
#定义训练数据的维度
seq_len=gConfig['seqlen']

#class vaeModel(object):

# def __init__(self, learning_rate,learning_rate_decay_factor):
# 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 read_data(source_file):
Expand Down Expand Up @@ -76,27 +73,63 @@ def decoder(sampled_z, keep_prob):
decoder_set = tf.reshape(y_out, shape=[-1, seq_len, 1])
return decoder_set
#定义计算loss以及进行优化器优化的一系列tensor
global_step = tf.Variable(0, trainable=False)
sampled, mn, sd = encoder(X_in, keep_prob)
dec = decoder(sampled, keep_prob)
unreshaped = tf.reshape(dec, [-1, seq_len*1])
img_loss = tf.reduce_sum(tf.squared_difference(unreshaped, Y_flat), 1)
latent_loss = -0.5 * tf.reduce_sum(1.0 + 2.0 * sd - tf.square(mn) - tf.exp(2.0 * sd), 1)
dst_loss=img_loss+latent_loss
loss = tf.reduce_mean(img_loss + latent_loss)
optimizer = tf.train.AdamOptimizer(gConfig['learning_rate']).minimize(loss)
optimizer = tf.train.AdamOptimizer(gConfig['learning_rate']).minimize(loss,global_step=global_step)
sess = tf.Session()
sess.run(tf.global_variables_initializer())

saver = tf.train.Saver(tf.all_variables())


def vae_train():

#开始vae的训练
for i in range(gConfig['vae_steps']):
batch=dataset
sess.run(optimizer, feed_dict = {X_in: batch, Y: batch, keep_prob: 0.8})
if not i % 200:
for i in range(gConfig['vae_steps']):
batch=dataset
#通过循环训练来通过优化器进行BP计算,进而更新参数使网络进行拟合,这里使用的是fullbatch模式
sess.run(optimizer, feed_dict = {X_in: batch, Y: batch, keep_prob: 0.8})
#根据checkpoint点对网络的收敛情况进行监测并保存下来模型
if not i % 200:
ls, d, i_ls, d_ls, mu, sampled_data = sess.run([loss, dec, img_loss, dst_loss, mn, sampled], feed_dict = {X_in: batch, Y: batch, keep_prob: 1.0})

print(i, ls, np.mean(i_ls), np.mean(d_ls))
checkpoint_path = os.path.join(gConfig['working_directory'], "vae.ckpt")
saver=tf.train.Saver()
saver.save(sess,checkpoint_path,global_step=global_step)
#最后将训练集的所有的数据的encoder数据保存下来,用于接下来的kmeans训练
sampled_data = sess.run(sampled, feed_dict = {X_in: batch, Y: batch, keep_prob: 1.0})
sampled_data=pd.DataFrame(sampled_data)
sampled_data.to_csv(gConfig['sampled_path'])

#定义
def vae_encoder(sess,input_data):


ckpt=tf.train.get_checkpoint_state(gConfig['working_directory'])
if ckpt and ckpt.model_checkpoint_path:
print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path)
graph = tf.get_default_graph()

sampled_data = sess.run(sampled, feed_dict = {X_in: input_data, Y: input_data, keep_prob: 1.0})

return sampled_data


if __name__=='__main__':
if len(sys.argv) - 1:
gConfig = getConfig(sys.argv[1])
else:
# get configuration from config.ini
gConfig = getConfig.get_config()
if gConfig['mode']=='train':
vae_train()
elif gConfig['mode']=='server':
print('Sever Usage:python3 app.py')


#保存训练的encode的结果,就是要进行特征压缩后的特征
sampled_data=pd.DataFrame(sampled_data)
sampled_data.to_csv(gConfig['sampled_path'])
3 changes: 2 additions & 1 deletion lessonThree/Anti-Fraud-App/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import hashlib
import threading
import execute
import VAE

#
app = Flask(__name__)
#路由注解,我们这里使用的是path的形式进行传参
#示例url:http://0.0.0.0:8088/predict/1/2/3/4/5/6/7/8/9/10/11/12 这里的1...12换成需要进行聚类的值就可以了
Expand All @@ -26,6 +26,7 @@ def predict(a,b,c,d,e,f,g,h,i,j,k,l):
lines=range(k)
lines=[int(i) for i in line]
lines=[lines]
lines=VAE.vae_encoder(VAE.sess,lines)
predict_result=execute.predicts(lines)

return jsonify( { 'result of cluster': str(predict_result) } )
Expand Down
4 changes: 2 additions & 2 deletions lessonThree/Anti-Fraud-App/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ steps=10
#VAE训练步数
vae_steps=300
#需要聚类的序列的长度,如果要将vae和clustering串起来用这个值要和enc_out_len保持一致
seqlen=12
seqlen=8
#vae encoder输出的中间向量的长度
encoutlen=8

Expand All @@ -25,7 +25,7 @@ keeps=0.5
[strings]
mode = train
working_directory=working_directory/
input_file=train_data/train_data.csv
input_file=train_data/sampled_data.csv
#这里model的文件路径,要根据kmeansMode中的文件夹的来设置
model_path=kmeansMode/1542258970
sampled_path=train_data/sampled_data.csv
Expand Down
1 change: 1 addition & 0 deletions lessonThree/Anti-Fraud-App/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
最新的代码将VAE和kmeans进行了结合,如果需要使用的话,需要先执行python3 VAE.py 然后执行python3 execute.py 最后再执行python3.py
Loading