def __init__(self): self._sess = None self._vae = None self.trained_model_save_folder = ue.get_content_dir( ) + "Scripts/trained_vae/" self.training_data_source = ue.get_content_dir( ) + "Scripts/training_data.csv" self.test_data_source = ue.get_content_dir() + "Scripts/test_data.csv"
def show(): #Data saveDir = r"\\vietsap002\projects\R6\04_WIP\tools\perforce\proxyCheck" rootDir = ue.get_content_dir() matchType = compile(r'\.uasset\Z') allstaticmeshes = ue.get_assets_by_class("StaticMesh") uassetfolders = ((root,files) for root, subdirs, files in os.walk(rootDir) if any(matchType.search(file) for file in files)) filterstaticmeshfolder = ((folder, list( staticmesh for staticmesh in allstaticmeshes if any(match("^{}".format(staticmesh.get_name()), ufile) for ufile in ufiles ))) for folder, ufiles in uassetfolders) staticmeshfolder = ((folder, files) for folder, files in filterstaticmeshfolder if files) #UI window = SWindow().resize(512,1024).set_title("Export Static Mesh") vertical = SVerticalBox() #style = ButtonStyle(Normal=SlateBrush(TintColor=SlateColor(SpecifiedColor=FLinearColor(1, 0, 0)))) #picker = SFilePathPicker(browse_title='Export To', browse_button_style=style, on_path_picked=path_picked) button = SButton().set_content(STextBlock().set_text("Export All").set_v_align(2) vertical.add_slot(button, v_align=0, h_align=0) vertical.add_slot(STextBlock().set_text("Export Folders:"), v_align=0, h_align=0) vertical.add_slot(picker, v_align=0, h_align=0) vertical.add_slot(STextBlock().set_text("Static Mesh Folders:"), v_align=0, h_align=0) for folder, ufiles in staticmeshfolder: button = SButton().set_content(STextBlock().set_text(os.path.relpath(folder, rootDir))).set_v_align(2) button.bind_on_clicked(partial(export, ufiles, saveDir)) #vertical.add_slot(button, v_align=2, h_align=2) vertical.add_slot(button, v_align=0, h_align=0) window.set_content(vertical) #window.set_modal(True) show()
def begin_play(self): self.actor = self.uobject.get_owner() self.actor.bind_event('OnPredictEvent', self.on_predict) self.data_path = ue.get_content_dir() + 'Scripts/MNIST_data' self.mnist = input_data.read_data_sets(self.data_path, one_hot=True) tf.reset_default_graph()
def onBeginTraining(self): ue.log("starting mnist simple training") self.scripts_path = ue.get_content_dir() + "Scripts" self.data_dir = self.scripts_path + '/dataset/mnist' mnist = input_data.read_data_sets(self.data_dir) # Create the model x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.matmul(x, W) + b # Define loss and optimizer y_ = tf.placeholder(tf.int64, [None]) # The raw formulation of cross-entropy, # # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)), # reduction_indices=[1])) # # can be numerically unstable. # # So here we use tf.losses.sparse_softmax_cross_entropy on the raw # outputs of 'y', and then average across the batch. cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #update session for this thread self.sess = tf.InteractiveSession() tf.global_variables_initializer().run() # Train for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) self.sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) if i % 100 == 0: ue.log(i) if(self.shouldStop): ue.log('early break') break # Test trained model correct_prediction = tf.equal(tf.argmax(y, 1), y_) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) finalAccuracy = self.sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}) ue.log('final training accuracy: ' + str(finalAccuracy)) #return trained model self.model = {'x':x, 'y':y, 'W':W,'b':b} #store optional summary information self.summary = {'x':str(x), 'y':str(y), 'W':str(W), 'b':str(b)} self.stored['summary'] = self.summary return self.stored
def __init__(self): self.texture_to_draw = None self.gray_data = None self.texture = ue.create_transient_texture(512, 512, EPixelFormat.PF_G8) self.texture.auto_root() filename = os.path.join(ue.get_content_dir(), 'haarcascade_frontalface_default.xml') self.cascade = cv2.CascadeClassifier(filename)
def show(): rootDir = ue.get_content_dir() matchType = compile(r'\.uasset\Z') folders = (root for root, subdirs, files in os.walk(rootDir) if any(matchType.search(file) for file in files)) window = SWindow().resize(512,1024).set_title("Export Static Mesh") vertical = SVerticalBox() vertical.add_slot(STextBlock().set_text("Export Folders:"), v_align=2, h_align=0) for folder in folders: button = SButton().set_content(STextBlock().set_text(os.path.relpath(folder, rootDir))).set_v_align(2) #vertical.add_slot(button, v_align=2, h_align=2) vertical.add_slot(button, v_align=0, h_align=0) window.set_content(vertical) #window.set_modal(True)
def onSetup(self): #Setup our paths self.scripts_path = ue.get_content_dir() + "Scripts" self.data_dir = self.scripts_path + '/dataset/mnist' self.model_directory = self.scripts_path + "/model/mnistSimple" self.model_path = self.model_directory + "/model.ckpt" #startup a session and try to obtain latest save self.sess = tf.InteractiveSession() self.graph = tf.get_default_graph() self.model_loaded = False with self.sess.as_default(): try: saver = tf.train.import_meta_graph(self.model_path + ".meta") ue.log('meta graph imported') saver.restore(self.sess, tf.train.latest_checkpoint(self.model_directory)) ue.log('graph restored') self.model_loaded = True #restore our weights self.graph = tf.get_default_graph() W = self.graph.get_tensor_by_name("W:0") b = self.graph.get_tensor_by_name("b:0") except: #no saved model, initialize our variables W = tf.get_variable('W', [784, 10], initializer=tf.zeros_initializer) b = tf.get_variable('b', [10], initializer=tf.zeros_initializer) print("no model saved, initializing variables") self.saver = tf.train.Saver() #The rest of the operations are the same x = tf.placeholder(tf.float32, [None, 784]) y = tf.matmul(x, W) + b #store the model in a class instance variable to easily reference in another function self.model = {'x': x, 'y': y, 'W': W, 'b': b}
def __init__(self, folder, num_actions, observation_shape, params={}, verbose=False): """ Initialize the CNN model with a set of parameters. Args: params: a dictionary containing values of the models' parameters. """ self.scripts_path = ue.get_content_dir() + "Scripts" self.model_directory = self.scripts_path + "/models" + "/" + folder #Create a folder if we dont have one already Path(self.model_directory).mkdir(parents=True, exist_ok=True) self.modemodel_loaded = False self.model_path = self.model_directory + "/model.ckpt" self.verbose = verbose self.num_actions = num_actions # observation shape will be a tuple self.observation_shape = observation_shape[0] logging.info('Initialized with params: {}'.format(params)) #hyperparameters self.use_cnn = params['use_cnn'] self.lr = params['lr'] self.reg = params['reg'] self.hidden_layers = params['hidden_layers'] self.conv_layers = params['conv_layers'] self.image_width = params['image_width'] self.image_height = params['image_height'] self.color_channels = params['color_channels'] self.W = [] self.b = [] self.conv = [] self.fc = [] self.use_maxpooling = params['use_maxpooling'] self.session = self.create_model()
def __init__(self, *args, **kwargs): super().__init__(parent=uQtWidgets.get_main_window()) self._exportFolder = "D:/temp" grp = QtWidgets.QGroupBox("Export Directory: ") layout = QtWidgets.QVBoxLayout() exportDirLayout = QtWidgets.QHBoxLayout() self.dirPathEdit = QtWidgets.QLineEdit(self._exportFolder) browsePathButton = QtWidgets.QPushButton() browsePathButton.clicked.connect(self.changeRootPath) exportDirLayout.addWidget(self.dirPathEdit) exportDirLayout.addWidget(browsePathButton) grp.setLayout(exportDirLayout) layout.addWidget(grp) button = QtWidgets.QPushButton("Export All") button.clicked.connect(self.exportAll) layout.addWidget(button) button = QtWidgets.QPushButton("Export Selected") button.clicked.connect(self.exportSelected) layout.addWidget(button) layout.addWidget(QtWidgets.QLabel("Folders Contain Static Meshes:")) self.listView = QtWidgets.QListWidget() rootDir = ue.get_content_dir() data = self.getStaticFolderMeshData(rootDir) for folder, meshes in data: item = QtWidgets.QListWidgetItem(os.path.relpath(folder, rootDir)) item.setData(QtCore.Qt.ItemDataRole.UserRole, meshes) self.listView.addItem(item) self.listView.itemDoubleClicked.connect(self.exportFolder) self.listView.setSelectionMode( QtWidgets.QAbstractItemView.ExtendedSelection) layout.addWidget(self.listView) self.setLayout(layout)
def onBeginTraining(self): ue.log("starting mnist keras cnn training") model_file_name = "mnistKerasCNN" model_directory = ue.get_content_dir() + "/Scripts/" model_sess_path = model_directory + model_file_name + ".tfsess" model_json_path = model_directory + model_file_name + ".json" my_file = Path(model_json_path) #reset the session each time we get training calls K.clear_session() #let's train batch_size = 128 num_classes = 10 epochs = 8 # input image dimensions img_rows, img_cols = 28, 28 # the data, shuffled and split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 ue.log('x_train shape:' + str(x_train.shape)) ue.log(str(x_train.shape[0]) + 'train samples') ue.log(str(x_test.shape[0]) + 'test samples') # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) # model.add(Dropout(0.2)) # model.add(Flatten()) # model.add(Dense(512, activation='relu')) # model.add(Dropout(0.2)) # model.add(Dense(num_classes, activation='softmax')) #model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test), callbacks=[self.stopcallback]) score = model.evaluate(x_test, y_test, verbose=0) ue.log("mnist keras cnn training complete.") ue.log('Test loss:' + str(score[0])) ue.log('Test accuracy:' + str(score[1])) self.session = K.get_session() self.model = model stored = {'model':model, 'session': self.session} #run a test evaluation ue.log(x_test.shape) result_test = model.predict(np.reshape(x_test[500],(1,28,28,1))) ue.log(result_test) #flush the architecture model data to disk #with open(model_json_path, "w") as json_file: # json_file.write(model.to_json()) #flush the whole model and weights to disk #saver = tf.train.Saver() #save_path = saver.save(K.get_session(), model_sess_path) #model.save(model_path) return stored
def onSetup(self): BERT_DIR = os.path.join(ue.get_content_dir(), 'Scripts', 'BertModel') self.imported = tf.saved_model.load(BERT_DIR) self.f = self.imported.signatures["serving_default"] VOCAB_PATH = os.path.join(BERT_DIR, "assets", "vocab.txt") self.tokenizer = FullTokenizer(VOCAB_PATH)
for item in ('unreal_engine', 'unreal_engine.classes', 'unreal_engine.structs', 'unreal_engine.enums'): data['completions'].append({'trigger': item, 'contents': item}) for item in dir(ue): value = 'unreal_engine.{0}'.format(item) data['completions'].append({'trigger': value, 'contents': value}) for item in dir(ue.UObject): value = 'unreal_engine.UObject.{0}'.format(item) data['completions'].append({'trigger': value, 'contents': value}) for _class in ue.all_classes(): data['completions'].append({ 'trigger': _class.get_name(), 'contents': _class.get_name() }) for function in _class.functions(): value = '{0}.{1}()'.format(_class.get_name(), function) data['completions'].append({'trigger': value, 'contents': value}) for _property in _class.properties(): value = '{0}.{1}'.format(_class.get_name(), _property) data['completions'].append({'trigger': value, 'contents': value}) j = json.dumps(data, indent=4) with open( os.path.join(ue.get_content_dir(), 'UnrealEnginePython..sublime-completions'), 'w') as f: f.write(j)
data = { 'scope': 'source.python', 'completions': [] } for item in ('unreal_engine', 'unreal_engine.classes', 'unreal_engine.structs', 'unreal_engine.enums'): data['completions'].append({'trigger': item, 'contents': item}) for item in dir(ue): value = 'unreal_engine.{0}'.format(item) data['completions'].append({'trigger': value, 'contents': value}) for item in dir(ue.UObject): value = 'unreal_engine.UObject.{0}'.format(item) data['completions'].append({'trigger': value, 'contents': value}) for _class in ue.all_classes(): data['completions'].append({'trigger': _class.get_name(), 'contents': _class.get_name()}) for function in _class.functions(): value = '{0}.{1}()'.format(_class.get_name(), function) data['completions'].append({'trigger': value, 'contents': value}) for _property in _class.properties(): value = '{0}.{1}'.format(_class.get_name(), _property) data['completions'].append({'trigger': value, 'contents': value}) j = json.dumps(data, indent=4) with open(os.path.join(ue.get_content_dir(), 'UnrealEnginePython..sublime-completions'), 'w') as f: f.write(j)
#import pandas as pd import nltk import tensorflow as tf import unreal_engine as ue from TFPluginAPI import TFPluginAPI #additional includes from tensorflow.python.keras import backend as K #to ensure things work well with multi-threading from tensorflow import keras import numpy as np #for reshaping input import operator #used for getting max prediction from 1x10 output array import random import os scripts_path = ue.get_content_dir() + "Scripts" word2vec_dir = scripts_path + '/GoogleNews-vectors-negative300.bin' word2vecmodel = KeyedVectors.load_word2vec_format(word2vec_dir, binary=True, limit=100000) class KerasBackProp(TFPluginAPI): #keras stop callback class StopCallback(tf.keras.callbacks.Callback): def __init__(self, outer): self.outer = outer self.monitor = 'acc' self.value = 1 self.verbose = 0
def onSetup(self): #==================== Initialize ====================# # File Path self.scripts_path = ue.get_content_dir() + "Scripts" self.model_directory = self.scripts_path + "/model" self.model_path = self.model_directory + "/model.ckpt" # Game self.Sequence = 1 self.PlayNumber = 1 # Epsilon self.Epsilon = EPSILONMINVALUE # ReplayMemory self.Memory = ReplayMemory() self.LastAction = -1 # State self.reset() #==================== Hypothesis ====================# self.input = tf.placeholder(tf.float32, shape=[None, INPUTS]) # Model w1 = tf.Variable(tf.truncated_normal(shape=[INPUTS, HIDDEN1S], stddev=1.0 / math.sqrt(float(INPUTS))), dtype=tf.float32, name='w1') b1 = tf.Variable(tf.truncated_normal(shape=[HIDDEN1S], stddev=0.01), dtype=tf.float32, name='b1') hidden1 = tf.nn.relu(tf.matmul(self.input, w1) + b1, name='hidden1') w2 = tf.Variable(tf.truncated_normal(shape=[HIDDEN1S, HIDDEN2S], stddev=1.0 / math.sqrt(float(HIDDEN1S))), dtype=tf.float32, name='w2') b2 = tf.Variable(tf.truncated_normal(shape=[HIDDEN2S], stddev=0.01), dtype=tf.float32, name='b2') hidden2 = tf.nn.relu(tf.matmul(hidden1, w2) + b2, name='hidden2') wo = tf.Variable(tf.truncated_normal(shape=[HIDDEN2S, OUTPUTS], stddev=1.0 / math.sqrt(float(HIDDEN2S))), dtype=tf.float32, name='wo') bo = tf.Variable(tf.truncated_normal(shape=[OUTPUTS], stddev=0.01), dtype=tf.float32, name='bo') self.output = tf.matmul(hidden2, wo) + bo # Target w1_t = tf.Variable(tf.truncated_normal(shape=[INPUTS, HIDDEN1S], stddev=1.0 / math.sqrt(float(INPUTS))), dtype=tf.float32, name='w1_t') b1_t = tf.Variable(tf.truncated_normal(shape=[HIDDEN1S], stddev=0.01), dtype=tf.float32, name='b1_t') hidden1_t = tf.nn.relu(tf.matmul(self.input, w1_t) + b1_t, name='hidden1') w2_t = tf.Variable(tf.truncated_normal(shape=[HIDDEN1S, HIDDEN2S], stddev=1.0 / math.sqrt(float(HIDDEN1S))), dtype=tf.float32, name='w2_t') b2_t = tf.Variable(tf.truncated_normal(shape=[HIDDEN2S], stddev=0.01), dtype=tf.float32, name='b2_t') hidden2_t = tf.nn.relu(tf.matmul(hidden1_t, w2_t) + b2_t, name='hidden2') wo_t = tf.Variable(tf.truncated_normal(shape=[HIDDEN2S, OUTPUTS], stddev=1.0 / math.sqrt(float(HIDDEN2S))), dtype=tf.float32, name='wo_t') bo_t = tf.Variable(tf.truncated_normal(shape=[OUTPUTS], stddev=0.01), dtype=tf.float32, name='bo_t') self.output_t = tf.matmul(hidden2_t, wo_t) + bo_t # Cost & Optimizer self.target = tf.placeholder(tf.float32, shape=[None, OUTPUTS]) self.cost = tf.reduce_mean(tf.square(self.output - self.target)) / 2 self.optimizer = tf.train.AdamOptimizer(LEARNINGRATE).minimize( self.cost) #==================== Session & Saver ====================# self.sess = tf.Session() self.saver = tf.train.Saver() ue.log('######################################################') try: self.saver.restore(self.sess, self.model_path) ue.log('#################### loaded model ####################') except: self.sess.run(tf.global_variables_initializer()) ue.log('################## no stored model ##################') ue.log('######################################################') pass