Skip to content

Commit 2245f36

Browse files
committed
update pytorch demo and tf2.0 demo.
1 parent aa47fe0 commit 2245f36

41 files changed

Lines changed: 5540 additions & 7579 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
from __future__ import absolute_import, division, print_function
7+
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
import pandas as pd
11+
import tensorflow as tf
12+
from tensorflow import keras
13+
from tensorflow.keras import layers
14+
15+
print(tf.__version__)
16+
17+
(train_images, train_labels), (test_images, test_labels) = keras.datasets.fashion_mnist.load_data()
18+
a = pd.DataFrame(train_labels)
19+
print(a)
20+
b = a[0].value_counts()
21+
print(b)
22+
23+
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
24+
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
25+
# 2.探索数据
26+
print(train_images.shape)
27+
print(train_labels.shape)
28+
print(test_images.shape)
29+
print(test_labels.shape)
30+
# 3.处理数据
31+
plt.figure()
32+
plt.imshow(train_images[0])
33+
plt.colorbar()
34+
plt.grid(False)
35+
plt.show()
36+
plt.savefig("0.png")
37+
38+
train_images = train_images / 255.0
39+
test_images = test_images / 255.0
40+
plt.figure(figsize=(10, 10))
41+
for i in range(25):
42+
plt.subplot(5, 5, i + 1)
43+
plt.xticks([])
44+
plt.yticks([])
45+
plt.grid(False)
46+
plt.imshow(train_images[i], cmap=plt.cm.binary)
47+
plt.xlabel(class_names[train_labels[i]])
48+
plt.show()
49+
50+
# 4.构造网络
51+
model = keras.Sequential(
52+
[
53+
layers.Flatten(input_shape=[28, 28]),
54+
layers.Dense(128, activation='relu'),
55+
layers.Dense(10, activation='softmax')
56+
])
57+
model.compile(optimizer='adam',
58+
loss='sparse_categorical_crossentropy',
59+
metrics=['accuracy'])
60+
# 5.train
61+
model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
62+
63+
# 6.save
64+
model_path = "image_model.h5"
65+
model.save(model_path)
66+
del model
67+
68+
# 7.load model
69+
model = keras.models.load_model(model_path)
70+
predictions = model.predict(test_images)
71+
print(predictions[0])
72+
print(np.argmax(predictions[0]))
73+
print(test_labels[0])
74+
75+
76+
def plot_image(i, predictions_array, true_label, img):
77+
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
78+
plt.grid(False)
79+
plt.xticks([])
80+
plt.yticks([])
81+
82+
plt.imshow(img, cmap=plt.cm.binary)
83+
84+
predicted_label = np.argmax(predictions_array)
85+
if predicted_label == true_label:
86+
color = 'blue'
87+
else:
88+
color = 'red'
89+
90+
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
91+
100 * np.max(predictions_array),
92+
class_names[true_label]),
93+
color=color)
94+
95+
96+
def plot_value_array(i, predictions_array, true_label):
97+
predictions_array, true_label = predictions_array[i], true_label[i]
98+
plt.grid(False)
99+
plt.xticks([])
100+
plt.yticks([])
101+
thisplot = plt.bar(range(10), predictions_array, color="#777777")
102+
plt.ylim([0, 1])
103+
predicted_label = np.argmax(predictions_array)
104+
105+
thisplot[predicted_label].set_color('red')
106+
thisplot[true_label].set_color('blue')
107+
108+
109+
i = 0
110+
plt.figure(figsize=(6, 3))
111+
plt.subplot(1, 2, 1)
112+
plot_image(i, predictions, test_labels, test_images)
113+
plt.subplot(1, 2, 2)
114+
plot_value_array(i, predictions, test_labels)
115+
plt.show()
116+
117+
# Plot the first X test images, their predicted label, and the true label
118+
# Color correct predictions in blue, incorrect predictions in red
119+
num_rows = 5
120+
num_cols = 3
121+
num_images = num_rows * num_cols
122+
plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows))
123+
for i in range(num_images):
124+
plt.subplot(num_rows, 2 * num_cols, 2 * i + 1)
125+
plot_image(i, predictions, test_labels, test_images)
126+
plt.subplot(num_rows, 2 * num_cols, 2 * i + 2)
127+
plot_value_array(i, predictions, test_labels)
128+
plt.show()
129+
130+
img = test_images[0]
131+
132+
img = (np.expand_dims(img, 0))
133+
134+
print(img.shape)
135+
predictions_single = model.predict(img)
136+
137+
print(predictions_single)
138+
plot_value_array(0, predictions_single, test_labels)
139+
_ = plt.xticks(range(10), class_names, rotation=45)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
import tensorflow as tf
10+
from tensorflow import keras
11+
12+
print(tf.__version__)
13+
14+
imdb = keras.datasets.imdb
15+
(train_x, train_y), (test_x, text_y) = keras.datasets.imdb.load_data(num_words=10000)
16+
print('train_x len:', len(train_x), train_x.shape)
17+
print("Training entries: {}, labels: {}".format(len(train_x), len(train_y)))
18+
19+
print(train_x[0])
20+
print('len: ', len(train_x[0]), len(train_x[1]))
21+
22+
word_index = imdb.get_word_index()
23+
24+
word2id = {k: (v + 3) for k, v in word_index.items()}
25+
word2id['<PAD>'] = 0
26+
word2id['<START>'] = 1
27+
word2id['<UNK>'] = 2
28+
word2id['<UNUSED>'] = 3
29+
30+
id2word = {v: k for k, v in word2id.items()}
31+
32+
33+
def get_words(sent_ids):
34+
return ' '.join([id2word.get(i, '?') for i in sent_ids])
35+
36+
37+
sent = get_words(train_x[0])
38+
print(sent)
39+
# prepare data
40+
# 句子末尾padding
41+
train_x = keras.preprocessing.sequence.pad_sequences(
42+
train_x, value=word2id['<PAD>'],
43+
padding='post', maxlen=256
44+
)
45+
test_x = keras.preprocessing.sequence.pad_sequences(
46+
test_x, value=word2id['<PAD>'],
47+
padding='post', maxlen=256
48+
)
49+
print(train_x[0])
50+
print('len: ', len(train_x[0]), len(train_x[1]))
51+
# build model
52+
import tensorflow.keras.layers as layers
53+
54+
vocab_size = 10000
55+
model = keras.Sequential()
56+
model.add(layers.Embedding(vocab_size, 16))
57+
model.add(layers.GlobalAveragePooling1D())
58+
model.add(layers.Dense(16, activation='relu'))
59+
model.add(layers.Dense(1, activation='sigmoid'))
60+
model.summary()
61+
model.compile(optimizer='adam',
62+
loss='binary_crossentropy',
63+
metrics=['accuracy'])
64+
65+
# 4.模型训练与验证
66+
x_val = train_x[:10000]
67+
x_train = train_x[10000:]
68+
69+
y_val = train_y[:10000]
70+
y_train = train_y[10000:]
71+
72+
history = model.fit(x_train, y_train,
73+
epochs=40, batch_size=512,
74+
validation_data=(x_val, y_val),
75+
verbose=1)
76+
77+
result = model.evaluate(test_x, text_y)
78+
print(result)
79+
80+
import matplotlib.pyplot as plt
81+
82+
history_dict = history.history
83+
history_dict.keys()
84+
acc = history_dict['accuracy']
85+
val_acc = history_dict['val_accuracy']
86+
loss = history_dict['loss']
87+
val_loss = history_dict['val_loss']
88+
epochs = range(1, len(acc) + 1)
89+
90+
plt.plot(epochs, loss, 'bo', label='train loss')
91+
plt.plot(epochs, val_loss, 'b', label='val loss')
92+
plt.title('Train and val loss')
93+
plt.xlabel('Epochs')
94+
plt.xlabel('loss')
95+
plt.legend()
96+
# plt.show()
97+
plt.savefig("val_loss.png")
98+
# plt accuracy
99+
plt.clf() # clear figure
100+
101+
plt.plot(epochs, acc, 'bo', label='Training acc')
102+
plt.plot(epochs, val_acc, 'b', label='Validation acc')
103+
plt.title('Training and validation accuracy')
104+
plt.xlabel('Epochs')
105+
plt.ylabel('Accuracy')
106+
plt.legend()
107+
108+
# plt.show()
109+
plt.savefig("val_acc.png")

0 commit comments

Comments
 (0)