Reputation:
Data is from CIFAR-10 I've made the following code originally this code was meant for only 2 conv layer and one fully connected layer. I added one more conv layer with 128 4X4 filter. I have defined a class for extracting batches of a training set. i used a batch size of 100 but now when I'm trying to find out my y_pred it's shape is becoming [200,10] while it should have been [100,10] because my batch size itself is 100 and not 200. if I'm removing the last conv layer which I added then my code is working perfectly fine, but I don't want to do that, please tell me what should I do so, please help
def one_hot_encode(vec, vals = 10):
n = len(vec)
out = np.zeros((n,vals))
out[range(n), vec] = 1
return out
class CifarHelper():
def __init__(self):
self.i = 0
self.all_train_batches = [data_batch1, data_batch2, data_batch3, data_batch4, data_batch5]
self.test_batch = [test_batch]
self.training_images = None
self.training_labels = None
self.test_images = None
self.test_labels = None
def set_up_images(self):
print('setting up Training images and labels')
self.training_images = np.vstack([d[b'data'] for d in self.all_train_batches])
train_len = len(self.training_images)
self.training_images = self.training_images.reshape(train_len, 3, 32, 32).transpose(0, 2, 3, 1)/255
self.training_labels = one_hot_encode(np.hstack([d[b"labels"] for d in self.all_train_batches]))
print('Setting up test images and labels')
self.test_images = np.vstack([d[b'data'] for d in self.test_batch])
test_len = len(self.test_images)
self.test_images = self.test_images.reshape(test_len, 3, 32, 32).transpose(0, 2, 3, 1)/255
self.test_labels = one_hot_encode(np.hstack([d[b"labels"] for d in self.test_batch]))
def next_batch(self, batch_size):
x = self.training_images[self.i:self.i+batch_size].reshape(batch_size, 32, 32, 3)
y = self.training_labels[self.i:self.i+batch_size]
self.i = (self.i + batch_size) % len(self.training_images)
return x, y
ch = CifarHelper()
ch.set_up_images()
x = tf.placeholder(tf.float32, [None, 32, 32, 3])
y_true = tf.placeholder(tf.float32, [None, 10])
hold_prob = tf.placeholder(tf.float32)
def init_weights(shape):
init_random_dist = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(init_random_dist)
def init_bais(shape):
init_bais_vals = tf.constant(0.1, shape = shape)
return tf.Variable(init_bais_vals)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2by2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def convolutional_layer(input_x, shape):
W = init_weights(shape)
b = init_bais([shape[3]])
return tf.nn.relu(conv2d(input_x, W) + b)
def normal_full_layer(input_layer, size):
input_size = int(input_layer.get_shape()[1])
W = init_weights([input_size, size])
b = init_bais([size])
return tf.matmul(input_layer, W) + b
convo_1 = convolutional_layer(x, [4, 4, 3, 32])
convo_1_pooling = max_pool_2by2(convo_1)
convo_2 = convolutional_layer(convo_1_pooling, [4, 4, 32, 64])
convo_2_pooling = max_pool_2by2(convo_2)
convo_3 = convolutional_layer(convo_2_pooling, [4, 4, 64, 128])
convo_3_pooling = max_pool_2by2(convo_3)
convo_3_flat = tf.reshape(convo_2_pooling, [-1, 4*4*128])
full_layer_one = tf.nn.relu(normal_full_layer(convo_3_flat, 1024))
full_one_dropout = tf.nn.dropout(full_layer_one, keep_prob = hold_prob)
y_pred = normal_full_layer(full_one_dropout, 10)
batch = ch.next_batch(100)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
a = sess.run(y_pred, feed_dict = {x:batch[0], y_true:batch[1], hold_prob:.5})
a.shape
expected shape of y_pred is [100,10] but actually, it is coming out to be [200,10]
Upvotes: 1
Views: 161
Reputation:
there's a problem in flattening the layer, i guess in reshaping. use tf.layers.flatten instead of making your own flatten layer. that would work.
Upvotes: 0