def train(self): """ Start training """ # 1: build a list of image filenames self.build_image_filenames_list() # 2: use list information to init our numpy variables self.init_np_variables() # 3: Add images to our Tensorflow dataset self.add_tf_dataset(self.list_cow_files, 0) self.add_tf_dataset(self.list_noncow_files, 1) # 4: Process TF dataset self.process_tf_dataset() # 5: Setup image preprocessing self.setup_image_preprocessing() # 6: Setup network structure self.setup_nn_network() # 7: Train our deep neural network tf_model = DNN(self.tf_network, tensorboard_verbose=3, checkpoint_path='model_cows.tfl.ckpt') tf_model.fit(self.tf_x, self.tf_y, n_epoch=100, shuffle=True, validation_set=(self.tf_x_test, self.tf_y_test), show_metric=True, batch_size=96, snapshot_epoch=True, run_id='model_cows') # 8: Save model tf_model.save('model_cows.tflearn')
def train( self, X_train, Y_train, X_val, Y_val ): with tf.Graph().as_default(): print("Building Model...........") network = build_CNN() model = DNN( network, tensorboard_dir="path_to_logs", tensorboard_verbose=0, checkpoint_path="path_to_checkpoints", max_checkpoints=1 ) if self.is_training: # Training phase print("start training...") print(" - emotions = {}".format(7)) print(" - optimizer = '{}'".format(self.optimizer)) print(" - learning_rate = {}".format(0.016)) print(" - learning_rate_decay = {}".format(self.learning_rate_decay)) print(" - otimizer_param ({}) = {}".format(self.optimizer, self.optimizer_param)) print(" - Dropout = {}".format(self.dropout)) print(" - epochs = {}".format(self.epochs)) start_time = time.time() model.fit( {'input': X_train.reshape(-1, 48, 48, 1)}, {'output': Y_train}, validation_set=( {'input': X_val.reshape(-1, 48, 48, 1)}, {'output': Y_val}, ), batch_size=128, n_epoch=10, show_metric=True, snapshot_step=100 ) training_time = time.time() - start_time print("training time = {0:.1f} sec".format(training_time)) print("saving model...") model.save("saved_model.bin")
def use_tflearn(x_train, y_train, x_test, y_test): net = input_data(shape=[None, x_train.shape[1]], name='input') net = fully_connected(net, 24, activation='sigmoid', bias_init='normal') net = fully_connected(net, 16, activation='sigmoid', bias_init='normal') net = fully_connected(net, 12, activation='sigmoid', bias_init='normal') net = fully_connected(net, 8, activation='sigmoid', bias_init='normal') net = regression(net) model = DNN(net, tensorboard_dir=TENSORBOARD_DIR.as_posix(), tensorboard_verbose=3, best_checkpoint_path=CHECKPOINT_PATH.as_posix()) model.fit(x_train, y_train, validation_set=(x_test, y_test), n_epoch=100, batch_size=10, show_metric=True, run_id='DNN-4f') model.save(MODEL_FILE.as_posix()) return model
""" Source : https://towardsdatascience.com/tflearn-soving-xor-with-a-2x2x1-feed-forward-neural-network-6c07d88689ed """ from tflearn import DNN from tflearn.layers.core import input_data, dropout, fully_connected from tflearn.layers.estimator import regression #Training examples X = [[0,0], [0,1], [1,0], [1,1]] Y = [[0], [1], [1], [0]] input_layer = input_data(shape=[None, 2]) #input layer of size 2 hidden_layer = fully_connected(input_layer , 2, activation='tanh') #hidden layer of size 2 output_layer = fully_connected(hidden_layer, 1, activation='tanh') #output layer of size 1 #use Stohastic Gradient Descent and Binary Crossentropy as loss function regression = regression(output_layer , optimizer='sgd', loss='binary_crossentropy', learning_rate=5) model = DNN(regression) #fit the model model.fit(X, Y, n_epoch=5000, show_metric=True); #predict all examples print ('Expected: ', [i[0] > 0 for i in Y]) print ('Predicted: ', [i[0] > 0 for i in model.predict(X)]) model.get_weights(hidden_layer.W) model.get_weights(output_layer.W) model.save("tflearn-xor")
def train(optimizer=HYPERPARAMS.optimizer, optimizer_param=HYPERPARAMS.optimizer_param, learning_rate=HYPERPARAMS.learning_rate, keep_prob=HYPERPARAMS.keep_prob, learning_rate_decay=HYPERPARAMS.learning_rate_decay, decay_step=HYPERPARAMS.decay_step, train_model=True): print "loading dataset " + DATASET.name + "..." if train_model: data, validation = load_data(validation=True) else: data, validation, test = load_data(validation=True, test=True) with tf.Graph().as_default(): print "building model..." network = build_model(optimizer, optimizer_param, learning_rate, keep_prob, learning_rate_decay, decay_step) model = DNN(network, tensorboard_dir=TRAINING.logs_dir, tensorboard_verbose=0, checkpoint_path=TRAINING.checkpoint_dir, max_checkpoints=TRAINING.max_checkpoints) #tflearn.config.init_graph(seed=None, log_device=False, num_cores=6) if train_model: # Training phase print "start training..." print " - emotions = {}".format(NETWORK.output_size) print " - optimizer = '{}'".format(optimizer) print " - learning_rate = {}".format(learning_rate) print " - learning_rate_decay = {}".format(learning_rate_decay) print " - otimizer_param ({}) = {}".format( 'beta1' if optimizer == 'adam' else 'momentum', optimizer_param) print " - keep_prob = {}".format(keep_prob) print " - epochs = {}".format(TRAINING.epochs) print " - use landmarks = {}".format(NETWORK.use_landmarks) print " - use hog + landmarks = {}".format( NETWORK.use_hog_and_landmarks) print " - use hog sliding window + landmarks = {}".format( NETWORK.use_hog_sliding_window_and_landmarks) print " - use batchnorm after conv = {}".format( NETWORK.use_batchnorm_after_conv_layers) print " - use batchnorm after fc = {}".format( NETWORK.use_batchnorm_after_fully_connected_layers) start_time = time.time() if NETWORK.use_landmarks: model.fit([data['X'], data['X2']], data['Y'], validation_set=([validation['X'], validation['X2']], validation['Y']), snapshot_step=TRAINING.snapshot_step, show_metric=TRAINING.vizualize, batch_size=TRAINING.batch_size, n_epoch=TRAINING.epochs) else: model.fit(data['X'], data['Y'], validation_set=(validation['X'], validation['Y']), snapshot_step=TRAINING.snapshot_step, show_metric=TRAINING.vizualize, batch_size=TRAINING.batch_size, n_epoch=TRAINING.epochs) validation['X2'] = None training_time = time.time() - start_time print "training time = {0:.1f} sec".format(training_time) if TRAINING.save_model: print "saving model..." model.save(TRAINING.save_model_path) if not(os.path.isfile(TRAINING.save_model_path)) and \ os.path.isfile(TRAINING.save_model_path + ".meta"): os.rename(TRAINING.save_model_path + ".meta", TRAINING.save_model_path) print "evaluating..." validation_accuracy = evaluate(model, validation['X'], validation['X2'], validation['Y']) print " - validation accuracy = {0:.1f}".format( validation_accuracy * 100) return validation_accuracy else: # Testing phase : load saved model and evaluate on test dataset print "start evaluation..." print "loading pretrained model..." if os.path.isfile(TRAINING.save_model_path): model.load(TRAINING.save_model_path) else: print "Error: file '{}' not found".format( TRAINING.save_model_path) exit() if not NETWORK.use_landmarks: validation['X2'] = None test['X2'] = None print "--" print "Validation samples: {}".format(len(validation['Y'])) print "Test samples: {}".format(len(test['Y'])) print "--" print "evaluating..." start_time = time.time() validation_accuracy = evaluate(model, validation['X'], validation['X2'], validation['Y']) print " - validation accuracy = {0:.1f}".format( validation_accuracy * 100) test_accuracy = evaluate(model, test['X'], test['X2'], test['Y']) print " - test accuracy = {0:.1f}".format(test_accuracy * 100) print " - evalution time = {0:.1f} sec".format(time.time() - start_time) return test_accuracy
class DNNBackend(BaseBackend): def create_model(self): """ Creates DNN model that is based on built algorithm. Needed algorithm is builded with self.build_algorithm call. """ self.log_named("model creation started") if self.algorithm is not None: self.model = DNN(self.algorithm, checkpoint_path=self.checkpoints_dir_path, max_checkpoints=1, tensorboard_verbose=3, tensorboard_dir=self.learn_logs_dir_path) self.log_named("model creation finished") else: self.log_named_warning( "model was not created, because algorithm is None!") def save_model(self): """ Saves created DNN model to a file. Path to is got from self.model_file_path property. """ if self.model is not None: self.model.save(self.model_file_path) self.log_named("model saved") else: self.log_named_warning( "model file was not saved, because model is None!") def load_model(self): """ Loads saved DNN model from a file. Path to is got from self.model_file_path property. """ if self.model is not None: if os.path.exists(self.model_file_dir_path) and len( os.listdir(self.model_file_dir_path)): self.model.load(self.model_file_path) self.log_named("model loaded") else: self.log_named_warning("model file doesn't exist!") else: self.log_named_warning("model is None!") def restore_model_learning(self): """ Restores model learning from the last checkpoint if such exists. """ if self.model is not None: if os.path.exists(self.checkpoints_dir_path): with open(os.path.join(self.checkpoints_dir_path, 'checkpoint')) as checkpoint_file: self.model.load( checkpoint_file.readline().split(': ')[-1][1:-2]) self.learn_model() self.save_model() else: self.log_named_warning("checkpoints directory doesn't exist!") else: self.log_named_warning( "can't restore model learning process, because model is None!")
class Bot: def __init__(self): self.words = [] self.labels = [] self.docs_x = [] self.docs_y = [] self.stemmer = LancasterStemmer() self.data = [] self.training = [] self.output = [] self.out_empty=[] self.model=[] self.count=-1 self.say="" self.Network=Network() def read(self): with open("src/models/intents.json") as f: self.data=load(f) def dump(self): with open("src/models/data.pickle", "wb") as f: dump((self.words, self.labels, self.training, self.output), f) def stem(self): for intent in self.data["intents"]: for pattern in intent["patterns"]: wrds = word_tokenize(pattern) self.words.extend(wrds) self.docs_x.append(wrds) self.docs_y.append(intent["tag"]) if intent["tag"] not in self.labels: self.labels.append(intent["tag"]) self.words = [self.stemmer.stem(w.lower()) for w in self.words if w != "?"] self.words = sorted(list(set(self.words))) self.labels = sorted(self.labels) def modelsetup(self): self.out_empty = [0 for _ in range(len(self.labels))] for x, doc in enumerate(self.docs_x): bag = [] wrds = [self.stemmer.stem(w.lower()) for w in doc] for w in self.words: if w in wrds: bag.append(1) else: bag.append(0) output_row = self.out_empty[:] output_row[self.labels.index(self.docs_y[x])] = 1 self.training.append(bag) self.output.append(output_row) self.training = array(self.training) self.output = array(self.output) self.dump() def setup(self): ops.reset_default_graph() net = input_data(shape=[None, len(self.training[0])]) net = fully_connected(net, 10) net = fully_connected(net, 10) net = fully_connected(net, len(self.output[0]), activation="softmax") net = regression(net) self.model = DNN(net) if exists("src/models/model.tflearn.index"): self.model.load("src/models/model.tflearn") else: self.model.fit(self.training, self.output, n_epoch=1000, batch_size=8, show_metric=True) self.model.save("src/models/model.tflearn") def indexWord(self,x,word): x=x.split(" ") ch="" for i in x: if i.find(word)!=-1: ch=i return ch def bag_of_words(self,s, words): bag = [0 for _ in range(len(words))] translate=[] s_words = word_tokenize(s) s_words = [self.stemmer.stem(word.lower()) for word in s_words] for se in s_words: for i, w in enumerate(words): if w == se: bag[i] = 1 if se not in words and se not in translate: translate.append(se) return array(bag),translate def chat(self,x,ui): try: self.count+=1 predinp,translate=self.bag_of_words(x, self.words) if translate: translate=self.indexWord(str(x),translate[0]) print(translate) results = self.model.predict([predinp]) results_index = argmax(results) tag = self.labels[results_index] except Exception as e: print(e) try: if results[0][results_index] > 0.4: for tg in self.data["intents"]: if tg['tag'] == tag: responses = tg['responses'] self.say=choice(responses) if self.say=="Looking up": self.say=self.Network.Connect(translate.upper()) ui.textEdit.setText(self.say) else: ui.textEdit.setText(self.say) else: self.say="Sorry i can't understand i am still learning try again." ui.textEdit.setText(self.say) except Exception as e: print(e)