Skip to content

Instantly share code, notes, and snippets.

@lucasjinreal
Created May 11, 2017 09:43
Show Gist options
  • Save lucasjinreal/a1ee9019cc9d4cfb20de89b9e0e45aa4 to your computer and use it in GitHub Desktop.
Save lucasjinreal/a1ee9019cc9d4cfb20de89b9e0e45aa4 to your computer and use it in GitHub Desktop.
Minimal Implementation of TensorFlow models.
# -*- coding: utf-8 -*-
# file: models.py
# author: JinTian
# time: 11/05/2017 3:03 PM
# Copyright 2017 JinTian. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------
"""
Minimal Implementation of TensorFlow models.
"""
from tensorflow.contrib import slim
import tensorflow as tf
class FaNet(object):
"""
FaNet, is a simple but powerful net implement by Jin FaGang
FaNet consist of 6 CNN layers build on tiny data set reaches
a state-of-art accuracy result.
"""
def __init__(self, num_classes, lr=0.0001):
self.num_classes = num_classes
self.lr = lr
self._make_graph()
def _make_graph(self):
self._init_inputs()
self._create_net()
self._init_optimizer()
def _init_inputs(self):
self.inputs = tf.placeholder(tf.float32, [None, 256, 256, 3])
self.labels = tf.placeholder(tf.int32, [None, self.num_classes])
def _create_net(self, is_train=True):
# net definition here
return net
def _init_optimizer(self):
self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.net, labels=self.labels))
self.train_op = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss)
self.global_step = tf.Variable(initial_value=0)
def inference_outputs(self, n_top=5):
outputs = tf.nn.softmax(self._create_net(is_train=False))
inference_index, inference_prob = tf.nn.top_k(outputs, k=n_top)
return inference_index, inference_prob
def make_train_inputs(self, inputs, labels):
if isinstance(inputs, tf.Tensor):
inputs = inputs.eval()
if isinstance(labels, tf.Tensor):
labels = labels.eval()
return {
self.inputs: inputs,
self.labels: labels
}
def make_inference_inputs(self, inputs):
return {
self.inputs: inputs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment