-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathcnn.py
More file actions
120 lines (102 loc) · 4.7 KB
/
cnn.py
File metadata and controls
120 lines (102 loc) · 4.7 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import tensorflow as tf
from model import Model
import numpy as np
IMAGE_SIZE = 28
class ClientModel(Model):
def __init__(self, seed, lr, num_classes):
self.num_classes = num_classes
super(ClientModel, self).__init__(seed, lr)
def create_model(self):
"""Model function for CNN."""
with tf.device('/gpu:0'):
features = tf.placeholder(
tf.float32, shape=[None, IMAGE_SIZE * IMAGE_SIZE], name='features')
labels = tf.placeholder(tf.int64, shape=[None], name='labels')
input_layer = tf.reshape(features, [-1, IMAGE_SIZE, IMAGE_SIZE, 1])
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
dense = tf.layers.dense(inputs=pool2_flat, units=2048, activation=tf.nn.relu)
logits = tf.layers.dense(inputs=dense, units=self.num_classes)
predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
# TODO: Confirm that opt initialized once is ok?
train_op = self.optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
eval_metric_ops = tf.count_nonzero(tf.equal(labels, predictions["classes"]))
return features, labels, train_op, eval_metric_ops, loss
# todo fedsp
def create_fedsp_model(self):
"""Model function for FedSP-CNN."""
features = tf.placeholder(
tf.float32, shape=[None, IMAGE_SIZE * IMAGE_SIZE], name='features')
labels = tf.placeholder(tf.int64, shape=[None], name='labels')
input_layer = tf.reshape(features, [-1, IMAGE_SIZE, IMAGE_SIZE, 1])
# global encoder
global_conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu, name='global_conv1')
global_pool1 = tf.layers.max_pooling2d(inputs=global_conv1, pool_size=[2, 2], strides=2)
global_conv2 = tf.layers.conv2d(
inputs=global_pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu, name='global_conv2')
global_pool2 = tf.layers.max_pooling2d(inputs=global_conv2, pool_size=[2, 2], strides=2)
global_pool2_flat = tf.reshape(global_pool2, [-1, 7 * 7 * 64])
# local encoder
local_conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu, name='local_conv1')
local_pool1 = tf.layers.max_pooling2d(inputs=local_conv1, pool_size=[2, 2], strides=2)
local_conv2 = tf.layers.conv2d(
inputs=local_pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu, name='local_conv2')
local_pool2 = tf.layers.max_pooling2d(inputs=local_conv2, pool_size=[2, 2], strides=2)
local_pool2_flat = tf.reshape(local_pool2, [-1, 7 * 7 * 64])
concat_res = tf.concat([global_pool2_flat, local_pool2_flat], 1)
dense = tf.layers.dense(inputs=concat_res, units=2048, activation=tf.nn.relu)
logits = tf.layers.dense(inputs=dense, units=self.num_classes)
predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
# TODO: Confirm that opt initialized once is ok?
train_op = self.optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
eval_metric_ops = tf.count_nonzero(tf.equal(labels, predictions["classes"]))
return features, labels, train_op, eval_metric_ops, loss
@staticmethod
def process_x(raw_x_batch):
return np.array(raw_x_batch)
@staticmethod
def process_y(raw_y_batch):
return np.array(raw_y_batch)