Skip to content

Instantly share code, notes, and snippets.

@juniorUsca
Last active May 4, 2017 16:14
Show Gist options
  • Save juniorUsca/0fd6ce77ee0c14c1d87e3e1de7424390 to your computer and use it in GitHub Desktop.
Save juniorUsca/0fd6ce77ee0c14c1d87e3e1de7424390 to your computer and use it in GitHub Desktop.
Tensorflow classifier example
  1. Instalar docker
curl -sSL https://get.docker.com/ | sh
sudo usermod -aG docker <tu-usuario>
  1. Descargar un dataset y organizar las imagenes en formato jpg por carpetas
mkdir tf_files_train

guarda las carpetas en tf_files_train

  1. Levantar una imagen de docker para poder desarrollar en tensorflow
docker run -tid --name tf-latest -v $HOME/tf_files_train:/tf_files_train/ gcr.io/tensorflow/tensorflow:latest-devel bash

2.1 Si ya hiciste este paso antes solo levanta el contenedor que se creo

docker start tf-latest
  1. Ahora ingresar al contenedor de docker
docker exec -ti tf-latest bash
  1. Ejecutar la fase de entrenamiento. Tomara tiempo dependiendo de la cantidad de clases e imagenes por clase
python /tensorflow/tensorflow/examples/image_retraining/retrain.py \
 --bottleneck_dir=/tf_files/bottlenecks \
 --how_many_training_steps 500 \
 --model_dir=/tf_files/inception \
 --output_graph=/tf_files/retrained_graph.pb \
 --output_labels=/tf_files/retrained_labels.txt \
 --image_dir=/tf_files_train
  1. Haremos las pruebas, coloca algunas imagenes de prueba en el directorio local: ~/tf_files_train/

  2. descarga label_image.py y colocalo en el directorio local: ~/tf_files_train/

  3. Ahora dentro de docker ejecuta

mv /tf_files_train/label_image.py /tf_files/
cd /tf_files
  1. Haz la prueba con tus imagenes
python label_image.py /tf_files_train/imagen-de-prueba-1.jpg

Referencias

## CONVERT A VIDEO ON IMAGES
for i in *.avi;
do name=`echo $i | cut -d'.' -f1`;
echo $name;
mkdir -p $name;
# ffmpeg -i "$i" "${name}.mov";
ffmpeg -i "$i" ${name}/${name}_%04d.jpg
done
import tensorflow as tf
import sys
# change this as you see fit
image_path = sys.argv[1]
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("/tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, \
{'DecodeJpeg/contents:0': image_data})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
print('%s (score = %.5f)' % (human_string, score))
@rosariohg
Copy link

Gracias 👍 👍 👍

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