Hezi-Resheff/Oreilly-Learning-TensorFlow

CNN code from chapter not running properly

abidmalikwaterloo opened this issue · 1 comments

I wrote the following code from the book ,

import tensorflow as tf
import os
import cPickle
import numpy as np

def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def conv2d(x,W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1],padding='SAME')

def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1],padding='SAME')

def conv_layer(input, shape):
W = weight_variable(shape)
b = bias_variable([shape[3]])
return tf.nn.relu(conv2d(input,W)+b)

def full_layer(input, size):
in_size = int(input.get_shape()[1])
W = weight_variable([in_size, size])
b = bias_variable([size])
return tf.matmul(input, W)+b

class CifarLoader(object):
def init(self, source_files):
self._source = source_files
self._i = 0
self.images = None
self.labels = None

    def     load(self):
            data    =       [unpickle(f) for f in self._source]
            images  =       np.vstack([d["data"] for d in data])
            n       =       len(images)
            self.images     =       images.reshape(n,3,32,32).transpose(0,2,3,1).astype(float)/255
            self.labels     =       one_hot(np.hstack([d["labels"] for d in data]), 10)
            return self

    def     next_batch(self, batch_size):
            x,y     =       self.images[self._i:self._i+batch_size],self.labels[self._i:self._i+batch_size]
            self._i =       (self._i+batch_size)%len(self.images)
            return  x,y

def unpickle(file):
with open(os.path.join(DATA_PATH, file), 'rb') as fo:
dict = cPickle.load(fo)
return dict

def one_hot(vec, vals=10):
n = len(vec)
out = np.zeros((n,vals))
out[range(n), vec]=1
return out

class CifarDataManager(object):
def init(self):
self.train = CifarLoader(["data_batch_{}".format(i) for i in range(1,6)]).load()
self.test = CifarLoader(["test_batch"]).load()

DATA_PATH ="/home/amalik/Programming/Tensorflow/chapter4/CIFAR10/cifar-10-batches-py"

cifar = CifarDataManager()

x = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])

y_ = tf.placeholder(tf.float32, shape=[None, 10])

keep_prob = tf.placeholder(tf.float32)

conv1 = conv_layer(x, shape=[5,5,3,32])
conv1_pool= max_pool_2x2(conv1)

conv2 = conv_layer(conv1_pool, shape=[5,5,32,64])
conv2_pool = max_pool_2x2(conv2)
conv2_flate = tf.reshape(conv2_pool, [-1,8864])

full_1 = tf.nn.relu(full_layer(conv2_flate, 1024))
full1_drop = tf.nn.dropout(full_1, keep_prob=keep_prob)

y_conv = full_layer(full1_drop, 10)

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)
correct_prediction= tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))

accueacy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

STEPS=10
BATCH_SIZE=1

def test(sess):
X = cifar.test.images.reshape(10, 1000, 32, 32, 3)
Y = cifar.test.labels.reshape(10, 1000, 10)
acc = np.mean([sess.run(accuracy, feed_dict={x:X[i], y_:Y[i], keep_prob:1.0}) for i in range(10)])
print "Accuracy : {:.4}%".format(acc*100)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(STEPS):
batch = cifar.train.next_batch(BATCH_SIZE)
sess.run(train_step, feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})
test(sess)


However, I am getting the following error:
python cnn.py
2018-05-29 14:42:48.295115: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
2018-05-29 14:42:48.520456: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: Tesla K20Xm major: 3 minor: 5 memoryClockRate(GHz): 0.732
pciBusID: 0000:08:00.0
totalMemory: 5.57GiB freeMemory: 152.44MiB
2018-05-29 14:42:48.520564: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K20Xm, pci bus id: 0000:08:00.0, compute capability: 3.5)
2018-05-29 14:42:48.536010: E tensorflow/stream_executor/cuda/cuda_driver.cc:936] failed to allocate 152.44M (159842304 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY
2018-05-29 14:42:49.618407: E tensorflow/stream_executor/cuda/cuda_blas.cc:366] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
2018-05-29 14:42:49.634824: E tensorflow/stream_executor/cuda/cuda_blas.cc:366] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
2018-05-29 14:42:49.649660: E tensorflow/stream_executor/cuda/cuda_blas.cc:366] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
2018-05-29 14:42:49.662774: E tensorflow/stream_executor/cuda/cuda_blas.cc:366] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
2018-05-29 14:42:49.675470: E tensorflow/stream_executor/cuda/cuda_blas.cc:366] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
2018-05-29 14:42:49.687850: E tensorflow/stream_executor/cuda/cuda_blas.cc:366] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
2018-05-29 14:42:50.614900: E tensorflow/stream_executor/cuda/cuda_dnn.cc:385] could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
2018-05-29 14:42:50.614959: E tensorflow/stream_executor/cuda/cuda_dnn.cc:352] could not destroy cudnn handle: CUDNN_STATUS_BAD_PARAM
2018-05-29 14:42:50.614979: F tensorflow/core/kernels/conv_ops.cc:667] Check failed: stream->parent()->GetConvolveAlgorithms( conv_parameters.ShouldIncludeWinogradNonfusedAlgo(), &algorithms)

I am using a very small batchs size.

Notice the line about the GPU being used:

totalMemory: 5.57GiB freeMemory: 152.44MiB

I assume there is something else running on it, not leaving enough memory for this program.