IndexError Using TFLearn and MNIST - python-3.x

I am having an Indexing Error when running the following code:
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist
X, Y, test_x, test_y = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
test_x = X.reshape([-1, 28, 28, 1])
convnet = input_data(shape=[None, 28, 28, 1], name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 10, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01,
loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet)
model.fit({'input':X},{'targets':Y}, n_epoch=10,
validation_set=({'input':test_x},{'targets':test_y}),
snapshot_step=500, show_metric=True, run_id='mnist')
model.save('tflearncnn.model')
I cannot figure out how to make the index larger than 0-9999 (10000) as i am not sure where the error is occurring.
here is the error in my Terminal:
---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Exception in thread Thread-5:oss: 0.13790 | time: 29.813s
Traceback (most recent call last):0 - acc: 0.9592 -- iter: 31936/55000
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
data = self.retrieve_data(batch_ids)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/data_flow.py", line 222, in retrieve_data
utils.slice_array(self.feed_dict[key], batch_ids)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/utils.py", line 187, in slice_array
return X[start]
IndexError: index 10000 is out of bounds for axis 0 with size 10000
this happens when i reach the point where a new Epoch is supposed to start as shown when step 499 is reached:
---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Training Step: 499 | total loss: 0.12698 | time: 27.880s
| Adam | epoch: 001 | loss: 0.12698 - acc: 0.9616 -- iter: 31936/55000
I have tried the following:
-Changing size of snapshot_steps
-changing the size of n_units in fully_connected()
-changing the nb_filter in conv_2d

This is just your typo
test_x = X.reshape([-1, 28, 28, 1])
test_x = test_x.reshape([-1, 28, 28, 1])

Related

ValueError when fitting keras model

I have the following code:
from sklearn.datasets import fetch_openml
import numpy as np
import keras
mnist = fetch_openml('mnist_784', version=1)
X, y = mnist["data"], mnist["target"]
y = y.astype(np.uint8)
X_digits = [np.array(X.iloc[i]) for i in range(len(X))]
X = np.array([some_digit.reshape(28, 28) for some_digit in X_digits])
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
model = keras.models.Sequential([
keras.layers.Conv2D(64, 7, activation="relu", padding="same",
input_shape=[28, 28, 1]),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(128, 3, activation="relu", padding="same"),
keras.layers.Conv2D(128, 3, activation="relu", padding="same"),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(256, 3, activation="relu", padding="same"),
keras.layers.Conv2D(256, 3, activation="relu", padding="same"),
keras.layers.MaxPooling2D(2),
keras.layers.Flatten(),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(64, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation="softmax")
])
model.compile(loss="categorical_crossentropy")
That all seems to work fine. But then on this line:
model.fit(X_train, y_train)
I get this error:
ValueError Traceback (most recent call last)
<ipython-input-19-d768f88d541e> in <module>()
----> 1 model.fit(X_train, y_train)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1127 except Exception as e: # pylint:disable=broad-except
1128 if hasattr(e, "ag_error_metadata"):
-> 1129 raise e.ag_error_metadata.to_exception(e)
1130 else:
1131 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 810, in train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1665, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 4994, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (32, 1) and (32, 10) are incompatible
What is going wrong here?
As #Dr. Snoopy the shape of your labels is not correct.
After you split your data into train and test should make sure, that your labels are poperly encode with the number of classes you want to have (in this case 10).
Simply put this after your split and it should work:
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
y_train.shape
Output should be:
(60000, 10)

TensorFlow2: ResNet50 - ValueError

I am trying to use Transfer Learning using ResNet-50 in TensorFlow2 and Keras on CIFAR-10 dataset which has (32, 32, 3) images.
The default ResNet-50's first conv layer uses a filter size of (7, 7) with stride = 2, the resulting CIFAR-10 is reduced too much spatially here which is to be avoided. As a 'hack', the images are attempted to be upscaled from (32, 32) to (224, 224). The code is:
import tensorflow.keras as K
# Define KerasTensor as input-
input_t = K.Input(shape = (32, 32, 3))
res_model = K.applications.ResNet50(
include_top = False,
weights = "imagenet",
input_tensor = input_t
)
# Since CIFAR-10 dataset is small as compared to ImageNet, the images are upscaled to (224, 224)-
to_res = (224, 224)
model = K.models.Sequential()
model.add(K.layers.Lambda(lambda image: tf.image.resize(image, to_res)))
model.add(res_model)
model.add(K.layers.Flatten())
model.add(K.layers.BatchNormalization())
model.add(K.layers.Dense(units = 10, activation = 'softmax'))
# Choose an optimizer and loss function for training-
loss_fn = tf.keras.losses.CategoricalCrossentropy()
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.1, momentum = 0.9)
model.compile(
# loss = 'categorical_crossentropy',
loss = loss_fn,
# optimizer = K.optimizers.RMSprop(lr=2e-5),
optimizer = optimizer,
metrics=['accuracy']
)
history = model.fit(
x = X_train, y = y_train,
batch_size = batch_size, epochs = 10,
validation_data = (X_test, y_test),
# callbacks=[check_point]
)
To which I get the error:
Epoch 1/10 WARNING:tensorflow:Model was constructed with shape (None,
32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32,
32, 3), dtype=tf.float32, name='input_1'), name='input_1',
description="created by layer 'input_1'"), but it was called on an
input with incompatible shape (None, 224, 224, 3).
ValueError Traceback (most recent call
last)
in ()
2 x = X_train, y = y_train,
3 batch_size = batch_size, epochs = 10,
----> 4 validation_data = (X_test, y_test),
5 # callbacks=[check_point]
6 )
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py
in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
ValueError: Input 0 is incompatible with layer resnet50: expected
shape=(None, 32, 32, 3), found shape=(None, 224, 224, 3)
The input of the model is still (32, 32, 3)
input_t = K.Input(shape = (32, 32, 3))

ValueError: Cannot feed value of shape, but shape seems good

I have some matrix from colored images (dimension 100*100), and I get an error when I try to run my neural network :
def simple_nn(X_training, Y_training, X_test, Y_test):
input = 100*100*3
batch_size = 25 #not used
X = tf.placeholder(tf.float32, [1, 100, 100, 3])
W = tf.Variable(tf.zeros([input, 2]))
b = tf.Variable(tf.zeros([2]))
init = tf.global_variables_initializer()
# model
Y = tf.nn.softmax(tf.matmul(tf.reshape(X, [-1, input]), W) + b)
# placeholder for correct labels
Y_ = tf.placeholder(tf.float32, [None, 2])
# loss function
cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y))
# % of correct answers found in batch
is_correct = tf.equal(tf.argmax(Y,1), tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
optimizer = tf.train.GradientDescentOptimizer(0.003)
train_step = optimizer.minimize(cross_entropy)
sess = tf.Session()
sess.run(init)
for i in range(len(X_training)):
# st = batch_size * i
# end = st + batch_size - 1
batch_X, batch_Y = X_training[i], Y_training[i]
train_data={X: batch_X, Y_: batch_Y}
sess.run(train_step, feed_dict=train_data)
a,c = sess.run([accuracy, cross_entropy], feed_dict=train_data)
# success on test data ?
test_data={X: X_test, Y_: Y_test}
a,c = sess.run([accuracy, cross_entropy], feed=test_data)
My error :
Traceback (most recent call last):
File "neural_net.py", line 90, in <module>
simple_nn(X_training, Y_training, X_test, Y_test)
File "neural_net.py", line 71, in simple_nn
sess.run(train_step, feed_dict=train_data)
File "/home/.../venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/home/.../venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1128, in _run
str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (100, 100, 3) for Tensor 'Placeholder:0', which has shape '(1, 100, 100, 3)'
I don't understand why I get this error since my batchs are of size 1, I have no clue how to change the shape to resolve this error.
And if I replace this line :
X = tf.placeholder(tf.float32, [1, 100, 100, 3])
By this one (without "1") :
X = tf.placeholder(tf.float32, [1, 100, 100, 3])
I get this error :
ValueError: Cannot feed value of shape (2,) for Tensor 'Placeholder_1:0', which has shape '(?, 2)'
I resolve my (dumb) issue, I replaced :
batch_X, batch_Y = X_training[i], Y_training[i]
By this :
batch_X, batch_Y = [X_training[i]], [Y_training[i]]
So I have one more dimension in my batches

Neupy neural network issues

I'm trying to train / use a convolutional neural network with neupy library for a project, but I'm getting errors in the training phase.
I have many images (rgb, shape=66, 160, 3) and I split them in the training and test sets. Then I'm trying to train one convolutional neural network (I'll try to optimize with different algorithm, layer number and size later). The target output for my project is a number [-1, 1], I'm solving a regression problem but I have issues before.
The error I'm getting right now is:
ValueError: Cannot shuffle matrices. All matrices should have the same number of rows
The relevant code:
print numpy.array(y_train).shape
# outputs (84, 66, 160, 3)
print numpy.array(y_test).shape
# outputs (15, 66, 160, 3)
cgnet = algorithms.Adadelta(
[
layers.Input((6, 66, 160*3)),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.MaxPooling((2, 2)),
layers.Reshape(),
layers.Linear(1024),
layers.Softmax(10),
],
error='categorical_crossentropy',
step=1.0,
verbose=True,
shuffle_data=True,
#shuffle_data=False,
reduction_freq=8,
addons=[algorithms.StepDecay],
)
print cgnet.architecture()
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)
Output:
Main information
[ALGORITHM] Adadelta
[OPTION] batch_size = 128
[OPTION] verbose = True
[OPTION] epoch_end_signal = None
[OPTION] show_epoch = 1
[OPTION] shuffle_data = True
[OPTION] step = 1.0
[OPTION] train_end_signal = None
[OPTION] error = categorical_crossentropy
[OPTION] addons = ['StepDecay']
[OPTION] decay = 0.95
[OPTION] epsilon = 1e-05
[OPTION] reduction_freq = 8
[THEANO] Initializing Theano variables and functions.
[THEANO] Initialization finished successfully. It took 7.01 seconds
Network's architecture
-------------------------------------------------
| # | Input shape | Layer Type | Output shape |
-------------------------------------------------
| 1 | (6, 66, 480) | Input | (6, 66, 480) |
| 2 | (6, 66, 480) | Convolution | (8, 64, 478) |
| 3 | (8, 64, 478) | Relu | (8, 64, 478) |
| 4 | (8, 64, 478) | Convolution | (8, 62, 476) |
| 5 | (8, 62, 476) | Relu | (8, 62, 476) |
| 6 | (8, 62, 476) | MaxPooling | (8, 31, 238) |
| 7 | (8, 31, 238) | Reshape | 59024 |
| 8 | 59024 | Linear | 1024 |
| 9 | 1024 | Softmax | 10 |
-------------------------------------------------
None
Start training
[TRAIN DATA] 84 samples, feature shape: (66, 160, 3)
[TEST DATA] 15 samples, feature shape: (66, 160, 3)
[TRAINING] Total epochs: 100
------------------------------------------------
| Epoch # | Train err | Valid err | Time |
------------------------------------------------
Traceback (most recent call last):
File "./ml_neupy.py", line 68, in <module>
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/constructor.py", line 539, in train
*args, **kwargs
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/learning.py", line 49, in train
summary=summary
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/base.py", line 409, in train
target_train)
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/utils.py", line 146, in shuffle
raise ValueError("Cannot shuffle matrices. All matrices should "
ValueError: Cannot shuffle matrices. All matrices should have the same number of rows
What is wrong with the input data or the network?
Thanks
There are a few things that you need to modify:
You've mentioned that you are trying to solve regression problem. Your network has a Softmax layer as the output, which means that your network can give you only outputs from [0, 1] range, instead of [-1, 1]. You can change it to Tanh layer. It will produce output from [-1, 1] range.
Cross entropy error suitable only for classification problems
error='categorical_crossentropy'
For regression you can use MSE or RMSE (more error functions you can find here)
error='mse'
I assume that in the (66, 160, 3) shape number 3 defines each of the RGB channels. NeuPy works with Theano library, which means that you need to define channel shape before the width and height of the image. The right order is: (n_samples, n_channels, height, width). In your case I assume that you have 84 samples, height of 66 pixels, width of 160 pixels and 3 channels (RGB). If that true then you need to transform your input as follows
# convert this shape (n_samples, height, width, n_channels)
# to (n_samples, n_channels, height, width)
x_train = x_train.transpose((0, 3, 1, 2))
print(x_train.shape) # (84, 3, 66, 160)
Output from the final layer should be 1 instead of 10. It means that you predict only one value per each sample, instead of vector with 10 values (use layers.Tanh(1) instead of layers.Softmax(10))
The following code works without errors (doesn't train properly, because values are random):
import numpy
from neupy import algorithms, layers
x_train = numpy.random.random((84, 3, 66, 160))
x_test = numpy.random.random((15, 3, 66, 160))
y_train = numpy.random.random(84)
y_test = numpy.random.random(15)
cgnet = algorithms.Adadelta(
[
layers.Input((3, 66, 160)),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.MaxPooling((2, 2)),
layers.Reshape(),
layers.Linear(1024),
layers.Tanh(1),
],
error='mse',
step=1.0,
verbose=True,
shuffle_data=True,
reduction_freq=8,
addons=[algorithms.StepDecay],
)
cgnet.architecture()
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)

Memory error while using keras

I am using keras for CNN but the problem is that there is memory leak. The error is
anushreej#cpusrv-gpu-109:~/12EC35005/MTP_Workspace/MTP$ python cnn_implement.py
Using Theano backend.
[INFO] compiling model...
Traceback (most recent call last):
File "cnn_implement.py", line 23, in <module>
model = CNNModel.build(width=150, height=150, depth=3)
File "/home/ms/anushreej/12EC35005/MTP_Workspace/MTP/cnn/networks/model_define.py", line 27, in build
model.add(Dense(depth*height*width))
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/models.py", line 146, in add
output_tensor = layer(self.outputs[0])
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 458, in __call__
self.build(input_shapes[0])
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/layers/core.py", line 604, in build
name='{}_W'.format(self.name))
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 61, in glorot_uniform
return uniform(shape, s, name=name)
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 32, in uniform
return K.variable(np.random.uniform(low=-scale, high=scale, size=shape),
File "mtrand.pyx", line 1255, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:13575)
File "mtrand.pyx", line 220, in mtrand.cont2_array_sc (numpy/random/mtrand/mtrand.c:2902)
MemoryError
Now I am unable to understand why is this happening. My training images are very small of the size 150*150*3.
The code is -:
# import the necessary packages
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
class CNNModel:
#staticmethod
def build(width, height, depth):
# initialize the model
model = Sequential()
# first set of CONV => RELU
model.add(Convolution2D(50, 5, 5, border_mode="same", batch_input_shape=(None, depth, height, width)))
model.add(Activation("relu"))
# second set of CONV => RELU
# model.add(Convolution2D(50, 5, 5, border_mode="same"))
# model.add(Activation("relu"))
# third set of CONV => RELU
# model.add(Convolution2D(50, 5, 5, border_mode="same"))
# model.add(Activation("relu"))
model.add(Flatten())
model.add(Dense(depth*height*width))
# if weightsPath is not None:
# model.load_weights(weightsPath)
return model
I faced the same problem, I think the issue is the number data points just before the Flattening layer are more than your system can handle(i tried in difference systems so one with high ram worked and with less ram gave this error). Just add more CNN layers to reduce the size and then add a flattening layer it works.
This gave me and error:
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax'))
This didnt give an error
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(Convolution2D(128, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax')
Hope it helps.

Resources