Error while dealing with Gray Scale images in keras over theano - computer-vision

I am doing object detection and i am using keras over theano to build a model. Here is my code.
from keras.preprocessing import image
from scipy.misc import toimage
from keras.optimizers import Adadelta,SGD
from matplotlib import pyplot as plt
from keras.models import Sequential,load_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
import pickle
import numpy as np
X=pickle.load(open('Xvalues.p','rb'))
y=pickle.load(open('yvalues.p','rb'))
X_train=X[:1100,:,:]
y_train=y[:1100]
X_test=X[1100:,:,:]
y_test=y[1100:]
X_train = X_train.reshape(X_train.shape[0], 50, 50,1).astype('float32')
X_test = X_test.reshape(X_test.shape[0],50, 50,1).astype('float32')
#X_train=X_train[:,:,:,np.newaxis]
#X_test=X_test[:,:,:,np.newaxis]
X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
print type(num_classes)
print y_train.shape
print y_test.shape
opt=Adadelta()
model=Sequential()
model.add(Conv2D(48,(5,5),input_shape=(50,50,1),activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(64,(5,5),activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(X_train,y_train,epochs=15, batch_size=32, verbose=1)
print model.summary()
model.save('train2.h5',overwrite=True)
scores = model.evaluate(X_test, y_test, verbose=1)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
when i use coloured image it works well, but when i do it with gray scale image, it gives following error
File "D:/ML/classify.py", line 39, in <module>
model.add(Conv2D(48,(5,5),input_shape=(50,50,1),activation='relu'))
Exception: ('The following error happened while compiling the node', Elemwise{Composite{(i0 + (i1 * i2))}}[(0, 2)](TensorConstant{(1L, 1L, 1..-0.0699854}, TensorConstant{(1L, 1L, 1..f 0.139971}, mrg_uniform{TensorType(float32, 4D),inplace}.1), '\n', 'Compilation failed (return status=1): C:\\Users\\DELL\\Anaconda2\\libs/python27.lib: error adding symbols: File in wrong format\r. collect2.exe: error: ld returned 1 exit status\r. ', '[Elemwise{Composite{(i0 + (i1 * i2))}}[(0, 2)](TensorConstant{(1L, 1L, 1..-0.0699854}, TensorConstant{(1L, 1L, 1..f 0.139971}, <TensorType(float32, (False, False, True, False))>)]')
I am not able to understand whats wrong.

Related

Error: __init__() got an unexpected keyword argument 'n_splits'

I am going to perform ShuffleSplit() method for California housing dataset (Source: https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html) to fit SGD regression.
However, the 'n_splits' error is occurred when method is applied.
The code is following:
from sklearn import cross_validation, grid_search, linear_model, metrics
import numpy as np
import pandas as pd
from sklearn.preprocessing import scale
from sklearn.cross_validation import ShuffleSplit
housing_data = pd.read_csv('cal_housing.csv', header = 0, sep = ',')
housing_data.fillna(housing_data.mean(), inplace=True)
df=pd.get_dummies(housing_data)
y_target = housing_data['median_house_value'].values
x_features = housing_data.drop(['median_house_value'], axis = 1)
from sklearn.cross_validation import train_test_split
from sklearn import model_selection
train_x, test_x, train_y, test_y = model_selection.train_test_split(x_features, y_target, test_size=0.2, random_state=4)
reg = linear_model.SGDRegressor(random_state=0)
cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0)
The error is below:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-8f8760b04f8c> in <module>()
----> 1 cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0)
TypeError: __init__() got an unexpected keyword argument 'n_splits'
I updated scikit-learn with 0.18 version.
Anaconda version: 4.5.8
Could you please advise on this issue?
You are mixing up two different modules.
Before 0.18, cross_validation was used for ShuffleSplit. In that, n_splits was not present. n was used to define the number of splits
But since you have updated to 0.18 now, cross_validation and grid_search has been deprecated in favor of model_selection.
This is mentioned in docs here, and these modules will be removed from version 0.20
So instead of this:
from sklearn.cross_validation import ShuffleSplit
from sklearn.cross_validation import train_test_split
Do this:
from sklearn.model_selection import ShuffleSplit
fro
m sklearn.model_selection import train_test_split
Then you can use n_splits.
cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0)

Why am I getting this error: TypeError: Input must be a 2D array

I am working on a python code to plot Eddy Kinetic Energy. I am fairly new to python and I'm confused about an error I have been getting. I'm not worried about plotting my data on a map just yet, I just want to see if I can get it to plot. Here is my code and error:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from pylab import *
from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
import matplotlib.cm as cm
from mpl_toolkits.basemap import shiftgrid
test = Dataset('p.34331101.atmos_daily.nc', 'r')
lat = test.variables['lat'][:]
lon = test.variables['lon'][:]
level = test.variables['level'][5]
time = test.variables['time'][:]
u = test.variables['ucomp'][:]
v = test.variables['vcomp'][:]
temp = test.variables['temp'][:]
print(lat.shape)
print(u.shape)
#uz = np.reshape(u, (30, 26, 90))
uzm = np.nanmean(u, axis=3)
#vz = np.reshape(v, (30, 26, 90))
vzm = np.nanmean(v, axis=3)
print(uzm.shape)
ustar = u-uzm[:,:,:,np.newaxis]
vstar = v-vzm[:,:,:,np.newaxis]
EKE = np.nanmean(.5*(ustar**2 + vstar**2), axis=3)
EKE1 = np.asarray(EKE)
%matplotlib inline
print(EKE.shape)
levels=[-10, -5, 0, 5, 10]
plt.contour(EKE[1,1,:])
#EKE is time, level, lat and the shape is (30, 26, 90)
TypeError: Input must be a 2D array.
Bret, you would probably get more help if you included a bit more info with your error, did you not get a line number to look at?
I would hazard a guess that your problem is passing a 1D array to contour(). This sometimes seems counter-intuitive but numpy reduces the dimensions 'automatically' when you specify a single value in an index.
i.e. try
print(EKE.shape)
print(EKE[1,1,:].shape)
print(EKE[1:2,1:2,:].shape)

save and restore check point in convolutional neural network

i am try to save the training for my network in a checkpoint instead to trained each time i testing. So i don't know what is problem in my code when i run the test file, it going to train again. any body can help me plz ?
this is the train file
saver = tf.train.Saver()
with tf.Session(graph=graph) as session:
num_steps = 1001
session.run()
print('Initialized')
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
print("batch_labels",batch_labels)
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = session.run(
[optimizer, loss, train_prediction ], feed_dict=feed_dict)
if (step % 50 == 0):
print('Minibatch loss at step %d: %f' % (step, l))
print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels))
save_path = saver.save(session, "/home/owner//tensorflow/tensorflow/models/image/mnist/new_dataset/models.ckpt")
print("Model saved in file: %s" % save_path)
and here is the test file:
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from six.moves import range
import time
from datetime import datetime
import tensorflow as tf
saver = tf.train.Saver()
init = tf.initialize_all_variables()
with tf.Session() as session:
saver.restore(session ,"/home/owner/tensorflow/tensorflow/models/image/mnist/new_dataset/models.ckpt")
print("Model restored.")
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval() , test_labels, force = False ))

Python rose chart

i was trying to build a windrose chart. i found this link to the code:
(http://youarealegend.blogspot.no/2008/09/windrose.html)
i get the following error, which points to numpy:
C:\Python27\lib\site-packages\numpy\core\fromnumeric.py:2768: RuntimeWarning: invalid value encountered in rint
return round(decimals, out)
has anyone encountered this before?
here is the code:
from windrose import WindroseAxes
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from numpy.random import random
from numpy import arange
#Create wind speed and direction variables
ws = random(500)*6
wd = random(500)*360
#A quick way to create new windrose axes...
def new_axes():
fig = plt.figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='w')
rect = [0.1, 0.1, 0.8, 0.8]
ax = WindroseAxes(fig, rect, axisbg='w')
fig.add_axes(ax)
return ax
#...and adjust the legend box
def set_legend(ax):
l = ax.legend(borderaxespad=-0.10)
plt.setp(l.get_texts(), fontsize=8)
#A stacked histogram with normed (displayed in percent) results :
ax = new_axes()
ax.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')
set_legend(ax)

Python Numpy : operands could not be broadcast together with shapes

I am getting this error "operands could not be broadcast together with shapes" for this code
import numpy as np
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
beantown = load_boston()
x=beantown.data
y=beantown.target
model = LinearRegression()
model = model.fit(x,y)
def mse(truth, predictions):
return ((truth - predictions) ** 2).mean(None)
print model.score(x,y)
print mse(x,y)
The error is on the line print mse(x,y)
Error is ValueError:
operands could not be broadcast together with shapes (506,13) (506,)
Reshape y:
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
beantown = load_boston()
x = beantown.data
y = beantown.target
y = y.reshape(y.size, 1)
model = LinearRegression()
model = model.fit(x, y)
def mse(truth, predictions):
return ((truth - predictions) ** 2).mean(None)
print model.score(x, y)
print mse(x, y)