import tensorflow as tf
import numpy as np
slim = tf.contrib.slim
from glob import glob
import os


class Classification_Model(object):

    def __init__(self):

        self.img_size = 28
        self.feature_size = 100
        self.end_points = {}
        self.input_fname_pattern = '*.png'
        self.batch_size = 50

    def transform(self, img):
        return img/127.5 - 1

    def model_arg_scope(self, weight_decay=0.0005, is_training = True):
        with slim.arg_scope([slim.conv2d, slim.fully_connected],
                            activation_fn = tf.nn.relu,
                            normalizer_fn = slim.batch_norm,
                            normalizer_params={'is_training': is_training, 'decay': 0.9},
                            weights_regularizer=slim.l2_regularizer(weight_decay),
                            weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                            biases_initializer=tf.zeros_initializer()
                            ):
            with slim.arg_scope([slim.conv2d, slim.max_pool2d], padding = 'SAME') as sc:
                return sc

    def net(self, inputs, is_training=True, dropout_keep_prob=0.5, reuse=None, scope='AlexNet'):

        with tf.variable_scope(scope, 'AlexNet', [inputs], reuse=reuse):
            net = slim.conv2d(inputs, 16, [3, 3], scope='conv1')
            net = slim.max_pool2d(net, [2, 2], scope='pool1')
            net = slim.conv2d(net, 32, [3, 3], scope='conv2')
            net = slim.conv2d(net, 64, [3, 3], scope='conv3')
            net = slim.max_pool2d(net, [2, 2], scope='pool3')
            net = slim.conv2d(net, 128, [3, 3], scope='conv4')
            net = slim.conv2d(net, 256, [3, 3], scope='conv5')
            net = slim.conv2d(net, 512, [3, 3], scope='conv6')
            net = slim.avg_pool2d(net, [7, 7], stride = 1, scope='average_pool')
            net = slim.flatten(net)
            features = slim.fully_connected(net, self.feature_size, scope='features')
            self.end_points['Features'] = features
            logits = slim.fully_connected(features, 10, activation_fn = None, scope='logits')
            self.end_points['Logits'] = logits
            predictions = tf.nn.softmax(logits, name='Predictions')
            self.end_points['Predictions'] = predictions
        return logits, self.end_points

    def losses(self, logits, labels, scope='model_losses'):

        with tf.name_scope(scope, 'model_losses'):
            losses = tf.nn.softmax_cross_entropy_with_logits(labels=labels,logits=logits)
            losses = tf.reduce_mean(losses)

        return losses

    def optimizer(self, learning_rate, scope = 'model_optimizer'):

        with tf.name_scope(scope, 'model_optimizer'):
            optimizer = tf.train.GradientDescentOptimizer(learning_rate)

        return optimizer

    def change_dataset(self, dataset_name = "0"):
        self.dataset_name = dataset_name
        self.file_list = glob(os.path.join("./samples", self.dataset_name, self.input_fname_pattern))
        self.filename_queue = tf.train.string_input_producer(self.file_list[:])
        self.image_reader = tf.WholeFileReader()
        _, self.image_file = self.image_reader.read(self.filename_queue)

        self.image = tf.image.decode_png(self.image_file)

        self.image = tf.cast(tf.reshape(self.image, shape=[28, 28, 1]), dtype=tf.float32)

    def get_batch(self):

        num_preprocess_threads = 1
        min_queue_examples = 256

        batch_images = tf.train.batch([self.image],
                                       batch_size=self.batch_size,
                                       num_threads=num_preprocess_threads,
                                       capacity=min_queue_examples + 3 * self.batch_size)
        batch_images = self.transform(batch_images)
        batch_labels = tf.one_hot(tf.ones([self.batch_size], dtype=tf.int32) * int(self.dataset_name), depth=10)
        return batch_images, batch_labels
@@ -3958,20 +3958,1174 @@
 images, batch_labels
+%0A%0A    def read_tf(self,filename_queue):%0A%0A        reader = tf.TFRecordReader()%0A        _,serialized_example = reader.read(filename_queue)%0A        features = tf.parse_single_example(%0A            serialized_example,%0A            features =%7B%0A                'image': tf.FixedLenFeature(%5B%5D,tf.string),%0A                'label': tf.FixedLenFeature(%5B%5D,tf.int64),%0A            %7D%0A        )%0A        label = tf.cast(features%5B'label'%5D,tf.int32)%0A        image = tf.decode_raw(features%5B'image'%5D,tf.uint8)%0A        image = tf.reshape(image,(28,28,1))%0A%0A        num_preprocess_threads = 1%0A        min_queue_examples = 256%0A%0A        image,label = tf.train.shuffle_batch(%5Bimage,label%5D,batch_size = self.batch_size,%0A            num_threads = num_preprocess_threads,capacity = min_queue_examples + 3*self.batch_size,%0A                min_after_dequeue = min_queue_examples)%0A        return image,label%0A%0A    def get_batch_tf(self,tfrecords_path):%0A        tfrecords_filename = glob(tfrecords_path + '*.tfrecord')%0A        filename_queue = tf.train.string_input_producer(tfrecords_filename,num_epochs = 100)%0A        image,label = self.read_tf(filename_queue)%0A        return image,label%0A

add read_tf