that was trained with the transfer learning method in Google's Tensorflow for Poets".
In this case we have used the Inception_V3 model. You need to clone the github repo with the command
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2 cd tensorflow-for-poets-2Next go to the subdirectory tf_files and create a new directory there called "galaxies" and put four subdirectories there: barredspiral, spiral, elliptical, irregular with each containing the corresponding training images. Next do
pip install --upgrade tensorflow
The command to "retrain" the inception model is
python -m scripts.retrain \ --bottleneck_dir=tf_files/bottlenecks \ --how_many_training_steps=500 \ --model_dir=tf_files/models/ \ --summaries_dir=tf_files/training_summaries/"inception_v3" \ --output_graph=tf_files/retrained_graph.pb \ --output_labels=tf_files/retrained_labels.txt \ --architecture="inception_v3" \ --image_dir=tf_files/galaxiesIf all goes well you will finally get the results that look like this
INFO:tensorflow:Final test accuracy = 88.9% (N=9)
The code that follows shows how to invoke the model. But first do
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import time
import numpy as np
import tensorflow as tf
def load_graph(model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
The following function resizes our images to 299 by 299 which is what is needed by the model.
def read_tensor_from_image_file(file_name, input_height=299, input_width=299,
input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)
return result
this makes a list of the class labels.
def load_labels(label_file):
label = []
proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
for l in proto_as_ascii_lines:
label.append(l.rstrip())
return label
The following are the parameters we will use to invoke the model. This is different from the ones in the documentation. That version is for the mobilenet model. this one is for inception_v3. note the input_layer and the output_layer and the height andd weight.
model_file = "/Users/dennisgannon/GitHub/tensorflow-for-poets-2/tf_files/retrained_graph.pb"
label_file = "/Users/dennisgannon/GitHub/tensorflow-for-poets-2/tf_files/retrained_labels.txt"
input_height = 299
input_width = 299
input_mean = 128
input_std = 128
input_layer = "Mul"
output_layer = "final_result"
graph = load_graph(model_file)
Let's try it out on one of the test cases (which is a spiral galaxy)
file_name = '/Users/dennisgannon/OneDrive/Docs9/galaxies/bigtest/t31.jpg'
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
start = time.time()
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
end=time.time()
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)
print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))
for i in top_k:
print(labels[i], results[i])
top_k[0]
which takes a filename and returns a tuple consisting of the top prediction and the score.
def predict_image(file_name):
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
start = time.time()
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
end=time.time()
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)
#print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))
return [labels[top_k[0]], results[top_k[0]]]
predict_image(file_name)
first for the test set and then for the training set
test_out = []
for i in range(0,40):
file_name = '/Users/dennisgannon/OneDrive/Docs9/galaxies/bigtest/t'+str(i)+'.jpg'
test_out.append(predict_image(file_name))
mydict = {'spiral': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0},
'barredspiral': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0},
'elliptical': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0},
'irregular': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0}}
for i in range(0,40):
truev = 'barredspiral'
if i > 9:
truev= 'elliptical'
if i > 19:
truev = 'irregular'
if i > 29:
truev = 'spiral'
mydict[truev][test_out[i][0]]+= 1
# print(i, truev, test_out[i])
import pandas as pd
df = pd.DataFrame.from_dict(mydict)
df.transpose()
traindict = {'spiral': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0},
'barredspiral': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0},
'elliptical': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0},
'irregular': {'spiral':0.0, 'barredspiral':0.0, 'elliptical':0.0, 'irregular':0.0}}
for i in range(1,20):
file_name = '/Users/dennisgannon/OneDrive/Docs9/galaxies/bigspiral/s'+str(i)+'.jpg'
traindict['spiral'][predict_image(file_name)[0]] +=1
for i in range(1,20):
file_name = '/Users/dennisgannon/OneDrive/Docs9/galaxies/bigbarred/bs'+str(i)+'.jpg'
traindict['barredspiral'][predict_image(file_name)[0]] +=1
for i in range(1,20):
file_name = '/Users/dennisgannon/OneDrive/Docs9/galaxies/bigelliptical/e'+str(i)+'.jpg'
traindict['elliptical'][predict_image(file_name)[0]] +=1
for i in range(1,20):
file_name = '/Users/dennisgannon/OneDrive/Docs9/galaxies/bigirregular/i'+str(i)+'.jpg'
traindict['irregular'][predict_image(file_name)[0]] +=1
tdf = pd.DataFrame.from_dict(traindict)
tdf.transpose()
at the end of the training: INFO:tensorflow:Final test accuracy = 88.9% (N=9)