Skip to content

Instantly share code, notes, and snippets.

@ritazh
Created June 2, 2017 06:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ritazh/a7c88875053c1106e407300fc4f1d8d6 to your computer and use it in GitHub Desktop.
Save ritazh/a7c88875053c1106e407300fc4f1d8d6 to your computer and use it in GitHub Desktop.
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
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
Copy link

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
Copy link

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