예제 #1
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
예제 #2
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')
예제 #3
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  ) 
    conv1a_3_3 = relu(batch_normalization(conv_2d(network, 32, 3, strides=2, bias=False, padding='VALID',activation=None,name='Conv2d_1a_3x3')))
    conv2a_3_3 = relu(batch_normalization(conv_2d(conv1a_3_3, 32, 3, bias=False, padding='VALID',activation=None, name='Conv2d_2a_3x3')))
    conv2b_3_3 = relu(batch_normalization(conv_2d(conv2a_3_3, 64, 3, bias=False, activation=None, name='Conv2d_2b_3x3')))
    maxpool3a_3_3 = max_pool_2d(conv2b_3_3, 3, strides=2, padding='VALID', name='MaxPool_3a_3x3')
    conv3b_1_1 = relu(batch_normalization(conv_2d(maxpool3a_3_3, 80, 1, bias=False, padding='VALID',activation=None, name='Conv2d_3b_1x1')))
    conv4a_3_3 = relu(batch_normalization(conv_2d(conv3b_1_1, 192, 3, bias=False, padding='VALID',activation=None, name='Conv2d_4a_3x3')))
    maxpool5a_3_3 = max_pool_2d(conv4a_3_3, 3, strides=2, padding='VALID', name='MaxPool_5a_3x3')

    tower_conv = relu(batch_normalization(conv_2d(maxpool5a_3_3, 96, 1, bias=False, activation=None, name='Conv2d_5b_b0_1x1')))

    tower_conv1_0 = relu(batch_normalization(conv_2d(maxpool5a_3_3, 48, 1, bias=False, activation=None, name='Conv2d_5b_b1_0a_1x1')))
    tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1_0, 64, 5, bias=False, activation=None, name='Conv2d_5b_b1_0b_5x5')))

    tower_conv2_0 = relu(batch_normalization(conv_2d(maxpool5a_3_3, 64, 1, bias=False, activation=None, name='Conv2d_5b_b2_0a_1x1')))
    tower_conv2_1 = relu(batch_normalization(conv_2d(tower_conv2_0, 96, 3, bias=False, activation=None, name='Conv2d_5b_b2_0b_3x3')))
    tower_conv2_2 = relu(batch_normalization(conv_2d(tower_conv2_1, 96, 3, bias=False, activation=None,name='Conv2d_5b_b2_0c_3x3')))

    tower_pool3_0 = avg_pool_2d(maxpool5a_3_3, 3, strides=1, padding='same', name='AvgPool_5b_b3_0a_3x3')
    tower_conv3_1 = relu(batch_normalization(conv_2d(tower_pool3_0, 64, 1, bias=False, activation=None,name='Conv2d_5b_b3_0b_1x1')))

    tower_5b_out = merge([tower_conv, tower_conv1_1, tower_conv2_2, tower_conv3_1], mode='concat', axis=3)

    net = repeat(tower_5b_out, 10, block35, scale=0.17)

    tower_conv = relu(batch_normalization(conv_2d(net, 384, 3, bias=False, strides=2,activation=None, padding='VALID', name='Conv2d_6a_b0_0a_3x3')))
    tower_conv1_0 = relu(batch_normalization(conv_2d(net, 256, 1, bias=False, activation=None, name='Conv2d_6a_b1_0a_1x1')))
    tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1_0, 256, 3, bias=False, activation=None, name='Conv2d_6a_b1_0b_3x3')))
    tower_conv1_2 = relu(batch_normalization(conv_2d(tower_conv1_1, 384, 3, bias=False, strides=2, padding='VALID', activation=None,name='Conv2d_6a_b1_0c_3x3')))
    tower_pool = max_pool_2d(net, 3, strides=2, padding='VALID',name='MaxPool_1a_3x3')
    net = merge([tower_conv, tower_conv1_2, tower_pool], mode='concat', axis=3)
    net = repeat(net, 20, block17, scale=0.1)

    tower_conv = relu(batch_normalization(conv_2d(net, 256, 1, bias=False, activation=None, name='Conv2d_0a_1x1')))
    # tower_conv0_1 = relu(batch_normalization(conv_2d(tower_conv, 384, 3, bias=False, strides=2, padding='VALID', activation=None,name='Conv2d_0a_1x1')))
    tower_conv0_1 = relu(batch_normalization(conv_2d(tower_conv, 384, 1, bias=False, strides=2, padding='VALID', activation=None,name='Conv2d_0a_1x1')))

    tower_conv1 = relu(batch_normalization(conv_2d(net, 256, 1, bias=False, padding='VALID', activation=None,name='Conv2d_0a_1x1')))
    # tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1,288,3, bias=False, strides=2, padding='VALID',activation=None, name='COnv2d_1a_3x3')))
    tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1,288,1, bias=False, strides=2, padding='VALID',activation=None, name='COnv2d_1a_3x3')))

    tower_conv2 = relu(batch_normalization(conv_2d(net, 256,1, bias=False, activation=None,name='Conv2d_0a_1x1')))
    tower_conv2_1 = relu(batch_normalization(conv_2d(tower_conv2, 288,3, bias=False, name='Conv2d_0b_3x3',activation=None)))
    # tower_conv2_2 = relu(batch_normalization(conv_2d(tower_conv2_1, 320, 3, bias=False, strides=2, padding='VALID',activation=None, name='Conv2d_1a_3x3')))
    tower_conv2_2 = relu(batch_normalization(conv_2d(tower_conv2_1, 320, 1, bias=False, strides=2, padding='VALID',activation=None, name='Conv2d_1a_3x3')))

    # tower_pool = max_pool_2d(net, 3, strides=2, padding='VALID', name='MaxPool_1a_3x3')
    tower_pool = max_pool_2d(net, 1, strides=2, padding='VALID', name='MaxPool_1a_3x3')
    net = merge([tower_conv0_1, tower_conv1_1,tower_conv2_2, tower_pool], mode='concat', axis=3)

    net = repeat(net, 9, block8, scale=0.2)
    net = block8(net, activation=None)

    net = relu(batch_normalization(conv_2d(net, 1536, 1, bias=False, activation=None, name='Conv2d_7b_1x1')))
    net = avg_pool_2d(net, net.get_shape().as_list()[1:3],strides=2, padding='VALID', name='AvgPool_1a_8x8')
    net = flatten(net)
    net = dropout(net, dropout_keep_prob)
    loss = fully_connected(net, num_classes,activation='softmax')


    network = tflearn.regression(loss, optimizer='RMSprop',
                         loss='categorical_crossentropy',
                         learning_rate=0.0001, name='targets')
    return network
예제 #4
0
def color():

    ###################################
    ### Import picture files 
    ###################################

    files_path = "./img_align_celeba/"

    glasses_files_path = os.path.join(files_path, '*.jpg')

    glasses_files = sorted(glob(glasses_files_path))

    n_files = len(glasses_files)
    #print(n_files)
    #size_image1 =178
    #size_image2=218
    size_image1=27
    size_image2=33
    allX = np.zeros((n_files, size_image1, size_image2, 3), dtype='float32')
    ally = np.zeros(n_files)
    count = 0
    for f in glasses_files:
        try:
            img = io.imread(f)
            new_img = skimage.transform.resize(img, (27, 33, 3))
            allX[count] = np.array(new_img)
            ally[count] = 0
            count += 1
        except:
            continue
    attribute=[]
    g = open('./list_attr_celeba.txt', 'r')
    text = g.readlines()
    text = np.array(text)
    attr2idx = dict()
    for i, attr in enumerate(text[1].split()):
        attr2idx[attr] = i
    attr_index = attr2idx['Eyeglasses']#'Eyeglasses'
    for i in text[2:]:
        value = i.split()
        attribute.append(value[attr_index + 1]) #First index is image name
    attribute = np.array(attribute,dtype= np.float32)
    #print("Converting Label.................")
    for i in range(0,len(attribute)):
        if (attribute[i] == 1):
            ally[i]=1
        else:
            ally[i]=0
    ally = np.array(ally)
    
    ########break up data into training, validation, and test sets
    train_limit = int(math.floor(0.8 * len(allX)))
    validate_limit = int(math.floor(0.1*len(allX)))
    #print (train_limit, validate_limit)


    X = allX[0:train_limit,:,]
    X_validation = allX[(train_limit+1):(train_limit+validate_limit),:,]
    X_test = allX[(train_limit+validate_limit+1):,:,]
    
    Y = ally[0:train_limit]
    Y_validation = ally[(train_limit+1):(train_limit+validate_limit)]
    Y_test = ally[(train_limit+validate_limit+1):]

    # encode the Ys
    Y = to_categorical(Y, 2)
    Y_test = to_categorical(Y_test, 2)
    Y_validation = to_categorical(Y_validation, 2)

    #take a subset of training dataset to find parameters
    x_sm = int(math.floor(0.8 * len(allX))*0.5)
    print (x_sm)
    X_sm = allX[0:x_sm,:,]
    Y_sm = ally[0:x_sm]
    Y_sm=to_categorical(Y_sm, 2)


    print (X.shape, Y.shape, allX.shape, ally.shape, X_sm.shape)
    
    ###################################
    # 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 27x33 image with 3 color channels (red, green and blue)
    network = input_data(shape=[None, 27, 33, 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')

    # 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: Convolution layer with 64 filters
    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, 1024, activation='relu')

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

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

    # Configure how the network will be trained
    acc = Accuracy(name="Accuracy")
    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001, metric=acc)

    # Wrap the network in a model object
    model = tflearn.DNN(network, checkpoint_path='model_glasses_6.tflearn', max_checkpoints = 3,
                        tensorboard_verbose = 3, tensorboard_dir='tmp/tflearn_logs/')
    ###################################
    # Train model for 1000 epochs
    ###################################
    model.fit(X_sm, Y_sm, validation_set=(X_validation, Y_validation), batch_size=50,
          n_epoch=1000, run_id='model_glasses_6', show_metric=True)

    model.save('model_glasses_6_final.tflearn')
    
    # Evaluate model
    score = model.evaluate(X_test, Y_test)
    print('Test accuarcy: %0.4f%%' % (score[0] * 100))
예제 #5
0
def test(hist, labels, filenames, img):

    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.)

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

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

    network = conv_2d(network, 64, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = conv_2d(network, 128, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = conv_2d(network, 64, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = conv_2d(network, 32, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = fully_connected(network, 1024, activation='relu')
    network = dropout(network, 0.8)

    network = fully_connected(network, 2, activation='softmax')
    network = regression(network,
                         optimizer='adam',
                         learning_rate=1e-3,
                         loss='categorical_crossentropy')

    clf = joblib.load('neural2_segmented.pkl')
    clf_svc = joblib.load('classifier_svc_segmented.pkl')
    clf_ran = joblib.load('classifier_ran_segmented.pkl')

    model = tflearn.DNN(network,
                        checkpoint_path='model_cat_dog_7.tflearn',
                        max_checkpoints=3,
                        tensorboard_verbose=3,
                        tensorboard_dir='tmp/tflearn_logs/')

    model.load('model_cat_dog_6_final.tflearn')

    preds = []
    preds_svc = []
    preds_ran = []
    preds_cnn = []
    for i in range(0, hist.shape[0]):
        a = hist[i]
        a = a.reshape(1, -1)
        preds.append(clf.predict(a))
        preds_svc.append(clf_svc.predict(a))
        preds_ran.append(clf_ran.predict(a))
        img[i] = np.reshape(img[i], (1, 64, 64, 3))

        img[i] = img[i].astype('float32')
        probs = model.predict(img[i])
        preds_cnn.append(np.argmax(probs))

        #print(clf_svc.predict(a))

    counter1 = 0
    counter2 = 0
    counter3 = 0
    counter4 = 0
    counter5 = 0
    counter6 = 0
    counter7 = 0
    counter8 = 0
    counter9 = 0
    counter10 = 0
    counter11 = 0
    counter12 = 0
    final_pred = []
    for i in range(0, hist.shape[0]):
        #print(preds[i])
        #print(labels[i])
        if (preds[i] == labels[i]):
            counter1 += 1
        else:
            counter2 += 1

        if (preds_svc[i] == labels[i]):
            counter3 += 1
        else:
            counter4 += 1

        if (preds_ran[i] == labels[i]):
            counter5 += 1
        else:
            counter6 += 1

        if (preds_cnn[i] == labels[i]):
            counter11 += 1
        else:
            counter12 += 1

        if ((int(preds[i]) + int(preds_svc[i]) + int(preds_ran[i])) == 3):
            final_pred.append(1)
            if (int(labels[i]) == 1):
                counter9 += 1
            else:
                counter10 += 1
        elif (int(preds[i]) + int(preds_svc[i]) + int(preds_ran[i]) == 0):
            final_pred.append(0)
            if (int(labels[i]) == 0):
                counter9 += 1
            else:
                counter10 += 1
        else:
            ok = np.random.randint(1, 2)
            final_pred.append(ok)
            if (ok == int(labels[i])):
                counter9 += 1
            else:
                counter10 += 1

        print(len(final_pred))
        if ((int(preds[i]) + int(preds_svc[i]) + int(preds_ran[i])) == 2 or
            (int(preds[i]) + int(preds_svc[i]) + int(preds_ran[i])) == 3):
            if (int(labels[i]) == 1):
                counter7 += 1
            else:
                counter8 += 1
        elif ((int(preds[i]) + int(preds_svc[i]) + int(preds_ran[i])) == 0
              or (int(preds[i]) + int(preds_svc[i]) + int(preds_ran[i])) == 1):
            if (int(labels[i]) == 0):
                counter7 += 1
            else:
                counter8 += 1

    def getint(name):
        print(name[0])
        basename = name[0].split('.')
        return int(basename[0])

    final_pred = [
        x for (y, x) in sorted(zip(filenames, final_pred), key=getint)
    ]
    filenames = [
        y for (y, x) in sorted(zip(filenames, final_pred), key=getint)
    ]

    with open('Resultss.csv', 'w', newline='') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',')
        spamwriter.writerow(["image", "label"])
        for i in range(0, len(preds)):
            spamwriter.writerow([filenames[i], final_pred[i]])

    accuracy = (counter1 / (counter1 + counter2)) * 100
    accuracy_svc = (counter3 / (counter3 + counter4)) * 100
    accuracy_ran = (counter5 / (counter5 + counter6)) * 100
    accuracy_vote = (counter7 / (counter7 + counter8)) * 100
    accuracy_unan = (counter9 / (counter9 + counter10)) * 100
    accuracy_cnn = (counter11 / (counter11 + counter12)) * 100

    print("mlp accuracy: %d" % accuracy)
    print("svm accuracy: %d" % accuracy_svc)
    print("random forest accuracy: %d" % accuracy_ran)
    print("voting accuracy: %d" % accuracy_vote)
    print("unanimous voting accuracy: %d" % accuracy_unan)
    print("cnn accuracy: %d" % accuracy_cnn)
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, 192, 192, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, 128, 3, activation='relu')
    network = conv_2d(network, 128, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, 1024, 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)
    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]))]))
예제 #7
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))
예제 #8
0
def getReseau():
    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 = settings.reseau

    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')

    # 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, checkpoint_path='dataviz-classifier.tfl.ckpt')
    model = tflearn.DNN(
        network, tensorboard_verbose=0
    )  # , checkpoint_path='data-classifier/dataviz-classifier.tfl.ckpt')

    return model
예제 #9
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
예제 #10
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
예제 #11
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
예제 #12
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个神经元
예제 #13
0
def cnn_model(x_shape, y_shape, archi="AlexNet"):
    image_aug = ImageAugmentation()
    image_aug.add_random_blur(1)
    image_aug.add_random_flip_leftright()
    image_aug.add_random_flip_updown()
    image_aug.add_random_rotation()
    image_aug.add_random_90degrees_rotation()

    # AlexNet, replacing local normalization with batch normalization.
    if archi == "AlexNet":
        net = input_data(shape=[None] + list(x_shape[1:]),
                         data_augmentation=image_aug)
        net = conv_2d(net, 96, 7, strides=2, activation='relu')

        net = batch_normalization(net)
        net = max_pool_2d(net, 2)
        net = dropout(net, 0.8)

        net = conv_2d(net, 256, 5, strides=2, activation='relu')
        net = batch_normalization(net)

        net = max_pool_2d(net, 2)
        net = dropout(net, 0.8)

        net = conv_2d(net, 384, 3, activation='relu')
        net = conv_2d(net, 384, 3, activation='relu')
        net = conv_2d(net, 256, 3, activation='relu')
        net = batch_normalization(net)
        net = max_pool_2d(net, 2)
        net = dropout(net, 0.8)

        net = fully_connected(net, 4096, activation='tanh')
        net = dropout(net, 0.5)
        net = fully_connected(net, 4096, activation='tanh')
        net = dropout(net, 0.5)
        net = fully_connected(net, y_shape[1], activation='softmax')
        net = regression(net,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.0001)

    # ResNet, with dropout.
    if archi == "ResNet":
        n = 5
        net = tflearn.input_data(shape=[None] + list(x_shape[1:]),
                                 data_augmentation=image_aug)
        net = tflearn.conv_2d(net,
                              16,
                              5,
                              strides=2,
                              regularizer='L2',
                              weight_decay=0.0001)
        net = tflearn.residual_block(net, n, 16)
        net = tflearn.residual_block(net, 1, 32, downsample=True)
        net = tflearn.dropout(net, 0.8)
        net = tflearn.residual_block(net, n - 1, 32)
        net = tflearn.residual_block(net, 1, 64, downsample=True)
        net = tflearn.dropout(net, 0.8)
        net = tflearn.residual_block(net, n - 1, 64)
        net = tflearn.batch_normalization(net)
        net = tflearn.activation(net, 'relu')
        net = tflearn.global_avg_pool(net)
        net = tflearn.fully_connected(net, y_shape[1], activation='softmax')
        net = tflearn.regression(net,
                                 optimizer='adam',
                                 loss='categorical_crossentropy',
                                 learning_rate=0.0001)

    return net
예제 #14
0
def getModel(size_image, is_dropout, learning_rate, is_batch_norm):
    ###################################
    # 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
    print ("----Begin data augmentation ----------")
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)
    print ("----End data augmentation ----------")
    
    ###########################################################################
    
    ################################### Define NETWORK ARCHITECTURE ###########
    
    ###########################################################################
    
#    winit = tflearn.initializations.uniform(minval=0, maxval=None, seed=0)

    
    # Input is a 16x16 image
    input_layer = input_data(shape=[None, size_image, size_image, 1],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    
    # 1: Convolution layer with 32 filters, each 3x3x3
    conv_1 = conv_2d(input_layer, 32, 3, activation='relu', name='conv_1' )
    
    # 2: Convolution layer with 32 filters
    conv_2 = conv_2d(conv_1, 32, 3, activation='relu', name='conv_2' )
    
    # 3: Max pooling layer
    mp1 = max_pool_2d(conv_2, 2)
    
    # Batch normalization
    if is_batch_norm : mp1 = tflearn.normalization.batch_normalization(mp1)
    
    # 4: Convolution layer with 32 filters
    conv_3 = conv_2d(mp1, 32, 3, activation='relu', name='conv_3' )
     
    # 5: Convolution layer with 32 filters
    conv_4 = conv_2d(conv_3, 32, 3, activation='relu', name='conv_4' )
     
    # 5: Max pooling layer
    mp2 = max_pool_2d(conv_4, 2)
    
    # Batch normalization
    if is_batch_norm : mp2 = tflearn.normalization.batch_normalization(mp2)
     
    # 6: Fully-connected 256 node layer
    dense1 = fully_connected(mp2, 256, activation='relu' )
     
    # 7: Dropout layer to combat overfitting
    dp1 = dropout(dense1, 0.5) if is_dropout else dense1
    
    dense2 = fully_connected(dp1, 256, activation='relu' )
    
    dp2 = dropout(dense2, 0.5) if is_dropout else dense2
    
    # 8: Fully-connected layer with 1 outputs ( binary)
    network = fully_connected(dp2, 2, activation='softmax' )
        
    # Configure how the network will be trained
    
    gd = tflearn.SGD(learning_rate=learning_rate, lr_decay=0.96, decay_step=500)
    
    network = regression(network, optimizer=gd, loss='categorical_crossentropy', shuffle_batches=True)
    
    # 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
예제 #16
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_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
예제 #17
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
예제 #18
0
#np.reshape(labels, (338,338))
#np.reshape(labels_test, (num_testing_images,338))

print(labels)

# shuffle images
images, labels = shuffle(images, labels)
images_test, labels_test = shuffle(images_test, labels_test)

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

# distort images
img_distortion = ImageAugmentation()

# only flip left/right for shape training
#img_distortion.add_random_flip_leftright()

img_distortion.add_random_rotation(max_angle=360)
img_distortion.add_random_blur(sigma_max=1.)



###
### network architecture
###

network = input_data(shape=[None, 64, 64, 1], 
	data_preprocessing=img_preprocessor,
예제 #19
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
예제 #20
0
def createCNN(args):

    size_entry_filter = 32
    size_mid_filter = 64
    filter_size = 3
    color_channels = 3
    model_main_activation = 'relu'
    model_exit_activation = 'softmax'
    max_pool_kernel_size = 2
    model_learning_rate = 0.001
    num_classes = args['num_classes']
    ###################################
    # 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.)
    img_aug.add_random_blur(sigma_max=3.)

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

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

    # 1: Convolution layer with 32 filters, each 3x3x3
    network = conv_2d(network,
                      size_entry_filter,
                      filter_size,
                      activation=model_main_activation)

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

    # 3: Convolution layer with 64 filters
    network = conv_2d(network,
                      size_mid_filter,
                      filter_size,
                      activation=model_main_activation)

    # 4: Convolution layer with 64 filters
    network = conv_2d(network,
                      size_mid_filter,
                      filter_size,
                      activation=model_main_activation)

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

    # 6: Convolution layer with 64 filters
    network = conv_2d(network,
                      size_mid_filter,
                      filter_size,
                      activation=model_main_activation)

    # 7: Convolution layer with 64 filters
    network = conv_2d(network,
                      size_mid_filter,
                      filter_size,
                      activation=model_main_activation)

    # 8: Max pooling layer
    network = max_pool_2d(network, max_pool_kernel_size)

    # 9: Fully-connected 512 node layer
    network = fully_connected(network, 512, activation=model_main_activation)

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

    # 11: Fully-connected layer with two outputs
    network = fully_connected(network,
                              num_classes,
                              activation=model_exit_activation)

    # Configure how the network will be trained
    acc = Accuracy(name="Accuracy")
    network = regression(network,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=model_learning_rate,
                         metric=acc)

    #main_dir = os.path.dirname(os.path.dirname(os.getcwd()))
    # Wrap the network in a model object
    model = tflearn.DNN(network,
                        tensorboard_verbose=0,
                        max_checkpoints=2,
                        best_checkpoint_path=os.path.join(
                            os.path.dirname(os.path.dirname(os.getcwd())),
                            args['id'] + '.tfl.ckpt'),
                        best_val_accuracy=args['accuracy'])
    return model
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)
예제 #22
0
def test(filenames, img):

    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.)

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

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

    network = conv_2d(network, 64, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = conv_2d(network, 128, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = conv_2d(network, 64, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = conv_2d(network, 32, 5, activation='relu')
    network = max_pool_2d(network, 5)

    network = fully_connected(network, 1024, activation='relu')
    network = dropout(network, 0.8)

    network = fully_connected(network, 2, activation='softmax')
    network = regression(network,
                         optimizer='adam',
                         learning_rate=1e-3,
                         loss='categorical_crossentropy')

    model = tflearn.DNN(network,
                        checkpoint_path='model_cat_dog_7.tflearn',
                        max_checkpoints=3,
                        tensorboard_verbose=3,
                        tensorboard_dir='tmp/tflearn_logs/')

    model.load('model_cat_dog_6_final.tflearn')

    preds_cnn = []
    final_pred = []
    for i in range(0, len(img)):

        img[i] = np.reshape(img[i], (1, 64, 64, 3))
        img[i] = img[i].astype('float32')
        probs = model.predict(img[i])
        preds_cnn.append(np.argmax(probs))
        final_pred.append(np.argmax(probs))

    def getint(name):
        basename = name[0].split('.')
        return int(basename[0])

    final_pred = [
        x for (y, x) in sorted(zip(filenames, final_pred), key=getint)
    ]
    filenames = [
        y for (y, x) in sorted(zip(filenames, final_pred), key=getint)
    ]

    with open('Results.csv', 'w', newline='') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',')
        spamwriter.writerow(["image", "label"])
        for i in range(0, len(final_pred)):
            spamwriter.writerow([filenames[i], final_pred[i]])
예제 #23
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
예제 #24
0
X_test = np.load("TEST.npy")
Y_test = np.load("TEST_LABEL.npy")
print("Total size :", len(X) + len(X_test))
print("Total train :", len(X))
print("Total test :", len(X_test))
###################################
# 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')
예제 #25
0
    def define_network(self):
        """
        Defines CNN architecture
        :return: CNN model
        """

        # My CNN 1 (type1)

        # # For data normalization
        # img_prep = ImagePreprocessing()
        # img_prep.add_featurewise_zero_center()
        # img_prep.add_featurewise_stdnorm()
        #
        # # For creating extra data(increase dataset). Flipped, Rotated, Blurred and etc. images
        # img_aug = ImageAugmentation()
        # img_aug.add_random_flip_leftright()
        # img_aug.add_random_rotation(max_angle=25.0)
        # img_aug.add_random_blur(sigma_max=3.0)
        #
        # self.network = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1],
        #                           data_augmentation=img_aug,
        #                           data_preprocessing=img_prep)
        # self.network = conv_2d(self.network, 64, 5, activation='relu')
        # self.network = max_pool_2d(self.network, 3, strides=2)
        # self.network = conv_2d(self.network, 64, 5, activation='relu')
        # self.network = max_pool_2d(self.network, 3, strides=2)
        # self.network = conv_2d(self.network, 128, 4, activation='relu')
        # self.network = dropout(self.network, 0.3)
        # self.network = fully_connected(self.network, 3072, activation='relu')
        # self.network = fully_connected(self.network, len(EMOTIONS), activation='softmax')
        # self.network = regression(self.network, optimizer='adam', loss='categorical_crossentropy')
        # self.model = tflearn.DNN(self.network, checkpoint_path=os.path.join(CHECKPOINTS_PATH + '/emotion_recognition'),
        #                          max_checkpoints=1, tensorboard_verbose=0)

        # My CNN 2 (type2)

        # For creating extra data(increase dataset). Flipped, Rotated, Blurred and etc. images
        img_aug = ImageAugmentation()
        img_aug.add_random_flip_leftright()
        img_aug.add_random_rotation(max_angle=25.0)
        img_aug.add_random_blur(sigma_max=3.0)

        self.network = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1],
                                  data_augmentation=img_aug)

        self.network = conv_2d(self.network, 64, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = conv_2d(self.network, 64, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = max_pool_2d(self.network, 2, strides=2)

        self.network = conv_2d(self.network, 128, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = conv_2d(self.network, 128, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = max_pool_2d(self.network, 2, strides=2)
        self.network = dropout(self.network, 0.2)

        self.network = conv_2d(self.network, 256, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = conv_2d(self.network, 256, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = max_pool_2d(self.network, 2, strides=2)
        self.network = dropout(self.network, 0.25)

        self.network = conv_2d(self.network, 512, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = conv_2d(self.network, 512, 3, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = max_pool_2d(self.network, 2, strides=2)
        self.network = dropout(self.network, 0.25)

        self.network = fully_connected(self.network, 1024, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = dropout(self.network, 0.45)

        self.network = fully_connected(self.network, 1024, activation='relu')
        self.network = batch_normalization(self.network)
        self.network = dropout(self.network, 0.45)

        self.network = fully_connected(self.network,
                                       len(EMOTIONS),
                                       activation='softmax')
        self.network = regression(self.network,
                                  optimizer='adam',
                                  loss='categorical_crossentropy')

        self.model = tflearn.DNN(
            self.network,
            checkpoint_path=os.path.join(CHECKPOINTS_PATH +
                                         '/emotion_recognition'),
            max_checkpoints=1,
            tensorboard_verbose=0)

        return self.model