def build_model_anything_happening():
    ### IS ANY OF THIS NECESSARY FOR LIGHT/DARK? IN GENERAL W/ STAIONARY CAMERA?
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()

    # Specify shape of the data, image prep
    network = input_data(shape=[None, 52, 64],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)

    # Since the image position remains consistent and are fairly similar, this can be spatially aware.
    # Using a fully connected network directly, no need for convolution.
    network = fully_connected(network, 2048, activation='relu')
    network = fully_connected(network, 2, activation='softmax')

    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.00003)

    model = tflearn.DNN(network, tensorboard_verbose=0)
    return model
    def generate_network(self):
        """ Return tflearn cnn network.
        """
        print(self.image_size, self.n_epoch, self.batch_size, self.person_ids)
        print(type(self.image_size), type(self.n_epoch),
              type(self.batch_size), type(self.person_ids))
        if not isinstance(self.image_size, list) \
            or not isinstance(self.n_epoch, int) \
            or not isinstance(self.batch_size, int) \
            or not isinstance(self.person_ids, list):
        # if self.image_size is None or self.n_epoch is None or \
        #     self.batch_size is None or self.person_ids is None:
            raise ValueError("Insufficient values to generate network.\n"
                             "Need (n_epoch, int), (batch_size, int),"
                             "(image_size, list), (person_ids, list).")

        # Real-time data preprocessing
        img_prep = ImagePreprocessing()
        img_prep.add_featurewise_zero_center()
        img_prep.add_featurewise_stdnorm()

        # Real-time data augmentation
        img_aug = ImageAugmentation()
        img_aug.add_random_rotation(max_angle=25.)
        img_aug.add_random_flip_leftright()

        # Convolutional network building
        network = input_data(
            shape=[None, self.image_size[0], self.image_size[1], 3],
            data_preprocessing=img_prep,
            data_augmentation=img_aug)
        network = conv_2d(network, self.image_size[0], self.IMAGE_CHANNEL_NUM,
                          activation='relu')
        network = max_pool_2d(network, 2)
        network = conv_2d(network, self.image_size[0] * 2,
                          self.IMAGE_CHANNEL_NUM,
                          activation='relu')
        network = conv_2d(network, self.image_size[0] * 2,
                          self.IMAGE_CHANNEL_NUM,
                          activation='relu')
        network = max_pool_2d(network, 2)
        network = fully_connected(network, self.image_size[0] * 2**4,
                                  activation='relu')
        network = dropout(network, 0.5)
        network = fully_connected(network, self.person_num,
                                  activation='softmax')
        network = regression(network, optimizer='adam',
                             loss='categorical_crossentropy',
                             learning_rate=0.001)
        return network
def build_model_specific():
    ### IS ANY OF THIS NECESSARY FOR LIGHT/DARK? IN GENERAL W/ STAIONARY CAMERA?
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()

    # Specify shape of the data, image prep
    network = input_data(shape=[None, 52, 64],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)

    # conv_2d incoming, nb_filter, filter_size
    # incoming: Tensor. Incoming 4-D Tensor.
    # nb_filter: int. The number of convolutional filters. # WHAT IS THIS?
    # filter_size: 'intor list ofints`. Size of filters.   # WHAT IS THIS?
    network = conv_1d(network, 512, 3, activation='relu')

    # (incoming, kernel_size)
    # incoming: Tensor. Incoming 4-D Layer.
    # kernel_size: 'intor list ofints`. Pooling kernel size.
    network = max_pool_1d(network, 2)

    network = conv_1d(network, 64, 3, activation='relu')
    network = conv_1d(network, 64, 3, activation='relu')
    network = max_pool_1d(network, 2)

    network = fully_connected(network, 512, activation='relu')

    network = dropout(network, 0.5)

    network = fully_connected(network, 4, activation='softmax')

    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.0003)

    model = tflearn.DNN(network, tensorboard_verbose=0)
    return model
Example #4
0
def stop_dnn():
    img_pre_processing = ImagePreprocessing()

    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=10.)

    network = input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_pre_processing,
                         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)
    return network
Example #5
0
def setup_model(checkpoint_path=None):
    """Sets up a deep belief network for image classification based on the set up described in

    :param checkpoint_path: string path describing prefix for model checkpoints
    :returns: Deep Neural Network
    :rtype: tflearn.DNN

    References:
        - Machine Learning is Fun! Part 3: Deep Learning and Convolutional Neural Networks

    Links:
        - https://medium.com/@ageitgey/machine-learning-is-fun-part-3-deep-learning-and-convolutional-neural-networks-f40359318721

    """
     # Make sure the data is normalized
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    # Create extra synthetic training data by flipping, rotating and blurring the
    # images on our data set.
    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.)

    # Input is a 32x32 image with 3 color channels (red, green and blue)
    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)
    if checkpoint_path:
        model = tflearn.DNN(network, tensorboard_verbose=3,
                            checkpoint_path=checkpoint_path)
    else:
        model = tflearn.DNN(network, tensorboard_verbose=3)

    return model
Example #6
0
import h5py

start_time = time.time()

X_dim = 1024
Y_dim = 512

h5f = h5py.File('data.h5', 'r')
X = h5f['X']
Y = to_categorical(h5f['Y'], 2)
testX = h5f['testX']
testY = to_categorical(h5f['testY'], 2)

#testX, testY = image_preloader('test.txt', image_shape=(Y_dim, X_dim), mode='file', categorical_labels=True, normalize=False)

img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()

conv_input = input_data(shape=[None, X_dim, Y_dim, 1],
                        name='input',
                        data_augmentation=img_aug)

conv_1 = conv_2d(conv_input,
                 16,
                 filter_size=7,
                 activation='leaky_relu',
                 strides=2)  #Outputs 512x256x16
conv_2 = conv_2d(conv_input,
                 32,
                 filter_size=3,
                 activation='leaky_relu',
Example #7
0
from tflearn.data_augmentation import ImageAugmentation
import pickle

#Loading dataset
X_train, Y_train, X_test, Y_test = pickle.load(open("full_dataset.pkl", "rb"))

#Shuffle dataset
X_train, Y_train = shuffle(X_train, Y_train)

#Normalise dataset
preprocessed_img = ImagePreprocessing()
preprocessed_img.add_featurewise_zero_center()
preprocessed_img.add_featurewise_stdnorm()

#Augmenting dataset
augmented_image = ImageAugmentation()
augmented_image.add_random_flip_leftright()
augmented_image.add_random_rotation(max_angle=25.)
augmented_image.add_random_blur(sigma_max=3.)

#Network

neural_net = input_data(shape=[None, 32, 32, 3],
                        data_preprocessing=preprocessed_img,
                        data_augmentation=augmented_image)

neural_net = conv_2d(neural_net, 32, 3, activation='relu')

neural_net = max_pool_2d(neural_net, 2)

neural_net = conv_2d(neural_net, 64, 3, activation='relu')
Example #8
0
        img = io.imread(f)
        new_img = imresize(img, (size_image, size_image, 3))
        testX[count] = np.array(new_img)
        testY[count] = 1
        count += 1
    except:
        continue

trainY = to_categorical(trainY, 2)
testY = to_categorical(testY, 2)

# Image transformations
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_crop([64, 64], padding=4)

# Define network
network = input_data(shape=[None, 64, 64, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
network = conv_2d(network, 64, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
Example #9
0
# encode the Ys
Y = to_categorical(Y, 4)
Y_test = to_categorical(Y_test, 4)

###################################
# Image transformations
###################################

# normalisation of images
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Create extra synthetic training data by flipping & rotating images
img_aug = ImageAugmentation()

#Lets do this earlier to prevent 90 degree images being represented as 270, and vice-versa
#img_aug.add_random_flip_leftright()

img_aug.add_random_rotation(max_angle=25.)

###################################
# Define network architecture
###################################

# Input is a 32x32 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, size_image, size_image, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
Example #10
0
tmp_img = tmp_img.resize((64, 64), Image.ANTIALIAS)
tmp_img = tmp_img.convert('L')
enh_c = ImageEnhance.Contrast(tmp_img)
tmp_img = enh_c.enhance(12.0)
tmp_img = tmp_img.filter(ImageFilter.SMOOTH_MORE)
tmp_img = tmp_img.filter(ImageFilter.SMOOTH_MORE)
tmp_img = tmp_img.filter(ImageFilter.EDGE_ENHANCE_MORE)
tmp_img = tmp_img.filter(ImageFilter.SMOOTH_MORE)
tmp_img = tmp_img.filter(ImageFilter.EDGE_ENHANCE_MORE)

image = np.reshape(tmp_img.getdata(), (64, 64, 1))

tmp_img.show()

img_preprocessor = ImagePreprocessing()
img_distortion = ImageAugmentation()

# SET UP NETWORK MODEL
network = input_data(shape=[None, 64, 64, 1],
                     data_preprocessing=img_preprocessor,
                     data_augmentation=img_distortion)

# convolution 2
network = conv_2d(network, 16, 5, activation='relu')
# max pooling 2
network = max_pool_2d(network, 2)
# convolution 2
network = conv_2d(network, 24, 3, activation='relu')
# max pooling 2
network = max_pool_2d(network, 2)
# dropout
classes = 0

arr_labels = []
for subdir, dirs, files in os.walk('..' + os.sep + 'training' + os.sep +
                                   'training_images_plot'):
    classes = len(dirs)
    arr_labels = dirs
    break

# 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_flip_updown()
#img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

network = input_data(shape=[None, dimsX, dimsY, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

network = conv_2d(network, 128, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3)
network = local_response_normalization(network)
Example #12
0
    def testing(self):

        map = {
            0: "Bottle without cap",
            1: "Random thing",
            2: "Bottle with cap"
        }

        (X, Y), (X_test, Y_test) = self.loadImages()
        # Real-time data preprocessing
        img_prep = ImagePreprocessing()
        img_prep.add_featurewise_zero_center()
        img_prep.add_featurewise_stdnorm()

        # Real-time data augmentation
        img_aug = ImageAugmentation()
        img_aug.add_random_flip_leftright()
        img_aug.add_random_rotation(max_angle=25.)

        # Convolutional network building
        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, 3, activation='softmax')
        network = regression(network,
                             optimizer='adam',
                             loss='categorical_crossentropy',
                             learning_rate=0.001)

        # Train using classifier
        model = tflearn.DNN(network, tensorboard_verbose=3)

        model.load('init_model.tflearn')

        correct_predictions = 0
        wrong_predictions = 0
        totalTests = X_test.__len__()

        # Run the model on all examples
        for i in range(0, totalTests):
            prediction = model.predict([X_test[i]])
            # print("Prediction: %s" % str(prediction[0]))

            maxIndex = np.argmax(prediction)

            if maxIndex == Y_test[i]:
                correct_predictions += 1
            else:
                wrong_predictions += 1
                print("Wrong prediction, true label is " + str(Y_test[i]) +
                      " Predicted label is: " + str(maxIndex))
                forShow = np.multiply(X_test[i], 100)
                forShow = np.asarray(forShow, int)

                cv2.imwrite("WrongPrediction" + str(maxIndex) + ".png",
                            forShow)

        correct_percent = correct_predictions / float(totalTests)
        wrong_percent = wrong_predictions / float(totalTests)

        print("Correct predictions: " + str(correct_predictions))
        print("Wrong predictions: " + str(wrong_predictions))
        print("Total: " + str(totalTests))

        print("Correct percent: " + str(correct_percent))
        print("Wrong percent: " + str(wrong_percent))
Example #13
0
Coding Just for Fun
Created by burness on 16/9/10.
'''
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, avg_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.merge_ops import merge
from tflearn.layers.estimator import regression
from tflearn.data_utils import image_preloader
from tflearn.data_utils import image_preloader
from tflearn.data_augmentation import ImageAugmentation

img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_flip_updown()
img_aug.add_random_crop([224, 224], 10)
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur()

X, Y = image_preloader('files_list',
                       image_shape=(224, 224),
                       mode='file',
                       categorical_labels=True,
                       normalize=True,
                       files_extension=['.jpg', '.png'])

network = input_data(shape=[None, 224, 224, 3], data_augmentation=img_aug)
conv1_7_7 = conv_2d(network,
class CowClassifier(object):
    """ Cow classifier """
    def __init__(self):
        """ default constructor """
        # Image
        self.image_size = 32  # 32x32

        # tensorflow network variables
        self.tf_img_prep = None
        self.tf_img_aug = None
        self.tf_network = None
        self.tf_model = None

        # 1: setup image preprocessing
        self.setup_image_preprocessing()

        # 2: setup neural network
        self.setup_nn_network()

    def setup_image_preprocessing(self):
        """ Setup image preprocessing """
        # normalization of images
        self.tf_img_prep = ImagePreprocessing()
        self.tf_img_prep.add_featurewise_zero_center()
        self.tf_img_prep.add_featurewise_stdnorm()

        # Randomly create extra image data by rotating and flipping images
        self.tf_img_aug = ImageAugmentation()
        self.tf_img_aug.add_random_flip_leftright()
        self.tf_img_aug.add_random_rotation(max_angle=30.)

    def setup_nn_network(self):
        """ Setup neural network structure """

        # our input is an image of 32 pixels high and wide with 3 channels (RGB)
        # we will also preprocess and create synthetic images
        self.tf_network = input_data(
            shape=[None, self.image_size, self.image_size, 3],
            data_preprocessing=self.tf_img_prep,
            data_augmentation=self.tf_img_aug)

        # layer 1: convolution layer with 32 filters (each being 3x3x3)
        layer_conv_1 = conv_2d(self.tf_network,
                               32,
                               3,
                               activation='relu',
                               name='conv_1')

        # layer 2: max pooling layer
        self.tf_network = max_pool_2d(layer_conv_1, 2)

        # layer 3: convolution layer with 64 filters
        layer_conv_2 = conv_2d(self.tf_network,
                               64,
                               3,
                               activation='relu',
                               name='conv_2')

        # layer 4: Another convolution layer with 64 filters
        layer_conv_3 = conv_2d(layer_conv_2,
                               64,
                               3,
                               activation='relu',
                               name='conv_3')

        # layer 5: Max pooling layer
        self.tf_network = max_pool_2d(layer_conv_3, 2)

        # layer 6: Fully connected 512 node layer
        self.tf_network = fully_connected(self.tf_network,
                                          512,
                                          activation='relu')

        # layer 7: Dropout layer (removes neurons randomly to combat overfitting)
        self.tf_network = dropout(self.tf_network, 0.5)

        # layer 8: Fully connected layer with two outputs (cow or non cow class)
        self.tf_network = fully_connected(self.tf_network,
                                          2,
                                          activation='softmax')

        # define how we will be training our network
        accuracy = Accuracy(name="Accuracy")
        self.tf_network = regression(self.tf_network,
                                     optimizer='adam',
                                     loss='categorical_crossentropy',
                                     learning_rate=0.0005,
                                     metric=accuracy)

    def load_model(self, model_path):
        """ Load model """
        self.tf_model = DNN(self.tf_network, tensorboard_verbose=0)
        self.tf_model.load(model_path)

    def predict_image(self, image_path):
        """ Predict image """
        # Load the image file
        img = scipy.ndimage.imread(image_path, mode="RGB")

        # Scale it to 32x32
        img = scipy.misc.imresize(img, (32, 32),
                                  interp="bicubic").astype(np.float32,
                                                           casting='unsafe')

        # Predict
        return self.tf_model.predict([img])
Example #15
0
def network(img_shape, name, LR):

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur(sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])

    network = input_data(shape=img_shape,
                         name=name,
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    conv1_7_7 = conv_2d(network,
                        64,
                        7,
                        strides=2,
                        activation='relu',
                        name='conv1_7_7_s2')
    pool1_3_3 = max_pool_2d(conv1_7_7, 3, strides=2)
    pool1_3_3 = local_response_normalization(pool1_3_3)
    conv2_3_3_reduce = conv_2d(pool1_3_3,
                               64,
                               1,
                               activation='relu',
                               name='conv2_3_3_reduce')
    conv2_3_3 = conv_2d(conv2_3_3_reduce,
                        192,
                        3,
                        activation='relu',
                        name='conv2_3_3')
    conv2_3_3 = local_response_normalization(conv2_3_3)
    pool2_3_3 = max_pool_2d(conv2_3_3,
                            kernel_size=3,
                            strides=2,
                            name='pool2_3_3_s2')
    inception_3a_1_1 = conv_2d(pool2_3_3,
                               64,
                               1,
                               activation='relu',
                               name='inception_3a_1_1')
    inception_3a_3_3_reduce = conv_2d(pool2_3_3,
                                      96,
                                      1,
                                      activation='relu',
                                      name='inception_3a_3_3_reduce')
    inception_3a_3_3 = conv_2d(inception_3a_3_3_reduce,
                               128,
                               filter_size=3,
                               activation='relu',
                               name='inception_3a_3_3')
    inception_3a_5_5_reduce = conv_2d(pool2_3_3,
                                      16,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_3a_5_5_reduce')
    inception_3a_5_5 = conv_2d(inception_3a_5_5_reduce,
                               32,
                               filter_size=5,
                               activation='relu',
                               name='inception_3a_5_5')
    inception_3a_pool = max_pool_2d(
        pool2_3_3,
        kernel_size=3,
        strides=1,
    )
    inception_3a_pool_1_1 = conv_2d(inception_3a_pool,
                                    32,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_3a_pool_1_1')

    # merge the inception_3a__
    inception_3a_output = merge([
        inception_3a_1_1, inception_3a_3_3, inception_3a_5_5,
        inception_3a_pool_1_1
    ],
                                mode='concat',
                                axis=3)

    inception_3b_1_1 = conv_2d(inception_3a_output,
                               128,
                               filter_size=1,
                               activation='relu',
                               name='inception_3b_1_1')
    inception_3b_3_3_reduce = conv_2d(inception_3a_output,
                                      128,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_3b_3_3_reduce')
    inception_3b_3_3 = conv_2d(inception_3b_3_3_reduce,
                               192,
                               filter_size=3,
                               activation='relu',
                               name='inception_3b_3_3')
    inception_3b_5_5_reduce = conv_2d(inception_3a_output,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_3b_5_5_reduce')
    inception_3b_5_5 = conv_2d(inception_3b_5_5_reduce,
                               96,
                               filter_size=5,
                               name='inception_3b_5_5')
    inception_3b_pool = max_pool_2d(inception_3a_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_3b_pool')
    inception_3b_pool_1_1 = conv_2d(inception_3b_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_3b_pool_1_1')

    #merge the inception_3b_*
    inception_3b_output = merge([
        inception_3b_1_1, inception_3b_3_3, inception_3b_5_5,
        inception_3b_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_3b_output')

    pool3_3_3 = max_pool_2d(inception_3b_output,
                            kernel_size=3,
                            strides=2,
                            name='pool3_3_3')
    inception_4a_1_1 = conv_2d(pool3_3_3,
                               192,
                               filter_size=1,
                               activation='relu',
                               name='inception_4a_1_1')
    inception_4a_3_3_reduce = conv_2d(pool3_3_3,
                                      96,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4a_3_3_reduce')
    inception_4a_3_3 = conv_2d(inception_4a_3_3_reduce,
                               208,
                               filter_size=3,
                               activation='relu',
                               name='inception_4a_3_3')
    inception_4a_5_5_reduce = conv_2d(pool3_3_3,
                                      16,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4a_5_5_reduce')
    inception_4a_5_5 = conv_2d(inception_4a_5_5_reduce,
                               48,
                               filter_size=5,
                               activation='relu',
                               name='inception_4a_5_5')
    inception_4a_pool = max_pool_2d(pool3_3_3,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4a_pool')
    inception_4a_pool_1_1 = conv_2d(inception_4a_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4a_pool_1_1')

    inception_4a_output = merge([
        inception_4a_1_1, inception_4a_3_3, inception_4a_5_5,
        inception_4a_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4a_output')

    inception_4b_1_1 = conv_2d(inception_4a_output,
                               160,
                               filter_size=1,
                               activation='relu',
                               name='inception_4a_1_1')
    inception_4b_3_3_reduce = conv_2d(inception_4a_output,
                                      112,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4b_3_3_reduce')
    inception_4b_3_3 = conv_2d(inception_4b_3_3_reduce,
                               224,
                               filter_size=3,
                               activation='relu',
                               name='inception_4b_3_3')
    inception_4b_5_5_reduce = conv_2d(inception_4a_output,
                                      24,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4b_5_5_reduce')
    inception_4b_5_5 = conv_2d(inception_4b_5_5_reduce,
                               64,
                               filter_size=5,
                               activation='relu',
                               name='inception_4b_5_5')

    inception_4b_pool = max_pool_2d(inception_4a_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4b_pool')
    inception_4b_pool_1_1 = conv_2d(inception_4b_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4b_pool_1_1')

    inception_4b_output = merge([
        inception_4b_1_1, inception_4b_3_3, inception_4b_5_5,
        inception_4b_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4b_output')

    inception_4c_1_1 = conv_2d(inception_4b_output,
                               128,
                               filter_size=1,
                               activation='relu',
                               name='inception_4c_1_1')
    inception_4c_3_3_reduce = conv_2d(inception_4b_output,
                                      128,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4c_3_3_reduce')
    inception_4c_3_3 = conv_2d(inception_4c_3_3_reduce,
                               256,
                               filter_size=3,
                               activation='relu',
                               name='inception_4c_3_3')
    inception_4c_5_5_reduce = conv_2d(inception_4b_output,
                                      24,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4c_5_5_reduce')
    inception_4c_5_5 = conv_2d(inception_4c_5_5_reduce,
                               64,
                               filter_size=5,
                               activation='relu',
                               name='inception_4c_5_5')

    inception_4c_pool = max_pool_2d(inception_4b_output,
                                    kernel_size=3,
                                    strides=1)
    inception_4c_pool_1_1 = conv_2d(inception_4c_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4c_pool_1_1')

    inception_4c_output = merge([
        inception_4c_1_1, inception_4c_3_3, inception_4c_5_5,
        inception_4c_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4c_output')

    inception_4d_1_1 = conv_2d(inception_4c_output,
                               112,
                               filter_size=1,
                               activation='relu',
                               name='inception_4d_1_1')
    inception_4d_3_3_reduce = conv_2d(inception_4c_output,
                                      144,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4d_3_3_reduce')
    inception_4d_3_3 = conv_2d(inception_4d_3_3_reduce,
                               288,
                               filter_size=3,
                               activation='relu',
                               name='inception_4d_3_3')
    inception_4d_5_5_reduce = conv_2d(inception_4c_output,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4d_5_5_reduce')
    inception_4d_5_5 = conv_2d(inception_4d_5_5_reduce,
                               64,
                               filter_size=5,
                               activation='relu',
                               name='inception_4d_5_5')
    inception_4d_pool = max_pool_2d(inception_4c_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4d_pool')
    inception_4d_pool_1_1 = conv_2d(inception_4d_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4d_pool_1_1')

    inception_4d_output = merge([
        inception_4d_1_1, inception_4d_3_3, inception_4d_5_5,
        inception_4d_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4d_output')

    inception_4e_1_1 = conv_2d(inception_4d_output,
                               256,
                               filter_size=1,
                               activation='relu',
                               name='inception_4e_1_1')
    inception_4e_3_3_reduce = conv_2d(inception_4d_output,
                                      160,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4e_3_3_reduce')
    inception_4e_3_3 = conv_2d(inception_4e_3_3_reduce,
                               320,
                               filter_size=3,
                               activation='relu',
                               name='inception_4e_3_3')
    inception_4e_5_5_reduce = conv_2d(inception_4d_output,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4e_5_5_reduce')
    inception_4e_5_5 = conv_2d(inception_4e_5_5_reduce,
                               128,
                               filter_size=5,
                               activation='relu',
                               name='inception_4e_5_5')
    inception_4e_pool = max_pool_2d(inception_4d_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4e_pool')
    inception_4e_pool_1_1 = conv_2d(inception_4e_pool,
                                    128,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4e_pool_1_1')

    inception_4e_output = merge([
        inception_4e_1_1, inception_4e_3_3, inception_4e_5_5,
        inception_4e_pool_1_1
    ],
                                axis=3,
                                mode='concat')

    pool4_3_3 = max_pool_2d(inception_4e_output,
                            kernel_size=3,
                            strides=2,
                            name='pool_3_3')

    inception_5a_1_1 = conv_2d(pool4_3_3,
                               256,
                               filter_size=1,
                               activation='relu',
                               name='inception_5a_1_1')
    inception_5a_3_3_reduce = conv_2d(pool4_3_3,
                                      160,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5a_3_3_reduce')
    inception_5a_3_3 = conv_2d(inception_5a_3_3_reduce,
                               320,
                               filter_size=3,
                               activation='relu',
                               name='inception_5a_3_3')
    inception_5a_5_5_reduce = conv_2d(pool4_3_3,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5a_5_5_reduce')
    inception_5a_5_5 = conv_2d(inception_5a_5_5_reduce,
                               128,
                               filter_size=5,
                               activation='relu',
                               name='inception_5a_5_5')
    inception_5a_pool = max_pool_2d(pool4_3_3,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_5a_pool')
    inception_5a_pool_1_1 = conv_2d(inception_5a_pool,
                                    128,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_5a_pool_1_1')

    inception_5a_output = merge([
        inception_5a_1_1, inception_5a_3_3, inception_5a_5_5,
        inception_5a_pool_1_1
    ],
                                axis=3,
                                mode='concat')

    inception_5b_1_1 = conv_2d(inception_5a_output,
                               384,
                               filter_size=1,
                               activation='relu',
                               name='inception_5b_1_1')
    inception_5b_3_3_reduce = conv_2d(inception_5a_output,
                                      192,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5b_3_3_reduce')
    inception_5b_3_3 = conv_2d(inception_5b_3_3_reduce,
                               384,
                               filter_size=3,
                               activation='relu',
                               name='inception_5b_3_3')
    inception_5b_5_5_reduce = conv_2d(inception_5a_output,
                                      48,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5b_5_5_reduce')
    inception_5b_5_5 = conv_2d(inception_5b_5_5_reduce,
                               128,
                               filter_size=5,
                               activation='relu',
                               name='inception_5b_5_5')
    inception_5b_pool = max_pool_2d(inception_5a_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_5b_pool')
    inception_5b_pool_1_1 = conv_2d(inception_5b_pool,
                                    128,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_5b_pool_1_1')
    inception_5b_output = merge([
        inception_5b_1_1, inception_5b_3_3, inception_5b_5_5,
        inception_5b_pool_1_1
    ],
                                axis=3,
                                mode='concat')

    pool5_7_7 = avg_pool_2d(inception_5b_output, kernel_size=7, strides=1)
    pool5_7_7 = dropout(pool5_7_7, 0.4)
    loss = fully_connected(pool5_7_7, class_num, activation='softmax')
    network = regression(loss,
                         optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=LR,
                         name='targets')
    return network
Example #16
0
        labels.append(label_set[i])

# shuffle images
print("shuffling images")
images, labels = shuffle(images, labels)
images_test, labels_test = shuffle(images_test, labels_test)

# create preprocessor to normalize images
print("creating preprocessor")
img_preprocessor = ImagePreprocessing()
img_preprocessor.add_featurewise_zero_center()
img_preprocessor.add_featurewise_stdnorm()

# distort images
print("adding distortion")
img_distortion = ImageAugmentation()

# only flip left/right for shape training
img_distortion.add_random_flip_leftright()
img_distortion.add_random_blur(sigma_max=1.5)

###
### network architecture
###
print("setting up network")
network = input_data(shape=[None, 64, 64, 1],
                     data_preprocessing=img_preprocessor,
                     data_augmentation=img_distortion)

# convolution 2
network = conv_2d(network, 32, 5, activation='relu')
Example #17
0
def define_network(size_img, N_classes):
    '''
  Function for defining a particular convolutional network including 
  3 convolution layers, and 2x max-pooling, as well as 2x fully connected
  Parameters
  ----------
  size_img: number of pixels of images to be used, with shape=(size_img,size_img)
  N_classes: The number of output classes/cathegories 
  '''
    ###################################
    # Image transformations
    ###################################

    # normalisation of images
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    # Create extra synthetic training data by flipping & rotating images
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)

    ###################################
    # Define network architecture
    ###################################

    # Input is a size_imagexsize_image, 3 color channels (red, green and blue)
    network = input_data(shape=[None, size_img, size_img, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)

    # 1: Convolution layer 32 filters, each 3x3x3
    conv_1 = conv_2d(network, 32, 3, activation='relu', name='conv_1')

    # 2: Max pooling layer
    network = max_pool_2d(conv_1, 2)

    # 3: Convolution layer with 64 filters
    conv_2 = conv_2d(network, 64, 3, activation='relu', name='conv_2')

    # 4: Once more...
    conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3')

    # 5: Max pooling layer
    network = max_pool_2d(conv_3, 2)

    # 6: Fully-connected 512 node layer
    network = fully_connected(network, 512, activation='relu')

    # 7: Dropout layer to combat overfitting
    network = dropout(network, 0.5)

    # 8: Fully-connected layer N_classes outputs
    network = fully_connected(network, N_classes, activation='softmax')

    # Configure of Network training
    acc = Accuracy(name="Accuracy")
    network = regression(network,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.0005,
                         metric=acc)

    return network
Example #18
0
def setup_model(checkpoint_path=None):
    img_prep = ImagePreprocessing()
    '''
    (optional step), you can feed data_preprocessing = None and the network will still train
    Data preprocessing is needed to resolve issues such as incompleteness, inconsistency,
    and/or lacking in certain behaviours or trends which occur often in real world data
    '''
    img_prep.add_featurewise_zero_center()
    """ 
    add_samplewise_zero_center.
    Zero center every sample with specified mean. If not specified,
    the mean is evaluated over all samples.
    Arguments:
        mean: `float` (optional). Provides a custom mean. If none
            provided, it will be automatically caluclated based on
            the training dataset. Default: None.
        per_channel: `bool`. If True, compute mean per color channel.
    Returns:
        Nothing.
    """
    img_prep.add_featurewise_stdnorm()
    """
    add_featurewise_stdnorm.
    Scale each sample by the specified standard deviation. If no std
    specified, std is evaluated over all samples data.
    Arguments:
        std: `float` (optional). Provides a custom standard derivation.
            If none provided, it will be automatically caluclated based on
            the training dataset. Default: None.
        per_channel: `bool`. If True, compute std per color channel.
    Returns:
        Nothing.
    """

    # Create extra synthetic training data by flipping, rotating and blurring the
    # images on our data set.
    img_aug = ImageAugmentation()
    '''
    (optional step), you can feed data_augmentation = None and the network will still train
    Data augmentation increases our data set by performing various operations on images such
    as flipping, rotation and blurring. Also, the training becomes better as the network will 
    be later able to identify blurred images, flipped images, rotated images as well.
    '''
    img_aug.add_random_flip_leftright()
    """ 
    Randomly flip an image (left to right).
    Returns:
        Nothing.
    """
    img_aug.add_random_rotation(max_angle=25.)
    """ 
    Randomly rotate an image by a random angle (-max_angle, max_angle).
    Arguments:
        max_angle: `float`. The maximum rotation angle.
    Returns:
        Nothing.
    """
    img_aug.add_random_blur(sigma_max=3.)
    """ 
    Randomly blur an image by applying a gaussian filter with a random
    sigma (0., sigma_max).
    Arguments:
        sigma: `float` or list of `float`. Standard deviation for Gaussian
            kernel. The standard deviations of the Gaussian filter are
            given for each axis as a sequence, or as a single number,
            in which case it is equal for all axes.
    Returns:
        Nothing.
    """

    network = input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    # Input is a 32x32 image with 3 color channels (red, green and blue)
    # The first placeholder in shape array must be None to denote the batch size
    # If not provided, will be added automatically
    # print network = Tensor("InputData/X:0", shape=(?, 32, 32, 3), dtype=float32)
    """ 
    This layer is used for inputting (aka. feeding) data to a network.
    A TensorFlow placeholder will be used if it is supplied,
    otherwise a new placeholder will be created with the given shape.
    Either a shape or placeholder must be provided, otherwise an
    exception will be raised.
    Input:
        List of `int` (Shape), to create a new placeholder.
            Or
        `Tensor` (Placeholder), to use an existing placeholder.
    Output:
        Placeholder Tensor with given shape.
    Arguments:
        shape: list of `int`. An array or tuple representing input data shape.
            It is required if no placeholder is provided. First element should
            be 'None' (representing batch size), if not provided, it will be
            added automatically.
        placeholder: A Placeholder to use for feeding this layer (optional).
            If not specified, a placeholder will be automatically created.
            You can retrieve that placeholder through graph key: 'INPUTS',
            or the 'placeholder' attribute of this function's returned tensor.
        dtype: `tf.type`, Placeholder data type (optional). Default: float32.
        data_preprocessing: A `DataPreprocessing` subclass object to manage
            real-time data pre-processing when training and predicting (such
            as zero center data, std normalization...).
        data_augmentation: `DataAugmentation`. A `DataAugmentation` subclass
            object to manage real-time data augmentation while training (
            such as random image crop, random image flip, random sequence
            reverse...).
        name: `str`. A name for this layer (optional).
    """
    network = conv_2d(network, 32, 3, activation='relu')
    # conv_2d(incoming, nb_filter, filter_size, strides=1, padding='same',
    #         activation='linear', bias=True, weights_init='uniform_scaling',
    #         bias_init='zeros', regularizer=None, weight_decay=0.001,
    #         trainable=True, restore=True, reuse=False, scope=None,
    #         name="Conv2D"):
    # Input:
    #     4-D Tensor [batch, height, width, in_channels].
    # Output:
    #     4-D Tensor [batch, new height, new width, nb_filter].
    # print network = Tensor("Conv2D/Relu:0", shape=(?, 32, 32, 32), dtype=float32)
    '''
    Here we're using relu activation function instead of the sigmoid function 
    that we looked at as relu has reduced likleyhood of the vanishing gradient
    that we saw in sigmoid

    This particular function takes the previously created network as input, the 
    number of convolutional filters being 32 with each filter size 3. 
    [http://cs231n.github.io/convolutional-networks/]
    '''
    network = max_pool_2d(network, 2)
    # max_pool_2d(incoming, kernel_size, strides=None, padding='same',
    #             name="MaxPool2D"):
    # Input:
    #     4-D Tensor [batch, height, width, in_channels].
    # Output:
    #     4-D Tensor [batch, pooled height, pooled width, in_channels].
    # print network = Tensor("MaxPool2D/MaxPool:0", shape=(?, 16, 16, 32), dtype=float32)
    '''
    It is common to periodically insert a Pooling layer in-between successive Conv 
    layers in a ConvNet architecture. Its function is to progressively reduce the 
    spatial size of the representation to reduce the amount of parameters and 
    computation in the network, and hence to also control overfitting.
    '''
    network = conv_2d(network, 64, 3, activation='relu')
    #print network = Tensor("Conv2D_1/Relu:0", shape=(?, 16, 16, 64), dtype=float32)
    network = conv_2d(network, 64, 3, activation='relu')
    # print network = Tensor("Conv2D_2/Relu:0", shape=(?, 16, 16, 64), dtype=float32)
    network = max_pool_2d(network, 2)
    # print network = Tensor("MaxPool2D_1/MaxPool:0", shape=(?, 8, 8, 64), dtype=float32)
    network = fully_connected(network, 512, activation='relu')
    # fully_connected(incoming, n_units, activation='linear', bias=True,
    #                 weights_init='truncated_normal', bias_init='zeros',
    #                 regularizer=None, weight_decay=0.001, trainable=True,
    #                 restore=True, reuse=False, scope=None,
    #                 name="FullyConnected"):
    #   incoming: `Tensor`. Incoming (2+)D Tensor.
    #   n_units: `int`, number of units for this layer.
    '''
    Neurons in a fully connected layer have full connections to all activations in the 
    previous layer, as seen in regular Neural Networks. Their activations can hence be
    computed with a matrix multiplication followed by a bias offset. 
    '''
    # print network = Tensor("FullyConnected/Relu:0", shape=(?, 512), dtype=float32)
    network = dropout(network, 0.5)
    # it's one of the method to prevent overfitting
    """ Dropout.
    Outputs the input element scaled up by `1 / keep_prob`. The scaling is so
    that the expected sum is unchanged.
    Arguments:
        incoming : A `Tensor`. The incoming tensor.
        keep_prob : A float representing the probability that each element
            is kept.
        name : A name for this layer (optional).
    References:
        Dropout: A Simple Way to Prevent Neural Networks from Overfitting.
        N. Srivastava, G. Hinton, A. Krizhevsky, I. Sutskever & R. Salakhutdinov,
        (2014), Journal of Machine Learning Research, 5(Jun)(2), 1929-1958.
    Links:
      [https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf]
        (https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf)
    """
    # print network = Tensor("Dropout/cond/Merge:0", shape=(?, 512), dtype=float32)
    network = fully_connected(network, 2, activation='softmax')
    # print network = Tensor("FullyConnected_1/Softmax:0", shape=(?, 2), dtype=float32)
    network = regression(network,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)
    """
    Regression.
    The regression layer is used in TFLearn to apply a regression (linear or
    logistic) to the provided input. It requires to specify a TensorFlow
    gradient descent optimizer 'optimizer' that will minimize the provided
    loss function 'loss' (which calculate the errors). A metric can also be
    provided, to evaluate the model performance.
    A 'TrainOp' is generated, holding all information about the optimization
    process. It is added to TensorFlow collection 'tf.GraphKeys.TRAIN_OPS'
    and later used by TFLearn 'models' classes to perform the training.
    An optional placeholder 'placeholder' can be specified to use a custom
    TensorFlow target placeholder instead of creating a new one. The target
    placeholder is added to the 'tf.GraphKeys.TARGETS' TensorFlow
    collection, so that it can be retrieved later.
    Additionaly, a list of variables 'trainable_vars' can be specified,
    so that only them will be updated when applying the backpropagation
    algorithm.
    Input:
        2-D Tensor Layer.
    Output:
        2-D Tensor Layer (Same as input).

    Arguments:
        incoming: `Tensor`. Incoming 2-D Tensor.
        placeholder: `Tensor`. This regression target (label) placeholder.
            If 'None' provided, a placeholder will be added automatically.
            You can retrieve that placeholder through graph key: 'TARGETS',
            or the 'placeholder' attribute of this function's returned tensor.
        optimizer: `str` (name), `Optimizer` or `function`. Optimizer to use.
            Default: 'adam' (Adaptive Moment Estimation).
        loss: `str` (name) or `function`. Loss function used by this layer
            optimizer. Default: 'categorical_crossentropy'.
        metric: `str`, `Metric` or `function`. The metric to be used.
            Default: 'default' metric is 'accuracy'. To disable metric
            calculation, set it to 'None'.
        learning_rate: `float`. This layer optimizer's learning rate.
        dtype: `tf.types`. This layer placeholder type. Default: tf.float32.
        batch_size: `int`. Batch size of data to use for training. tflearn
            supports different batch size for every optimizers. Default: 64.
        shuffle_batches: `bool`. Shuffle or not this optimizer batches at
            every epoch. Default: True.
        to_one_hot: `bool`. If True, labels will be encoded to one hot vectors.
            'n_classes' must then be specified.
        n_classes: `int`. The total number of classes. Only required when using
            'to_one_hot' option.
        trainable_vars: list of `Variable`. If specified, this regression will
            only update given variable weights. Else, all trainale variable
            are going to be updated.
        restore: `bool`. If False, variables related to optimizers such
            as moving averages will not be restored when loading a
            pre-trained model.
        op_name: A name for this layer optimizer (optional).
            Default: optimizer op name.
        validation_monitors: `list` of `Tensor` objects.  List of variables
            to compute during validation, which are also used to produce
            summaries for output to TensorBoard.  For example, this can be
            used to periodically record a confusion matrix or AUC metric, 
            during training.  Each variable should have rank 1, i.e. 
            shape [None].
        validation_batch_size: `int` or None. Specifies the batch
            size to be used for the validation data feed.
        name: A name for this layer's placeholder scope.
    Attributes:
        placeholder: `Tensor`. Placeholder for feeding labels.
    """
    if checkpoint_path:
        model = tflearn.DNN(network,
                            tensorboard_verbose=3,
                            checkpoint_path=checkpoint_path)
    else:
        model = tflearn.DNN(network, tensorboard_verbose=3)
    return model
Example #19
0
def getReseau(index, nb_class):
    data = json.load(open("models/" + str(index) + "_" + str(nb_class) + "/result.json"))
    settings = data['settings']

    size = settings['size']
    nb_filter = settings['nb_filter']
    filter_size = settings['filter_size']

    # Make sure the data is normalized
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    # Create extra synthetic training data by flipping, rotating and blurring the
    # images on our data set.
    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.)

    # Define our network architecture:

    # Input is a 32x32 image with 3 color channels (red, green and blue)
    network = input_data(shape=[None, size, size, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)

    reseau = index

    if reseau == 1:
        # Step 1: Convolution
        network = conv_2d(network, nb_filter, filter_size, activation='relu')

        # Step 2: Max pooling
        network = max_pool_2d(network, 2)

        # Step 3: Convolution again
        network = conv_2d(network, nb_filter * 4, filter_size, activation='relu')

        # Step 4: Convolution yet again
        network = conv_2d(network, nb_filter * 4, filter_size, activation='relu')

        # Step 5: Max pooling again
        network = max_pool_2d(network, 2)

        # Step 6: Fully-connected 512 node neural network
        network = fully_connected(network, nb_filter * 16, activation='relu')

        # Step 7: Dropout - throw away some data randomly during training to prevent over-fitting
        network = dropout(network, 0.5)


    elif reseau == 2:
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 3, activation='relu')
        network = max_pool_2d(network, 2)
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 3, activation='relu')
        network = max_pool_2d(network, 2)
        network = fully_connected(network, 512, activation='relu')
        network = fully_connected(network, 512, activation='relu')

    elif reseau == 3:
        network = conv_2d(network, 32, 3, activation='relu')
        network = avg_pool_2d(network, 2)
        network = conv_2d(network, 32, 3, activation='relu')
        network = avg_pool_2d(network, 2)
        network = conv_2d(network, 32, 3, activation='relu')
        network = avg_pool_2d(network, 2)
        network = fully_connected(network, 512, activation='relu')
        network = fully_connected(network, 512, activation='relu')
        network = dropout(network, 0.5)

    elif reseau == 4:
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 5, padding='valid', activation='relu')
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 5, padding='valid', activation='relu')
        network = fully_connected(network, 512, activation='relu')
        network = dropout(network, 0.5)

    elif reseau == 5:
        network = conv_2d(network, 64, 3, activation='relu')
        network = conv_2d(network, 64, 3, activation='relu')
        network = avg_pool_2d(network, 2)
        network = conv_2d(network, 32, 3, activation='relu')
        network = conv_2d(network, 32, 3, activation='relu')
        network = max_pool_2d(network, 2)
        network = fully_connected(network, 512, activation='relu')
        network = fully_connected(network, 512, activation='relu')

    # Step 8: Fully-connected neural network with three outputs (0=isn't a bird, 1=is a bird) to make the final prediction
    # network = fully_connected(network, ld.getLabelsNumber(), activation='softmax')
    network = fully_connected(network, nb_class, activation='softmax')
    # Tell tflearn how we want to train the network
    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=settings['learning_rate'])

    # Wrap the network in a model object
    model = tflearn.DNN(network,
                        tensorboard_verbose=0)

    return model
def build_network(output_dims=[20, 100],
                  get_hidden_reps=False,
                  get_fc_softmax_activations=False):

    assert (
        len(output_dims) == 2
    ), "output_dims needs to be of length 2, containing coarse_dim and fine_dim."
    # outputdims is a list of num_classes

    coarse_dim, fine_dim = tuple(output_dims)
    # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)

    network = input_data(shape=[None, 32, 32, 3], 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)

    coarse_network = conv_2d(network,
                             64,
                             3,
                             activation='relu',
                             name="unique_conv_1_coarse")
    coarse_network = conv_2d(coarse_network,
                             64,
                             3,
                             activation='relu',
                             name="unique_conv_2_coarse")
    coarse_network = max_pool_2d(coarse_network, 2)

    coarse_network = fully_connected(coarse_network,
                                     512,
                                     activation='relu',
                                     name="unique_fc_1_coarse")
    coarse_hidden_reps = coarse_network
    coarse_network = dropout(coarse_network, 0.5)
    coarse_network = fully_connected(coarse_network,
                                     coarse_dim,
                                     activation='softmax',
                                     name="unique_fc_2_coarse")

    fine_network = conv_2d(network,
                           64,
                           3,
                           activation='relu',
                           name="unique_conv_1_fine")
    fine_network = conv_2d(fine_network,
                           64,
                           3,
                           activation='relu',
                           name="unique_conv_2_fine")
    fine_network = max_pool_2d(fine_network, 2)
    fine_network = fully_connected(fine_network,
                                   512,
                                   activation='relu',
                                   name="unique_fc_1_fine")
    fine_hidden_reps = fine_network
    fine_network = dropout(fine_network, 0.5)
    fine_network = fully_connected(fine_network,
                                   fine_dim,
                                   activation='softmax',
                                   name="unique_fc_2_fine")

    if get_hidden_reps:
        return (coarse_hidden_reps, fine_hidden_reps)

    if get_fc_softmax_activations:
        # return the last layer of the coarse and the fine branch.
        # needed so we can write a custom function which takes the activations, computes the confidence scores and
        # decides at what level of the hierarchy to predict.
        return (coarse_network, fine_network)

    # coarse_confidence =
    # fine_confidence =

    stacked_coarse_and_fine_net = tf.concat(1, [coarse_network, fine_network])

    target_placeholder = tf.placeholder(dtype=tf.float32,
                                        shape=(None, coarse_dim + fine_dim))

    def coarse_and_fine_joint_loss(incoming, placeholder):
        # coarse_dim = 20
        # fine_dim = 40
        # has access to coarse_dim and fine_dim
        coarse_pred = incoming[:, :coarse_dim]
        fine_pred = incoming[:, coarse_dim:]
        coarse_target = placeholder[:, :coarse_dim]
        fine_target = placeholder[:, coarse_dim:]

        coarse_loss = tflearn.categorical_crossentropy(coarse_pred,
                                                       coarse_target)
        fine_loss = tflearn.categorical_crossentropy(fine_pred, fine_target)

        return coarse_loss + fine_loss

    def coarse_and_fine_accuracy(y_pred, y_true, x):
        coarse_pred = y_pred[:, :coarse_dim]
        fine_pred = y_pred[:, coarse_dim:]
        coarse_target = y_true[:, :coarse_dim]
        fine_target = y_true[:, coarse_dim:]

        coarse_acc = tflearn.metrics.accuracy_op(coarse_pred, coarse_target)
        fine_acc = tflearn.metrics.accuracy_op(fine_pred, fine_target)

        # rounded_coarse_acc = tf.to_float(tf.round(coarse_acc * 1000) * 100000)
        # return tf.add(rounded_coarse_acc, fine_acc)
        return fine_acc

    joint_network = regression(stacked_coarse_and_fine_net,
                               placeholder=target_placeholder,
                               optimizer='adam',
                               loss=coarse_and_fine_joint_loss,
                               metric=coarse_and_fine_accuracy,
                               learning_rate=0.001)
    return joint_network
train_data = xs_train[msk].reshape([-1, 20, 20, 1])
train_labels = ys_train[msk].as_matrix()

validation_data = xs_train[~msk].reshape([-1, 20, 20, 1])
validation_labels = ys_train[~msk].as_matrix()

del xs_train
del ys_train

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=10.)
img_aug.add_random_blur()

# Building convolutional network
network = input_data(shape=[None, 20, 20, 1], name='input',
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
network = conv_2d(network, 128, 3, activation='relu')
network = conv_2d(network, 128, 3, activation='relu')
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 2)
# Training and Validation Split
X, X_val, Y, Y_val = train_test_split(Train_Samples,
                                      Train_Labels,
                                      test_size=0.25,
                                      random_state=42)

# Convert class vectors to binary class matrix
Y = to_categorical(Y, 2)
Y_val = to_categorical(Y_val, 2)

# Data Augmentation and Image Processing
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_flip_updown()
img_aug.add_random_90degrees_rotation()
img_aug.add_random_blur()
img_aug.add_random_rotation(max_angle=25)

# Define the Model Architecture
net = input_data(shape=[None, Img_size, Img_size, 3],
                 data_preprocessing=img_prep,
                 data_augmentation=img_aug)

conv_1 = conv_2d(net, 32, 3, activation='relu', name='conv_1')
net = max_pool_2d(conv_1, 2)
conv_2 = conv_2d(net, 64, 3, activation='relu', name='conv_2')
conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3')
Example #23
0
def main(_):
    # dirname = os.path.join(FLAGS.buckets, "")
    # (X, Y), (X_test, Y_test) = load_data(dirname)
    # print("load data done")

    # X, Y = shuffle(X, Y)
    # Y = to_categorical(Y, 10)
    # Y_test = to_categorical(Y_test, 10)

    # Real-time data preprocessing
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)

    # Convolutional network building
    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, 10, activation='softmax')
    network = regression(network,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)

    # Train using classifier
    model = tflearn.DNN(network, tensorboard_verbose=0)
    # model.fit(X, Y, n_epoch=100, shuffle=True, validation_set=(X_test, Y_test),
    #           show_metric=True, batch_size=96, run_id='cifar10_cnn')
    model_path = os.path.join(FLAGS.checkpointDir, "model.tfl")
    print(model_path)
    model.load(model_path)

    # predict_pic = os.path.join(FLAGS.buckets, "bird_mount_bluebird.jpg")
    # file_paths = tf.train.match_filenames_once(predict_pic)
    # input_file_queue = tf.train.string_input_producer(file_paths)
    # reader = tf.WholeFileReader()
    # file_path, raw_data = reader.read(input_file_queue)
    # img = tf.image.decode_jpeg(raw_data, 3)
    # img = tf.image.resize_images(img, [32, 32])
    # prediction = model.predict([img])
    # print (prediction[0])
    predict_pic = os.path.join(FLAGS.buckets, "bird_bullocks_oriole.jpg")
    img_obj = file_io.read_file_to_string(predict_pic, True)
    file_io.write_string_to_file("bird_bullocks_oriole.jpg", img_obj)

    img = scipy.ndimage.imread("bird_bullocks_oriole.jpg", 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])
    print(prediction[0])
    print(prediction[0])
    #print (prediction[0].index(max(prediction[0])))
    num = [
        'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
        'horse', 'ship', 'truck'
    ]
    print("This is a %s" %
          (num[prediction[0].tolist().index(max(prediction[0]))]))
Example #24
0
image_size = (64, 64)

import load_data
(X, Y), (X_test, Y_test) = load_data.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, num_classes)
Y_test = to_categorical(Y_test, num_classes)


# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=180.)
img_aug.add_random_crop(image_size, padding=6)

def build_network(image_size, batch_size=None, n_channels=3):
    network = input_data(shape=[batch_size, image_size[0], image_size[1], n_channels],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
    network = conv_2d(network, 16, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, num_classes, activation='softmax')
    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
Example #25
0
x = all.astype(np.uint8)

imsize = 64

#labelY=label_Y.astype(np.uint8)
###################################
# Image transformations
###################################

# normalisation of images
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Create extra synthetic training data by flipping & rotating images
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=89.)
img_aug.add_random_blur(sigma_max=3.)
img_aug.add_random_flip_updown()
img_aug.add_random_90degrees_rotation(rotations=[0, 1, 2, 3])

###################################
# Define network architecture
###################################

# Input is a 64x64 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, 64, 64, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
Example #26
0
# get command line arguments
arch = sys.argv[1]  # name of architecture
modelpath = sys.argv[2]  # path to saved model
layer = sys.argv[3]  # layer name, for example, Conv2D or Conv2D_1
ichannel = int(
    sys.argv[4])  # input channel for displaying kernels and convolutions

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_samplewise_zero_center(
)  # per sample (featurewise is a global value)
img_prep.add_samplewise_stdnorm()  # per sample (featurewise is a global value)

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_flip_updown()
img_aug.add_random_rotation(max_angle=10.)

# network definition
network = input_data(
    shape=[None, HEIGHT, WIDTH, CHANNELS],  # shape=[None,IMAGE, IMAGE] for RNN
    data_preprocessing=None,
    data_augmentation=None)

#in2 = input_data(shape=[None,1])
#print(network.shape)
#print(in2.shape,"\n")
#network = architectures.build_merge_test(network,in2,CLASSES)
Example #27
0
import os
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.metrics import Accuracy
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation

###################################
# Image transformations
###################################

# normalisation of images
img_prep = ImagePreprocessing()
# Create extra synthetic training data by flipping & rotating images
img_aug = ImageAugmentation()

###################################
# Define network architecture
###################################

# Input is a 32x32 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, 64, 64, 1],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

# 1: Convolution layer with 32 filters, each 3x3x3
conv_1 = conv_2d(network, 64, 3, activation='relu', name='conv_1')

# 2: Max pooling layer
network = max_pool_2d(conv_1, 2)
Example #28
0
def build_network(n_classes, get_hidden_reps=False):
    #assert n_output_units is not None, \
    #    "You need to specify how many tokens are in the output classification sequence."
    # n_classes represents the total number of classes

    # input tensor is prefeature_size x n_output_units
    n_output_units = 2
    prefeature_embedding_size = 512
    single_output_token_size = (100 + 20 + 1)

    # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)

    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 = conv_2d(network,
                      64,
                      3,
                      activation='relu',
                      name="unique_Conv2D_3")
    network = conv_2d(network,
                      64,
                      3,
                      activation='relu',
                      name="unique_Conv2D_4")
    network = max_pool_2d(network, 2)

    network = fully_connected(network,
                              512,
                              activation='relu',
                              name="unique_FullyConnected")
    network = fully_connected(network,
                              512,
                              activation='relu',
                              name="unique_FullyConnected_1")

    net = network  # can preload weights up until this point to possibly make faster

    # Basically, this repeats the input several times to be fed into the LSTM
    net = tf.tile(net, [1, n_output_units])
    net = tf.reshape(net, [-1, n_output_units, prefeature_embedding_size])

    net = tflearn.lstm(
        net,
        single_output_token_size,
        return_seq=True,
        name="actuallyunique_lstm"
    )  # This returns [# of samples, # of timesteps, output dim]

    fine_network, coarse_network = net
    fine_network = fully_connected(fine_network,
                                   single_output_token_size,
                                   activation='softmax',
                                   name="actuallyunique_fine_fc")
    coarse_network = fully_connected(coarse_network,
                                     single_output_token_size,
                                     activation='softmax',
                                     name="actuallyunique_fine_fc")

    stacked_coarse_and_fine_net = tf.concat(1, [coarse_network, fine_network])

    # We need to split this and attach different losses
    target_placeholder = tf.placeholder(dtype=tf.float32,
                                        shape=(None, n_output_units *
                                               single_output_token_size))

    def coarse_and_fine_joint_loss(incoming, placeholder):
        coarse_pred = incoming[:, :single_output_token_size]
        coarse_target = placeholder[:, :single_output_token_size]
        coarse_loss = tflearn.categorical_crossentropy(coarse_pred,
                                                       coarse_target)

        fine_pred = incoming[:, single_output_token_size:]
        fine_target = placeholder[:, single_output_token_size:]
        fine_loss = tflearn.categorical_crossentropy(fine_pred, fine_target)

        return 4 * coarse_loss + fine_loss

    def coarse_and_fine_accuracy(y_pred, y_true, x):
        raw_predictions = y_pred
        raw_predictions = tf.reshape(raw_predictions,
                                     (-1, 2, single_output_token_size))
        raw_predictions = tf.argmax(raw_predictions, 2)

        raw_corrects = y_true
        raw_corrects = tf.reshape(raw_corrects,
                                  (-1, 2, single_output_token_size))
        raw_corrects = tf.argmax(raw_corrects, 2)

        hierarchical_pred = simplify_to_hierarchical_format(raw_predictions)
        hierarchical_target = simplify_to_hierarchical_format(raw_corrects)

        hierarchical_corrects = tf.equal(hierarchical_pred,
                                         hierarchical_target)
        hierarchical_acc = tf.reduce_mean(tf.cast(hierarchical_corrects,
                                                  tf.float32),
                                          name="Hierarchical_accuracy")

        return hierarchical_acc

        ##### To calculate the average accuracy, do:
        #####
        # coarse_pred = y_pred[:, :single_output_token_size]
        # fine_pred = y_pred[:, single_output_token_size:]
        # coarse_target = y_true[:, :single_output_token_size]
        # fine_target = y_true[:, single_output_token_size:]
        #
        # coarse_acc = tflearn.metrics.accuracy_op(coarse_pred, coarse_target)
        # fine_acc = tflearn.metrics.accuracy_op(fine_pred, fine_target)
        # return (fine_acc + coarse_acc) / 2.0
        #####
        #####

        ## LISA --- All this code isn't required to be here.... but after taking 6 hours to figure out how to make this
        ##          work... it felt wrong to delete all of this... lol

        # rounded_coarse_acc = tf.to_float(tf.round(coarse_acc * 1000) * 100000)
        # return tf.add(rounded_coarse_acc, fine_acc)
        #with tf.Graph().as_default():
        #import tflearn.helpers.summarizer as s
        #s.summarize(coarse_acc, 'scalar', "test_summary")
        #summaries.get_summary(type, name, value, summary_collection)
        #tflearn.summaries.get_summary("scalar", "coarse_acc", coarse_acc, "test_summary_collection")
        #tflearn.summaries.get_summary("scalar", "fine_acc", fine_acc, "test_summary_collection")
        #summaries.add_gradients_summary(grads, "", "", summary_collection)
        #sum1 = tf.scalar_summary("coarse_acc", coarse_acc)
        #tf.merge_summary([sum1])

        #tf.merge_summary(tf.get_collection("test_summary_collection"))
        #tflearn.summaries.get_summary("scalar", "coarse_acc", coarse_acc)
        #tflearn.summaries.get_summary("scalar", "fine_acc", fine_acc)
        #tf.scalar_summary("fine_acc", fine_acc)

        #tf_summarizer.summarize(coarse_acc, "scalar", "Coarse_accuracy")
        #tf_summarizer.summarize(fine_acc, "scalar", "Fine_accuracy")

    #test_const = tf.constant(32.0, name="custom_constant")
    #sum1 = tf.scalar_summary("dumb_contant", test_const)
    #tf.merge_summary([sum1])

    # Class predictions are in the form [[A, B], ...], where A=Coarse, B=Fine
    def simplify_to_hierarchical_format(class_labels, end_token=120):
        fine_labels = class_labels[:, 1]
        coarse_labels = class_labels[:, 0]

        coarse_labels_mask = tf.cast(tf.equal(fine_labels, end_token),
                                     tf.int64)
        coarse_labels_to_permit = coarse_labels * coarse_labels_mask

        fine_labels_mask = tf.cast(tf.not_equal(fine_labels, end_token),
                                   tf.int64)
        fine_labels_to_permit = fine_labels * fine_labels_mask

        hierarchical_labels = coarse_labels_to_permit + fine_labels_to_permit

        return hierarchical_labels

    with tf.name_scope('Accuracy'):
        with tf.name_scope('Coarse_Accuracy'):
            coarse_pred = coarse_network
            coarse_target = target_placeholder[:, :single_output_token_size]
            correct_coarse_pred = tf.equal(tf.argmax(coarse_pred, 1),
                                           tf.argmax(coarse_target, 1))
            coarse_acc_value = tf.reduce_mean(
                tf.cast(correct_coarse_pred, tf.float32))

        with tf.name_scope('Fine_Accuracy'):
            fine_pred = fine_network
            fine_target = target_placeholder[:, single_output_token_size:]
            correct_fine_pred = tf.equal(tf.argmax(fine_pred, 1),
                                         tf.argmax(fine_target, 1))
            fine_acc_value = tf.reduce_mean(
                tf.cast(correct_fine_pred, tf.float32))

    with tf.name_scope('Combination_Accuracies'):
        with tf.name_scope('Both_Correct_Accuracy'):
            both_correct_acc_value = tflearn.metrics.accuracy_op(
                stacked_coarse_and_fine_net, target_placeholder)
        with tf.name_scope('Average_Accuracy'):
            avg_acc_value = (coarse_acc_value + fine_acc_value) / 2
        with tf.name_scope('Hierarchical_Accuracy'):
            raw_predictions = stacked_coarse_and_fine_net
            raw_predictions = tf.reshape(raw_predictions,
                                         (-1, 2, single_output_token_size))
            raw_predictions = tf.argmax(raw_predictions, 2)

            raw_corrects = target_placeholder
            raw_corrects = tf.reshape(raw_corrects,
                                      (-1, 2, single_output_token_size))
            raw_corrects = tf.argmax(raw_corrects, 2)

            hierarchical_pred = simplify_to_hierarchical_format(
                raw_predictions)
            hierarchical_target = simplify_to_hierarchical_format(raw_corrects)

            hierarchical_corrects = tf.equal(hierarchical_pred,
                                             hierarchical_target)
            hierarchical_acc = tf.reduce_mean(
                tf.cast(hierarchical_corrects, tf.float32))
            # Reduce the prediction and the actual to ONE dimension

    net = regression(stacked_coarse_and_fine_net,
                     placeholder=target_placeholder,
                     optimizer='adam',
                     loss=coarse_and_fine_joint_loss,
                     metric=coarse_and_fine_accuracy,
                     validation_monitors=[
                         coarse_acc_value, fine_acc_value,
                         both_correct_acc_value, avg_acc_value,
                         hierarchical_acc
                     ],
                     learning_rate=0.0001)

    return net
Example #29
0
from scipy.misc import imread, imresize
from imagenet_classes import class_names
import csv

tflearn.config.init_graph (num_cores=4, gpu_memory_fraction=0.5)

dataset_file = '../../dataset/val.txt'
r = csv.reader(open(dataset_file,'r'), delimiter=' ')
X, Y = image_preloader(dataset_file, image_shape=(256, 256),   mode='file', categorical_labels=True,   normalize=True)


img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

img_aug = ImageAugmentation()
img_aug.add_random_crop((224,224))
img_aug.add_random_flip_leftright()

inp = input_data(shape=[None, 224, 224, 3], data_preprocessing=img_prep, data_augmentation=img_aug, name='input')

conv1_1 = conv_2d(inp, 64, 3, activation='relu', name="conv1_1")
conv1_2 = conv_2d(conv1_1, 64, 3, activation='relu', name="conv1_2")
pool1 = max_pool_2d(conv1_2, 2, strides=2)

conv2_1 = conv_2d(pool1, 128, 3, activation='relu', name="conv2_1")
conv2_2 = conv_2d(conv2_1, 128, 3, activation='relu', name= "conv2_2")
pool2 = max_pool_2d(conv2_2, 2, strides=2)

conv3_1 = conv_2d(pool2, 256, 3, activation='relu', name="conv3_1")
conv3_2 = conv_2d(conv3_1, 256, 3, activation='relu', name="conv3_2")
Example #30
0
def initialize_network():

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    # Create extra synthetic training data by flipping, rotating and blurring the
    # images on our data set.
    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.)

    # Define our network architecture:

    # Input is a 32x32 image with 3 color channels (red, green and blue)
    network = input_data(shape=[None, 128, 128, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)

    dropout_chance = 0.5

    print("Step 1")

    # Step 1: Convolution
    network = conv_2d(network, 32, 3, activation='relu')

    print("Step 2")

    # Step 2: Max pooling
    network = max_pool_2d(network, 2)

    #network = dropout(network, dropout_chance)

    print("Step 3")

    # Step 3: Convolution again
    network = conv_2d(network, 64, 3, activation='relu')
    #network = max_pool_2d(network, 2)
    #network = dropout(network, dropout_chance)

    print("Step 4")

    # Step 4: Convolution yet again
    network = conv_2d(network, 64, 4, activation='relu')
    #network = max_pool_2d(network, 2)
    #network = dropout(network, dropout_chance)
    print("Step 5")

    # Step 5: Max pooling again
    network = max_pool_2d(network, 2)

    print("Step 6")

    # Step 6: Fully-connected 512 node neural network
    network = fully_connected(network, 512, activation='relu')

    print("Step 7")

    # Step 7: Dropout - throw away some data randomly during training to prevent over-fitting
    #network = dropout(network, dropout_chance)

    # Step 6: Fully-connected 512 node neural network
    #network = fully_connected(network, 512, activation='relu')

    print("Step 7")

    # Step 7: Dropout - throw away some data randomly during training to prevent over-fitting
    network = dropout(network, dropout_chance)

    print("Step 8")

    # Step 8: Fully-connected neural network with two outputs (0=isn't a bird, 1=is a bird) to make the final prediction
    network = fully_connected(network, 7, activation='softmax')

    print("Step 9")

    # Tell tflearn how we want to train the network
    network = regression(network,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)

    # Wrap the network in a model object
    model = tflearn.DNN(network, tensorboard_verbose=0)

    return model
Example #31
0
def google_graph(X):
    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_crop([64, 64], padding=4)
    network = input_data(shape=[None, 64, 64, 3], data_preprocessing=img_prep, data_augmentation=img_aug)
    conv1_7_7 = conv_2d(network, 64, 7, strides=2, activation='relu', name='conv1_7_7_s2')
    pool1_3_3 = max_pool_2d(conv1_7_7, 3, strides=2)
    pool1_3_3 = local_response_normalization(pool1_3_3)
    conv2_3_3_reduce = conv_2d(pool1_3_3, 64, 1, activation='relu', name='conv2_3_3_reduce')
    conv2_3_3 = conv_2d(conv2_3_3_reduce, 192, 3, activation='relu', name='conv2_3_3')
    conv2_3_3 = local_response_normalization(conv2_3_3)
    pool2_3_3 = max_pool_2d(conv2_3_3, kernel_size=3, strides=2, name='pool2_3_3_s2')
    inception_3a_1_1 = conv_2d(pool2_3_3, 64, 1, activation='relu', name='inception_3a_1_1')
    inception_3a_3_3_reduce = conv_2d(pool2_3_3, 96, 1, activation='relu', name='inception_3a_3_3_reduce')
    inception_3a_3_3 = conv_2d(inception_3a_3_3_reduce, 128, filter_size=3, activation='relu', name='inception_3a_3_3')
    inception_3a_5_5_reduce = conv_2d(pool2_3_3, 16, filter_size=1, activation='relu', name='inception_3a_5_5_reduce')
    inception_3a_5_5 = conv_2d(inception_3a_5_5_reduce, 32, filter_size=5, activation='relu', name='inception_3a_5_5')
    inception_3a_pool = max_pool_2d(pool2_3_3, kernel_size=3, strides=1, )
    inception_3a_pool_1_1 = conv_2d(inception_3a_pool, 32, filter_size=1, activation='relu',
                                    name='inception_3a_pool_1_1')

    # merge the inception_3a__
    inception_3a_output = merge([inception_3a_1_1, inception_3a_3_3, inception_3a_5_5, inception_3a_pool_1_1],
                                mode='concat', axis=3)

    inception_3b_1_1 = conv_2d(inception_3a_output, 128, filter_size=1, activation='relu', name='inception_3b_1_1')
    inception_3b_3_3_reduce = conv_2d(inception_3a_output, 128, filter_size=1, activation='relu',
                                      name='inception_3b_3_3_reduce')
    inception_3b_3_3 = conv_2d(inception_3b_3_3_reduce, 192, filter_size=3, activation='relu', name='inception_3b_3_3')
    inception_3b_5_5_reduce = conv_2d(inception_3a_output, 32, filter_size=1, activation='relu',
                                      name='inception_3b_5_5_reduce')
    inception_3b_5_5 = conv_2d(inception_3b_5_5_reduce, 96, filter_size=5, name='inception_3b_5_5')
    inception_3b_pool = max_pool_2d(inception_3a_output, kernel_size=3, strides=1, name='inception_3b_pool')
    inception_3b_pool_1_1 = conv_2d(inception_3b_pool, 64, filter_size=1, activation='relu',
                                    name='inception_3b_pool_1_1')

    # merge the inception_3b_*
    inception_3b_output = merge([inception_3b_1_1, inception_3b_3_3, inception_3b_5_5, inception_3b_pool_1_1],
                                mode='concat', axis=3, name='inception_3b_output')

    pool3_3_3 = max_pool_2d(inception_3b_output, kernel_size=3, strides=2, name='pool3_3_3')
    inception_4a_1_1 = conv_2d(pool3_3_3, 192, filter_size=1, activation='relu', name='inception_4a_1_1')
    inception_4a_3_3_reduce = conv_2d(pool3_3_3, 96, filter_size=1, activation='relu', name='inception_4a_3_3_reduce')
    inception_4a_3_3 = conv_2d(inception_4a_3_3_reduce, 208, filter_size=3, activation='relu', name='inception_4a_3_3')
    inception_4a_5_5_reduce = conv_2d(pool3_3_3, 16, filter_size=1, activation='relu', name='inception_4a_5_5_reduce')
    inception_4a_5_5 = conv_2d(inception_4a_5_5_reduce, 48, filter_size=5, activation='relu', name='inception_4a_5_5')
    inception_4a_pool = max_pool_2d(pool3_3_3, kernel_size=3, strides=1, name='inception_4a_pool')
    inception_4a_pool_1_1 = conv_2d(inception_4a_pool, 64, filter_size=1, activation='relu',
                                    name='inception_4a_pool_1_1')

    inception_4a_output = merge([inception_4a_1_1, inception_4a_3_3, inception_4a_5_5, inception_4a_pool_1_1],
                                mode='concat', axis=3, name='inception_4a_output')

    inception_4b_1_1 = conv_2d(inception_4a_output, 160, filter_size=1, activation='relu', name='inception_4a_1_1')
    inception_4b_3_3_reduce = conv_2d(inception_4a_output, 112, filter_size=1, activation='relu',
                                      name='inception_4b_3_3_reduce')
    inception_4b_3_3 = conv_2d(inception_4b_3_3_reduce, 224, filter_size=3, activation='relu', name='inception_4b_3_3')
    inception_4b_5_5_reduce = conv_2d(inception_4a_output, 24, filter_size=1, activation='relu',
                                      name='inception_4b_5_5_reduce')
    inception_4b_5_5 = conv_2d(inception_4b_5_5_reduce, 64, filter_size=5, activation='relu', name='inception_4b_5_5')

    inception_4b_pool = max_pool_2d(inception_4a_output, kernel_size=3, strides=1, name='inception_4b_pool')
    inception_4b_pool_1_1 = conv_2d(inception_4b_pool, 64, filter_size=1, activation='relu',
                                    name='inception_4b_pool_1_1')

    inception_4b_output = merge([inception_4b_1_1, inception_4b_3_3, inception_4b_5_5, inception_4b_pool_1_1],
                                mode='concat', axis=3, name='inception_4b_output')

    inception_4c_1_1 = conv_2d(inception_4b_output, 128, filter_size=1, activation='relu', name='inception_4c_1_1')
    inception_4c_3_3_reduce = conv_2d(inception_4b_output, 128, filter_size=1, activation='relu',
                                      name='inception_4c_3_3_reduce')
    inception_4c_3_3 = conv_2d(inception_4c_3_3_reduce, 256, filter_size=3, activation='relu', name='inception_4c_3_3')
    inception_4c_5_5_reduce = conv_2d(inception_4b_output, 24, filter_size=1, activation='relu',
                                      name='inception_4c_5_5_reduce')
    inception_4c_5_5 = conv_2d(inception_4c_5_5_reduce, 64, filter_size=5, activation='relu', name='inception_4c_5_5')

    inception_4c_pool = max_pool_2d(inception_4b_output, kernel_size=3, strides=1)
    inception_4c_pool_1_1 = conv_2d(inception_4c_pool, 64, filter_size=1, activation='relu',
                                    name='inception_4c_pool_1_1')

    inception_4c_output = merge([inception_4c_1_1, inception_4c_3_3, inception_4c_5_5, inception_4c_pool_1_1],
                                mode='concat', axis=3, name='inception_4c_output')

    inception_4d_1_1 = conv_2d(inception_4c_output, 112, filter_size=1, activation='relu', name='inception_4d_1_1')
    inception_4d_3_3_reduce = conv_2d(inception_4c_output, 144, filter_size=1, activation='relu',
                                      name='inception_4d_3_3_reduce')
    inception_4d_3_3 = conv_2d(inception_4d_3_3_reduce, 288, filter_size=3, activation='relu', name='inception_4d_3_3')
    inception_4d_5_5_reduce = conv_2d(inception_4c_output, 32, filter_size=1, activation='relu',
                                      name='inception_4d_5_5_reduce')
    inception_4d_5_5 = conv_2d(inception_4d_5_5_reduce, 64, filter_size=5, activation='relu', name='inception_4d_5_5')
    inception_4d_pool = max_pool_2d(inception_4c_output, kernel_size=3, strides=1, name='inception_4d_pool')
    inception_4d_pool_1_1 = conv_2d(inception_4d_pool, 64, filter_size=1, activation='relu',
                                    name='inception_4d_pool_1_1')

    inception_4d_output = merge([inception_4d_1_1, inception_4d_3_3, inception_4d_5_5, inception_4d_pool_1_1],
                                mode='concat', axis=3, name='inception_4d_output')

    inception_4e_1_1 = conv_2d(inception_4d_output, 256, filter_size=1, activation='relu', name='inception_4e_1_1')
    inception_4e_3_3_reduce = conv_2d(inception_4d_output, 160, filter_size=1, activation='relu',
                                      name='inception_4e_3_3_reduce')
    inception_4e_3_3 = conv_2d(inception_4e_3_3_reduce, 320, filter_size=3, activation='relu', name='inception_4e_3_3')
    inception_4e_5_5_reduce = conv_2d(inception_4d_output, 32, filter_size=1, activation='relu',
                                      name='inception_4e_5_5_reduce')
    inception_4e_5_5 = conv_2d(inception_4e_5_5_reduce, 128, filter_size=5, activation='relu', name='inception_4e_5_5')
    inception_4e_pool = max_pool_2d(inception_4d_output, kernel_size=3, strides=1, name='inception_4e_pool')
    inception_4e_pool_1_1 = conv_2d(inception_4e_pool, 128, filter_size=1, activation='relu',
                                    name='inception_4e_pool_1_1')

    inception_4e_output = merge([inception_4e_1_1, inception_4e_3_3, inception_4e_5_5, inception_4e_pool_1_1], axis=3,
                                mode='concat')

    pool4_3_3 = max_pool_2d(inception_4e_output, kernel_size=3, strides=2, name='pool_3_3')

    inception_5a_1_1 = conv_2d(pool4_3_3, 256, filter_size=1, activation='relu', name='inception_5a_1_1')
    inception_5a_3_3_reduce = conv_2d(pool4_3_3, 160, filter_size=1, activation='relu', name='inception_5a_3_3_reduce')
    inception_5a_3_3 = conv_2d(inception_5a_3_3_reduce, 320, filter_size=3, activation='relu', name='inception_5a_3_3')
    inception_5a_5_5_reduce = conv_2d(pool4_3_3, 32, filter_size=1, activation='relu', name='inception_5a_5_5_reduce')
    inception_5a_5_5 = conv_2d(inception_5a_5_5_reduce, 128, filter_size=5, activation='relu', name='inception_5a_5_5')
    inception_5a_pool = max_pool_2d(pool4_3_3, kernel_size=3, strides=1, name='inception_5a_pool')
    inception_5a_pool_1_1 = conv_2d(inception_5a_pool, 128, filter_size=1, activation='relu',
                                    name='inception_5a_pool_1_1')

    inception_5a_output = merge([inception_5a_1_1, inception_5a_3_3, inception_5a_5_5, inception_5a_pool_1_1], axis=3,
                                mode='concat')

    inception_5b_1_1 = conv_2d(inception_5a_output, 384, filter_size=1, activation='relu', name='inception_5b_1_1')
    inception_5b_3_3_reduce = conv_2d(inception_5a_output, 192, filter_size=1, activation='relu',
                                      name='inception_5b_3_3_reduce')
    inception_5b_3_3 = conv_2d(inception_5b_3_3_reduce, 384, filter_size=3, activation='relu', name='inception_5b_3_3')
    inception_5b_5_5_reduce = conv_2d(inception_5a_output, 48, filter_size=1, activation='relu',
                                      name='inception_5b_5_5_reduce')
    inception_5b_5_5 = conv_2d(inception_5b_5_5_reduce, 128, filter_size=5, activation='relu', name='inception_5b_5_5')
    inception_5b_pool = max_pool_2d(inception_5a_output, kernel_size=3, strides=1, name='inception_5b_pool')
    inception_5b_pool_1_1 = conv_2d(inception_5b_pool, 128, filter_size=1, activation='relu',
                                    name='inception_5b_pool_1_1')
    inception_5b_output = merge([inception_5b_1_1, inception_5b_3_3, inception_5b_5_5, inception_5b_pool_1_1], axis=3,
                                mode='concat')
    pool5_7_7 = avg_pool_2d(inception_5b_output, kernel_size=7, strides=1)
    pool5_7_7 = dropout(pool5_7_7, 0.4)
    network = fully_connected(pool5_7_7, 2, activation='softmax')

    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.0005)
    google_model = tflearn.DNN(network)
    google_model.load('model\\google\\jun_glnet_cat_dog_final.tflearn')
    google_result = google_model.predict(X)
    return google_result
def label_on_fly(im, model_name,cwd_checkpoint, stride, box_size):
    '''Converts pixels of image into labels

    Goes through smaller image and prepares it in the same way the images 
    were prepared for training the model which will be used to predict a label
    Goes through the image line by line to avoid using too much memory 

    :param PIL image im: for prediction, 
    :parammodel to load, 
    :param path to model, 
    :param stride of scanning image
    :param box_size
    :return: rgb values of the label for each pixel
    :rtype: int tuples
    '''
    input_size = box_size
    kernel_size = get_kernel_size(input_size)
    # Real-time data preprocessing
    im_prep = ImagePreprocessing()
    im_prep.add_featurewise_zero_center()
    im_prep.add_featurewise_stdnorm()
    # Real-time data augmentation
    im_aug = ImageAugmentation()
    im_aug.add_random_flip_leftright()
    im_aug.add_random_rotation(max_angle=360.)
    im_aug.add_random_blur(sigma_max=3.)
    im_aug.add_random_flip_updown()
    # Convolutional network building
    network = input_data(shape=[None, input_size, input_size, 3],
                     data_preprocessing=im_prep,
                     data_augmentation=im_aug)
    network = conv_2d(network, input_size/2, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, input_size, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, input_size*2, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, input_size*2*2, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, 128, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 128, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 6, activation='softmax')
    network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)
    # Defining model
    model = tflearn.DNN(network, tensorboard_verbose=0,tensorboard_dir=cwd_checkpoint,checkpoint_path=cwd_checkpoint)
    print('Loading model:', model_name)
    model.load(model_name)
    print('Sucessfully loaded model')
    
    max_box_size = box_size
    labels_predcited = []
    #Define the width and the height of the image to be cut up in smaller images
    width, height = im.size
    box = 0.2*width,0.2*height,0.8*width,0.8*height
    im = im.crop(box)
    width, height = im.size
    #Go through the height (y-axes) of the image
    for i in xrange(int((height-max_box_size)/stride)):
        center_point_y = max_box_size/2+i*stride
        im_temp = []
        predictions_temp = []
        labels_temp = []
        #Go through the width (x-axes) of the image using the same centerpoint independent of boxsize
        for j in xrange(int((width- max_box_size)/stride)):
            center_point_x = max_box_size/2+j*stride
            box = center_point_x-box_size/2, center_point_y-box_size/2, center_point_x+box_size/2,center_point_y+box_size/2
            im_temp.append(im.crop(box))
        predictions_temp = model.predict(im_temp)
        #labels_temp = [colorizer(get_label_from_cnn(predictions_temp[k])) for k in xrange(len(predictions_temp))]
        labels_temp = [get_label_from_cnn(predictions_temp[k]) for k in xrange(len(predictions_temp))]
        labels_predcited.append(labels_temp)
        print('Line %s done' % str(i))
    labels_final = [item for m in xrange(len(labels_predcited)) for item in labels_predcited[m]]
    return(labels_final)
# encode the Ys
Y = to_categorical(Y, 2)
Y_test = to_categorical(Y_test, 2)

###################################
# Image transformations
###################################

# normalisation of images
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Create extra synthetic training data by flipping & rotating images
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

###################################
# Define network architecture
###################################

# Input is a 32x32 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, 64, 64, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

# 1: Convolution layer with 32 filters, each 3x3x3
conv_1 = conv_2d(network, 32, 3, activation='relu', name='conv_1')
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImageProcessing
from tflearn.data_augmentation import ImageAugmentation
import pickle


X, Y, X_test, Y_test = pickle.load(open("full_dataset.pkl","rb"))

X, Y = shuffle(X,Y)

img_prep = ImageProcessing()
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.)


#define network architecture

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)
Example #35
0
h5f = h5py.File('dataset.h5', 'r')
X = h5f['X']
Y = h5f['Y']

#build_hdf5_image_dataset(validation_file, image_shape=[48, 48, 3], mode='file', output_path='validation.h5', categorical_labels=True)

h5f = h5py.File('validation.h5', 'r')
X_val = h5f['X']
Y_val = h5f['Y']

X, Y = shuffle(X, Y)

img_prep = DataPreprocessing()
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, 48, 48, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
network = conv_2d(network, nb_filter=64, filter_size=[5, 5], activation='relu')
#network = local_response_normalization(network)
network = max_pool_2d(network, kernel_size=[3, 3], strides=2)
network = conv_2d(network, nb_filter=64, filter_size=[5, 5], activation='relu')
network = max_pool_2d(network, kernel_size=[3, 3], strides=2)
network = conv_2d(network,
                  nb_filter=128,
                  filter_size=[4, 4],
                  activation='relu')
Example #36
0
from tflearn.data_augmentation import ImageAugmentation

# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()

# Convolutional network building
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, 10, activation='softmax')
network = regression(network, optimizer='adam',
Example #37
0
X, Y, X_test, Y_test = load_CIFAR10(data_path)
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# 实时数据预处理
# Real-time data preprocessing
img_pre = ImagePreprocessing()
img_pre.add_featurewise_zero_center(
)  # 零中心化,将每个样本的中心设为零,并指定平均值。如果未指定,则对所有样本求平均值。
img_pre.add_featurewise_stdnorm(
)  # STD标准化,按指定的标准偏差缩放每个样本。如果未指定std,则对所有样本数据进行std评估。

# 实时数据扩充
# Real-time data augmentation
img_aug = ImageAugmentation()  # 实时数据增强
img_aug.add_random_flip_leftright()  # 左右翻转
img_aug.add_random_rotation(max_angle=25.)  # 随机旋转

# 卷积网络的建立
# Convolutional network building
network = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_pre,
                     data_augmentation=img_aug)
network = conv_2d(network, 32, 3,
                  activation='relu')  # 第一层卷积层。32个卷积核,尺寸3x3,激活函数ReLU
network = max_pool_2d(network, 2)  # 最大池化层。滑动窗口步幅为2
network = conv_2d(network, 64, 3, activation='relu')  #第二层卷积层,64个卷积核
network = conv_2d(network, 64, 3, activation='relu')  #第三层卷积层,64个卷积核
network = max_pool_2d(network, 2)  # 最大池化层
network = fully_connected(network, 512, activation='relu')  #全连接层,512个神经元
Example #38
0
    count += 1
    
# Training and Validation Split
X,X_val,Y,Y_val = train_test_split(Train_Samples,Train_Labels,
                                   test_size=0.25,random_state=42)

# Convert class vectors to binary class matrix
Y = to_categorical(Y,2)
Y_val = to_categorical(Y_val,2)

# Data Augmentation and Image Processing
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_flip_updown()
img_aug.add_random_90degrees_rotation()
img_aug.add_random_blur()
img_aug.add_random_rotation(max_angle=25)

# Define the Model Architecture
net = input_data(shape=[None, Img_size, Img_size, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

conv_1 = conv_2d(net, 32, 3, activation='relu', name='conv_1')
net = max_pool_2d(conv_1, 2)
conv_2 = conv_2d(net, 64, 3, activation='relu', name='conv_2')
conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3')
Created by burness on 16/9/10.
'''
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, avg_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.merge_ops import merge
from tflearn.layers.estimator import regression
from tflearn.data_utils import image_preloader
from tflearn.data_utils import image_preloader
from tflearn.data_augmentation import ImageAugmentation


img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_flip_updown()
img_aug.add_random_crop([224, 224], 10)
img_aug.add_random_blur()
img_aug.add_random_rotation(max_angle=25.)

X,Y = image_preloader('files_list', image_shape = (224,224),mode='file',categorical_labels=True,normalize=True,files_extension=['.jpg', '.jpeg','.png'])


network = input_data(shape=[None, 224, 224, 3],data_augmentation=img_aug)
conv1_7_7 = conv_2d(network, 64, 7, strides=2, activation='relu', name = 'conv1_7_7_s2')
pool1_3_3 = max_pool_2d(conv1_7_7, 3,strides=2)
pool1_3_3 = local_response_normalization(pool1_3_3)
conv2_3_3_reduce = conv_2d(pool1_3_3, 64,1, activation='relu',name = 'conv2_3_3_reduce')
conv2_3_3 = conv_2d(conv2_3_3_reduce, 192,3, activation='relu', name='conv2_3_3')
Example #40
0
 def augmentation(self):
     img_aug = ImageAugmentation()
     img_aug.add_random_flip_leftright()
     img_aug.add_random_rotation(max_angle=90.)
     img_aug.add_random_blur(sigma_max=3.)
     return img_aug
Example #41
0
from tflearn.data_augmentation import ImageAugmentation

# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y)
Y_test = to_categorical(Y_test)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
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, 10, activation='softmax')
Example #42
0
def evaluate(index, zone):

    if (not os.path.isfile('models/zone' + str(zone) + '/' + str(index) +
                           '/checkpoint')):
        return [], [], []

    # 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.)
    #adding here
    if (zone == 13 or zone == 14):
        dimX = 256
        dimY = 75
    elif (zone == 11 or zone == 12):
        dimX = 256
        dimY = 75
    elif (zone == 10):
        dimX = 237
        dimY = 80
    elif (zone == 9):
        dimX = 50
        dimY = 80
    elif (zone == 8):
        if (index == 1 or index == 13):
            dimX = 225
            dimY = 80
        elif (index == 9):
            dimX = 237
            dimY = 80
    elif (zone == 6 or zone == 7):
        dimX = 256
        dimY = 60
    elif (zone == 5):
        dimX = 512
        dimy = 80
    else:
        dimX = 0
        dimY = 0
    network = input_data(
        shape=[None, dimY, dimX, 1],  #zone14,13
        #network = input_data(shape=[None, 80, 512, 1], #zone5
        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, 128, 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='models/zone' + str(zone) + '/' +
                        str(index) + '/TSA-Zone' + str(zone) + '-Angle-' +
                        str(index) + '.tfl')  #zone14
    model.load("models/zone" + str(zone) + "/" + str(index) + "/TSA-Zone" +
               str(zone) + "-Angle-" + str(index) + ".tfl")  #zone14

    apsid = []
    predArray = []
    bnotb = []

    mypath = "../aps/test_data/"
    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    tp = 0
    tn = 0
    fp = 0
    fn = 0
    for fname in onlyfiles:
        if fname.endswith(".aps"):
            # Load the image file
            #img = scipy.ndimage.imread(args.image, mode="RGB")
            apsid.append(fname.split('.')[0])
            single_image = sg.get_single_image("../aps/test_data/" + fname,
                                               index)
            img = sg.convert_to_grayscale(single_image)
            crop_dim = sg.get_crop_dimensions(index, zone)
            img = img[crop_dim[0]:crop_dim[1], crop_dim[2]:crop_dim[3]]
            img = np.asfarray(img).reshape(dimY, dimX, 1)  #zone14,13

            # Predict
            prediction = model.predict([img])
            #print(prediction)
            predArray.append(prediction)
            # Check the result.
            is_threat = np.argmax(prediction[0]) == 1

            bnotb.append(is_threat)
            final_result = []
            filename = 'test_labels.csv'
            with open(filename) as csvfile:
                readCSV = csv.reader(csvfile, delimiter=',')
                for row in readCSV:
                    final_result.append(row)
            if is_threat:
                print("That's detetced: " + str(fname))
                flag = True
                for value in final_result:
                    if value[0] + ".aps" == fname:
                        label = int(value[1])
                        if zone == label:
                            tp = tp + 1
                            flag = False
                            break
                if flag:
                    fp = fp + 1
            else:
                flag = True
                for value in final_result:
                    if value[0] + ".aps" == fname:
                        label = int(value[1])
                        if zone == label:
                            fn = fn + 1
                            flag = False
                            break
                if flag:
                    tn = tn + 1
    print('True positives', tp)
    print('False positives', fp)
    print('True negatives', tn)
    print('False negatives', fn)
    return apsid, predArray, bnotb
Example #43
0
# Data loading and pre processing
from tflearn.datasets import cifar10

(X,Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X,Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation()

# Building the CNN
network = input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep, data_augmentation=img_aug, name='first_layer')
network = max_pool_2d(network, 2) # Max pooling layer
network = conv_2d(network, 64, 3 , activation='relu')
network = conv_2d(network, 64, 3 , activation='relu') # Multiple convolution layers
network = max_pool_2d(network, 2) # Max pooling layer
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 10, activation='softmax') # Layer responsible for prediction
network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001)

# Training using classifier
Example #44
0
 def generate_image_augumentation(self):
     # Real-time data augmentation
     img_aug = ImageAugmentation()
     img_aug.add_random_flip_leftright()
     img_aug.add_random_rotation(max_angle=25.)
     return img_aug