-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNN_demo.py
More file actions
68 lines (56 loc) · 1.55 KB
/
CNN_demo.py
File metadata and controls
68 lines (56 loc) · 1.55 KB
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
# -*- coding: utf-8 -*-
#
# @method : CNN 图像分类Demo
# @Time : 2018/4/20
# @Author : wooght
# @File : CNN_demo.py
from wooght.image.load_data import load_data
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, Flatten, MaxPooling2D
from keras.optimizers import Adam
img_data, target= load_data('wooght/image')
img_data = img_data/255 # 规范数据
target = np_utils.to_categorical(target, num_classes=10)
print(target)
model = Sequential() # 全连接层
model.add(Convolution2D(
batch_input_shape=(None, 1, 28, 28),
filters=32,
kernel_size=5,
strides=1,
padding='same',
data_format='channels_first'
))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding='same',
data_format='channels_first'
))
model.add(Convolution2D(
filters=64,
kernel_size=5,
strides=1,
padding='same',
data_format='channels_first'
))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding='same',
data_format='channels_first'
))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(optimizer=Adam(lr=1e-4),
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(img_data[:8000,:], target[:8000,:], epochs=1, batch_size=32)
loss, accuracy = model.evaluate(img_data[8000:,:], target[8000:,:])
print('loss:',loss,'\naccuracy:', accuracy)