the prediction function gives the same output everytime - python

from keras.datasets import mnist
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
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape((X_train.shape[0], 28, 28, 1)).astype('float32')
X_test = X_test.reshape((X_test.shape[0], 28, 28, 1)).astype('float32')
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]
def larger_model():
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(28, 28, 1), activation='relu'))
model.add(MaxPooling2D())
model.add(Conv2D(15, (3, 3), activation='relu'))
model.add(MaxPooling2D())
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = larger_model()
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Large CNN Error: %.2f%%" % (100-scores[1]*100))
model.save('good_model.h5')
print("Model saved")
After running this code, we get a '.h5' model, then to predict this image
i added this code:
import cv2
model = load_model('good_model.h5')
file = cv2.imread('screenshot.png')
file = cv2.resize(file, (28, 28))
file = cv2.cvtColor(file, cv2.COLOR_BGR2GRAY)
file = file.reshape((-1, 28, 28,1))
result = model.predict(file)
print(result[0])
t = (np.argmax(result[0]))
print("I predict this number is a:", t)
But I always get the same answer which is 4. above I tried to load the image with cv and convert it to gray and then reshape to the size of the input. It takes the input correctly but the answer is always the same no matter what image I give it as input

You need to invert the image before prediction. Once you invert the image, it will predict correctly. The given example is predicting as "2" but I checked with other images such as "7" and it is correctly predicting.
file = cv2.bitwise_not(file)
Other than the above, I made one change. I imported modules from Tensorflow 2.x. Please check the full code here.

Related

How to extract the image names and labels in the training set after completing the active learning loop and write them to a CSV file?

I am using the Keras script at https://modal-python.readthedocs.io/en/latest/content/examples/Keras_integration.html to perform active learning for a binary classification task. After completing the active learning loop, how do we extract the image names and labels in the training set that gives the optimal test performance and write them to a CSV file?
from tensorflow import keras
import numpy as np
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from modAL.models import ActiveLearner
from modAL.uncertainty import entropy_sampling
# build function for the Keras' scikit-learn API
def create_keras_model():
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
return model
# create the classifier
classifier = KerasClassifier(create_keras_model)
# read training data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 28, 28, 1).astype('float32') / 255
X_test = X_test.reshape(10000, 28, 28, 1).astype('float32') / 255
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
# assemble initial data
n_initial = 1000
initial_idx = np.random.choice(range(len(X_train)), size=n_initial, replace=False)
X_initial = X_train[initial_idx]
y_initial = y_train[initial_idx]
# generate the pool
# remove the initial data from the training dataset
X_pool = np.delete(X_train, initial_idx, axis=0)
y_pool = np.delete(y_train, initial_idx, axis=0)
"""
Training the ActiveLearner
"""
# initialize ActiveLearner
learner = ActiveLearner(
estimator=classifier,
X_training=X_initial, y_training=y_initial,
verbose=1
)
# the active learning loop
n_queries = 100
for idx in range(n_queries):
query_idx, query_instance = learner.query(X_pool, n_instances=100, verbose=0)
print(query_idx)
learner.teach(
X=X_pool[query_idx], y=y_pool[query_idx], only_new=True,
verbose=1
)
# remove queried instance from pool
X_pool = np.delete(X_pool, query_idx, axis=0)
y_pool = np.delete(y_pool, query_idx, axis=0)
# the final accuracy score
print(learner.score(X_test, y_test, verbose=1))

Getting an error for dimension in Neural Network

Hi I a trying to make a Neural Network and getting an error ValueError: Error when checking input: expected conv2d_27_input to have 4 dimensions, but got array with shape (60000, 28, 28)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
# load data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# normalize inputs from 0-255 to 0.0-1.0
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train / 255.0
x_test = x_test / 255.0
# Encode the outputs
y_train = np_utils.to_categorical(y_train) #Converts a class vector (integers) to binary class matrix.
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
# Build the model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 2), activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D())
model.add(Flatten())
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
epochs = 5
lrate = 0.002
decay = lrate/epochs
Can someone help in understanding it?
Reshape your data before/after normalisation
# ...
# reshape to be [samples][width][height][channels]
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
X_train = X_train / 255.0
X_test = X_test / 255.0

How to save images properly classified with CNN?

Handwriting recognition problem with CNN. There is a requirement: from 10000 test images, save 1000 images (.png or .jpg)accurate classified each folder of 100 images (0 -> 9). How do I do? i need an instruction about code. thanks! code :
import keras
from keras.datasets import mnist
from keras.layers import Dense, Activation, Flatten, Conv2D,
MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
(train_X,train_Y), (test_X,test_Y) = mnist.load_data()
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
test_X = test_X / 255
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape=(28, 28, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
model.fit(train_X, train_Y_one_hot, batch_size=64, epochs=1)
test_loss, test_acc = model.evaluate(test_X, test_Y_one_hot)
print('Test loss', test_loss)
print('Test accuracy', test_acc)
model.save('123.model')
predictions = model.predict(test_X)
print(np.argmax(np.round(predictions[235])))
plt.imshow(test_X[235].reshape(28, 28), cmap = 'Greys_r')
plt.show()
Complete Code for Saving the Correctly Classified Test Images in the respective folders of their Labels
(0 to 9), 100 images in each folder is mentioned below:
import keras
from keras.datasets import mnist
from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
(train_X,train_Y), (test_X,test_Y) = mnist.load_data()
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
train_X = train_X.astype('float32')
train_X = train_X/255
test_X = test_X.astype('float32')
test_X = test_X / 255
#train_Y_one_hot = to_categorical(train_Y)
#test_Y_one_hot = to_categorical(test_Y)
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape=(28, 28, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(loss=keras.losses.sparse_categorical_crossentropy,
optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
#model.fit(train_X, train_Y_one_hot, batch_size=64, epochs=1)
model.fit(train_X, train_Y, batch_size=64, epochs=1)
#test_loss, test_acc = model.evaluate(test_X, test_Y_one_hot)
test_loss, test_acc = model.evaluate(test_X, test_Y)
print('Test loss', test_loss)
print('Test accuracy', test_acc)
predictions = model.predict(test_X)
#****Actual Code which you need is mentioned below
import matplotlib
import matplotlib.pyplot as plt
import os
def save_fig(fig_id, Label):
path = os.path.join('MNIST_Images', Label, fig_id + "." + "png")
plt.tight_layout()
plt.savefig(path, format="png", dpi=300)
plt.close()
%matplotlib agg
%matplotlib agg #These 2 lines are required to prohibit Graph being displayed in Jupyter Notebook. You can comment these if you are using other IDE
No_Of_Rows = predictions.shape[0]
Count_Dict = {}
for i in range(10):
key = 'Count_' + str(i)
Count_Dict[key] = 0
for Each_Row in range(No_Of_Rows):
if np.argmax(predictions[Each_Row]) == test_Y[Each_Row]:
Label = str(test_Y[Each_Row])
Count_Dict['Count_' + Label] = Count_Dict['Count_' + Label] + 1
Count_Of_Label = Count_Dict['Count_' + Label]
if Count_Of_Label <= 100:
plt.imshow(test_X[Each_Row].reshape(28, 28), cmap = 'Greys_r')
plt.show()
save_fig(str(Count_Of_Label), Label)
I've commented the below lines of code which are not required, as the Labels are already in Numeric Format.
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
Also, I've replaced categorical_crossentropy with sparse_categorical_crossentropy in model.compile, as we are not Encoding the Variables.

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (454, 512, 512)

I used this code to produce my dataset in keras. but when I implement my code it produces this error:
ValueError: Error when checking input: expected conv2d_1_input to
have 4 dimensions, but got array with shape (454, 512, 512)
and I can not solve it. could you please tell me what is the problem? I expand the dimension before using in network but it does not work! could you please answer me fast, due to I search for several days but I could not find the solution and I do not have enough time:
import os,cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras import backend as K
#K.set_image_dim_ordering('th')
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,adam
#%%
PATH = os.getcwd()
# Define data path
data_path = r"E:\PhD\thesis\deepwatermark\databasetest\train"
data_dir_list = os.listdir(data_path)
img_rows=512
img_cols=512
num_channel=1
num_epoch=20
# Define the number of classes
num_classes = 7
labels_name={'CRP':0,'GF':1,'GN':2,'JPG':3,'MED':4,'ROT':5,'SP':6}
img_data_list=[]
labels_list = []
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loading the images of dataset-'+'{}\n'.format(dataset))
label = labels_name[dataset]
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
input_img_resize=cv2.resize(input_img,(512,512))
img_data_list.append(input_img_resize)
labels_list.append(label)
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data /= 255
print (img_data.shape)
labels = np.array(labels_list)
# print the count of number of samples for different classes
print(np.unique(labels,return_counts=True))
# convert class labels to on-hot encoding
Y = np_utils.to_categorical(labels, num_classes)
#Shuffle the dataset
x,y = shuffle(img_data,Y, random_state=2)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
img_data= np.expand_dims(img_data, axis=4)**
print (img_data.shape)
# Defining the model
input_shape=img_data[0].shape
model = Sequential()
model.add(Convolution2D(32, 3,3,border_mode='same',input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
#model.add(Convolution2D(64, 3, 3))
#model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=["accuracy"])
model.compile(loss='categorical_crossentropy', optimizer='rmsprop',metrics=["accuracy"])
# Viewing model_configuration
model.summary()
model.get_config()
model.layers[0].get_config()
model.layers[0].input_shape
model.layers[0].output_shape
model.layers[0].get_weights()
np.shape(model.layers[0].get_weights()[0])
model.layers[0].trainable
#%%
# Training
hist = model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test))
my new code with generator is here, did you see any problem? my dataset is the same as before.
import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras import backend as K
#K.set_image_dim_ordering('th')
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,adam
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
#
valid_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
#
test_datagen = ImageDataGenerator(rescale=1./255)
#
train_generator = train_datagen.flow_from_directory(
directory=r"E:\databasetest\train",
target_size=(512, 512),
color_mode="grayscale",
batch_size=32,
class_mode="categorical",
shuffle=True,
seed=42
)
#
valid_generator = valid_datagen.flow_from_directory(
directory=r"E:\databasetest\validation",
target_size=(512, 512),
color_mode="grayscale",
batch_size=32,
class_mode="categorical",
shuffle=True,
seed=42
)
#
test_generator = test_datagen.flow_from_directory(
directory=r"E:\databasetest\test",
target_size=(512, 512),
color_mode="grayscale",
batch_size=16,
class_mode=None,
shuffle=False,
seed=42
)
#
## neural network model
model = Sequential()
model.add(Conv2D(32, (3,3),border_mode='same', input_shape = (512, 512, 1), activation = 'relu'))
model.add(Activation('relu'))
model.add(Conv2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(64, 3, 3))
model.add(Activation('relu'))
#model.add(Convolution2D(64, 3, 3))
#model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(7))
model.add(Activation('softmax'))
model.summary()
model.compile(loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=10
)
but when I implement it I received this error again:
ResourceExhaustedError: OOM when allocating tensor with shape[32,32,512,512] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[Node: conv2d_1/convolution = Conv2D[T=DT_FLOAT, data_format="NCHW", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](conv2d_1/convolution-0-TransposeNHWCToNCHW-LayoutOptimizer, conv2d_1/kernel/read)]]
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
input_img_resize=cv2.resize(input_img,(512,512))
--->input_img_resize = np.expand_dims(input_img_resize, axis=-1)
img_data_list.append(input_img_resize)
labels_list.append(label)
this will make all your arrays 512x512x1, which should do the trick and ends up zith an array of shape (454, 512, 512, 1). You sure you want to use grayscaled images though?
Another thing is this snippet of code
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, `random_state=2)`
img_data= np.expand_dims(img_data, axis=4)**
print (img_data.shape)
You apply another dimension to your img_data, after you've already declared x_train, etc. And in the end you feed x_train, which is not extended, hence the error. If you do it in the beginning, and remove the expanding in the end, then your code should work.
EDIT OOM
I recommend creating a separate question for the OOM problem, so more people see it. Possible problems are the size of the images and the batch size. Reduce the image size to 64 x 64 and change the batch size to 5. If that still raises the error, try also kicking out this dense layer.
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
If these reductions still cause the error then I have the following questions: are you running on GPU/CPU, and which one?
Just to repeat myself: the code is fine, just needs a few changes perhaps.

Prediction using Keras

I learned to implement an image classifier from a code which i got from a post and it was very helpful,but i don't know how to predict on an image.I tried but its giving a Value Error.I am still a beginner
Keras Code:-
from __future__ import print_function
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
import numpy as np
#seed = 7
#np.random.seed(seed)
batch_size = 50
nb_classes = 10
nb_epoch = 150
data_augmentation = False
# input image dimensions
img_rows, img_cols = 32, 32
# the CIFAR10 images are RGB
img_channels = 3
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='same',
input_shape=X_train.shape[1:]))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
# let's train the model using SGD + momentum (how original).
#sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
sgd= Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
if not data_augmentation:
print('Not using data augmentation.')
model.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test),
shuffle=True)
else:
print('Using real-time data augmentation.')
# this will do preprocessing and realtime data augmentation
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)
# fit the model on the batches generated by datagen.flow()
model.fit_generator(datagen.flow(X_train, Y_train,
batch_size=batch_size),
samples_per_epoch=X_train.shape[0],
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test))
model.save('CIFAR10.h5')
My Prediction Code:-
from __future__ import print_function
from keras.models import load_model
from keras.utils import np_utils
import numpy as np
import cv2
img_rows, img_cols = 32, 32
model = load_model('CIFAR10.h5')
img = cv2.imread('D:/Study_Material/Python_3_Tutorial/PythonScripts/Machine_Learning/Project/Images/Deer.jpg')
img = cv2.resize(img,(img_rows,img_cols))
Image = np.array(img)
print(model.predict(Image))
Error:-
Warning (from warnings module):
File "C:\Users\Na462\AppData\Local\Programs\Python\Python35\lib\site-packages\h5py\__init__.py", line 36
from ._conv import register_converters as _register_converters
FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
Using TensorFlow backend.
Traceback (most recent call last):
File "D:\Study_Material\Python_3_Tutorial\PythonScripts\Machine_Learning\Project\Keras(Prediction).py", line 12, in <module>
print(model.predict(Image))
File "C:\Users\Na462\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\models.py", line 1025, in predict
steps=steps)
File "C:\Users\Na462\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 1817, in predict
check_batch_axis=False)
File "C:\Users\Na462\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking : expected conv2d_1_input to have 4 dimensions, but got array with shape (32, 32, 3)
Please Tell me the right way to Predict in Keras so that i can implement it on different test cases.
The error you are getting is because all the frameworks assume an image input is a batch of images making it a 4d tensor, instead of one image (3d tensor). To just do single image batches, expand the input to be of size (1, 32, 32, 3), the 1 in the beginning being the batch size of 1.
I do not know keras specifically very well, but since you are passing in a numpy array, you can modify your 'Image' object like so (see second to last line):
img_rows, img_cols = 32, 32
model = load_model('CIFAR10.h5')
img = cv2.imread('D:/Study_Material/Python_3_Tutorial/PythonScripts/Machine_Learning/Project/Images/Deer.jpg')
img = cv2.resize(img,(img_rows,img_cols))
Image = np.expand_dims(np.array(img), axis=0)
print(model.predict(Image))

Categories