Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from keras.models import load_model
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import img_to_array, load_img
import numpy as np
import tensorflow as tf
# dimensions of our images.
img_width, img_height = 150, 150
input_shape = (img_width, img_height, 3)
test_model = Sequential()
test_model.add(Conv2D(32, (3, 3), input_shape=input_shape))
test_model.add(Activation('relu'))
test_model.add(MaxPooling2D(pool_size=(2, 2)))
test_model.add(Conv2D(32, (3, 3)))
test_model.add(Activation('relu'))
test_model.add(MaxPooling2D(pool_size=(2, 2)))
test_model.add(Conv2D(64, (3, 3)))
test_model.add(Activation('relu'))
test_model.add(MaxPooling2D(pool_size=(2, 2)))
test_model.add(Flatten())
test_model.add(Dense(64))
test_model.add(Activation('relu'))
test_model.add(Dropout(0.5))
test_model.add(Dense(1))
test_model.add(Activation('sigmoid'))
test_model = load_model('first_model.h5')
def predict(basedir, model):
for i in range(1401,1411):
path = basedir + str(i) + '.jpg'
img = load_img(path,False,target_size=(img_width,img_height))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
preds = model.predict_classes(x)
probs = model.predict_proba(x)
print(probs)
basedir = "data/test/cat."
predict(basedir, test_model)
basedir = "data/test/dog."
predict(basedir, test_model)
print('done')
@barnajit

This comment has been minimized.

Copy link

barnajit commented Jan 7, 2018

Hi, I would add a change to your code by adding intensity normalization for test images.
img = load_img(path,False,target_size=(img_width,img_height))
x = img_to_array(img)
x= x / 255
x = np.expand_dims(x, axis=0)
The training images had this normalisation, therefore the test images should go through it too.
Thanks for the code.

@gledsonmelotti

This comment has been minimized.

Copy link

gledsonmelotti commented Mar 20, 2018

Hello @ritazh and @barnajit. Could you explain to me what the range means (1401, 1411)? How to determine this value?

Thanks for the code.

@mezaacor

This comment has been minimized.

Copy link

mezaacor commented Mar 28, 2018

hello I am Trying to use the classificator, but in the last part I get the follow error: predict() fp = builtins.open(filename, "rb") PermissionError: [Errno 13] Permission denied:
Some recommendation for the path route?, I found that this error could be admin permision, but I don't know...
I appreciate your help,
Best regards,

RM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.