-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
91 lines (73 loc) · 2.73 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import os
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
FLAGS = None
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
cwd='./data/'
classes={'class0','class1'}
def main(_):
for name in enumerate(classes):
label = name[1][-1]
# print label
# print name[1]
class_path=cwd+name[1]
# print class_path
for data_path in os.listdir(class_path):
data_path=class_path+'/'+data_path #每一个图片的地址
#print(data_path)
input = []
filename = data_path
for img in os.listdir(filename):
data = np.genfromtxt(filename+'/'+img, delimiter=',')
input.append(data[0:200, 1:4])
input[0] = np.expand_dims(input[0], axis=0)
for i in range(1,4):
input[i] = np.expand_dims(input[i], axis=0)
input[0] = np.concatenate((input[0], input[i]), axis=0)
input[0] = input[0].reshape(1, 2400)
#print(input[0].shape) # (1, 2400)
train_data = input[0]
print(train_data.shape)
tmp = train_data
train_data = np.concatenate((tmp, train_data), axis=0)
print(train_data.shape) # (2, 2400)
label = np.array(([0, 1], [1, 0]))
print(label.shape)
# Import data
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
# Create the model
x = tf.placeholder(tf.float32, [None, 2400])
W = tf.Variable(tf.zeros([2400, 2]))
b = tf.Variable(tf.zeros([2]))
y = tf.matmul(x, W) + b
print(mnist.test.images.shape) # numpy.ndarray
print(mnist.test.labels.shape)
y_ = tf.placeholder(tf.float32, [None, 2])
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# Train
for _ in range(1):
#batch_xs, batch_ys = mnist.train.next_batch(100)
#sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
sess.run(train_step, feed_dict={x: train_data, y_: label})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: train_data,
y_: label}))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)