コード例 #1
0
ファイル: models.py プロジェクト: gcm0621/pygta5
def alexnet(width, height, lr, output=3):
    network = input_data(shape=[None, width, height, 1], name='input')
    network = conv_2d(network, 96, 11, strides=4, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 256, 5, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, output, activation='softmax')
    network = regression(network, optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=lr, name='targets')

    model = tflearn.DNN(network, checkpoint_path='model_alexnet',
                        max_checkpoints=1, tensorboard_verbose=0, tensorboard_dir='log')

    return model
コード例 #2
0
ファイル: cnn.py プロジェクト: Emersonxuelinux/2book
def alexnet():
    X, Y = oxflower17.load_data(one_hot=True, resize_pics=(227, 227))

    # Building 'AlexNet'
    network = input_data(shape=[None, 227, 227, 3])
    network = conv_2d(network, 96, 11, strides=4, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 256, 5, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 17, activation='softmax')
    network = regression(network, optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)

    # Training
    model = tflearn.DNN(network, checkpoint_path='model_alexnet',
                        max_checkpoints=1, tensorboard_verbose=2)
    model.fit(X, Y, n_epoch=1000, validation_set=0.1, shuffle=True,
              show_metric=True, batch_size=64, snapshot_step=200,
              snapshot_epoch=False, run_id='alexnet')
コード例 #3
0
def neural_network_model(input_size):

    network = input_data(shape=[None, input_size, 1], name='input')

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

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

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

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

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

    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=LR,
                         loss='categorical_crossentropy', name='targets')
    model = tflearn.DNN(network, tensorboard_dir='log')

    return model
コード例 #4
0
def createModel(nbClasses,imageSize):
	print("[+] Creating model...")
	convnet = input_data(shape=[None, imageSize, imageSize, 1], name='input')

	convnet = conv_2d(convnet, 64, 2, activation='elu', weights_init="Xavier")
	convnet = max_pool_2d(convnet, 2)

	convnet = conv_2d(convnet, 128, 2, activation='elu', weights_init="Xavier")
	convnet = max_pool_2d(convnet, 2)

	convnet = conv_2d(convnet, 256, 2, activation='elu', weights_init="Xavier")
	convnet = max_pool_2d(convnet, 2)

	convnet = conv_2d(convnet, 512, 2, activation='elu', weights_init="Xavier")
	convnet = max_pool_2d(convnet, 2)

	convnet = fully_connected(convnet, 1024, activation='elu')
	convnet = dropout(convnet, 0.5)

	convnet = fully_connected(convnet, nbClasses, activation='softmax')
	convnet = regression(convnet, optimizer='rmsprop', loss='categorical_crossentropy')

	model = tflearn.DNN(convnet)
	print("    Model created! ✅")
	return model
コード例 #5
0
ファイル: review.py プロジェクト: Emersonxuelinux/2book
def do_cnn_doc2vec(trainX, testX, trainY, testY):
    global max_features
    print "CNN and doc2vec"

    #trainX = pad_sequences(trainX, maxlen=max_features, value=0.)
    #testX = pad_sequences(testX, maxlen=max_features, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Building convolutional network
    network = input_data(shape=[None,max_features], name='input')
    network = tflearn.embedding(network, input_dim=1000000, output_dim=128,validate_indices=False)
    branch1 = conv_1d(network, 128, 3, padding='valid', activation='relu', regularizer="L2")
    branch2 = conv_1d(network, 128, 4, padding='valid', activation='relu', regularizer="L2")
    branch3 = conv_1d(network, 128, 5, padding='valid', activation='relu', regularizer="L2")
    network = merge([branch1, branch2, branch3], mode='concat', axis=1)
    network = tf.expand_dims(network, 2)
    network = global_max_pool(network)
    network = dropout(network, 0.8)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.001,
                         loss='categorical_crossentropy', name='target')
    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit(trainX, trainY,
              n_epoch=5, shuffle=True, validation_set=(testX, testY),
              show_metric=True, batch_size=100,run_id="review")
コード例 #6
0
def build_model_anything_happening():
    ### IS ANY OF THIS NECESSARY FOR LIGHT/DARK? IN GENERAL W/ STAIONARY CAMERA?
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()

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

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

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

    model = tflearn.DNN(network, tensorboard_verbose=0)
    return model
コード例 #7
0
ファイル: review.py プロジェクト: Emersonxuelinux/2book
def do_cnn_doc2vec_2d(trainX, testX, trainY, testY):
    print "CNN and doc2vec 2d"

    trainX = trainX.reshape([-1, max_features, max_document_length, 1])
    testX = testX.reshape([-1, max_features, max_document_length, 1])


    # Building convolutional network
    network = input_data(shape=[None, max_features, max_document_length, 1], name='input')
    network = conv_2d(network, 16, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = fully_connected(network, 128, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 256, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 10, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.01,
                         loss='categorical_crossentropy', name='target')

    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit({'input': trainX}, {'target': trainY}, n_epoch=20,
               validation_set=({'input': testX}, {'target': testY}),
               snapshot_step=100, show_metric=True, run_id='review')
コード例 #8
0
ファイル: cnn.py プロジェクト: Emersonxuelinux/2book
def cnn():
    X, Y, testX, testY = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])

    # Building convolutional network
    network = input_data(shape=[None, 28, 28, 1], name='input')
    network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = fully_connected(network, 128, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 256, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 10, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.01,
                         loss='categorical_crossentropy', name='target')

    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit({'input': X}, {'target': Y}, n_epoch=20,
               validation_set=({'input': testX}, {'target': testY}),
               snapshot_step=100, show_metric=True, run_id='cnn_demo')
コード例 #9
0
 def build_network(self):
   # Building 'AlexNet'
   # https://github.com/tflearn/tflearn/blob/master/examples/images/alexnet.py
   # https://github.com/DT42/squeezenet_demo
   # https://github.com/yhenon/pysqueezenet/blob/master/squeezenet.py
   print('[+] Building CNN')
   self.network = input_data(shape = [None, SIZE_FACE, SIZE_FACE, 1])
   self.network = conv_2d(self.network, 96, 11, strides = 4, activation = 'relu')
   self.network = max_pool_2d(self.network, 3, strides = 2)
   self.network = local_response_normalization(self.network)
   self.network = conv_2d(self.network, 256, 5, activation = 'relu')
   self.network = max_pool_2d(self.network, 3, strides = 2)
   self.network = local_response_normalization(self.network)
   self.network = conv_2d(self.network, 256, 3, activation = 'relu')
   self.network = max_pool_2d(self.network, 3, strides = 2)
   self.network = local_response_normalization(self.network)
   self.network = fully_connected(self.network, 1024, activation = 'tanh')
   self.network = dropout(self.network, 0.5)
   self.network = fully_connected(self.network, 1024, activation = 'tanh')
   self.network = dropout(self.network, 0.5)
   self.network = fully_connected(self.network, len(EMOTIONS), activation = 'softmax')
   self.network = regression(self.network,
     optimizer = 'momentum',
     loss = 'categorical_crossentropy')
   self.model = tflearn.DNN(
     self.network,
     checkpoint_path = SAVE_DIRECTORY + '/alexnet_mood_recognition',
     max_checkpoints = 1,
     tensorboard_verbose = 2
   )
   self.load_model()
コード例 #10
0
ファイル: train.py プロジェクト: richardbored/customData
def _model1():
    global yTest, img_aug
    tf.reset_default_graph()
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    network = input_data(shape=[None, inputSize, inputSize, dim],
                 name='input',
                 data_preprocessing=img_prep,
                 data_augmentation=img_aug)

    network = conv_2d(network, 32, 3, strides = 4, activation='relu')
    network = max_pool_2d(network, 2, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 64, 3, strides = 2, activation='relu')
    network = max_pool_2d(network, 2, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 128, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 256, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, len(Y[0]), activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.001,
                 loss='categorical_crossentropy', name='target')

    model = tflearn.DNN(network, tensorboard_verbose=3)
    model.fit(X, Y, n_epoch=epochNum, validation_set=(xTest, yTest),
       snapshot_step=500, show_metric=True, batch_size=batchNum, shuffle=True, run_id=_id + 'artClassification')
    if modelStore: model.save(_id + '-model.tflearn')
コード例 #11
0
ファイル: ann_playing.py プロジェクト: Eudie/Online-Practice
def neural_network_model(input_size):
    """
    Function is to build NN based on the input size
    :param input_size: feature size of each observation
    :return: tensorflow model
    """
    network = input_data(shape=[None, input_size], name='input')

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

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

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

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

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

    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, learning_rate=LR,  name='targets')
    model = tflearn.DNN(network, tensorboard_dir='logs/ann/ann_0')

    return model
コード例 #12
0
ファイル: 17-2.py プロジェクト: DemonZeros/1book
def  do_cnn(trainX, trainY,testX, testY):
    global n_words
    # Data preprocessing
    # Sequence padding
    trainX = pad_sequences(trainX, maxlen=MAX_DOCUMENT_LENGTH, value=0.)
    testX = pad_sequences(testX, maxlen=MAX_DOCUMENT_LENGTH, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Building convolutional network
    network = input_data(shape=[None, MAX_DOCUMENT_LENGTH], name='input')
    network = tflearn.embedding(network, input_dim=n_words+1, output_dim=128)
    branch1 = conv_1d(network, 128, 3, padding='valid', activation='relu', regularizer="L2")
    branch2 = conv_1d(network, 128, 4, padding='valid', activation='relu', regularizer="L2")
    branch3 = conv_1d(network, 128, 5, padding='valid', activation='relu', regularizer="L2")
    network = merge([branch1, branch2, branch3], mode='concat', axis=1)
    network = tf.expand_dims(network, 2)
    network = global_max_pool(network)
    network = dropout(network, 0.5)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.001,
                         loss='categorical_crossentropy', name='target')
    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit(trainX, trainY, n_epoch = 20, shuffle=True, validation_set=(testX, testY), show_metric=True, batch_size=32)
コード例 #13
0
 def __init__(self):
     network = tflearn.input_data(shape=[None, 784], name="input")
     network = self.make_core_network(network)
     network = regression(network, optimizer='adam', learning_rate=0.01,
                          loss='categorical_crossentropy', name='target')
     
     model = tflearn.DNN(network, tensorboard_verbose=0)
     self.model = model
コード例 #14
0
def main():
    """

    :return:
    """
    # pickle parameters
    fg_or_bg = 'background'
    sdr_type = 'sdr'
    feature = 'sim_mat'
    beat_spec_len = 432

    # set up training, testing, & validation partitions
    sim_mat_array, sdr_array = get_generated_data(feature, fg_or_bg, sdr_type)

    # training params
    n_classes = 10
    n_training_steps = 1000
    training_step_size = 100
    training_percent = 0.85
    testing_percent = 0.15
    validation_percent = 0.00

    sdr_array_1h, hist = sdrs_to_one_hots(sdr_array, n_classes, True)

    train, test, validate = split_into_sets(len(sim_mat_array), training_percent,
                                            testing_percent, validation_percent)

    # Building convolutional network
    network = input_data(shape=[None, beat_spec_len, 1], name='input')
    network = conv_1d(network, 32, 3, activation='relu', regularizer="L2")
    network = max_pool_1d(network, 2)
    # network = local_response_normalization(network)
    # network = batch_normalization(network)
    # network = conv_1d(network, 64, 3, activation='relu', regularizer="L2")
    # network = max_pool_1d(network, 2)
    # network = local_response_normalization(network)
    # network = batch_normalization(network)
    # network = fully_connected(network, 128, activation='tanh')
    # network = dropout(network, 0.5)
    network = fully_connected(network, 512, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, n_classes, activation='softmax')
    # network = fully_connected(network, 1, activation='linear')
    network = regression(network, optimizer='adagrad', learning_rate=0.01,
                         loss='categorical_crossentropy', name='target')

    X = np.expand_dims(sim_mat_array, -1)
    Y = np.array(sdr_array_1h)
    # X = np.expand_dims([beat_spec_array[i] for i in train], -1)
    # Y = np.array([sdr_array_1h[i] for i in train])
    # testX = np.expand_dims([beat_spec_array[i] for i in test], -1)
    # testY = np.array([sdr_array[i] for i in test])

    # Training
    model = tflearn.DNN(network, tensorboard_verbose=1)
    model.fit({'input': X}, {'target': Y}, n_epoch=20,
              validation_set=0.1,
              snapshot_step=1000, show_metric=True, run_id='{} classes'.format(n_classes - 1))
コード例 #15
0
ファイル: train.py プロジェクト: ajfisher/nodebot_ai
def build_model():
	network = input_data(shape=[None, 128, 128, 1], name='input')
	network = conv_2d(network, nb_filter=2, filter_size=5, strides=1, activation='tanh')
	network = fully_connected(network, 1, activation='linear')
	network = regression(network, optimizer='adam', learning_rate=0.001,
						 loss='mean_square', name='target')

	model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='checkpoints/road_model1')
	return model
コード例 #16
0
ファイル: train.py プロジェクト: astaroth106/LearningAgents
def build_model():
	init = tf.truncated_normal_initializer(stddev=1e-4)

	network = input_data(shape=[None, 128, 128, 1], name='input')
	network = conv_2d(network, nb_filter=2, filter_size=5, strides=2, activation='tanh', weights_init=init)
	network = fully_connected(network, 1, activation='tanh', weights_init=init)
	network = regression(network, optimizer='sgd', learning_rate=learning_rate,
						 loss='mean_square', name='target')

	model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='checkpoints/road_model1')
	return model
コード例 #17
0
ファイル: webshell.py プロジェクト: Emersonxuelinux/2book
def do_cnn(x,y):
    global max_document_length
    print "CNN and tf"
    trainX, testX, trainY, testY = train_test_split(x, y, test_size=0.4, random_state=0)
    y_test=testY

    trainX = pad_sequences(trainX, maxlen=max_document_length, value=0.)
    testX = pad_sequences(testX, maxlen=max_document_length, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Building convolutional network
    network = input_data(shape=[None,max_document_length], name='input')
    network = tflearn.embedding(network, input_dim=1000000, output_dim=128)
    branch1 = conv_1d(network, 128, 3, padding='valid', activation='relu', regularizer="L2")
    branch2 = conv_1d(network, 128, 4, padding='valid', activation='relu', regularizer="L2")
    branch3 = conv_1d(network, 128, 5, padding='valid', activation='relu', regularizer="L2")
    network = merge([branch1, branch2, branch3], mode='concat', axis=1)
    network = tf.expand_dims(network, 2)
    network = global_max_pool(network)
    network = dropout(network, 0.8)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.001,
                         loss='categorical_crossentropy', name='target')

    model = tflearn.DNN(network, tensorboard_verbose=0)
    #if not os.path.exists(pkl_file):
        # Training
    model.fit(trainX, trainY,
                  n_epoch=5, shuffle=True, validation_set=0.1,
                  show_metric=True, batch_size=100,run_id="webshell")
    #    model.save(pkl_file)
    #else:
    #    model.load(pkl_file)

    y_predict_list=model.predict(testX)
    #y_predict = list(model.predict(testX,as_iterable=True))

    y_predict=[]
    for i in y_predict_list:
        print  i[0]
        if i[0] > 0.5:
            y_predict.append(0)
        else:
            y_predict.append(1)
    print 'y_predict_list:'
    print y_predict_list
    print 'y_predict:'
    print  y_predict
    #print  y_test

    do_metrics(y_test, y_predict)
コード例 #18
0
ファイル: models.py プロジェクト: ErwanGalline/test
def build_model_1_conv(learning_rate, input_shape, nb_classes, base_path , drop):
    network = input_data(shape=input_shape, name='input')
    network = conv_2d(network, 64, [4, 16], activation='relu')
    network = fully_connected(network, 128, activation='relu')
    network = dropout(network, drop)
    network = fully_connected(network, 64, activation='relu')
    network = dropout(network, drop)
    network = fully_connected(network, nb_classes, activation='softmax')
    network = regression(network, optimizer='sgd', learning_rate=learning_rate,
                         loss='categorical_crossentropy', name='target')
    model = tflearn.DNN(network, tensorboard_verbose=3, tensorboard_dir=base_path + "/tflearn_logs/",
                        checkpoint_path=base_path + "/checkpoints/step")
    return model
コード例 #19
0
    def generate_network(self):
        """ Return tflearn cnn network.
        """
        print(self.image_size, self.n_epoch, self.batch_size, self.person_ids)
        print(type(self.image_size), type(self.n_epoch),
              type(self.batch_size), type(self.person_ids))
        if not isinstance(self.image_size, list) \
            or not isinstance(self.n_epoch, int) \
            or not isinstance(self.batch_size, int) \
            or not isinstance(self.person_ids, list):
        # if self.image_size is None or self.n_epoch is None or \
        #     self.batch_size is None or self.person_ids is None:
            raise ValueError("Insufficient values to generate network.\n"
                             "Need (n_epoch, int), (batch_size, int),"
                             "(image_size, list), (person_ids, list).")

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

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

        # Convolutional network building
        network = input_data(
            shape=[None, self.image_size[0], self.image_size[1], 3],
            data_preprocessing=img_prep,
            data_augmentation=img_aug)
        network = conv_2d(network, self.image_size[0], self.IMAGE_CHANNEL_NUM,
                          activation='relu')
        network = max_pool_2d(network, 2)
        network = conv_2d(network, self.image_size[0] * 2,
                          self.IMAGE_CHANNEL_NUM,
                          activation='relu')
        network = conv_2d(network, self.image_size[0] * 2,
                          self.IMAGE_CHANNEL_NUM,
                          activation='relu')
        network = max_pool_2d(network, 2)
        network = fully_connected(network, self.image_size[0] * 2**4,
                                  activation='relu')
        network = dropout(network, 0.5)
        network = fully_connected(network, self.person_num,
                                  activation='softmax')
        network = regression(network, optimizer='adam',
                             loss='categorical_crossentropy',
                             learning_rate=0.001)
        return network
コード例 #20
0
ファイル: evaluate.py プロジェクト: richardbored/customData
def _model3():
    global yTest, img_aug
    tf.reset_default_graph()
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    network = input_data(shape=[None, inputSize, inputSize, dim],
                             data_preprocessing=img_prep,
                             data_augmentation=img_aug)
    network = conv_2d(network, 96, 11, strides=4, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 256, 5, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, len(yTest[0]), activation='softmax')
    network = regression(network, optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)
    print('Model has been made!!!?')
    # Training
    model = tflearn.DNN(network, checkpoint_path='model_densenet_cifar10',
                        max_checkpoints=10, tensorboard_verbose=0,
                        clip_gradients=0.)
    model.load(_path)
    pred = model.predict(xTest)

    df = pd.DataFrame(pred)
    df.to_csv(_path + ".csv")

    newList = pred.copy()
    newList = convert2(newList)
    if _CSV: makeCSV(newList)
    pred = convert2(pred)
    pred = convert3(pred)
    yTest = convert3(yTest)
    print(metrics.confusion_matrix(yTest, pred))
    print(metrics.classification_report(yTest, pred))
    print('Accuracy', accuracy_score(yTest, pred))
    print()
    if _wrFile: writeTest(pred)
コード例 #21
0
    def __init__(self):
        inputs = tflearn.input_data(shape=[None, 784], name="input")

        with tf.variable_scope("scope1") as scope:
            net_conv = Model1.make_core_network(inputs)	# shape (?, 10)
        with tf.variable_scope("scope2") as scope:
            net_dnn = Model2.make_core_network(inputs)	# shape (?, 10)

        network = tf.concat([net_conv, net_dnn], 1, name="concat")	# shape (?, 20)
        network = tflearn.fully_connected(network, 10, activation="softmax")
        network = regression(network, optimizer='adam', learning_rate=0.01,
                             loss='categorical_crossentropy', name='target')

        self.model = tflearn.DNN(network, tensorboard_verbose=0)
コード例 #22
0
ファイル: train.py プロジェクト: andykitchen/nn-locate
def build_network(image_size, batch_size=None, n_channels=3):
    network = input_data(shape=[batch_size, image_size[0], image_size[1], n_channels],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
    network = conv_2d(network, 16, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, num_classes, activation='softmax')
    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.0001)

    return network
コード例 #23
0
def create_single_digit_model():
    input_layer, last_cnn_layer = create_cnn_layers()

    # h = Dense(256, activation='relu')(last_cnn_layer)
    h = fully_connected(last_cnn_layer, 256, activation='relu', weights_init=INIT)
    # h = Dropout(0.5)(h)
    h = dropout(h, 1-0.5)
    # output_layer = Dense(CLASS_COUNT, activation='softmax', name='out')(h)
    output_layer = fully_connected(h, CLASS_COUNT, activation='softmax', weights_init=INIT)
    network = regression(output_layer, optimizer=OPTIMIZER,
                     learning_rate=0.1,
                     loss='categorical_crossentropy', name='out')
    # model = Model(input_layer, output_layer)
    model = tflearn.DNN(network, tensorboard_verbose=3, tensorboard_dir='./logs/')
    return model
コード例 #24
0
ファイル: model.py プロジェクト: mathcass/catclassifier
def setup_model(checkpoint_path=None):
    """Sets up a deep belief network for image classification based on the set up described in

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

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

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

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

    # Create extra synthetic training data by flipping, rotating and blurring the
    # images on our data set.
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)
    img_aug.add_random_blur(sigma_max=3.)

    # Input is a 32x32 image with 3 color channels (red, green and blue)
    network = input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, 64, 3, activation='relu')
    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, 512, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)
    if checkpoint_path:
        model = tflearn.DNN(network, tensorboard_verbose=3,
                            checkpoint_path=checkpoint_path)
    else:
        model = tflearn.DNN(network, tensorboard_verbose=3)

    return model
コード例 #25
0
ファイル: SuironML.py プロジェクト: tanklarry/suiron
def get_nn_model(checkpoint_path='nn_motor_model', session=None):
    # Input is a single value (raw motor value)
    network = input_data(shape=[None, 1], name='input')

    # Hidden layer no.1,  
    network = fully_connected(network, 12, activation='linear')
    
    # Output layer
    network = fully_connected(network, 1, activation='tanh')

    # regression
    network = regression(network, loss='mean_square', metric='accuracy', name='target')

    # Verbosity yay nay
    model = tflearn.DNN(network, tensorboard_verbose=3, checkpoint_path=checkpoint_path, session=session)
    return model
コード例 #26
0
ファイル: SuironML.py プロジェクト: tanklarry/suiron
def get_cnn_model(checkpoint_path='cnn_servo_model', width=72, height=48, depth=3, session=None):
    
    # Inputs
    network = input_data(shape=[None, height, width, depth], name='input')

    # Convolution no.1
    # Relu introduces non linearity into training
    network = conv_2d(network, 8, [5, 3], activation='relu')

    # Convolution no.2
    network = conv_2d(network, 12, [5, 8], activation='relu')
    
    # Convolution no.3
    network = conv_2d(network, 16, [5, 16], activation='relu')

    # Convolution no.4
    network = conv_2d(network, 24, [3, 20], activation='relu')

    # Convolution no.5
    network = conv_2d(network, 24, [3, 24], activation='relu')

    # Fully connected no.1
    network = fully_connected(network, 256, activation='relu')
    network = dropout(network, 0.8)

    # Fully connected no.2
    network = fully_connected(network, 100, activation='relu')
    network = dropout(network, 0.8)

    # Fully connected no.3
    network = fully_connected(network, 50, activation='relu')
    network = dropout(network, 0.8)

    # Fully connected no.4
    network = fully_connected(network, 10, activation='relu')
    network = dropout(network, 0.8)
 
    # Fully connected no.5
    network = fully_connected(network, 1, activation='tanh')

    # Regression
    network = regression(network, loss='mean_square', metric='accuracy', learning_rate=1e-4,name='target') 

    # Verbosity yay nay
    # 0 = nothing
    model = tflearn.DNN(network, tensorboard_verbose=2, checkpoint_path=checkpoint_path, session=session) 
    return model
コード例 #27
0
def convolutional_neural_network(width=5, height=6):
    """Create the neural network model.

    Args:
        width: Width of the pseudo image
        height: Height of the pseudo image

    Returns:
        convnet: Output

    """
    # Initialize key variables
    conv1_filter_count = 32
    conv2_filter_count = 64
    fc_units = 1024
    image_height = height
    image_width = width
    filter_size = 2
    pooling_kernel_size = 2
    keep_probability = 0.6
    fully_connected_units = 10

    # Create the convolutional network stuff
    convnet = input_data(
        shape=[None, image_width, image_height, 1], name='input')

    convnet = conv_2d(
        convnet, conv1_filter_count, filter_size, activation='relu')
    convnet = max_pool_2d(convnet, pooling_kernel_size)

    convnet = conv_2d(
        convnet, conv2_filter_count, filter_size, activation='relu')
    convnet = max_pool_2d(convnet, pooling_kernel_size)

    convnet = fully_connected(convnet, fc_units, activation='relu')
    convnet = dropout(convnet, keep_probability)

    convnet = fully_connected(
        convnet, fully_connected_units, activation='softmax')
    convnet = regression(
        convnet,
        optimizer='adam',
        learning_rate=0.01,
        loss='categorical_crossentropy',
        name='targets')

    return convnet
コード例 #28
0
ファイル: sms.py プロジェクト: Emersonxuelinux/2book
def do_cnn_word2vec_2d(trainX, testX, trainY, testY):
    global max_features
    global max_document_length
    print "CNN and word2vec2d"
    y_test = testY
    #trainX = pad_sequences(trainX, maxlen=max_features, value=0.)
    #testX = pad_sequences(testX, maxlen=max_features, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Building convolutional network
    network = input_data(shape=[None,max_document_length,max_features,1], name='input')

    network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = fully_connected(network, 128, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 256, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.01,
                         loss='categorical_crossentropy', name='target')

    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit(trainX, trainY,
              n_epoch=5, shuffle=True, validation_set=(testX, testY),
              show_metric=True,run_id="sms")

    y_predict_list = model.predict(testX)
    print y_predict_list

    y_predict = []
    for i in y_predict_list:
        print  i[0]
        if i[0] > 0.5:
            y_predict.append(0)
        else:
            y_predict.append(1)

    print(classification_report(y_test, y_predict))
    print metrics.confusion_matrix(y_test, y_predict)
コード例 #29
0
ファイル: cnn.py プロジェクト: Emersonxuelinux/2book
def vggnet():
    X, Y = oxflower17.load_data(one_hot=True,resize_pics=(227, 227))

    # Building 'VGG Network'
    network = input_data(shape=[None, 227, 227, 3])

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

    network = conv_2d(network, 128, 3, activation='relu')
    network = conv_2d(network, 128, 3, activation='relu')
    network = max_pool_2d(network, 2, strides=2)

    network = conv_2d(network, 256, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 2, strides=2)

    network = conv_2d(network, 512, 3, activation='relu')
    network = conv_2d(network, 512, 3, activation='relu')
    network = conv_2d(network, 512, 3, activation='relu')
    network = max_pool_2d(network, 2, strides=2)

    network = conv_2d(network, 512, 3, activation='relu')
    network = conv_2d(network, 512, 3, activation='relu')
    network = conv_2d(network, 512, 3, activation='relu')
    network = max_pool_2d(network, 2, strides=2)

    network = fully_connected(network, 4096, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 17, activation='softmax')

    network = regression(network, optimizer='rmsprop',
                         loss='categorical_crossentropy',
                         learning_rate=0.0001)

    # Training
    model = tflearn.DNN(network, checkpoint_path='model_vgg',
                        max_checkpoints=1, tensorboard_verbose=0)
    model.fit(X, Y, n_epoch=500, shuffle=True,
              show_metric=True, batch_size=32, snapshot_step=500,
              snapshot_epoch=False, run_id='vgg')
コード例 #30
0
ファイル: models.py プロジェクト: ErwanGalline/test
def create_model(learning_rate, input_shape, nb_classes, base_path, drop=1):
    network = input_data(shape=input_shape, name='input')
    network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = fully_connected(network, 128, activation='tanh')
    network = dropout(network, drop)
    network = fully_connected(network, 256, activation='tanh')
    network = dropout(network, drop)
    network = fully_connected(network, nb_classes, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=learning_rate,
                         loss='categorical_crossentropy', name='target')
    model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path=base_path + "/checkpoints/step")

    return model
コード例 #31
0
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

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

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

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

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet,
                     optimizer='adam',
                     learning_rate=LR,
                     loss='categorical_crossentropy',
                     name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

if os.path.exists(
        'C:/Users/H/Desktop/KaggleDogsvsCats/{}.meta'.format(MODEL_NAME)):
    model.load(MODEL_NAME)
    print('model loaded!')

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
Y = [i[1] for i in train]
コード例 #32
0
                             name='Conv1D_3')
network9 = tflearn.layers.conv.conv_1d (network8,
                             nb_filter=256,
                             filter_size=3,
                             strides=1,
                             padding='same',
                             activation='relu',
                             bias=True,
                             weights_init='xavier',
                             bias_init='zeros',
                             regularizer='L2',
                             weight_decay=0.0001,
                             trainable=True,
                             restore=True,
                             reuse=False,
                             scope=None,
                             name='Conv1D_4')
network10 = batch_normalization(network9)
########

network11 = fully_connected(network10,256,activation='softmax')
network12 = dropout(network11,0.5)
network13 = fully_connected(network12,3,activation='softmax')
network14 = regression(network13, optimizer='adam',
                       loss='categorical_crossentropy',learning_rate=0.0001)

model = tflearn.DNN(network14, tensorboard_verbose=2)
model.fit(X, Y, n_epoch=100, validation_set=0.3, snapshot_step=400, shuffle= True,
          show_metric=True, batch_size=128,run_id='ConvNet3')

model.save('saved_model/ConvNet3')
コード例 #33
0
                  activation='relu',
                  weights_init='truncated_normal',
                  bias_init='truncated_normal')
network = conv_2d(network,
                  64,
                  5,
                  strides=2,
                  activation='relu',
                  weights_init='truncated_normal',
                  bias_init='truncated_normal')
network = max_pool_2d(network, 2, strides=2)
network = fully_connected(network, 256, activation='relu')
network = dropout(network, dropout_rate)
network = fully_connected(network, n_classes, activation='softmax')
network = regression(network,
                     optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=learning_rate)

# Training
model = tflearn.DNN(network,
                    checkpoint_path='model.tfl.ckpt',
                    tensorboard_verbose=0,
                    max_checkpoints=1)

model.load("model.tfl")

print("Total and final accuracy test: " + str(model.evaluate(testX, testY)[0]))


# Predicts what an image is given its path
def predict_image(image_path):
コード例 #34
0
def foo(img_fn, model_fn='../data/model/model_weights'):
    img = cv2.imread(img_fn, cv2.IMREAD_GRAYSCALE)

    haar_fn = '../data/haarcascade_russian_plate_number.xml'
    haar = cv2.CascadeClassifier(haar_fn)
    detected = haar.detectMultiScale(img)
    plates = []
    for x, y, w, h in detected:
        obj = img[y:y + h, x:x + w]
        plates.append(obj)

    chars = plates[0] < filters.threshold_minimum(plates[0])

    labeled_chars, a = ndi.label(chars)
    labeled_chars = (labeled_chars > 1).astype(np.int8)

    c = measure.find_contours(labeled_chars, .1)

    letters = []
    for i, v in enumerate(c):
        xs, ys = zip(*[i for i in v])
        x = int(min(xs))
        y = int(min(ys))
        w = int(max(xs) - x + 2)
        h = int(max(ys) - y + 2)
        if w < 15:
            continue
        letters.append((y, x, h, w))

    letters = sorted(letters)

    letters_img = [plates[0][x:x + w, y:y + h] for y, x, h, w in letters]

    letters_img = [i for i in letters_img if i[0, 0] > 127]

    sizes = [image.size for image in letters_img]
    median = np.median(sizes)
    allowed_size = median + median / 4

    letters_img = [image for image in letters_img if image.size < allowed_size]

    size = 64

    normalized_img = []
    for i in letters_img:
        ratio = i.shape[0] / i.shape[1]
        img1 = transform.resize(i, [size, int(size / ratio)], mode='constant')
        width = img1.shape[1]
        missing = (size - width) // 2
        ones = np.ones([size, missing])
        img2 = np.append(ones, img1, 1)
        img3 = np.append(img2, ones, 1)
        if 2 * missing + width != size:
            one = np.ones([size, 1])
            img4 = np.append(img3, one, 1)
        else:
            img4 = img3
        normalized_img.append(img4 * 255)

    net_input = input_data(shape=[None, 64, 64, 1])

    conv1 = conv_2d(net_input,
                    nb_filter=4,
                    filter_size=5,
                    strides=[1, 1, 1, 1],
                    activation='relu')
    max_pool1 = max_pool_2d(conv1, kernel_size=2)

    conv2 = conv_2d(max_pool1,
                    nb_filter=8,
                    filter_size=5,
                    strides=[1, 2, 2, 1],
                    activation='relu')
    max_pool2 = max_pool_2d(conv2, kernel_size=2)

    conv3 = conv_2d(max_pool2,
                    nb_filter=12,
                    filter_size=4,
                    strides=[1, 1, 1, 1],
                    activation='relu')
    max_pool3 = max_pool_2d(conv3, kernel_size=2)

    fc1 = fully_connected(max_pool3, n_units=200, activation='relu')
    drop1 = dropout(fc1, keep_prob=.5)

    fc2 = fully_connected(drop1, n_units=36, activation='softmax')
    net = regression(fc2)

    model = DNN(network=net)
    model.load(model_file=model_fn)

    labels = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')

    predicted = []
    for i in normalized_img:
        y = model.predict(i.reshape([1, 64, 64, 1]))
        y_pred = np.argmax(y[0])
        predicted.append(labels[y_pred])

    return ''.join(predicted)
コード例 #35
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
コード例 #36
0
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.embedding_ops import embedding
from tflearn.layers.recurrent import bidirectional_rnn, BasicLSTMCell
from tflearn.layers.estimator import regression

# IMDB Dataset loading
train, test, _ = imdb.load_data(path='imdb.pkl',
                                n_words=10000,
                                valid_portion=0.1)
trainX, trainY = train
testX, testY = test

# Data preprocessing
# Sequence padding
trainX = pad_sequences(trainX, maxlen=200, value=0.)
testX = pad_sequences(testX, maxlen=200, value=0.)
# Converting labels to binary vectors
trainY = to_categorical(trainY, nb_classes=2)
testY = to_categorical(testY, nb_classes=2)

# Network building
net = input_data(shape=[None, 200])
net = embedding(net, input_dim=20000, output_dim=128)
net = bidirectional_rnn(net, BasicLSTMCell(128), BasicLSTMCell(128))
net = dropout(net, 0.5)
net = fully_connected(net, 2, activation='softmax')
net = regression(net, optimizer='adam', loss='categorical_crossentropy')

# Training
model = tflearn.DNN(net, clip_gradients=0., tensorboard_verbose=2)
model.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=64)
コード例 #37
0
network = fully_connected(network, 512, activation='relu')
network = batch_normalization(network)
network = dropout(network, 0.7)

network = fully_connected(network, 512, activation='relu')
network = batch_normalization(network)
network = dropout(network, 0.8)

# use a softmax activation which outputs a probability distribution over two classes
network = fully_connected(network, 2, activation='softmax')
adam = tflearn.optimizers.Adam(learning_rate=0.0001,
                               epsilon=1e-06)  # use the ADAM optimizer

# the loss function is "categorical cross entropy"
network = regression(network,
                     optimizer=adam,
                     loss='categorical_crossentropy',
                     name='target')

# set up the DNN. A neat feature: a snapshot is saved whenever the best achieved test accuracy until now is surpassed,
# i.e. the *latest* file in the snapshots folder will be the model with the best accuracy
model = tflearn.DNN(network,
                    tensorboard_verbose=0,
                    tensorboard_dir='/tmp/tflearn_logY/',
                    best_checkpoint_path='/tmp/snapshots/',
                    best_val_accuracy=0.96)

# the actual training: the batch_size of 512 is rather high, but showed a good compromise between GPU load and accuracy
# (at least when training with GPU, we used here a single NVidia GTX 1070)
model.fit({
    'input': X,
    'in_climate': Xclim
コード例 #38
0
                    params['conv_filter'],
                    activation='relu',
                    regularizer='L2')
    pool3 = max_pool_2d(conv3, params['pool_width'], params['pool_stride'])
    lrn3 = local_response_normalization(pool3)

    flat = flatten(lrn3)

    fully1 = fully_connected(lrn3, 384, activation='relu')
    drop1 = dropout(fully1, 0.5)
    fully2 = fully_connected(drop1, 384 / 2, activation='relu')
    drop2 = dropout(fully2, 0.5)
    fully3 = fully_connected(drop2, 10, activation='softmax')
    network = regression(fully3,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001,
                         name='Target')

    # Train using classifier
    model = tflearn.DNN(network,
                        tensorboard_verbose=0,
                        tensorboard_dir='../log/')
    model.fit(X,
              Y,
              n_epoch=params['epoch'],
              shuffle=True,
              validation_set=(X_test, Y_test),
              show_metric=True,
              batch_size=128,
              run_id=params['id'])
コード例 #39
0
def analysis(filepath):

	verify_data = process_verify_data(filepath)

	str_label = "Cannot make a prediction."
	status = "Error"

	tf.reset_default_graph()

	convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')

	'''
	# relu:

	Relu is used in the middle / hidden layers of the network to regularize the activation.
	It is essentialy the function: max(0, x)
	Activation should not be in negative, either it should be zero or more than that.

	# softmax: 

	Softmax is used for the output layer in multi class classification problems.
	It is essentialy the function: log(1 + e^x)
	It outputs a vector of probabilities of each class.

	'''

	convnet = conv_2d(convnet, 32, 3, activation='relu')
	convnet = max_pool_2d(convnet, 3)

	convnet = conv_2d(convnet, 64, 3, activation='relu')
	convnet = max_pool_2d(convnet, 3)

	convnet = conv_2d(convnet, 128, 3, activation='relu')
	convnet = max_pool_2d(convnet, 3)

	convnet = conv_2d(convnet, 32, 3, activation='relu')
	convnet = max_pool_2d(convnet, 3)

	convnet = conv_2d(convnet, 64, 3, activation='relu')
	convnet = max_pool_2d(convnet, 3)

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

	convnet = fully_connected(convnet, 4, activation='softmax')
	convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

	model = tflearn.DNN(convnet, tensorboard_dir='log')

	if os.path.exists('{}.meta'.format(MODEL_NAME)):
		model.load(MODEL_NAME)
		print ('Model loaded successfully.')
	else:
		print ('Error: Create a model using neural_network.py first.')

	img_data, img_name = verify_data[0], verify_data[1]

	orig = img_data
	data = img_data.reshape(IMG_SIZE, IMG_SIZE, 3)

	model_out = model.predict([data])[0]

	if np.argmax(model_out) == 0: str_label = 'Healthy'
	elif np.argmax(model_out) == 1: str_label = 'Bacterial'
	elif np.argmax(model_out) == 2: str_label = 'Viral'
	elif np.argmax(model_out) == 3: str_label = 'Lateblight'

	if str_label =='Healthy': status = 'Healthy'
	else: status = 'Unhealthy'

	result = 'Status: ' + status + '.'
	
	if (str_label != 'Healthy'): result += '\nDisease: ' + str_label + '.'

	return result
コード例 #40
0
net = max_pool_2d(net, kernel_size=[2, 2])

####################################################################
# No need to reshape
####################################################################

# layer3
net = fully_connected(net, n_units=500, activation="relu")

# layer4
net = fully_connected(net, n_units=10, activation="softmax")

# calc optimizer

net = regression(net,
                 optimizer='sgd',
                 loss='categorical_crossentropy',
                 learning_rate=0.01)

# create Deep NN

model = tflearn.DNN(net, tensorboard_verbose=0)

# train: just like "fit" in sklearn
model.fit(
    X_train,
    y_train,
    n_epoch=20,
    validation_set=([X_test, y_test]),
    show_metric=True,
    shuffle=True,
)
コード例 #41
0
def analysis():
    import cv2  # working with, mainly resizing, images
    import numpy as np  # dealing with arrays
    import os  # dealing with directories
    from random import shuffle  # mixing up or currently ordered data that might lead our network astray in training.
    from tqdm import \
        tqdm  # a nice pretty percentage bar for tasks. Thanks to viewer Daniel BA1/4hler for this suggestion
    verify_dir = 'testpicture'
    IMG_SIZE = 50
    LR = 1e-3
    MODEL_NAME = 'healthyvsunhealthy-{}-{}.model'.format(LR, '2conv-basic')
    message = tk.Label(text='Status: ',
                       background="lightgreen",
                       fg="Brown",
                       font=("", 15))
    message.grid(column=0, row=3, padx=10, pady=10)
    disease = tk.Label(text=' ',
                       background="lightgreen",
                       fg="Black",
                       font=("", 15))
    disease.grid(column=0, row=4, padx=10, pady=10)
    disease = tk.Label(text='Disease Name: ',
                       background="lightgreen",
                       fg="Black",
                       font=("", 15))

    disease.grid(column=0, row=4, padx=10, pady=10)

    def process_verify_data():
        verifying_data = []
        for img in tqdm(os.listdir(verify_dir)):
            path = os.path.join(verify_dir, img)
            img_num = img.split('.')[0]
            img = cv2.imread(path, cv2.IMREAD_COLOR)
            img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
            verifying_data.append([np.array(img), img_num])
        np.save('verify_data.npy', verifying_data)
        return verifying_data

    verify_data = process_verify_data()
    #verify_data = np.load('verify_data.npy')

    import tflearn
    from tflearn.layers.conv import conv_2d, max_pool_2d
    from tflearn.layers.core import input_data, dropout, fully_connected
    from tflearn.layers.estimator import regression
    import tensorflow as tf
    tf.reset_default_graph()

    convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')

    convnet = conv_2d(convnet, 32, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 64, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 128, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 32, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 64, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

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

    convnet = fully_connected(convnet, 4, activation='softmax')
    convnet = regression(convnet,
                         optimizer='adam',
                         learning_rate=LR,
                         loss='categorical_crossentropy',
                         name='targets')

    model = tflearn.DNN(convnet, tensorboard_dir='log')

    if os.path.exists('{}.meta'.format(MODEL_NAME)):
        model.load(MODEL_NAME)
        print('model loaded!')

    import matplotlib.pyplot as plt
    fig = plt.figure()
    message.grid_forget()
    disease.grid_forget()
    global fn1
    #for num, data in enumerate(verify_data):
    print(fn1)
    #img_num = data[1]
    #img_data = data[0]
    d_type = label[label['image'] == fn1]['level'].values
    #y = fig.add_subplot(3, 4, num + 1)
    #orig = img_data
    #data = img_data.reshape(IMG_SIZE, IMG_SIZE, 3)
    # model_out = model.predict([data])[0]
    #model_out = model.predict([data])[0]
    model_out = d_type[0]

    if model_out == 0:
        str_label = '0-class'
    elif model_out == 1:
        str_label = '1-class'
    elif model_out == 2:
        str_label = '2-class'
    elif model_out == 3:
        str_label = '3-class'
    if model_out == 4:
        str_label = "4-class"

    #message = tk.Label(text='Status: '+status, background="lightgreen",fg="Brown", font=("", 15))
    #message.grid(column=0, row=3, padx=10, pady=10)
    messagebox.showinfo("probable disease", str_label)
image = picamera.array.PiRGBArray(cap, size=(640, 480))

convnet = input_data(shape=[None, 224, 224, 3], name='input')
convnet = conv_2d(convnet, 32, 5, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 32, 5, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 5, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 2 * 1024, activation='relu')
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 8, activation='softmax')
convnet = regression(convnet,
                     optimizer='sgd',
                     learning_rate=0.01,
                     loss='categorical_crossentropy',
                     name='target')
model = tflearn.DNN(convnet)

model.load('Model.model')

for img in cap.capture_continuous(image, format="bgr", use_video_port=True):
    frame = img.array
    frame_ = cv2.resize(frame, (224, 224))
    test = np.reshape(frame_, [1, 224, 224, 3])
    #test = np.zeros([1,224,224,3])
    t = time.time()
    pred = model.predict(test)
    print(pred)
    act = command.get(np.argmax(pred))
コード例 #43
0
def create_googlenet(num_classes):
    # Building 'GoogleNet'
    network = input_data(shape=[None, 227, 227, 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')

    # 3a
    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, name='inception_3a_pool')
    inception_3a_pool_1_1 = conv_2d(inception_3a_pool, 32, filter_size=1, activation='relu', name='inception_3a_pool_1_1')
    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)

    # 3b
    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')
    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')

    # 4a
    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')

    # 4b
    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')

    # 4c
    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')

    # 4d
    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')

    # 4e
    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')

    # 5a
    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')

    # 5b
    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)

    #fc
    loss = fully_connected(pool5_7_7, num_classes, activation='softmax')
    network = regression(loss, optimizer='momentum',
                     loss='categorical_crossentropy',
                     learning_rate=0.01)

    return network
コード例 #44
0
def get_network():
    biases = zeros(shape=[9, 19, 1, 256])
    biases2 = zeros(shape=[19, 19, 1])
    network = input_data(shape=[None, 19, 19, 2], name='input')
    network = conv_2d(network,
                      256,
                      5,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev5),
                      bias=False) + biases[0]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[1]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[2]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[3]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[4]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[5]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[6]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[7]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      256,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases[8]
    network = local_response_normalization(network)
    network = conv_2d(network,
                      1,
                      3,
                      activation='elu',
                      regularizer="L2",
                      weights_init=truncated_normal(stddev=stddev3),
                      bias=False) + biases2
    network = fully_connected(network, 19 * 19, activation='softmax')
    momentum = Momentum(learning_rate=0.002)
    network = regression(network,
                         optimizer=momentum,
                         loss='categorical_crossentropy',
                         name='target')
    return network
コード例 #45
0
output = fully_connected(output, n1, activation="relu")

# output = fully_connected(output, n2, activation="relu")
#
# output = fully_connected(output, n3, activation="relu")
#
# output = fully_connected(output, n4, activation="relu")
#
# output = fully_connected(output, n5, activation="relu")

output = fully_connected(output, classes, activation="softmax")

output = regression(output,
                    optimizer="adam",
                    learning_rate=learning_rate,
                    loss="categorical_crossentropy",
                    name="targets")

model = tflearn.DNN(output)

print("Model Fitting:")
model.fit({"input": feature_vector_train}, {"targets": labels_train},
          n_epoch=num_epochs,
          validation_set=({
              "input": feature_vector_test
          }, {
              "targets": labels_test
          }),
          snapshot_step=500,
          show_metric=True,
コード例 #46
0
# Building convolutional network
network = input_data(shape=[None, 28, 28, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 10, activation='softmax')
network = regression(network,
                     optimizer='adam',
                     learning_rate=0.01,
                     loss='categorical_crossentropy',
                     name='target')

#model complile
model = tflearn.DNN(network, tensorboard_verbose=0)

#model fitting
model.fit({'input': trainx}, {'target': trainy},
          n_epoch=20,
          validation_set=({
              'input': testx
          }, {
              'target': testy
          }),
          snapshot_step=100,
コード例 #47
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))
コード例 #48
0
    if in_args.architecture == 'VGG16':

        if not _is_valid_size(224, in_args.patch_size, in_args.extract_level):
            raise RuntimeError("Real size of extracted patch less than network input size!")

        img_prep.add_random_crop((224, 224))
        cnn_network = input_data(shape=[None, 224, 224, 3],
                                 data_preprocessing=img_prep,
                                 data_augmentation=img_aug)

        cnn_network = create_vgg16_network( cnn_network, num_classes=n_classes,
                                            normalize_batch=apply_batchnorm,
                                            add_color_transfer=apply_colortransform)

        MomOpt = Momentum(learning_rate=0.01, lr_decay=0.8, decay_step=200)
        cnn_network = regression(cnn_network, optimizer=MomOpt,
                                 loss='categorical_crossentropy')

    elif in_args.architecture == 'simple':

        if not _is_valid_size(28, in_args.patch_size, in_args.extract_level):
            raise RuntimeError("Real size of extracted patch less than network input size!")

        img_prep.add_random_crop((28, 28))
        cnn_network = input_data(shape=[None, 28, 28, 3],
                                 data_preprocessing=img_prep,
                                 data_augmentation=img_aug)

        cnn_network = create_simple_network( cnn_network, num_classes=n_classes)

        cnn_network = network = regression(cnn_network, optimizer='adam',
                                           loss='categorical_crossentropy',
コード例 #49
0
ファイル: image_CNN.py プロジェクト: punit-gupta/ml
#third Convolution Layer
model = conv_2d(model, 38, 5, activation='relu')
model = max_pool_2d(model, 5)

#fully Connected Layer
model = fully_connected(model, 1024, activation='relu')
model = dropout(model, 0.7)

model = fully_connected(model, 2, activation='softmax')

# In[9]:

model = regression(model,
                   optimizer='adam',
                   loss='categorical_crossentropy',
                   learning_rate=0.003,
                   name='output')

# In[10]:

cnnmodel = tflearn.DNN(model)

# In[11]:

train = train_data[:-500]  #24500 for training
test = train_data[-500:]  #500 for testing

# In[12]:

X = np.array([i[0] for i in train]).reshape(-1, 50, 50, 1)
コード例 #50
0
import tflearn.datasets.oxflower17 as oxflower17
X, Y = oxflower17.load_data(one_hot=True, resize_pics=(227, 227))

# Building 'AlexNet'
network = input_data(shape=[None, 227, 227, 3])
network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 17, activation='softmax')
network = regression(network, optimizer='momentum',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Training
model = tflearn.DNN(network, checkpoint_path='model_alexnet',
                    max_checkpoints=1, tensorboard_verbose=2)
model.fit(X, Y, n_epoch=1000, validation_set=0.1, shuffle=True,
          show_metric=True, batch_size=64, snapshot_step=200,
          snapshot_epoch=False, run_id='alexnet_oxflowers17')
コード例 #51
0
ファイル: Analyse.py プロジェクト: RevanthVNR/leaf
def analysis():
    import cv2
    import numpy as np
    import os
    from random import shuffle
    from tqdm import \
        tqdm
    verify_dir = 'testpicture'
    IMG_SIZE = 50
    LR = 1e-3
    MODEL_NAME = 'healthyvsunhealthy-{}-{}.model'.format(LR, '2conv-basic')

    def process_verify_data():
        verifying_data = []
        for img in tqdm(os.listdir(verify_dir)):
            path = os.path.join(verify_dir, img)
            img_num = img.split('.')[0]
            img = cv2.imread(path, cv2.IMREAD_COLOR)
            img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
            verifying_data.append([np.array(img), img_num])
        np.save('verify_data.npy', verifying_data)
        return verifying_data

    verify_data = process_verify_data()

    import tflearn
    from tflearn.layers.conv import conv_2d, max_pool_2d
    from tflearn.layers.core import input_data, dropout, fully_connected
    from tflearn.layers.estimator import regression
    import tensorflow as tf
    tf.reset_default_graph()

    convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')

    convnet = conv_2d(convnet, 32, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 64, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 128, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 32, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

    convnet = conv_2d(convnet, 64, 3, activation='relu')
    convnet = max_pool_2d(convnet, 3)

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

    convnet = fully_connected(convnet, 4, activation='softmax')
    convnet = regression(convnet,
                         optimizer='adam',
                         learning_rate=LR,
                         loss='categorical_crossentropy',
                         name='targets')

    model = tflearn.DNN(convnet, tensorboard_dir='log')

    if os.path.exists('{}.meta'.format(MODEL_NAME)):
        model.load(MODEL_NAME)
        print('model loaded!')

    import matplotlib.pyplot as plt

    fig = plt.figure()

    for num, data in enumerate(verify_data):

        img_num = data[1]
        img_data = data[0]

        y = fig.add_subplot(3, 4, num + 1)
        orig = img_data
        data = img_data.reshape(IMG_SIZE, IMG_SIZE, 3)
        # model_out = model.predict([data])[0]
        model_out = model.predict([data])[0]

        if np.argmax(model_out) == 0:
            str_label = 'healthy'
        elif np.argmax(model_out) == 1:
            str_label = 'bacterial'
        elif np.argmax(model_out) == 2:
            str_label = 'viral'
        elif np.argmax(model_out) == 3:
            str_label = 'lateblight'

        if str_label == 'healthy':
            status = "HEALTHY"
        else:
            status = "UNHEALTHY"

        message = tk.Label(text='Status: ' + status,
                           background="lightgreen",
                           fg="Brown",
                           font=("", 15))
        message.grid(column=0, row=3, padx=10, pady=10)
        if str_label == 'bacterial':
            diseasename = "Bacterial Spot "
            disease = tk.Label(text='Disease Name: ' + diseasename,
                               background="lightgreen",
                               fg="Black",
                               font=("", 15))
            disease.grid(column=0, row=4, padx=10, pady=10)
            r = tk.Label(text='Click below for remedies...',
                         background="lightgreen",
                         fg="Brown",
                         font=("", 15))
            r.grid(column=0, row=5, padx=10, pady=10)
            button3 = tk.Button(text="Remedies", command=bact)
            button3.grid(column=0, row=6, padx=10, pady=10)
        elif str_label == 'viral':
            diseasename = "Yellow leaf curl virus "
            disease = tk.Label(text='Disease Name: ' + diseasename,
                               background="lightgreen",
                               fg="Black",
                               font=("", 15))
            disease.grid(column=0, row=4, padx=10, pady=10)
            r = tk.Label(text='Click below for remedies...',
                         background="lightgreen",
                         fg="Brown",
                         font=("", 15))
            r.grid(column=0, row=5, padx=10, pady=10)
            button3 = tk.Button(text="Remedies", command=vir)
            button3.grid(column=0, row=6, padx=10, pady=10)
        elif str_label == 'lateblight':
            diseasename = "Late Blight "
            disease = tk.Label(text='Disease Name: ' + diseasename,
                               background="lightgreen",
                               fg="Black",
                               font=("", 15))
            disease.grid(column=0, row=4, padx=10, pady=10)
            r = tk.Label(text='Click below for remedies...',
                         background="lightgreen",
                         fg="Brown",
                         font=("", 15))
            r.grid(column=0, row=5, padx=10, pady=10)
            button3 = tk.Button(text="Remedies", command=latebl)
            button3.grid(column=0, row=6, padx=10, pady=10)
        else:
            r = tk.Label(text='Plant is healthy',
                         background="lightgreen",
                         fg="Black",
                         font=("", 15))
            r.grid(column=0, row=4, padx=10, pady=10)
            button = tk.Button(text="Exit", command=exit)
            button.grid(column=0, row=9, padx=20, pady=20)
コード例 #52
0
def construct_inceptionv1onfire(x, y):

    # from Dunnings/Breckon research paper 2018

    network = input_data(shape=[None, y, x, 3])

    conv1_7_7 = conv_2d(network,
                        64,
                        5,
                        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,
                        128,
                        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')

    pool5_7_7 = avg_pool_2d(inception_4a_output, kernel_size=5, strides=1)
    pool5_7_7 = dropout(pool5_7_7, 0.4)
    loss = fully_connected(pool5_7_7, 2, activation='softmax')
    network = regression(loss,
                         optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)
    model = tflearn.DNN(network,
                        checkpoint_path='sp-inceptiononv1onfire',
                        max_checkpoints=1,
                        tensorboard_verbose=2)

    return model
コード例 #53
0
ファイル: 3d_self.py プロジェクト: xinYyuan/3d-Self-coding
def self(x_train, y_train, x_test, y_test):
    int_put = input_data(shape=[None, 224, 5, 5, 1], )

    conv1 = conv_3d(
        int_put,
        24,
        [24, 3, 3],
        padding='VALID',
        strides=[1, 1, 1, 1, 1],
        activation='prelu',
    )
    print('conv1', conv1.get_shape().as_list())
    batch_norm = batch_normalization(conv1)

    conv2 = conv_3d(
        batch_norm,
        12,
        [24, 3, 3],
        padding='VALID',
        strides=[1, 1, 1, 1, 1],
        activation='prelu',
    )
    print('conv2', conv2.get_shape().as_list())
    batch_norm_con = batch_normalization(conv2)

    decon2 = conv_3d_transpose(batch_norm_con,
                               24, [24, 3, 3],
                               padding='VALID',
                               output_shape=[201, 3, 3, 24])
    batch_norm = batch_normalization(decon2)
    print('a')
    decon2 = conv_3d_transpose(batch_norm,
                               1, [24, 3, 3],
                               padding='VALID',
                               output_shape=[224, 5, 5, 1])
    batch_norm = batch_normalization(decon2)

    network = regression(batch_norm,
                         optimizer='Adagrad',
                         loss='mean_square',
                         learning_rate=0.01,
                         metric='R2')
    model = tflearn.DNN(network,
                        tensorboard_verbose=0,
                        tensorboard_dir="./tflearn_logs/")

    for i in range(10):
        model.fit(x_train,
                  x_train,
                  n_epoch=20,
                  shuffle=True,
                  show_metric=True,
                  validation_set=(x_test, x_test),
                  batch_size=32,
                  run_id='3d_net_self')
        x_pre = model.predict(x_train)
        x_pre = np.array(x_pre)
        x_true = np.array(x_train)
        psnr(x_true, x_pre)

    model.save('my_model_self.tflearn')
    '''
コード例 #54
0
ファイル: create_model.py プロジェクト: Coloriser/tf-model-3
def make_model(x, y):

    print("X :", x.shape)
    print("Y :", y.shape)

    # Building convolutional network
    network = input_data(shape=[None, x.shape[1], x.shape[2], 1], name='input')

    #1
    # network = conv_2d(network, 128, activation='sigmoid', regularizer="L2")
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    print(network)
    #2
    # network = conv_2d(network, 128, activation='sigmoid', regularizer="L2")
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    print(network)
    #3
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    print(network)
    #4
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    #5
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    #6
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    #7
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    #8
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    #9
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)
    #10
    network = fully_connected(network, 128, activation='sigmoid')
    network = dropout(network, 0.8)

    network = fully_connected(network, y.shape[1], activation='sigmoid')
    network = regression(network,
                         optimizer='adam',
                         learning_rate=0.01,
                         loss='categorical_crossentropy',
                         name='target')

    # Training
    model = tflearn.DNN(
        network,
        checkpoint_path="./model/intermediate_models/model.ckpt",
        max_checkpoints=1,
        tensorboard_verbose=0)
    model.fit(
        {'input': x},
        {'target': y},
        n_epoch=100,
        batch_size=10,
        show_metric=True,
        snapshot_epoch=True,
    )

    return model
コード例 #55
0
ファイル: 3d_self.py プロジェクト: xinYyuan/3d-Self-coding
def test1(x_train, y_train, x_test, y_test):
    # Train using classifier

    #define network
    int_put = input_data(shape=[None, 224, 5, 5, 1], )

    conv1 = conv_3d(int_put,
                    24, [24, 3, 3],
                    padding='VALID',
                    strides=[1, 1, 1, 1, 1],
                    activation='prelu',
                    weight_decay=0.05)
    print('conv1', conv1.get_shape().as_list())
    batch_norm = batch_normalization(conv1)
    #act1=tflearn.activations.relu(batch_norm)
    #pool1=max_pool_3d(act1,[1,1,2,2,1],strides=[1,1,1,1,1])

    conv2 = conv_3d(batch_norm,
                    12, [24, 3, 3],
                    padding='VALID',
                    strides=[1, 1, 1, 1, 1],
                    activation='prelu',
                    weight_decay=0.05)
    print('conv2', conv2.get_shape().as_list())
    batch_norm = batch_normalization(conv2)
    #act = tflearn.activations.relu(batch_norm)
    #pool2=max_pool_3d(act,[1,1,2,2,1],strides=[1,1,1,1,1])

    net = residual_block_concat(batch_norm,
                                2,
                                16,
                                batch_norm=None,
                                downsample_strides=1,
                                weight_decay=0.05)
    #net = residual_block(net, 5, 16)
    #net = residual_block(net, 1, 32, )
    #net = residual_block(net, 4, 32)
    #net = residual_block(net, 1, 64, downsample=True)
    #net = residual_block(net, 2, 64)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, 'relu')
    '''
    conv3=conv_3d(batch_norm,24,[24,1,1],padding='VALID',strides=[1,5,1,1,1],activation='prelu')
    print('conv3', conv3.get_shape().as_list())
    batch_norm = batch_normalization(conv3)
    #act=tflearn.activations.relu(batch_norm)
    #pool3=max_pool_3d(act,[1,1,2,2,1],strides=[1,1,1,1,1])
    '''

    flat = flatten(net)
    print('flat', flat.get_shape().as_list())
    ip1 = fully_connected(
        flat,
        100,
        activation='prelu',
    )
    dro = dropout(ip1, 0.9)
    ip2 = fully_connected(
        dro,
        20,
        activation='softmax',
    )
    network = regression(ip2,
                         optimizer='Adagrad',
                         loss='categorical_crossentropy',
                         learning_rate=0.01)

    model = tflearn.DNN(network,
                        tensorboard_verbose=0,
                        tensorboard_dir="./tflearn_logs/")
    model.fit(x_train,
              y_train,
              n_epoch=200,
              shuffle=True,
              validation_set=(x_test, y_test),
              show_metric=True,
              batch_size=32,
              run_id='3d_net')
コード例 #56
0
fc_1 = fully_connected(merging,
                       600,
                       activation='leakyrelu',
                       weights_init="xavier",
                       name='fully1')
drop_2 = dropout(fc_1, 0.8)
fc_2 = fully_connected(drop_2,
                       300,
                       activation='leakyrelu',
                       weights_init="xavier",
                       name='fully2')
drop_3 = dropout(fc_2, 0.8)
linear = fully_connected(drop_3, 1, activation='linear', name='fully3')
reg = regression(linear,
                 optimizer='adam',
                 learning_rate=0.001,
                 loss='mean_square',
                 name='target')

# Training
model = tflearn.DNN(reg,
                    tensorboard_verbose=0,
                    tensorboard_dir='./mytensor/',
                    checkpoint_path="./checkpoints/")

######### Setting weights

model.set_weights(prot_embd_W[0], prot_embd_init)
model.set_weights(prot_gru_1_gate_matrix[0], prot_gru_1_gates_kernel_init)
model.set_weights(prot_gru_1_gate_bias[0], prot_gru_1_gates_bias_init)
model.set_weights(prot_gru_1_candidate_matrix[0],
コード例 #57
0
test_img = np.asarray(test_img, dtype=np.int64)

CNN = input_data(shape=[None, 224, 224, 3], name="input_x")

CNN = conv_2d(CNN, 32, 7, activation='relu', regularizer="L2")
CNN = avg_pool_2d(CNN, 2)
CNN = dropout(CNN, keep_prob=0.5)

CNN = conv_2d(CNN, 45, 5, activation='relu', regularizer="L2")
CNN = avg_pool_2d(CNN, 2)
CNN = dropout(CNN, keep_prob=0.5)

CNN = conv_2d(CNN, 10, 2, activation='relu', regularizer='L2')
CNN = avg_pool_2d(CNN, 2)

fl = fully_connected(CNN, 1, activation='softmax')
output = regression(fl,
                    learning_rate=0.0005,
                    loss='binary_crossentropy',
                    name='targets')

model = tflearn.DNN(output,
                    tensorboard_verbose=0,
                    tensorboard_dir='./walk_run',
                    checkpoint_path='./walk_run/checkpoint')
model.fit({'input_x': train_img}, {'targets': train_label},
          show_metric=True,
          n_epoch=20,
          batch_size=600)
model.evaluate({'input_x': test_img}, {'targets': test_label})
コード例 #58
0
def inceptionv3(width, height, frame_count, lr, output=9, model_name = 'inceptionv3.model'):
    network = input_data(shape=[None, width, height,3], name='input')
    conv1_7_7 = conv_2d(network, 64, 28, strides=4, activation='relu', name = 'conv1_7_7_s2')
    pool1_3_3 = max_pool_2d(conv1_7_7, 9,strides=4)
    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,12, 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=12, 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=12,  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=15, activation='relu', name= 'inception_3a_5_5')
    inception_3a_pool = max_pool_2d(pool2_3_3, kernel_size=12, 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=9,  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=15,  name = 'inception_3b_5_5')
    inception_3b_pool = max_pool_2d(inception_3a_output, kernel_size=12, 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.45)

    loss = fully_connected(pool5_7_7, output,activation='softmax')

    network = regression(loss, optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=lr, name='targets')

    model = tflearn.DNN(network,
                        max_checkpoints=0, tensorboard_verbose=0,tensorboard_dir='log')

    return model