I trained a CNN using my own custom dataset, where Resizing the dataset images was done with the PIL library. Now When I try to predict the label of the input images in the dataset it works fine. My predict function within the CNN class is as follows:
def predict(self, X):
X = np.asarray(X, dtype=np.float32)
if self.pred is None:
try:
self.pred = theano.function([self.input_var], T.argmax(self.get_output(network=self.network, deterministic=True), axis=1))
except:
print("failed")
else:
pass
dim = X.ndim
if dim == 3:
X = X[newaxis,:,:,:]
elif dim == 2:
X = X[newaxis, newaxis, :, :]
else:
pass
try:
predicted_label = self.pred(X)
return int(predicted_label)
except:
print("failed")
Which is called from main like this:
[bilder, label, names] = read_images1(dataset_path, sz=im_size, na=False)
bilder = np.asarray(bilder, dtype=np.float32)
print("The shape of bilder is; number of images: {}, height: {}, width: {}".format(bilder.shape[0], bilder.shape[1], bilder.shape[2]))
pred = model.predict(bilder[0])
print("Predicted label for image is: {}".format(pred))
bilder1 = bilder[0]
#bilder1 = cv2.resize(bilder1, (58, 58), interpolation=cv2.INTER_CUBIC)
pred1 = model.predict(bilder[400])
print("Predicted label for image is: {}".format(pred1))
bilder is pretty much an array with shape (images, height, width) and if I run the code the output is as follows:
The shape of bilder is; number of images: 442, height: 58, width: 58
Predicted label for image is: 3
Predicted label for image is: 39
Process finished with exit code 0
Notice that:
#bilder1 = cv2.resize(bilder1, (58, 58), interpolation=cv2.INTER_CUBIC)
Is commented and not in use. Now if I comment that in I get the following output:
The shape of bilder is; number of images: 442, height: 58, width: 58
Predicted label for image is: 3
Meaning it gets stuck on this part until i stop the code myself:
try:
predicted_label = self.pred(X)
return int(predicted_label
This I don't understand at all. The image stored in bilder1 isn't even used in the prediction, as it's just stored in bilder1, then when I continue with trying to predict image nr 400 in bilder it gets stuck.. This was just a test as im trying to do live recognition via webcam where same problem arises after resizing the face image.
EDIT: The problem seems to be consistent with any changes done to images by using the cv2 library. Whether these images are used in the neural network or not doesn't seem to matter.