Пример #1
0
def dsd(config):
    model = dict()

    model = cnn.cnn(model, config, 'cnn')
    model = rnn.rnn(model, config, 'rnn', 'cnn')
    model = ctc.ctc(model, config, 'ctc', 'rnn')
    model = fnn.fnn(model, config, 'fnns', 'rnn')
    model = ctc.ctc(model, config, 'ctc', 'fnns')
    model = fnn.fnn(model, config, 'fnnd', 'rnn')
    model = ces.ces(model, config, 'ces', 'fnnd')
    model = dia.dia(model, config, 'dia', 'fnnd', 'rnn')
    model = ced.ced(model, config, 'ced', 'dia')

    model['loss'] = model['ctc_loss']
    model['step'] = tf.Variable(0, trainable=False, name='step')
    model['lrate'] = tf.train.exponential_decay(
        config.getfloat('global', 'lrate'),
        model['step'],
        config.getint('global', 'dstep'),
        config.getfloat('global', 'drate'),
        staircase=False,
        name='lrate')
    model['optim'] = getattr(tf.train, config.get('global', 'optim'))(
        model['lrate']).minimize(model['loss'],
                                 global_step=model['step'],
                                 name='optim')

    return model
Пример #2
0
    def __init__(self):
        self.conf_ = conf()

        pre_processor_ = pre_processor()
        train_x, train_y, val_x, val_y = pre_processor_.get_data()

        # Build Computation Graph
        self.cnn = cnn()
        self.cnn.forward_prop()
        self.cnn.compute_loss()
        self.cnn.compute_acc()
        self.cnn.back_prop()

        # initialize tensors
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())
        self.sess.run(tf.local_variables_initializer())

        logging.info("training...")
        self.saver = tf.train.Saver(max_to_keep=1)

        for epoch in range(self.cnn.epochs):

            iters = int(len(train_x) / self.cnn.batch_size)

            for iter_ in range(iters):

                # extract batch data
                batch_x = train_x[iter_ * self.cnn.batch_size:(iter_ + 1) *
                                  self.cnn.batch_size, :]
                batch_y = train_y[iter_ * self.cnn.batch_size:(iter_ + 1) *
                                  self.cnn.batch_size, :]

                # train
                self.sess.run(self.cnn.tf_train,
                              feed_dict={
                                  self.cnn.tf_X: batch_x,
                                  self.cnn.tf_y: batch_y
                              })

            # train loss
            _, loss_train, acc_train = self.cnn.analyze_epoch(
                train_x, train_y, self.sess)

            # val loss
            _, loss_val, acc_val = self.cnn.analyze_epoch(
                val_x, val_y, self.sess)

            # save model
            _ = self.saver.save(self.sess,
                                "../models/sess",
                                global_step=int(epoch + 1))

            # log the performance at each epoch
            logging.info(
                "epoch: {0}, loss_train: {1}, acc_train: {2}, loss_val: {3}, acc_val: {4}"
                .format(epoch + 1, loss_train, acc_train, loss_val, acc_val))

        self.sess.close()
 def __init__(self):
     self.win = tk.Tk()
     self.button_dict = {}
     self.actions_list = ['*', '/', '+', '-', '.', '=']
     self.EQUAL_SIGN = '='
     self.DOT_SIGN = '.'
     self.recent_action = "n"
     self.mySVM = learner.Learner()
     self.my_cnn = cnn.cnn()
Пример #4
0
def main():

    nb_epochs = 50
    nb_train_samples = 4000 + 3840  #3840 pedestrian - 4000 notPedestrian
    nb_validation_samples = 1000 + 960  #960 pedestrian/ 1000 notPedestrian
    images_dim = 18, 36  # heigths = 36, width = 18

    model = cnn(images_dim)
    train(model, nb_epochs, nb_train_samples, nb_validation_samples,
          images_dim)
Пример #5
0
def ds2(config):
	model = dict()

	model = cnn.cnn(model, config, 'cnn')
	model = rnn.rnn(model, config, 'rnn', 'cnn')
	model = fnn.fnn(model, config, 'fnn', 'rnn')
	model = ctc.ctc(model, config, 'ctc', 'fnn')

	model['loss'] = model['ctc_loss']
	model['step'] = tf.Variable(0, trainable = False, name = 'step')
	model['lrate'] = tf.train.exponential_decay(config.getfloat('global', 'lrate'), model['step'], config.getint('global', 'dstep'), config.getfloat('global', 'drate'), staircase = False, name = 'lrate')
	model['optim'] = getattr(tf.train, config.get('global', 'optim'))(model['lrate']).minimize(model['loss'], global_step = model['step'], name = 'optim')

	return model
Пример #6
0
def run_cnn():
    cnn_obj = cnn(data_path)

    # Flag makes it run with new simplified code and does not run validation accuracy for quicker response
    legacy_run = False
    training = True
    ''' WE NEED THIS FOR LOOKING AT HEAT MAP OVER IMAGE'''
    single_layer_fnn = True

    ## GET INPUT  DATA
    #    input_data = nn_utilities_obj.prepare_digits_image_inputs()
    #    input_data = nn_utilities_obj.load_mnist_digit_data()
    #    input_data = nn_utilities_obj.load_emnist_alphadigit_data()
    #    input_data = nn_utilities_obj.load_emnist_letters_data()

    #    input_data = nn_utilities_obj.load_fashion_data()
    input_data = nn_utilities_obj.load_PneumothoraxDataset()

    ## Override the default learning rate
    cnn_obj.learning_rate_var = 0.0001

    ## 2 LAYER FNN INPUTS
    hiddenlayer_1_width = 500
    hiddenlayer_2_width = 500

    ## Assuming it's a SQUARE IMAGE
    image_height = int(np.sqrt(input_data["x_train"].shape[1]))
    image_width = image_height

    if legacy_run == True:
        ## CREATE CNN & DNN MODEL
        optimizer, cost, accuracy, cnn_fnn_model = cnn_obj.create_model(
            [image_height, image_width], hiddenlayer_1_width,
            hiddenlayer_2_width, input_data["y_train"].shape[1],
            single_layer_fnn)
    else:
        ## CREATE CNN & DNN MODEL
        optimizer, cost, accuracy, cnn_fnn_model = cnn_obj.create_simplified_model(
            [image_height, image_width], hiddenlayer_1_width,
            hiddenlayer_2_width, input_data["y_train"].shape[1],
            single_layer_fnn)

    ## TRAIN THE MODEL AND TEST PREDICTION
    run_nn(cnn_obj, input_data, optimizer, cost, accuracy, cnn_fnn_model,
           "cnn\\" + input_data["name"] + "\\" + input_data["name"], True,
           training)
    def __init__(self,
                 data,
                 epochs,
                 batch_size,
                 training_ratio,
                 sequance_length,
                 lstmCells=10,
                 LSTMDL1units=20,
                 LSTMDL2units=5,
                 LSTMDL3units=1,
                 CL1filters=1,
                 CL1kernal_size=2,
                 CL1strides=1,
                 PL1pool_size=1,
                 CNNDL1units=20,
                 CNNDL2units=5,
                 CNNDL3units=1,
                 lstmWeight=0.5,
                 cnnWeight=0.5,
                 learningRate=0.001):

        self.lstm_model = lstm.lstm(data=data,
                                    epochs=epochs,
                                    batch_size=batch_size,
                                    training_ratio=training_ratio,
                                    sequance_length=sequance_length,
                                    lstmCells=lstmCells,
                                    learningRate=learningRate)

        self.cnn_model = cnn.cnn(data=data,
                                 epochs=epochs,
                                 batch_size=batch_size,
                                 training_ratio=training_ratio,
                                 sequance_length=sequance_length,
                                 CL1filters=CL1filters,
                                 CL1kernal_size=CL1kernal_size,
                                 CL1strides=CL1strides,
                                 PL1pool_size=PL1pool_size,
                                 DL1units=CNNDL1units,
                                 DL2units=CNNDL2units,
                                 DL3units=CNNDL3units,
                                 learningRate=learningRate)

        self.lstmWeight = lstmWeight
        self.cnnWeight = cnnWeight
Пример #8
0
    def get_model(self, model_type, parameters):
        if model_type == "multi_arma":
            return arma(parameters=parameters)

        elif model_type == "multi_arima":
            return arima(parameters=parameters)

        elif model_type == "multi_lstm":
            return lstm(parameters=parameters)

        elif model_type == "multi_cnn":
            return cnn(parameters=parameters)

        elif model_type == "multi_lstmcnn":
            return lstmcnn(parameters=parameters)

        elif model_type == "multi_lstmcnn_kerascombinantion":
            return lstmcnn_kerascombinantion(parameters=parameters)
Пример #9
0
def main():
    print("Opening data & labels")
    train_set, train_labels = ld.get_train_set(img_width=128, img_height=128)
    test_set, test_labels = ld.get_test_set(img_width=128, img_height=128)
    test_set = reshape(test_set)
    test_labels = reshape(test_labels)
    train_labels = reshape(train_labels)
    train_set = reshape(train_set)
    print("Open Model")
    epochs = 10
    batch_size = 16
    weights = "model.hdf5"
    if os.path.exists(weights):
        my_cnn = load_model(weights)
    else:
        my_cnn = cnn.cnn(img_width=128, img_height=128)
    if train:
        # checkpoint
        filepath = "out/weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
        checkpoint = ModelCheckpoint(filepath,
                                     monitor='val_acc',
                                     verbose=1,
                                     save_best_only=True,
                                     mode='max')
        callbacks_list = [checkpoint]
        # train
        history = my_cnn.fit(
            x=train_set,  # Input should be (train_cases, 128, 128, 1)
            y=train_labels,
            batch_size=batch_size,
            epochs=epochs,
            verbose=2,
            callbacks=callbacks_list,
            validation_data=(test_set, test_labels))
        cnn.plot_val_acc(history=history)
    # Confusion Matrix
    Y_pred = my_cnn.predict_classes(test_set)
    # Y_pred_classes = np.argmax(Y_pred, axis=1)
    # import pdb; pdb.set_trace()
    confusion_mtx = confusion_matrix(test_labels, Y_pred)
    cnn.plot_confusion_matrix(confusion_mtx,
                              classes=list(dict_characters.values()))
    import pdb
    pdb.set_trace()
Пример #10
0
def main():
	name = sys.argv[1]
	batch_size = sys.argv[2]
	end_epoch = sys.argv[3]
	hidden_units = sys.argv[4]
	hidden_layers = sys.argv[5]
	Sampling = sys.argv[6]
	X_train_path = sys.argv[7]
	Y_train_path = sys.argv[8]
	eval_path = sys.argv[9]
	if str(name) == 'sln':
		feed_forward = sln.ff(name,batch_size,hidden_units,1,end_epoch)
		feed_forward.run(Sampling,X_train_path,Y_train_path, eval_path)
	elif name == 'cnn':
		cnn_net = cn.cnn(name,batch_size,hidden_units,1,end_epoch)
		cnn_net.run(Sampling,X_train_path,Y_train_path, eval_path)
	elif name == 'mln':
		feed_forward = mln.ff(name,batch_size,hidden_units,hidden_layers,end_epoch)
		feed_forward.run(Sampling,X_train_path,Y_train_path, eval_path)
Пример #11
0
    def __init__(self, parameters):
        lstm_parmeters = {
            # for all
            "data":parameters["data"],
            "training_ratio":parameters["training_ratio"],
            "no_of_prediction_points":parameters["no_of_prediction_points"],

            # for LSTM 
            "epochs":parameters["epochs"], 
            "batch_size":parameters["batch_size"],
            "sequance_length":parameters["sequance_length"],
            "DL1units":parameters["LSTMDL1units"], 
            "DL2units":parameters["LSTMDL2units"], 
            "DL3units":parameters["LSTMDL3units"],
            "lstmCells":parameters["lstmCells"],
        }
        self.lstm_model = lstm(lstm_parmeters)
        
        cnn_parmeters = {
            # for all
            "data":parameters["data"],
            "training_ratio":parameters["training_ratio"],
            "no_of_prediction_points":parameters["no_of_prediction_points"],

            # for LSTM 
            "epochs":parameters["epochs"], 
            "batch_size":parameters["batch_size"],
            "sequance_length":parameters["sequance_length"],
            "DL1units":parameters["CNNDL1units"], 
            "DL2units":parameters["CNNDL2units"], 
            "DL3units":parameters["CNNDL3units"],

            "CL1filters":parameters["CL1filters"], 
            "CL1kernal_size":parameters["CL1kernal_size"], 
            "CL1strides":parameters["CL1strides"], 
            "PL1pool_size":parameters["PL1pool_size"], 
        }
        self.cnn_model = cnn(cnn_parmeters)

        self.lstmWeight = parameters["lstmWeight"]
        self.cnnWeight = parameters["cnnWeight"]
        self.no_of_prediction_points = parameters["no_of_prediction_points"]
Пример #12
0
def main():

	for line in open('dataset_www2018_rand.txt'):
		line = line.strip().split('\t')
		hashtag = line[0]
		active_period = int(line[1])
		htg_acpd_dict[hashtag] = active_period


	hashtag_trainingset = get_trainingset_hashtag()
	print '!!!\nhashtags of the trainingset have been picked out.\n!!!'
	
	target = tf.placeholder(tf.float32, [None, num_classes])

	lstm_data = tf.placeholder(tf.float32, [None, lstm_max_length, num_lstm_features])	
	lstm_sequence_length_vector = tf.placeholder(tf.int32, [None])
	lstm_dropout = tf.placeholder(tf.float32)
	lstm_output = lstm.lstm(lstm_data, lstm_sequence_length_vector, lstm_dropout)


	cnn_data = tf.placeholder(tf.float32, [None, None])
	reshape_cnn_data = tf.reshape(data,[-1,1,100,1])
	cnn_output = cnn.cnn(reshape_cnn_data)

	#getcountlengthembeddings part
	length_and_count
	length_and_count_embeddings = get_length_and_count_embeddings()

	prediction_output = overall_prediction(lstm_output, cnn_output, length_and_count_embeddings)
	cost_output = cost(prediction_output,target)
	optimizer_output = optimizer(cost)

	sess = tf.Session()
	sess.run(tf.global_variables_initializer())



	for epoch in range(num_epochs):
		np.random.shuffle(hashtag_trainingset)
		mse_sum = 0
		for i in range(num_iterations):
Пример #13
0
Файл: rri.py Проект: jzbjyb/rri
 def get_doc_repr():
     nonlocal d_region, max_jump_offset, word_vector_dim, separate, d_offset, doc_arch, doc_after_pool_size
     d_region = tf.pad(
         d_region,
         [[0, 0], [0, max_jump_offset - tf.shape(d_region)[1]],
          [0, 0]],
         'CONSTANT',
         constant_values=0)
     d_region.set_shape([None, max_jump_offset, word_vector_dim])
     with vs.variable_scope('DocCNN' if separate else 'CNN'):
         doc_dpool_index = DynamicMaxPooling.dynamic_pooling_index_1d(
             d_offset, max_jump_offset)
         doc_repr = cnn(d_region,
                        architecture=doc_arch,
                        activation='relu',
                        dpool_index=doc_dpool_index)
     with vs.variable_scope('LengthOrderAwareMaskPooling'):
         mask_prob = tf.minimum(
             tf.ceil(doc_after_pool_size**2 / dq_size[:, 0]),
             doc_after_pool_size) / 50
         # length-aware mask
         mask_ber = tf.distributions.Bernoulli(probs=mask_prob)
         mask = tf.transpose(mask_ber.sample([doc_after_pool_size]),
                             [1, 0])
         # order-aware pooling
         #mask_for_zero = tf.cast(tf.expand_dims(tf.range(doc_after_pool_size), axis=0) < \
         #    (doc_after_pool_size - tf.reduce_sum(mask, axis=1, keep_dims=True)), dtype=tf.int32)
         #mask = tf.cast(tf.concat([mask, mask_for_zero], axis=1), dtype=tf.bool)
         #doc_repr = tf.boolean_mask(tf.concat([doc_repr, tf.zeros_like(doc_repr)], axis=1), mask)
         #doc_repr = tf.reshape(doc_repr, [bs, doc_after_pool_size, doc_arch[-2][-1]])
         # normal pooling
         doc_repr = doc_repr * tf.cast(
             tf.expand_dims(mask, axis=-1), dtype=tf.float32)
         # pooling
         doc_repr = tf.layers.max_pooling1d(doc_repr,
                                            pool_size=[5],
                                            strides=[5],
                                            padding='SAME',
                                            name='pool')
     return doc_repr
Пример #14
0
    def __init__(self):
        #TODO take in the settings file
        self.cnn_input_obj = cnn_input()
        self.cnn_obj = cnn()

        self.power_map_file = "./work/current_map_processed.csv"
        self.cong_map_file = "./output/congestion_map.csv"
        #congestion_map_file = "./work/congestion_processed.csv"
        self.settings_obj = T6_PSI_settings.load_obj()
        
        if (len(sys.argv)>1 and sys.argv[1] == "no_congestion"):
            self.congestion_enabled = 0 
        else: 
            self.congestion_enabled = 1 
        
        
        if self.congestion_enabled ==1:
            self.checkpoint_dir = self.settings_obj.checkpoint_dir
        else:
            self.checkpoint_dir = self.settings_obj.checkpoint_dir_wo_cong
        normalization_file = self.checkpoint_dir+self.settings_obj.normalization_file
        # Golden template numbers needed for comparison
        # This corresponds only to the first current map
        #indices = np.zeros((self.settings_obj.NUM_REGIONS_X * self.settings_obj.NUM_REGIONS_Y))
        self.test_size = self.settings_obj.NUM_REGIONS_X * self.settings_obj.NUM_REGIONS_Y
        # Hard coded after seeing training data, need to fix
        with open(normalization_file) as f:
            norm_data = json.load(f)
        min_cur = norm_data['currents']['min']
        max_cur = norm_data['currents']['max']
        self.scl_cur = 1 / (max_cur - min_cur)
        
        if self.congestion_enabled == 1:
            min_cong = norm_data['congestion']['min']
            max_cong = norm_data['congestion']['max']
            self.scl_cong = 1 / (max_cong - min_cong)
        
        self.template_map_file = "./output/template_map.txt"
Пример #15
0
def main(_):

    # ### Create the multimnist set.
    # create_multimnist(is_training=cfg.is_training)
    # trX, trY, num_tr_batch, valX, valY, num_val_batch = load_multimnist(is_training=cfg.is_training)
    # while True:
    #     index = np.random.randint(550000)
    #     print(trY[index])
    #     plt.imshow(np.squeeze(trX[index]))
    #     plt.show()
    # sys.exit()

    # The number of labels for the data.
    num_label = 10

    # Generate the model.
    tf.logging.info('Loading Graph...')
    if cfg.use_cnn == True:
        model = cnn()
    else:
        model = CapsNet()       

    tf.logging.info('Graph loaded')

    # Generate the supervisor.
    sv = tf.train.Supervisor(graph=model.graph, logdir=cfg.logdir, save_model_secs=0)
    
    # Run training or evaluation.
    if cfg.is_training:
        tf.logging.info('Start training')
        train(model, sv, num_label)
        tf.logging.info('Training done')
    else:
        tf.logging.info('Start evaluation')
        evaluation(model, sv, num_label)
        tf.logging.info('Evaluation done')
Пример #16
0
Файл: rri.py Проект: jzbjyb/rri
 def get_query_repr():
     nonlocal q_region, max_jump_offset, word_vector_dim, separate, q_offset, query_arch
     q_region = tf.pad(
         q_region,
         [[0, 0], [0, max_jump_offset - tf.shape(q_region)[1]],
          [0, 0]],
         'CONSTANT',
         constant_values=0)
     q_region.set_shape([None, max_jump_offset, word_vector_dim])
     with vs.variable_scope('QueryCNN' if separate else 'CNN'):
         if not separate:
             vs.get_variable_scope().reuse_variables()
         query_dpool_index = DynamicMaxPooling.dynamic_pooling_index_1d(
             q_offset, max_jump_offset)
         query_repr = cnn(q_region,
                          architecture=query_arch,
                          activation='relu',
                          dpool_index=query_dpool_index)
         query_repr = tf.layers.max_pooling1d(query_repr,
                                              pool_size=[10],
                                              strides=[10],
                                              padding='SAME',
                                              name='pool')
     return query_repr
Пример #17
0
	def __init__(self):
		
		# load network configuration
		self.conf_ = conf()

		# get test data
		pre_proc = pre_processor()
		test_x, test_y = pre_proc.get_data(testing=True)

		self.sess = tf.Session()

		# Build Computation Graph
		self.cnn = cnn()
		meta_filepath = "../models/sess-" + str(self.cnn.epochs) + ".meta"
		ckpt_filepath = "../models/sess-" + str(self.cnn.epochs)
		self.cnn.forward_prop(testing=True, sess=self.sess, meta_filepath=meta_filepath, ckpt_filepath=ckpt_filepath)
		self.cnn.compute_loss()
		self.cnn.compute_acc()

		# Initialize local variables only (not global variables)
		self.sess.run(tf.local_variables_initializer())

		# start testing
		self.start(test_x, test_y)
Пример #18
0
#     # result_path = '/home/jdwang/corprocessor/coprocessor/word2vec/20160325/output/20160329#2/20160326#1#2.csv'
#     # test_corpus[['seg_pos','pred']].to_csv(result_path,sep='\t')


if __name__ == '__main__':
    print 'load data...'
    corpus = load_train_data()
    print 'split data to fit and test set...'
    train_corpus,test_corpus = split_train_test(corpus,train_size=0.7)
    # save_train_test_corpus(train_corpus,test_corpus)

    print 'finished...'
    # print train_corpus.head()
    # print test_corpus.head()
    # print train_corpus.shape[0]
    # print test_corpus.shape[0]

    train_X = np.array([ item for item in train_corpus['vector'].as_matrix()])
    train_y = train_corpus['label'].as_matrix()
    # print train_X.shape
    # print train_y.dtype

    test_X = np.array([ item for item in test_corpus['vector'].as_matrix()])
    test_y = test_corpus['label'].as_matrix()

    # lr()
    # cnn_model.sgd_optimization(50, len(which_file), train_X, train_y,train_X, train_y)
    cnn.cnn(train_X, train_y, (train_X.shape[0], 1, 50, 1), (5, 1, 5, 1))


Пример #19
0
from load import dataset
from cnn import cnn

train_x, train_y = dataset(onehot = True)
cnn(train_x, train_y)
Пример #20
0
def web_scraper(input):

    key_words = input.split(',')
    keys = []
    #dates = []

    time = str(datetime.datetime.now().date())

    for key_word in key_words:
        try:
            while (key_word[0] == ' '):
                key_word = key_word[1:]
            while (key_word[len(key_word) - 1] == ' '):
                key_word = key_word[:-1]
        except IndexError:
            continue

        if (len(key_word) == 0):
            continue

        inputs = key_word.split(' ')
        key = ''
        for i in inputs:
            key += i
            key += '%20'
        key = key[:-3].lower()
        key = re.sub(r'[ ]+', ' ', key)
        keys.append(key)
        #dates.append(check.check_date(data_base, kkeys[i]))

    for i in range(0, len(keys)):

        previous_len = 0
        try:
            for lines in open("./documents/web/Articles.bank",
                              'r',
                              encoding='utf-8'):
                previous_len += 1
        except FileNotFoundError:
            pass

        stored_keys = check.load_keywords_info()
        data_base = check.load_url_info()
        data_print = {}

        kkey = fmt.file_name(keys[i], '_')

        for kk in stored_keys:
            if abs(len(kkey) - len(kk)) < 2 and check.MinEditDist(kkey,
                                                                  kk) == 1:
                kkey = kk

        if kkey in stored_keys:
            date = int(re.sub(r'-', '', stored_keys[kkey]))
        else:
            date = 0

        stored_keys[kkey] = time

        if kkey not in data_base:
            data_base[kkey] = []

        cnn.cnn(data_base, data_print, keys[i], date, previous_len)
        foxnews.foxnews(data_base, data_print, keys[i], date, previous_len)
        cbs.cbs(data_base, data_print, keys[i], date, previous_len)
        politico.politico(data_base, data_print, keys[i], date, previous_len)
        #washington_post.washington_post(data_base,data_print,key,date)

        if len(data_print) > 1:
            check.save_keywords_info(stored_keys)
            check.save_url_info(data_base)
        output_file.output(data_print, data_base, previous_len)

    print("Update date: " + time)
Пример #21
0
import cv2
import sys
import os
import cnn

for img_path in sys.argv[1:]:

    if (os.path.isfile(img_path) == False):
        try:
            raise FileNotFoundError('no such file')
        except FileNotFoundError:
            raise

    image = cv2.imread(img_path, 0)

    di_path = "./traind_model/"

    cnn_model = cnn.cnn(40, di_path + "model_40px")

    cnn.predict(image)
Пример #22
0
def main():
    if args.arch not in {'lstm', 'mdlstm', 'cnn'}:
        raise Exception(
            'not support arch type (should be one of "lstm", "mdlstm", "cnn").'
        )

    # config
    visualization = 'saliency'  # kernel, saliency
    learning_rate = 0.001 * 0.5
    anisotropy = False
    distribution = 'power_law'
    mean_match_query_term = 3
    mean_match_count = 5
    batch_size = 256
    h = 5
    w = 10
    channels = 1
    hidden_size = 50
    mean_match_doc_term = max(
        1, int(mean_match_count * h / mean_match_query_term))
    cnn_arch = [[3, 3, 1, 16], [1, 2, 2, 1], [3, 3, 16, 32], [1, 2, 2, 1],
                [2, 2, 32, 64], [1, 2, 2, 1]]
    #cnn_arch = [[3, 3, 1, 1], [1, 1, 1, 1]]
    cnn_activation = 'relu'

    # graph
    #grad_debugger = tf_debug.GradientsDebugger()
    #if args.tf_summary:
    #    global_step = tf.train.get_or_create_global_step()
    #    summary_writer = tf.contrib.summary.create_file_writer(args.tf_summary_path, flush_millis=10000)

    tf.set_random_seed(SEED)
    x = tf.placeholder(tf.float32, [None, h, w, channels])
    y = tf.placeholder(tf.float32, [None, h, w, channels])
    x_w = tf.placeholder(tf.float32, [None, h, w])
    bs = tf.shape(x)[0]

    #with summary_writer.as_default(), tf.contrib.summary.always_record_summaries():
    if args.arch == 'mdlstm':
        print('Using Multi Dimensional LSTM !')
        if args.rnn_type == 'dynamic':
            nn_out, rnn_states = multi_dimensional_rnn_while_loop(
                rnn_size=hidden_size, input_data=x, sh=[1, 1])
        elif args.rnn_type == 'static':
            nn_out, rnn_states = multi_dimensional_rnn_static(
                rnn_size=hidden_size, input_data=x, sh=[1, 1])
        #debug_rnn_states = grad_debugger.identify_gradient(rnn_states)
    elif args.arch == 'lstm':
        print('Using Standard LSTM !')
        nn_out = standard_lstm(input_data=x, rnn_size=hidden_size)
    elif args.arch == 'cnn':
        print('Using CNN !')
        nn_out = cnn(x, architecture=cnn_arch, activation=cnn_activation)
        nn_out = tf.reshape(nn_out, (bs, int(np.prod(nn_out.get_shape()[1:]))))

    # linear transformation (no activation)
    model_out = slim.fully_connected(inputs=nn_out,
                                     num_outputs=1,
                                     activation_fn=None)

    if args.arch == 'cnn':
        loss = 1e4 * tf.reduce_sum(tf.abs(
            tf.reshape(tf.boolean_mask(y, tf.expand_dims(x_w, axis=-1) > 0), [bs, 1]) - model_out)) / \
               tf.cast(bs, tf.float32)
    else:
        loss = 1e4 * tf.reduce_sum(tf.abs(y - model_out) * tf.expand_dims(x_w, axis=-1)) / \
               tf.reduce_sum(x_w)

    optimizer = tf.train.AdamOptimizer(learning_rate)
    grad_update = optimizer.minimize(loss)
    if args.arch == 'cnn':
        saliency = tf.gradients(-loss, x)
    else:
        used_model_out = model_out * tf.expand_dims(x_w, axis=-1)
        saliency = tf.gradients(used_model_out, x)
        if args.rnn_type == 'static':
            rnn_states_grad = tf.gradients(used_model_out,
                                           [s.c for s in rnn_states])
    saver = tf.train.Saver()
    init = tf.global_variables_initializer()
    #merged_summary = tf.summary.merge_all()
    #merged_summary = tf.contrib.summary.all_summary_ops()

    # session
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))
    if args.debug:
        sess = tf_debug.LocalCLIDebugWrapperSession(sess)
    #if args.tf_summary:
    #    tf.contrib.summary.initialize(graph=sess.graph, session=sess)
    #    train_writer = tf.summary.FileWriter(args.tf_summary_path, sess.graph)
    # train_writer.add_graph(sess.graph)

    # load model or init model
    if type(args.load_model_path) is str:
        logging.info('load model from "{}"'.format(args.load_model_path))
        saver.restore(sess, args.load_model_path)
    else:
        sess.run(init)

    # train model
    epochs = args.epoch
    for i in range(epochs):
        if args.data == 'gau':
            batch = next_batch(args.data, batch_size, h, w, anisotropy)
        elif args.data == 'ir':
            batch = next_batch(args.data,
                               batch_size,
                               h,
                               w,
                               mean_match_query_term=mean_match_query_term,
                               mean_match_doc_term=mean_match_doc_term,
                               dist=distribution)
        st = time()
        batch_x = np.expand_dims(batch[0], axis=3)
        batch_y = np.expand_dims(batch[1], axis=3)
        batch_x_w = batch[2]

        #if args.debug:
        #    print(batch[0][0])
        #    print(batch[1][0])
        #    print(batch[2][0])
        #    input()

        if args.arch == 'lstm' and i == 0:
            print(
                'Shuffling the batch in the height dimension for the standard LSTM.'
                'Its like having h LSTM on the width axis.')
            perms = np.random.permutation(list(range(w)))
            batch_x = batch_x[:, perms, :, :]
            batch_y = batch_y[:, perms, :, :]
            pass

        loss_val, _ = sess.run([loss, grad_update],
                               feed_dict={
                                   x: batch_x,
                                   y: batch_y,
                                   x_w: batch_x_w
                               })
        # console output
        if i % 50 == 0:
            print('epochs = {0} | loss = {1:.3f} | time {2:.3f}'.format(
                str(i).zfill(3), loss_val,
                time() - st))
            #print([v[0] for v in get_variables(sess)])
            #input()
        # save model
        if args.save_model_path and args.save_model_epoch > 0 and i > 0 and \
                (i % args.save_model_epoch == 0 or i == epochs - 1):
            logging.info('save model to "{}" at epochs {}'.format(
                args.save_model_path, i))
            saver.save(sess, args.save_model_path)
        # visualize model
        if args.visualize and i % args.visualize == 0:
            cnn_vis = CNNVis()
            if visualization == 'kernel' and args.arch == 'cnn':
                conv_kernels, = sess.run([tf.get_collection('conv_kernel')])
                kmax = np.max([np.max(k) for k in conv_kernels])
                kmin = np.min([np.min(k) for k in conv_kernels])
                print('kernal max: {}, min: {}'.format(kmax, kmin))
                kmax = max(abs(kmax), abs(kmin)) or 1
                kmin = -kmax
                cnn_vis.set_max_min(kmax, kmin)
                for i, c in enumerate(tf.get_collection('conv_kernel')):
                    cnn_vis.plot_conv_kernel(conv_kernels[i], c.name)
                    input('press to continue')
            elif visualization == 'saliency':
                saliency_map, = \
                    sess.run(saliency, feed_dict={x: batch_x, y: batch_y, x_w: batch_x_w})
                if args.arch == 'mdlstm':
                    # erase the gradiant at the top-left corner when the corresponding input is zero
                    saliency_mask = np.any(
                        np.abs(batch_x[:, 0, 0, :]) > UNIFORM_NOISE,
                        axis=1,
                        keepdims=True).astype(np.float32)
                    saliency_map[:, 0,
                                 0, :] = saliency_map[:, 0,
                                                      0, :] * saliency_mask
                cnn_vis.plot_saliency_map(batch_x, saliency_map)
                if args.rnn_type == 'static':
                    rnn_states_val = sess.run([s.h for s in rnn_states],
                                              feed_dict={
                                                  x: batch_x,
                                                  y: batch_y,
                                                  x_w: batch_x_w
                                              })
                    rnn_states_grad_val = \
                        sess.run(rnn_states_grad, feed_dict={x: batch_x, y: batch_y, x_w: batch_x_w})
                    rnn_vis = RNNVis()
                    rnn_vis.plot_hidden_grad(
                        np.transpose(np.stack(rnn_states_val), [1, 0, 2]),
                        np.transpose(np.stack(rnn_states_grad_val), [1, 0, 2]),
                        sequence=np.reshape(batch_x, [batch_size, h, w]),
                        shape=[1, h])
        # summarize model
        #if args.tf_summary and i % args.tf_summary == 0:
        #    logging.info('summarize model to "{}" at epochs {}'.format(args.tf_summary_path, i))
        #    summary = sess.run([merged_summary], feed_dict={x: batch_x, y: batch_y, x_w: batch_x_w})
        #    train_writer.add_summary(summary, i)
        # eval model
        if args.eval and i % args.eval == 0:
            act = input('press "c" to continue')
            while act != 'c':
                batch = next_batch(args.data,
                                   1,
                                   h,
                                   w,
                                   mean_match_query_term=mean_match_query_term,
                                   mean_match_doc_term=mean_match_doc_term,
                                   dist=distribution)
                model_out_val, loss_val = sess.run(
                    [model_out, loss],
                    feed_dict={
                        x: np.expand_dims(batch[0], axis=3),
                        y: np.expand_dims(batch[1], axis=3),
                        x_w: batch[2]
                    })
                print('matrix:')
                with printoptions(precision=3, suppress=True):
                    eval_matrix = np.rint(np.abs(batch[0][0] * w)).astype(int)
                    print(eval_matrix)
                    print('TF: {}'.format(np.sum(eval_matrix)))
                if args.arch == 'cnn':
                    print('target: {0:.3f}, output: {1:.3f}'.format(
                        batch[1][0, h - 1, w - 1], model_out_val[0, 0]))
                else:
                    print('target: {0:.3f}, output: {1:.3f}'.format(
                        batch[1][0, h - 1, w - 1], model_out_val[0, h - 1,
                                                                 w - 1, 0]))
                print('loss: {0:.3f}'.format(loss_val))
                act = input('press "c" to continue')
Пример #23
0
            -2.35924799e-01, 1.78741791e+00, 1.98098145e+00, 2.45745910e+00,
            -1.92949099e+00, 4.40071032e-01, 1.67750682e+00, 2.29228560e+00,
            1.91505982e+00, -2.21207919e+00, 1.56207029e+00, 1.21830856e+00,
            7.31777196e-01, 1.89957659e+00, -1.55168259e+00, 1.53547002e+00,
            2.96527969e-02, 3.32278097e-01
        ])
    ]
])


def use():
    return model


#voorbeeldje
print cnn.cnn("dataset/0/img001-001.png", model, [1, 0])

lettersArray = ['0', '1']
dataSet = []

#alle labels
lettersArray = ['0', '1']
#vectors voor classificatie systeem genereren
AllLabels = np.zeros((len(lettersArray), len(lettersArray)))
for x in xrange(len(lettersArray)):
    for y in xrange(len(lettersArray)):
        if x == y:
            AllLabels[x][y] = 1

#importen van data
labelSet = []
Пример #24
0
def train(alpha, totalGeneration, modelfile, metadatafile):

    #zorgen dat data goed wordt weggeschreven
    with open(metadatafile, "w") as myfile:
        myfile.write('\n\n\n\n')

    #tijd en generatie bijhouden
    startTime = int(time.time())

    vervangTekst(metadatafile, 0, "Training gestart op " + str(int(time.time())) + "..\n")

    #alle labels
    lettersArray = ['0','1']

    #vectors voor classificatie systeem genereren
    AllLabels = np.zeros((len(lettersArray), len(lettersArray)))
    for x in xrange(len(lettersArray)):
        for y in xrange(len(lettersArray)):
            if x == y:
                AllLabels[x][y] = 1

    #het maken van de startweights
    hiddenSize = 150
    #hiddenSize zijn het aantal nodes aan het einde van de 2e hidden layer
    model = np.asarray([
        
        np.asarray([[-1, 0, 2, 0, -1, 0, -1, 3, -1, 0, 2, 3, 4, 3, 2, 0, -1, 3, -1, 0, -1, 0, 2, 0, -1],
         [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 3, -5, 4, 1, 0, -1, 3, -1, 0, 0, -2, 1, -2, 0],
         [0, -1, 3, -1, 0, 0, -1, 3, -1, 0, 0, -1, 3, -1, 0, 0, -1, 3, -1, 0, 0, -1, 3, -1, 0],
         [0, 0, 0, 0, 0, -1, -1, -1, -1, -1, 3, 3, 3, 3, 3, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0],
         [0, -2, 1, -2, 0, 0, -1, 3, -1, 0, 1, 3, -5, 3, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
         [2, 1, -1, 1, 2, 1, 3, 1, 3, 1, -1, 1, 4, 1, -1, 1, 3, 1, 3, 1, 2, 1, -1, 1, 2]]),

        np.asarray([[1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1],
         [4, 4, 4, 4, 4, 4, 2, 2, 2, 4, 4, 2, 0, 2, 4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 4, 4, 2, 0, 2, 4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 4, 4, 2, 0, 2, 4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 4, 4, 2, 0, 2, 4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 4, 4, 2, 0, 2, 4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 4, 4, 2, 0, 2, 4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4],
         [0, 0, 0, 0, 0, -1, -1, 1, -1, -1, 1, 1, 2, 1, 1, -1, -1, 1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 4, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, -2, -2, -2, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 2, 0, 1, 1, 2, 1, 1, 0, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 4, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 0, 0, 0, 1, 2, 1, 0, 1, 2, 4, 2, 1, 0, 1, 2, 1, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, -1, -1, -1, 0, -1, -1, -2, -1, -1, 0, -1, -1, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, -1, -1, 0, 0, 0, -1, -1, 0, 1, 0, -1, -1, 0, 0, 0, -1, -1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, -1, -1, 0, -1, -1, -2, -1, -1, 0, -1, -1, -1, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 2, 0, 1, 1, 4, 1, 1, 0, 2, 1, 2, 0, 0, 0, 1, 0, 0],
         [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 4, 2, 0, 2, 4, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 3, 2, 0, 1, 3, 4, 3, 1, 0, 2, 3, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, -1, -2, -1, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 2, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -2, -3, -2, 0, -1, -3, -4, -3, -1, 0, -2, -3, -2, 0, 0, 0, -1, 0, 0, 0, -1, 1, 3, 4, -1, -1, 1, 2, 3, 1, 1, 1, 1, 1, 3, 2, 1, -1, -1, 4, 3, 1, -1, 0],
         [0, 0, 2, 0, 0, 0, 1, 2, 1, 0, 2, 2, 3, 2, 2, 0, 1, 2, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, -1, -2, -1, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 3, 1, 0, 0, 1, 4, 1, 0, 0, 1, 3, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 2, 3, 4, 3, 2, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, -1, -2, -1, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 3, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0]
        ]),
        
        [np.asarray(np.random.rand((hiddenSize + 1) * len(lettersArray)))]
        
    ])

    #importen van data
    labelSet = []
    dataSet = []

    for x in xrange(len(lettersArray)):
        y = 0
        while os.path.isfile("dataset/" + lettersArray[x] + "/img" + str(x + 1).zfill(3)  + "-" + str(y + 1).zfill(3) + ".png"):
            dataSet.append(str("dataset/" + lettersArray[x] + "/img" + str(x + 1).zfill(3)  + "-" + str(y + 1).zfill(3) + ".png"))
            labelSet.append(int(x))
            y += 1
        y = 0
        while os.path.isfile("dataset/" + lettersArray[x] + "/img" + str(x + 1).zfill(3)  + "-" + str(y + 1).zfill(5) + ".png"):
            dataSet.append(str("dataset/" + lettersArray[x] + "/img" + str(x + 1).zfill(3)  + "-" + str(y + 1).zfill(5) + ".png"))
            labelSet.append(int(x))
            y += 1
    dataSet = np.asarray(dataSet)

    #label koppelen aan data
    allData = []
    i = 0
    for data in dataSet:
        allData.append([data, labelSet[i]])
        i += 1

    #start van nieuwe generatie
    for numLoop in xrange(totalGeneration):
        #metadatafile aanpassen
        vervangTekst(metadatafile, 1, "Bezig met generatie " + str(numLoop) + "..\n")

        #willekeurige data kiezen
        gebruikteData = allData[random.randint(0, len(allData) - 1)]
        print gebruikteData[0]
        
        #neurale netwerk uitvoeren
        outputData = cnn.cnn(gebruikteData[0], model, AllLabels[gebruikteData[1]], True)

        #backpropagation
        for i in range(len(model[2][0])):
            x = i%(hiddenSize + 1)
            y = math.floor(i/(hiddenSize + 1))
            derivitive = (outputData[2][y] - outputData[1][y]) * outputData[3][x]
            model[2][0][i] -= alpha * derivitive
        
        #gegevens in de documenten aanpassen
        with open(modelfile, "w") as myfile:
            myfile.write(str(model[0]) + ',\n')
            myfile.write(str(model[1]) + ',\n')
            myfile.write(str(np.asarray(model[2])) + ',\n')
            
        with open(metadatafile, "a") as myfile:
            myfile.write(str(numLoop) + ' ' + str(outputData[1][1]) + ' ' + str(outputData[0]) + ' ' + str(outputData[2][0]) + '\n')
Пример #25
0
            -1.28151456e+00, -3.84942979e-01, 1.82922878e+00, 1.51436484e+00,
            1.75569719e+00, -1.44607211e+00, 9.42831695e-01, 1.25711253e+00,
            1.57347694e+00, 1.74176362e+00, -1.79957228e+00, 1.18676917e+00,
            5.11967633e-01, 1.28159856e+00, 1.68257140e+00, -1.45429579e+00,
            7.37992814e-01, -3.13833775e-01, -2.36384065e-01, 2.39000231e+00,
            -1.13071734e+00, -2.15551674e-01, 7.06773267e-01, 1.24026000e+00,
            3.31196233e+00, -2.80934925e+00, -2.70676026e-01, 2.51773927e+00,
            2.75172941e+00, 3.27117161e+00, -3.15860714e+00, 8.73311068e-01,
            2.48942632e+00, 2.41422698e+00, 2.17283531e+00, -2.89391240e+00,
            1.93921713e+00, 1.73801437e+00, 1.49028309e+00, 2.74509100e+00,
            -1.78934362e+00, 1.46492456e+00, -3.18648174e-01, -7.67231784e-01,
            5.90848896e-01, 1.28089161e+00, -2.59931874e+00, 5.69308542e-02,
            5.24040403e-01, 2.13501819e+00, -2.28464136e-01, -1.39081488e+00,
            1.76397903e+00, 2.57345867e+00, 2.07920380e+00, -4.70071076e-01,
            -6.50439517e-01, 2.78756990e+00, 2.37350360e+00, 1.21889865e+00,
            3.96533847e-02, 2.07270976e-01, 1.62793631e+00, 1.72390425e+00,
            -1.17826976e+00, -6.05697727e-02, 5.26893235e-01, -7.59343269e-01,
            -3.02901884e-01, 1.89771149e+00, -1.01298394e+00, 1.25809902e-01,
            3.86857040e-01, 9.22495501e-01, 2.89913946e+00, -2.79842942e+00,
            -2.35924799e-01, 1.78741791e+00, 1.98098145e+00, 2.45745910e+00,
            -1.92949099e+00, 4.40071032e-01, 1.67750682e+00, 2.29228560e+00,
            1.91505982e+00, -2.21207919e+00, 1.56207029e+00, 1.21830856e+00,
            7.31777196e-01, 1.89957659e+00, -1.55168259e+00, 1.53547002e+00,
            2.96527969e-02, 3.32278097e-01
        ])
    ]
])

imgPath = sys.argv[1]
print cnn.cnn(imgPath, model, [1, 0])[0]
Пример #26
0
logger.info('Load data')

###############################################################################
# Build the model
###############################################################################
OPTIMIZER = {'Adam': optim.Adam, 'SGD': optim.SGD}

logger.info('Build the model')
if os.path.isfile(os.path.join(args.data_dir, 'model.tar.gz')):

    logger.info('Model exists. Reload model for continued training')
    tar = tarfile.open(os.path.join(args.data_dir, 'model.tar.gz'))
    tar.extractall(args.data_dir)
    tar.close()
    model = cnn(args.similarity_dims, 152)
    model.load_state_dict(torch.load(os.path.join(args.data_dir, 'model.pth')))

else:

    logger.info(
        'No model found. Loading a new ResNet model with default pre-trained weights'
    )
    model = cnn(args.similarity_dims, 152)

optimizer = OPTIMIZER[args.optimizer](model.parameters(),
                                      lr=args.learning_rate)

# Save arguments used to create model for restoring the model later
with open(MODEL_INFO_PATH, 'wb') as f:
    model_info = {'simililarity-dims': args.similarity_dims}
Пример #27
0

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--file',
        type=pathlib.Path,
        help='Include CSV file using --file arg'
    )
    parser.add_argument('--cnn', action='store_true')
    parser.add_argument('--rnn', action='store_true')
    parser.add_argument('--ae', action='store_true')
    parser.add_argument('--ffnn', action='store_true')
    args = parser.parse_args()

    if not args.file:
        sys.exit('Include dataset to run network using: --file <PATH>')

    x_train, y_train = translate.run(args.file)

    if args.ffnn:
        ffnn(x_train, y_train)
    elif args.rnn:
        rnn(x_train, y_train)
    elif args.cnn:
        cnn(x_train, y_train)
    elif args.ae:
        ae(x_train, y_train)
    else:
        sys.exit('Include network type: [--cnn, --rnn, --ae, --ffnn]')
Пример #28
0
def cnn_rnn(match_matrix, dq_size, query, query_emb, doc, doc_emb, word_vector,
            **kwargs):
    '''
  Use CNN to select regions and RNN to represent
  '''
    bs = tf.shape(match_matrix)[0]
    max_q_len = tf.shape(query)[1]
    max_d_len = tf.shape(doc)[1]
    thres = kwargs['threshold']
    time = kwargs['time']
    state_ta = kwargs['state_ta']
    print('threshold: {}'.format(thres))
    # use CNN to choose regions
    with vs.variable_scope('CNNRegionFinder'):
        cnn_decision_value = cnn(tf.expand_dims(match_matrix, axis=-1),
                                 architecture=[(5, 5, 1, 4), (1, 1),
                                               (1, 1, 4, 1), (1, 1)],
                                 activation='tanh')
        cnn_decision_value = tf.Print(cnn_decision_value,
                                      [cnn_decision_value[0, :20, :5, 0]],
                                      message='cnn',
                                      summarize=100)
        cnn_decision_value = tf.reshape(cnn_decision_value,
                                        tf.shape(cnn_decision_value)[:3])
        # mask out the words beyond the boundary
        doc_mask = tf.expand_dims(tf.range(max_d_len), dim=0) < tf.reshape(
            dq_size[:1], [bs, 1])
        query_mask = tf.expand_dims(tf.range(max_q_len), dim=0) < tf.reshape(
            dq_size[1:], [bs, 1])
        mask = tf.cast(tf.logical_and(tf.expand_dims(doc_mask, axis=2),
                                      tf.expand_dims(query_mask, axis=1)),
                       dtype=tf.float32)
        cnn_decision_value = (cnn_decision_value - thres) * mask + thres
        # make decision by "or"
        doc_decision_value = tf.reduce_max(cnn_decision_value, axis=2)
        query_decision_value = tf.reduce_max(cnn_decision_value, axis=1)
        doc_decision = doc_decision_value > thres
        query_decision = query_decision_value > thres
        # print debug
        doc_decision = tf.Print(doc_decision, [
            tf.reduce_mean(
                tf.reduce_sum(tf.cast(doc_decision, tf.int32), axis=1))
        ],
                                message='avg all doc piece:')
        query_decision = tf.Print(query_decision, [
            tf.reduce_mean(
                tf.reduce_sum(tf.cast(query_decision, tf.int32), axis=1))
        ],
                                  message='avg all query piece:')
    '''
  with vs.variable_scope('ThresholdRegionFinder'):
    # randomly mask some part to avoid very long sequence
    ber = tf.contrib.distributions.Bernoulli(probs=0.99)
    ber = ber.sample([1, tf.shape(match_matrix)[1], 1])
    match_matrix *= tf.cast(ber, dtype=match_matrix.dtype)
    decision = tf.logical_or(match_matrix>0.4, match_matrix<-0.0)
    doc_decision = tf.reduce_any(decision, axis=2)
    doc_decision_value = tf.ones_like(doc_decision, dtype=tf.float32)
    #query_decision = tf.reduce_any(decision, axis=1)
    query_decision = tf.reduce_any(tf.logical_not(tf.equal(match_matrix, 0.0)), axis=1) # special for query
    query_decision_value = tf.ones_like(query_decision, dtype=tf.float32)
    doc_decision = tf.Print(doc_decision, 
      [tf.reduce_mean(tf.reduce_sum(tf.cast(doc_decision, tf.float32), axis=1))], 
      message='avg all doc piece:')
    query_decision = tf.Print(query_decision, 
      [tf.reduce_mean(tf.reduce_sum(tf.cast(query_decision, tf.float32), axis=1))], 
      message='avg all query piece:')
  '''
    with vs.variable_scope('RNNRegionRepresenter'):
        doc_piece_emb, doc_piece_num = piece_rnn(doc_decision,
                                                 doc_decision_value, doc,
                                                 dq_size[0], word_vector,
                                                 **kwargs)
        #doc_piece_emb, doc_piece_num = whole_rnn(doc, dq_size[0], word_vector, **kwargs)
        vs.get_variable_scope().reuse_variables()
        query_piece_emb, query_piece_num = piece_rnn(query_decision,
                                                     query_decision_value,
                                                     query, dq_size[1],
                                                     word_vector, **kwargs)
        #query_piece_emb, query_piece_num = whole_rnn(query, dq_size[1], word_vector, **kwargs)
    with vs.variable_scope('KNRMAggregator'):
        # interaction
        match_matrix = tf.matmul(doc_piece_emb,
                                 tf.transpose(query_piece_emb, [0, 2, 1]))
        max_q_len = tf.shape(query_piece_emb)[1]
        max_d_len = tf.shape(doc_piece_emb)[1]
        dq_size = tf.stack([doc_piece_num, query_piece_num], axis=0)
        # use K-NRM
        if 'input_mu' in kwargs and kwargs['input_mu'] != None:
            input_mu = kwargs['input_mu']
        else:
            input_mu = np.array(list(range(-10, 10 + 1, 2))) / 10
        #input_mu = [1.0]
        number_of_bin = len(input_mu) - 1
        input_sigma = [0.1] * number_of_bin + [0.1]
        state_ta = tf.cond(
            tf.greater(time, 0), lambda: state_ta, lambda: state_ta.write(
                0, tf.zeros([bs, number_of_bin + 1], dtype=tf.float32)))
        print('mu: {}, sigma: {}'.format(input_mu, input_sigma))
        mu = tf.constant(input_mu, dtype=tf.float32)
        sigma = tf.constant(input_sigma, dtype=tf.float32)
        mu = tf.reshape(mu, [1, 1, 1, number_of_bin + 1])
        sigma = tf.reshape(sigma, [1, 1, 1, number_of_bin + 1])
        # kernelize
        match_matrix = tf.expand_dims(match_matrix, axis=-1)
        # totally discard some part of the matrix
        #print('discard some part of the matrix in K-NRM')
        #discard_match_matrix = tf.logical_and(match_matrix>=-0.1, match_matrix<=0.5)
        match_matrix = tf.exp(-tf.square(match_matrix - mu) /
                              (tf.square(sigma) * 2))
        # have to use mask because the weight is masked
        query_mask = tf.expand_dims(tf.range(max_q_len), dim=0) < tf.reshape(
            dq_size[1:], [bs, 1])
        doc_mask = tf.expand_dims(tf.range(max_d_len), dim=0) < tf.reshape(
            dq_size[:1], [bs, 1])
        query_mask = tf.cast(tf.reshape(query_mask, [bs, 1, max_q_len, 1]),
                             dtype=tf.float32)
        doc_mask = tf.cast(tf.reshape(doc_mask, [bs, max_d_len, 1, 1]),
                           dtype=tf.float32)
        match_matrix = match_matrix * query_mask * doc_mask
        # totally discard some part of the matrix
        #match_matrix *= 1-tf.cast(discard_match_matrix, dtype=tf.float32)
        # sum and log
        representation = tf.reduce_sum(match_matrix, axis=[1, 2])
        # this is for manually masking out some kernels
        #                 [-1 -0.8 -0.6 -0.4 -0.2  0.   0.2  0.4  0.6  0.8  1.]
        #representation *= [[0,  0,    0,   1,   1,  0,    0,   0,   1,   1,  1]]
        #representation = tf.log(1+representation) # log is used in K-NRM
        # use a MLP to model interactions between evidence of different strength
        #mlp_arch = [number_of_bin+1, number_of_bin+1]
        #print('use MLP with structure {}'.format(mlp_arch))
        #representation = mlp(representation, architecture=mlp_arch, activation='relu')
        return state_ta, representation
Пример #29
0
from tensorflow.keras.optimizers import Adam

from cnn import cnn
from model_preprocessor import Preprocessor

model = cnn()

x_train, x_test, y_train, y_test = Preprocessor.load_data_binary(10000)

# Define Deep Learning Model
''' Training phrase '''
epochs = 15
batch_size = 64
adam = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])

model.fit(x_train,
          y_train,
          epochs=epochs,
          batch_size=batch_size,
          validation_split=0.11)
Пример #30
0
PATH_IMGS     = "imnet-val/val/"
FILE_FEATURES = "imnet-val/cnn-50000.p"
FNAME_OFFSET  = 48 #prefix of stored file names to chop off
IMSIZE        = (224, 224)

data = dataset(FILE_FEATURES,PATH_IMGS,IMSIZE,normalize=True,fname_offt=FNAME_OFFSET)
X = data.X
d,n = X.shape

b = 200 #hash bits
M = 50  #number of permutations
k = 3
L = 16
model = LSH(X=X,b=b,M=M)

cnn_model = cnn.cnn("mobilenet")

cap = cv2.VideoCapture(0)
time.sleep(0.1)
pause = False

empty = np.zeros((DISPLAY_SIZE[1], DISPLAY_SIZE[0], 3), dtype=np.uint8)

while(True):
  if not pause:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # frame = cv2.flip(frame, 1)

    # frame = image_resize(frame, width = DISPLAY_SIZE)
    resized = cv2.resize(frame, DISPLAY_SIZE, interpolation = cv2.INTER_AREA)
Пример #31
0
task_name = "1dcnn_RMSprop_no_L2"
model_name = "1dcnn"
optimizer_name = "RMSprop"
lr = 0.001
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
epochs = 100
logging.info("""{}:
    - model name: {}
    - optimizer: {}
    - learning rate: {}
    - device : {}
    - epochs: {}
 """.format(task_name, model_name, optimizer_name, lr, device, epochs))

if __name__ == "__main__":
    model = cnn.cnn()
    # model.initialize()

    # get the param to update
    params_to_update = []
    for name, param in model.named_parameters():
        param.requires_grad = True
        params_to_update.append(param)

    # optimizer
    optimizer = getattr(optim, optimizer_name)(params_to_update, lr=lr)

    # dataLoaders
    dataLoaders = dataLoader.get_dataLoaders()

    # train and test
Пример #32
0
if args.cuda:
    train_iter = data.Iterator(train, batch_size=args.batch_size, device=torch.device('cuda', args.gpu), train=True,
                               repeat=False, sort=False, shuffle=True, sort_within_batch=False)
    dev_iter = data.Iterator(dev, batch_size=args.batch_size, device=torch.device('cuda', args.gpu), train=False,
                             repeat=False, sort=False, shuffle=False, sort_within_batch=False)
else:
    train_iter = data.Iterator(train, batch_size=args.batch_size, train=True, repeat=False, sort=False, shuffle=True,
                               sort_within_batch=False)
    dev_iter = data.Iterator(dev, batch_size=args.batch_size, train=False, repeat=False, sort=False, shuffle=False,
                             sort_within_batch=False)

config = args
config.words_num = len(TEXT.vocab)
config.label = args.embed_dim
config.words_dim = words_dim
model = cnn(config)

model.embed.weight.data.copy_(TEXT.vocab.vectors)

args.cuda = 0
if args.cuda:
    modle = model.to(torch.device("cuda:{}".format(args.gpu)))
    print("Shift model to GPU")
    # Embedding for MID
    predicates_emb = predicates_emb.cuda()

total_num = len(dev)
print(config)
print("VOCAB num",len(TEXT.vocab))
print("Train instance", len(train))
print("Dev instance", total_num)
Пример #33
0
    findRadius(rot_img, rot_empty)

    return radii[maxR]
"""
import size_classification
import cv2

model = load_model('model_backup.h5')
weights = model.get_weights()

size = 150
W = 2 * size
H = 2 * size

my_cnn = cnn.cnn(img_width=W, img_height=H)
my_cnn.set_weights(weights)

img1 = cv2.imread('data_cinta/good__02/nuez0_000050.png')
img2 = cv2.imread('test/data/nuez6_000000.png')
img = np.concatenate((img1, img2), axis=1)
img = cv2.resize(img, (4 * size, 2 * size))

pred = my_cnn.predict_classes(img.reshape([-1, 300, 600, 3]), batch_size=1)
print(pred)
exit(1111)
empty1 = cv2.imread('data_cinta/empty/empty0.png')
empty2 = cv2.imread('test/data/empty6.png')
print("start")
size1 = findRadius(img1, empty1)
print("start")
lengthsequence = x_train.shape[1]
sizevocab = len(vocabprocess.vocabulary_)
epoch = 50
# # learning_rate = 0.001
batch_size = 32
num_class = y_train.shape[1]

# print(vocabulary)
print(num_class)

logdir = "./logs/nn_logs"

with tf.Session() as sess:

    model = cnn(sizevocab, lengthsequence, num_class, batch_size)

    init_op = tf.compat.v1.global_variables_initializer()
    sess.run(init_op)
    saver = tf.compat.v1.train.Saver(tf.global_variables())

    # tf.summary.text("text", b)

    # tf.summary.histogram("weights", weight)
    tf.summary.histogram("fc1", model.fc)
    # tf.summary.histogram("fc2", model.fc3)
    # tf.summary.histogram("fc3", model.fc3)

    tf.summary.scalar("accuracy", model.accuracy)
    tf.summary.scalar("loss", model.loss)
    merge = tf.summary.merge_all()