/
keras_vgg19_cat_dog.py
85 lines (72 loc) · 3.15 KB
/
keras_vgg19_cat_dog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import glob
import numpy as np
import pandas as pd
import keras
from keras.preprocessing import image as image_utils
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
#####################
# helper functions
#####################
# define image preprocessor for use with keras vgg19
def image_preprocessor(image_path):
image = image_utils.load_img(image_path, target_size=(224, 224))
image = image_utils.img_to_array(image)
image = np.expand_dims(image, axis=0)
image = preprocess_input(image)
return(image)
#function to read in files housing all cat/dog breeds in imagenet labels
def read_dog_cat_labels(path):
labs = list(open(path))
labs = [item.split(',') for item in labs]
labs = [item.strip().replace(' ','_') for sublist in labs for item in sublist]
return(labs)
#------------------------------------------------
#####################
# prep images for classification
#####################
# define image paths to classify
image_files = glob.glob('images/*/*')
# preprocess images
image_list = [image_preprocessor(path) for path in image_files]
#------------------------------------------------
#####################
# load model and make predictions
#####################
# load vgg19 model pretrained with imagenet
model = keras.applications.VGG19(weights='imagenet')
# get model predictions
preds = [model.predict(image) for image in image_list]
preds = [list(decode_predictions(pred, top=1)[0][0]) for pred in preds]
# convert list of predictions to df and drop class name column
pred_df = pd.DataFrame(preds)
pred_df = pred_df.drop(0, 1)
#make names match names in R output for consistency
pred_df.columns = ['class_description', 'score']
#------------------------------------------------
#####################
# add dog/cat labels, add file name, sort by score
#####################
#read in breed labels
dog_labs = read_dog_cat_labels('data/dog_classes.txt')
cat_labs = read_dog_cat_labels('data/cat_classes.txt')
#create column for labeling dog breeds as dog and cat breeds as cat
pred_df['catdog'] = np.nan
pred_df.loc[pred_df.class_description.isin(dog_labs), 'catdog'] = 'Dog'
pred_df.loc[pred_df.class_description.isin(cat_labs), 'catdog'] = 'Cat'
#add column with image paths
pred_df['file_name'] = image_files
pred_df = pred_df.sort_values('score', ascending=False)
#------------------------------------------------
#####################
# print output
#####################
print(pred_df)
# class_description score catdog file_name
# 1 Chihuahua 0.942092 Dog images/dogs/tonks_proposal_sign.jpg
# 0 Chihuahua 0.785131 Dog images/dogs/tonks_jasper_announce_sign.jpg
# 2 malinois 0.580209 Dog images/dogs/tonks_announce_sign.jpg
# 6 tabby 0.477750 Cat images/cats/google_tabby_cat.jpg
# 4 Egyptian_cat 0.449422 Cat images/cats/goober_lounging.jpg
# 5 Scotch_terrier 0.303835 Dog images/cats/jasper_announce_sign.jpg
# 3 lynx 0.147593 Cat images/cats/lilly_perch.jpg
#------------------------------------------------