-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
184 lines (144 loc) · 6.69 KB
/
data.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 19:41:39 2019
@author: 78237
#get_ipython().magic(u'matplotlib inline')
"""
from __future__ import print_function
from PIL import Image
import numpy as np
import tensorflow as tf
import os
import glob
import matplotlib.pyplot as plt
# In[2]:
def conv_block(inputs, out_channels, name='conv'):
with tf.variable_scope(name):
conv = tf.layers.conv2d(inputs, out_channels, kernel_size=3, padding='SAME')
conv = tf.contrib.layers.batch_norm(conv, updates_collections=None, decay=0.99, scale=True, center=True)
conv = tf.nn.relu(conv)
conv = tf.contrib.layers.max_pool2d(conv, 2)
return conv
# In[3]:
def encoder(x, h_dim, z_dim, reuse=False):
with tf.variable_scope('encoder', reuse=reuse):
net = conv_block(x, h_dim, name='conv_1')
net = conv_block(net, h_dim, name='conv_2')
net = conv_block(net, h_dim, name='conv_3')
net = conv_block(net, z_dim, name='conv_4')
net = tf.contrib.layers.flatten(net)
return net
def euclidean_distance(a, b):
# a.shape = N x D
# b.shape = M x D
N, D = tf.shape(a)[0], tf.shape(a)[1]
M = tf.shape(b)[0]
a = tf.tile(tf.expand_dims(a, axis=1), (1, M, 1))
b = tf.tile(tf.expand_dims(b, axis=0), (N, 1, 1))
return tf.reduce_mean(tf.square(a - b), axis=2)
# In[5]:
n_epochs = 5
n_episodes = 5
n_way = 60
n_shot = 5
n_query = 5
n_examples = 20
im_width, im_height, channels = 224, 224, 1
h_dim = 64
z_dim = 64
root_dir = './car_dataset'
train_split_path = os.path.join(root_dir,'devkit','train_perfect_preds.txt')
with open(train_split_path, 'r') as train_split:
train_classes = [line.rstrip() for line in train_split.readlines()]
n_classes = len(train_classes)
train_dataset = np.zeros([n_classes, n_examples,im_height, im_width], dtype=np.float32)
#n_batch = n_examples*n_classes / batch_size
for i, tc in enumerate(train_classes):
#alphabet, character, rotation = tc.split('/')
#rotation = float(rotation[3:])
im_dir = os.path.join(root_dir, 'train_crop')
im_files = sorted(glob.glob(os.path.join(train_split_path, '*.jpg')))
for j, im_file in enumerate(im_files):
im = 1. - np.array(Image.open(im_file).resize((im_width, im_height)), np.float32, copy=False)
train_dataset[i,j] = im
print(train_dataset.shape)
# In[7]:
x = tf.placeholder(tf.float32, [None, None, im_height, im_width, channels])
q = tf.placeholder(tf.float32, [None, None, im_height, im_width, channels])
x_shape = tf.shape(x)
q_shape = tf.shape(q)
num_classes, num_support = x_shape[0], x_shape[1]
num_queries = q_shape[1]
y = tf.placeholder(tf.int64, [None, None])
y_one_hot = tf.one_hot(y, depth=num_classes)
emb_x = encoder(tf.reshape(x, [num_classes * num_support, im_height, im_width, channels]), h_dim, z_dim)
emb_dim = tf.shape(emb_x)[-1]
emb_x = tf.reduce_mean(tf.reshape(emb_x, [num_classes, num_support, emb_dim]), axis=1)
emb_q = encoder(tf.reshape(q, [num_classes * num_queries, im_height, im_width, channels]), h_dim, z_dim, reuse=True)
dists = euclidean_distance(emb_q, emb_x)
log_p_y = tf.reshape(tf.nn.log_softmax(-dists), [num_classes, num_queries, -1])
ce_loss = -tf.reduce_mean(tf.reshape(tf.reduce_sum(tf.multiply(y_one_hot, log_p_y), axis=-1), [-1]))
acc = tf.reduce_mean(tf.to_float(tf.equal(tf.argmax(log_p_y, axis=-1), y)))
# In[8]:
train_op = tf.train.AdamOptimizer().minimize(ce_loss)
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allocator_type = 'BFC'
config.gpu_options.per_process_gpu_memory_fraction = 0.40
sess = tf.InteractiveSession()
init_op = tf.global_variables_initializer()
sess.run(init_op)
# In[10]:
for ep in range(n_epochs):
for epi in range(n_episodes):
epi_classes = np.random.permutation(n_classes)[:n_way]
support = np.zeros([n_way, n_shot, im_height, im_width], dtype=np.float32)
query = np.zeros([n_way, n_query, im_height, im_width], dtype=np.float32)
for i, epi_cls in enumerate(epi_classes):
selected = np.random.permutation(n_examples)[:n_shot + n_query]
support[i] = train_dataset[epi_cls, selected[:n_shot]]
query[i] = train_dataset[epi_cls, selected[n_shot:]]
support = np.expand_dims(support, axis=-1)
query = np.expand_dims(query, axis=-1)
labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_query)).astype(np.uint8)
_, ls, ac = sess.run([train_op, ce_loss, acc], feed_dict={x: support, q: query, y:labels})
if (epi+1) % 50 == 0:
print('[epoch {}/{}, episode {}/{}] => loss: {:.5f}, acc: {:.5f}'.format(ep+1, n_epochs, epi+1, n_episodes, ls, ac))
# Load Test Dataset
root_dir = './car_dataset'
test_split_path = os.path.join(root_dir, 'devkit', 'test.txt')
with open(test_split_path, 'r') as test_split:
test_classes = [line.rstrip() for line in test_split.readlines()]
n_test_classes = len(test_classes)
test_dataset = np.zeros([n_test_classes, n_examples, im_height, im_width], dtype=np.float32)
for i, tc in enumerate(test_classes):
#alphabet, character, rotation = tc.split('/')
#rotation = float(rotation[3:])
im_dir = os.path.join(root_dir, 'test_crop', alphabet, character)
im_files = sorted(glob.glob(os.path.join(im_dir, '*.jpg')))
for j, im_file in enumerate(im_files):
im = 1. - np.array(Image.open(im_file).resize((im_width, im_height)), np.float32, copy=False)
test_dataset[i, j] = im
print(test_dataset.shape)
n_test_episodes = 1000
n_test_way = 20
n_test_shot = 5
n_test_query = 15
print('Testing...')
avg_acc = 0.
for epi in range(n_test_episodes):
epi_classes = np.random.permutation(n_test_classes)[:n_test_way]
support = np.zeros([n_test_way, n_test_shot, im_height, im_width], dtype=np.float32)
query = np.zeros([n_test_way, n_test_query, im_height, im_width], dtype=np.float32)
for i, epi_cls in enumerate(epi_classes):
selected = np.random.permutation(n_examples)[:n_test_shot + n_test_query]
support[i] = test_dataset[epi_cls, selected[:n_test_shot]]
query[i] = test_dataset[epi_cls, selected[n_test_shot:]]
support = np.expand_dims(support, axis=-1)
query = np.expand_dims(query, axis=-1)
labels = np.tile(np.arange(n_test_way)[:, np.newaxis], (1, n_test_query)).astype(np.uint8)
ls, ac = sess.run([ce_loss, acc], feed_dict={x: support, q: query, y:labels})
avg_acc += ac
if (epi+1) % 50 == 0:
print('[test episode {}/{}] => loss: {:.5f}, acc: {:.5f}'.format(epi+1, n_test_episodes, ls, ac))
avg_acc /= n_test_episodes
print('Average Test Accuracy: {:.5f}'.format(avg_acc))