Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Created June 12, 2016 23:04
Show Gist options
  • Star 95 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
  • Save ageitgey/a40dded08e82e59724c70da23786bbf0 to your computer and use it in GitHub Desktop.
Save ageitgey/a40dded08e82e59724c70da23786bbf0 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import scipy
import numpy as np
import argparse
parser = argparse.ArgumentParser(description='Decide if an image is a picture of a bird')
parser.add_argument('image', type=str, help='The image image file to check')
args = parser.parse_args()
# Same network definition as before
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)
network = input_data(shape=[None, 32, 32, 3],
data_preprocessing=img_prep,
data_augmentation=img_aug)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='bird-classifier.tfl.ckpt')
model.load("bird-classifier.tfl.ckpt-50912")
# Load the image file
img = scipy.ndimage.imread(args.image, mode="RGB")
# Scale it to 32x32
img = scipy.misc.imresize(img, (32, 32), interp="bicubic").astype(np.float32, casting='unsafe')
# Predict
prediction = model.predict([img])
# Check the result.
is_bird = np.argmax(prediction[0]) == 1
if is_bird:
print("That's a bird!")
else:
print("That's not a bird!")
@simicvm
Copy link

simicvm commented Jul 29, 2016

If somebody is still wondering, this script is a part of Adam's nice tutorial on machine learning, over on Medium. https://medium.com/@ageitgey/machine-learning-is-fun-80ea3ec3c471#.7pmvr7722.
You'll find a reference for this bird dataset in part 3.

@avanish
Copy link

avanish commented Oct 25, 2017

Hi, can you tell me what your Y dataset looks like? The way I did it was [[1., 0.], ... , [0., 1.]]. I'm always getting [0., 1.] as my prediction.

@harishyadav1465
Copy link

how to run can u explain

@adolfoyanes
Copy link

in parser.add_argument('image', type=str, help='The image image file to check') does 'image' refers to the path to the image file?

@sahukk
Copy link

sahukk commented Jan 10, 2018

types of image file is not clear. Should it be jpeg, bmp, GIF, TIF for testing data ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment