def main(unused_argv): tf.reset_default_graph() sess = tf.Session() lenet = LeNet(sess=sess, weights=None) lenet.train(learning_rate=0.001, training_epochs=40, batch_size=1000, keep_prob=0.7) lenet.evaluate(batch_size=1000, keep_prob=0.7)
def clear_session(): global _SESSION global _LEARNING_PHASE tf.reset_default_graph() reset_uids() _SESSION = None _LEARNING_PHASE = tf.placeholder(dtype='uint8', name='keras_learning_phase')
def run(data_seed, ema_decay_during_rampup, ema_decay_after_rampup, test_phase=False, n_labeled=250, n_extra_unlabeled=0, model_type='mean_teacher'): minibatch_size = 100 hyperparams = model_hyperparameters(model_type, n_labeled, n_extra_unlabeled) tf.reset_default_graph() model = Model(RunContext(__file__, data_seed)) svhn = SVHN(n_labeled=n_labeled, n_extra_unlabeled=n_extra_unlabeled, data_seed=data_seed, test_phase=test_phase) model['ema_consistency'] = hyperparams['ema_consistency'] model['max_consistency_cost'] = hyperparams['max_consistency_cost'] model['apply_consistency_to_labeled'] = hyperparams['apply_consistency_to_labeled'] model['training_length'] = hyperparams['training_length'] model['ema_decay_during_rampup'] = ema_decay_during_rampup model['ema_decay_after_rampup'] = ema_decay_after_rampup training_batches = minibatching.training_batches(svhn.training, minibatch_size, hyperparams['n_labeled_per_batch']) evaluation_batches_fn = minibatching.evaluation_epoch_generator(svhn.evaluation, minibatch_size) tensorboard_dir = model.save_tensorboard_graph() LOG.info("Saved tensorboard graph to %r", tensorboard_dir) model.train(training_batches, evaluation_batches_fn)
def __init__(self, params=params, dyn='FCC'): tf.reset_default_graph() data = self.sample_mog(params['batch_size']) noise = ds.Normal(tf.zeros(params['z_dim']), tf.ones(params['z_dim'])).sample(params['batch_size']) # Construct generator and discriminator nets with slim.arg_scope([slim.fully_connected], weights_initializer=tf.orthogonal_initializer(gain=1.4)): samples = self.generator(noise, output_dim=params['x_dim']) real_score = self.discriminator(data) fake_score = self.discriminator(samples, reuse=True) # Saddle objective loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=real_score, labels=tf.ones_like(real_score)) + tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_score, labels=tf.zeros_like(fake_score))) gen_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "generator") disc_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "discriminator") gen_shapes = [tuple(v.get_shape().as_list()) for v in gen_vars] disc_shapes = [tuple(v.get_shape().as_list()) for v in disc_vars] # Generator gradient g_opt = tf.train.GradientDescentOptimizer(learning_rate=params['gen_learning_rate']) g_grads = g_opt.compute_gradients(-loss, var_list=gen_vars) # Discriminator gradient d_opt = tf.train.GradientDescentOptimizer(learning_rate=params['disc_learning_rate']) d_grads = d_opt.compute_gradients(loss, var_list=disc_vars) # Squared Norm of Gradient: d/dx 1/2||F||^2 = J^T F grads_norm_sep = [tf.reduce_sum(g[0]**2) for g in g_grads+d_grads] grads_norm = 0.5*tf.reduce_sum(grads_norm_sep) # Gradient of Squared Norm JTF = tf.gradients(grads_norm, xs=gen_vars+disc_vars) sess = tf.Session() sess.run(tf.global_variables_initializer()) self.params = params self.data = data self.samples = samples self.gen_vars = gen_vars self.disc_vars = disc_vars self.gen_shapes = gen_shapes self.disc_shapes = disc_shapes self.Fg = g_grads self.Fd = d_grads self.JTF = JTF self.sess = sess self.findiff_step = params['findiff_step'] self.gamma = params['gamma'] self.dyn = dyn if dyn == 'FCC': self.F = self.FCC else: self.F = self._F
def setUp(self): tf.reset_default_graph() self.m = AddModel() self.m._compile() rng = np.random.RandomState(0) self.x = rng.randn(10, 20) self.y = rng.randn(10, 20)
def generate_testdata(self): tf.reset_default_graph() sess = tf.Session() placeholder = tf.constant('I am deprecated.') # Previously, we had used a means of creating text summaries that used # plugin assets (which loaded JSON files containing runs and tags). The # plugin must continue to be able to load summaries of that format, so we # create a summary using that old plugin asset-based method here. plugin_asset_summary = tf.summary.tensor_summary('old_plugin_asset_summary', placeholder) assets_directory = os.path.join(self.logdir, 'fry', 'plugins', 'tensorboard_text') # Make the directory of assets if it does not exist. if not os.path.isdir(assets_directory): try: os.makedirs(assets_directory) except OSError as err: self.assertFail('Could not make assets directory %r: %r', assets_directory, err) json_path = os.path.join(assets_directory, 'tensors.json') with open(json_path, 'w+') as tensors_json_file: # Write the op name to a JSON file that the text plugin later uses to # determine the tag names of tensors to fetch. tensors_json_file.write(json.dumps([plugin_asset_summary.op.name])) run_name = 'fry' subdir = os.path.join(self.logdir, run_name) writer = tf.summary.FileWriter(subdir) writer.add_graph(sess.graph) summ = sess.run(plugin_asset_summary) writer.add_summary(summ) writer.close()
def generate_testdata(self, include_text=True, logdir=None): tf.reset_default_graph() sess = tf.Session() placeholder = tf.placeholder(tf.string) summary_tensor = tf.summary.text('message', placeholder) vector_summary = tf.summary.text('vector', placeholder) scalar_summary = tf.summary.scalar('twelve', tf.constant(12)) run_names = ['fry', 'leela'] for run_name in run_names: subdir = os.path.join(logdir or self.logdir, run_name) writer = tf.summary.FileWriter(subdir) writer.add_graph(sess.graph) step = 0 for gem in GEMS: message = run_name + ' *loves* ' + gem feed_dict = { placeholder: message, } if include_text: summ = sess.run(summary_tensor, feed_dict=feed_dict) writer.add_summary(summ, global_step=step) step += 1 vector_message = ['one', 'two', 'three', 'four'] if include_text: summ = sess.run(vector_summary, feed_dict={placeholder: vector_message}) writer.add_summary(summ) summ = sess.run(scalar_summary, feed_dict={placeholder: []}) writer.add_summary(summ) writer.close()
def __init__(self,d_max_length=100,q_max_length=27,a_max_length=27,rnn_size=64,embedding_size=300,num_symbol=10000,atten_sim='nn',layer=2,atten_decoder=False,encode_type='cnn',d_max_sent=29): tf.reset_default_graph() self.d_max_sent = d_max_sent self.d_max_length = d_max_length self.q_max_length = q_max_length self.a_max_length = a_max_length self.rnn_size = rnn_size self.lr = 1e-2 self.input_net = {} self.output_net = {} self.input_net['drop'] = tf.placeholder(tf.float32,[]) self.cell = tf.nn.rnn_cell.GRUCell(self.rnn_size) self.cell = tf.nn.rnn_cell.DropoutWrapper(self.cell,self.input_net['drop']) self.fw_cell = tf.nn.rnn_cell.GRUCell(self.rnn_size) self.bw_cell = tf.nn.rnn_cell.GRUCell(self.rnn_size) self.result_cell = tf.nn.rnn_cell.GRUCell(self.rnn_size) self.result_cell = tf.nn.rnn_cell.DropoutWrapper(self.result_cell,self.input_net['drop']) #self.decoder_cell = tf.nn.rnn_cell.GRUCell(self.rnn_size) #self.decoder_cell = tf.nn.rnn_cell.DropoutWrapper(self.decoder_cell,self.input_net['drop']) self.embedding_size = embedding_size self.num_symbol = num_symbol self.output_net['loss'] = 0. #self.output_net['test_loss'] = 0. self.atten_sim = atten_sim self.atten_decoder = atten_decoder self.num_class = 2 self.l2_loss = tf.constant(0.0) self.sess = tf.Session() self.encode_type = encode_type
def rename(checkpoint, replace_from, replace_to, add_prefix, dry_run, force_prefix=False): import tensorflow as tf tf.reset_default_graph() with tf.Session() as sess: for var_name, _ in tf.contrib.framework.list_variables(checkpoint): # Load the variable var = tf.contrib.framework.load_variable(checkpoint, var_name) # Set the new name new_name = var_name if None not in [replace_from, replace_to]: new_name = new_name.replace(replace_from, replace_to) if add_prefix: if force_prefix or not new_name.startswith(add_prefix): # force prefix or add prefix if it does not exist yet new_name = add_prefix + new_name if dry_run: print('%s would be renamed to %s.' % (var_name, new_name)) else: if var_name == new_name: print('No change for {}'.format(var_name)) else: print('Renaming %s to %s.' % (var_name, new_name)) # Rename the variable tf.Variable(var, name=new_name) if not dry_run: # Save the variables saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) saver.save(sess, checkpoint) tf.reset_default_graph()
def tfliteInvoke(self, graph, test_inputs, outputs): tf.reset_default_graph() # Turn the input into placeholder of shape 1 tflite_input = tf.placeholder( "float", [1, self.time_steps, self.n_input], name="INPUT_IMAGE_LITE") tf.import_graph_def(graph, name="", input_map={"INPUT_IMAGE": tflite_input}) with tf.Session() as sess: curr = sess.graph_def curr = convert_op_hints_to_stubs(graph_def=curr) curr = optimize_for_inference_lib.optimize_for_inference( curr, ["INPUT_IMAGE_LITE"], ["OUTPUT_CLASS"], [tf.float32.as_datatype_enum]) tflite = tf.lite.toco_convert( curr, [tflite_input], [outputs], allow_custom_ops=False) interpreter = tf.lite.Interpreter(model_content=tflite) try: interpreter.allocate_tensors() except ValueError: assert False input_index = (interpreter.get_input_details()[0]["index"]) interpreter.set_tensor(input_index, test_inputs) interpreter.invoke() output_index = (interpreter.get_output_details()[0]["index"]) result = interpreter.get_tensor(output_index) # Reset all variables so it will not pollute other inferences. interpreter.reset_all_variables() return result
def tf_baseline_conv2d(): import tensorflow as tf import cntk.contrib.crosstalk.crosstalk_tensorflow as crtf ci = crtf.instance tf.reset_default_graph() x = tf.placeholder(tf.float32, [batch_size, num_chars, char_emb_dim]) filter_bank = tf.get_variable("char_filter_bank", shape=[filter_width, char_emb_dim, num_filters], dtype=tf.float32) bias = tf.get_variable("char_filter_biases", shape=[num_filters], dtype=tf.float32) char_conv = tf.expand_dims(tf.transpose(tf.nn.conv1d(x, filter_bank, stride=1, padding='VALID') + bias, perm=[0,2,1]), -1) ci.watch(cstk.Conv2DArgs(W=crtf.find_trainable('char_filter_bank'), b=crtf.find_trainable('char_filter_biases')), 'conv2d', var_type=cstk.Conv2DAttr, attr=cstk.Conv2DAttr(filter_shape=(filter_width, char_emb_dim,), num_filters=num_filters)) ci.watch(char_conv, 'conv2d_out', var_type=crtf.VariableType) # note the output is transposed to NCHW with tf.Session() as sess: sess.run(tf.global_variables_initializer()) data = {x:input_data} ci.set_workdir(workdir) ci.set_data(sess, data) ci.fetch('conv2d_out', save=True) ci.fetch('conv2d', save=True) ci.assign('conv2d', load=True) assert ci.compare('conv2d_out') ci.reset() sess.close()
def setUp(self): tf.reset_default_graph() class FlatModel(GPflow.model.Model): def build_likelihood(self): return 0 self.m = FlatModel()
def setUp(self): tf.reset_default_graph() rng = np.random.RandomState(0) X = rng.rand(20,1)*10 Y = np.sin(X) + 0.9 * np.cos(X*1.6) + rng.randn(*X.shape)* 0.8 self.Xtest = rng.rand(10,1)*10 m1 = GPflow.gpr.GPR(X, Y, kern=GPflow.kernels.RBF(1),\ mean_function=GPflow.mean_functions.Constant()) m2 = GPflow.vgp.VGP(X, Y, GPflow.kernels.RBF(1), likelihood=GPflow.likelihoods.Gaussian(),\ mean_function=GPflow.mean_functions.Constant()) m3 = GPflow.svgp.SVGP(X, Y, GPflow.kernels.RBF(1), likelihood=GPflow.likelihoods.Gaussian(), Z=X.copy(), q_diag=False,\ mean_function=GPflow.mean_functions.Constant()) m3.Z.fixed = True m4 = GPflow.svgp.SVGP(X, Y, GPflow.kernels.RBF(1), likelihood=GPflow.likelihoods.Gaussian(), Z=X.copy(), q_diag=False, whiten=True,\ mean_function=GPflow.mean_functions.Constant()) m4.Z.fixed=True m5 = GPflow.sgpr.SGPR(X, Y, GPflow.kernels.RBF(1), Z=X.copy(),\ mean_function=GPflow.mean_functions.Constant()) m5.Z.fixed = True m6 = GPflow.sgpr.GPRFITC(X, Y, GPflow.kernels.RBF(1), Z=X.copy(),\ mean_function=GPflow.mean_functions.Constant()) m6.Z.fixed = True self.models = [m1, m2, m3, m4, m5, m6] for m in self.models: m.optimize(display=False, max_iters=300) print('.') # stop travis timing out
def __init__(self, iWidth, iHeight, outputSize, trainData, trainLabels, testData, testLabels): tf.reset_default_graph() self.imageWidth = iWidth self.imageHeight = iHeight # the 1600 is the number of pixels in an image and the 10 is the number of images in a batch # ...aka for labels self.X = tf.placeholder(tf.float32, shape=[None, iHeight, iWidth, 1]) self.Y = tf.placeholder(tf.float32, shape=[None, outputSize]) self.trX = np.asarray(trainData).reshape(-1, iHeight, iWidth, 1) self.trY = trainLabels self.teX = np.asarray(testData).reshape(-1, iHeight, iWidth, 1) self.teY = testLabels w = self.init_weights([3, 3, 1, 32]) # 3x3x1 conv, 32 outputs w2 = self.init_weights([3, 3, 32, 64]) # 3x3x32 conv, 64 outputs w3 = self.init_weights([3, 3, 64, 128]) # 3x3x32 conv, 128 outputs w4 = self.init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputs w_o = self.init_weights([625, outputSize]) # FC 625 inputs, 10 outputs (labels) self.p_keep_conv = tf.placeholder("float") self.p_keep_hidden = tf.placeholder("float") self.py_x = self.model(self.X, w, w2, w3, w4, w_o, self.p_keep_conv, self.p_keep_hidden) self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.py_x, self.Y)) self.train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(self.cost) self.predict_op = tf.argmax(self.py_x, 1) self.loose_predict_op = self.py_x
def eval_snapshot(envname, checkptfile, last_snapshot_idx, n_trajs, mode): import tensorflow as tf if mode == 'rltools': import h5py with h5py.File(checkptfile, 'r') as f: args = json.loads(f.attrs['args']) elif mode == 'rllab': params_file = os.path.join(checkptfile, 'params.json') with open(params_file, 'r') as df: args = json.load(df) env = envname2env(envname, args) bestidx = 0 bestret = -np.inf bestevr = {} for idx in range((last_snapshot_idx - 10), (last_snapshot_idx + 1)): tf.reset_default_graph() minion = Evaluator(env, args, args['max_traj_len'] if mode == 'rltools' else args['max_path_length'], n_trajs, False, mode) if mode == 'rltools': evr = minion(checkptfile, file_key='snapshots/iter%07d' % idx) elif mode == 'rllab': evr = minion(os.path.join(checkptfile, 'itr_{}.pkl'.format(idx))) if np.mean(evr['ret']) > bestret: bestret = np.mean(evr['ret']) bestevr = evr bestidx = idx return bestevr, bestidx
def predict(self, n225_open, today=np.datetime64("today"), downloadData=False): if downloadData: self.stockdata.download() start_date = today - np.timedelta64(14, 'D') end_date = today predictors_tf, _, raw_values = self.create_data(start_date=start_date, end_date=end_date) raw_values= raw_values["N225_close"][~np.isnan(raw_values["N225_close"])] # df = self.stockdata.get_data_for_prediction(today) # Get last values predictors_tf = predictors_tf[-2:-1] raw_values = raw_values[-1:] predictors_tf["N225_jump"] = (n225_open / raw_values.values[0]) - 1 # print("Yesterday's N225 close value was", raw_values.values[0]) # print("Will use this value for N225_jump:", predictors_tf["N225_jump"].values[0]) tf.reset_default_graph() with tf.Session() as sess: feature_data = tf.placeholder("float", [None, self.num_predictors]) layers = [self.num_predictors] + self.hidden_layers + [self.num_classes] model = self.inference(feature_data, layers) # Restore parameters saver = tf.train.Saver() saver.restore(sess, self.model_filename) prediction = sess.run(tf.argmax(model, 1), feed_dict={feature_data: predictors_tf.values}) pred2str = {0: "Up", 1: "Down", 2: "Same"} print("\n") print("Today's N225:", pred2str[prediction[0]]) print("\n")
def train(config, inputs, args): gan = setup_gan(config, inputs, args) sampler = lookup_sampler(args.sampler or TrainingVideoFrameSampler)(gan) samples = 0 #metrics = [batch_accuracy(gan.inputs.x, gan.uniform_sample), batch_diversity(gan.uniform_sample)] #sum_metrics = [0 for metric in metrics] for i in range(args.steps): gan.step() if args.action == 'train' and i % args.save_every == 0 and i > 0: print("saving " + save_file) gan.save(save_file) if i % args.sample_every == 0: sample_file="samples/%06d.png" % (samples) samples += 1 sampler.sample(sample_file, args.save_samples) #if i > args.steps * 9.0/10: # for k, metric in enumerate(gan.session.run(metrics)): # print("Metric "+str(k)+" "+str(metric)) # sum_metrics[k] += metric tf.reset_default_graph() return []#sum_metrics
def testBasic(self): base_path = tf.test.test_src_dir_path( "contrib/session_bundle/example/half_plus_two/00000123") tf.reset_default_graph() sess, meta_graph_def = session_bundle.LoadSessionBundleFromPath( base_path, target="", config=tf.ConfigProto(device_count={"CPU": 2})) self.assertTrue(sess) asset_path = os.path.join(base_path, constants.ASSETS_DIRECTORY) with sess.as_default(): path1, path2 = sess.run(["filename1:0", "filename2:0"]) self.assertEqual( compat.as_bytes(os.path.join(asset_path, "hello1.txt")), path1) self.assertEqual( compat.as_bytes(os.path.join(asset_path, "hello2.txt")), path2) collection_def = meta_graph_def.collection_def signatures_any = collection_def[constants.SIGNATURES_KEY].any_list.value self.assertEquals(len(signatures_any), 1) signatures = manifest_pb2.Signatures() signatures_any[0].Unpack(signatures) default_signature = signatures.default_signature input_name = default_signature.regression_signature.input.tensor_name output_name = default_signature.regression_signature.output.tensor_name y = sess.run([output_name], {input_name: np.array([[0], [1], [2], [3]])}) # The operation is y = 0.5 * x + 2 self.assertEqual(y[0][0], 2) self.assertEqual(y[0][1], 2.5) self.assertEqual(y[0][2], 3) self.assertEqual(y[0][3], 3.5)
def train(self, eval_on_test=False): """ Train model and save it to file. Train model with given hidden layers. Training data is created by prepare_training_data(), which must be called before this function. """ tf.reset_default_graph() with tf.Session() as sess: feature_data = tf.placeholder("float", [None, self.num_predictors]) labels = tf.placeholder("float", [None, self.num_classes]) layers = [self.num_predictors] + self.hidden_layers + [self.num_classes] model = self.inference(feature_data, layers) cost, cost_summary_op = self.loss(model, labels) training_op = self.training(cost, learning_rate=0.0001) correct_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) # Merge all variable summaries and save the results to log file # summary_op = tf.merge_all_summaries() accuracy_op_train = tf.scalar_summary("Accuracy on Train", accuracy) summary_op_train = tf.merge_summary([cost_summary_op, accuracy_op_train]) if eval_on_test: accuracy_op_test = tf.scalar_summary("Accuracy on Test", accuracy) summary_op_test = tf.merge_summary([accuracy_op_test]) summary_writer = tf.train.SummaryWriter(self.log_dir + self.model_name, sess.graph) train_dict = { feature_data: self.training_predictors_tf.values, labels: self.training_classes_tf.values.reshape(len(self.training_classes_tf.values), self.num_classes)} if eval_on_test: test_dict = { feature_data: self.test_predictors_tf.values, labels: self.test_classes_tf.values.reshape(len(self.test_classes_tf.values), self.num_classes)} init = tf.initialize_all_variables() sess.run(init) for i in range(1, self.max_iteration): sess.run(training_op, feed_dict=train_dict) # Write summary to log if i % 100 == 0: summary_str = sess.run(summary_op_train, feed_dict=train_dict) summary_writer.add_summary(summary_str, i) if eval_on_test: summary_str = sess.run(summary_op_test, feed_dict=test_dict) summary_writer.add_summary(summary_str, i) summary_writer.flush() # Print current accuracy to console if i%5000 == 0: print (i, sess.run(accuracy, feed_dict=train_dict)) # Save trained parameters saver = tf.train.Saver() saver.save(sess, self.model_filename)
def demonstrate_loading_weights_into_different_scope(): print("="*60 + " Demonstrate loading weights saved in scopeQ, into variables now in scopeA") tf.reset_default_graph() with tf.variable_scope("scopeA") as scope: m1a = Model1() print ("=" * 60 + " Trying to load model1 weights from scopeQ into scopeA") m1a.model.load("model1_scopeQ.tfl", variable_name_map=("scopeA", "scopeQ"), verbose=True)
def train(model_path, num_classes, data): # setup training label train_image = [] train_label = [] for (path, k) in data: train_image.append(__read(path)) train_label.append(__label(num_classes, k)) # convert to numpy train_image = np.asarray(train_image) train_label = np.asarray(train_label) with tf.Graph().as_default(): with tf.variable_scope('ejs') as scope: reuse = True # create expression acc = __accuracy(num_classes) saver = tf.train.Saver() # run training batch sess = __train(train_image, train_label, acc) # save trained model saver.save(sess, model_path) tf.reset_default_graph() sess.close()
def generate_run(self, run_name, include_graph): """Create a run with a text summary, metadata, and optionally a graph.""" tf.reset_default_graph() k1 = tf.constant(math.pi, name='k1') k2 = tf.constant(math.e, name='k2') result = (k1 ** k2) - k1 expected = tf.constant(20.0, name='expected') error = tf.abs(result - expected, name='error') message_prefix_value = 'error ' * 1000 true_length = len(message_prefix_value) assert true_length > self._MESSAGE_PREFIX_LENGTH_LOWER_BOUND, true_length message_prefix = tf.constant(message_prefix_value, name='message_prefix') error_message = tf.string_join([message_prefix, tf.as_string(error, name='error_string')], name='error_message') summary_message = tf.summary.text('summary_message', error_message) sess = tf.Session() writer = tf.summary.FileWriter(os.path.join(self.logdir, run_name)) if include_graph: writer.add_graph(sess.graph) options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() s = sess.run(summary_message, options=options, run_metadata=run_metadata) writer.add_summary(s) writer.add_run_metadata(run_metadata, self._METADATA_TAG) writer.close()
def fit_em(X, initial_mus, max_steps, tol, min_covar=MIN_COVAR_DEFAULT): tf.reset_default_graph() N, D = X.shape K, Dmu = initial_mus.shape assert D == Dmu mus0 = initial_mus sigmas0 = np.tile(np.var(X, axis=0), (K, 1)) alphas0 = np.ones(K) / K X = tf.constant(X) mus, sigmas, alphas = (tf.Variable(x, dtype='float64') for x in [mus0, sigmas0, alphas0]) all_ll, resp = estep(X, mus, sigmas, alphas) cmus, csigmas, calphas = mstep(X, resp, min_covar=min_covar) update_mus_step = tf.assign(mus, cmus) update_sigmas_step = tf.assign(sigmas, csigmas) update_alphas_step = tf.assign(alphas, calphas) init_op = tf.initialize_all_variables() ll = prev_ll = -np.inf with tf.Session() as sess: sess.run(init_op) for i in range(max_steps): ll = sess.run(tf.reduce_mean(all_ll)) sess.run((update_mus_step, update_sigmas_step, update_alphas_step)) #print('EM iteration', i, 'log likelihood', ll) if abs(ll - prev_ll) < tol: break prev_ll = ll m, s, a = sess.run((mus, sigmas, alphas)) return ll, m, s, a
def run(data_seed, dropout, input_noise, augmentation, test_phase=False, n_labeled=250, n_extra_unlabeled=0, model_type='mean_teacher'): minibatch_size = 100 hyperparams = model_hyperparameters(model_type, n_labeled, n_extra_unlabeled) tf.reset_default_graph() model = Model(RunContext(__file__, data_seed)) svhn = SVHN(n_labeled=n_labeled, n_extra_unlabeled=n_extra_unlabeled, data_seed=data_seed, test_phase=test_phase) model['ema_consistency'] = hyperparams['ema_consistency'] model['max_consistency_cost'] = hyperparams['max_consistency_cost'] model['apply_consistency_to_labeled'] = hyperparams['apply_consistency_to_labeled'] model['training_length'] = hyperparams['training_length'] model['student_dropout_probability'] = dropout model['teacher_dropout_probability'] = dropout model['input_noise'] = input_noise model['translate'] = augmentation training_batches = minibatching.training_batches(svhn.training, minibatch_size, hyperparams['n_labeled_per_batch']) evaluation_batches_fn = minibatching.evaluation_epoch_generator(svhn.evaluation, minibatch_size) tensorboard_dir = model.save_tensorboard_graph() LOG.info("Saved tensorboard graph to %r", tensorboard_dir) model.train(training_batches, evaluation_batches_fn)
def run(test_phase, data_seed, n_labeled, training_length, rampdown_length): minibatch_size = 100 n_labeled_per_batch = 100 tf.reset_default_graph() model = Model(RunContext(__file__, data_seed)) cifar = SVHN(n_labeled=n_labeled, data_seed=data_seed, test_phase=test_phase) model['ema_consistency'] = True model['max_consistency_cost'] = 0.0 model['apply_consistency_to_labeled'] = False model['rampdown_length'] = rampdown_length model['training_length'] = training_length # Turn off augmentation model['translate'] = False model['flip_horizontally'] = False training_batches = minibatching.training_batches(cifar.training, minibatch_size, n_labeled_per_batch) evaluation_batches_fn = minibatching.evaluation_epoch_generator(cifar.evaluation, minibatch_size) tensorboard_dir = model.save_tensorboard_graph() LOG.info("Saved tensorboard graph to %r", tensorboard_dir) model.train(training_batches, evaluation_batches_fn)
def saveAndRestoreModel(self, fw_lstm_layer, bw_lstm_layer, sess, saver, is_dynamic_rnn): """Saves and restores the model to mimic the most common use case. Args: fw_lstm_layer: The forward lstm layer either a single lstm cell or a multi lstm cell. bw_lstm_layer: The backward lstm layer either a single lstm cell or a multi lstm cell. sess: Old session. saver: saver created by tf.compat.v1.train.Saver() is_dynamic_rnn: use dynamic_rnn or not. Returns: A tuple containing: - Input tensor of the restored model. - Prediction tensor of the restored model. - Output tensor, which is the softwmax result of the prediction tensor. - new session of the restored model. """ model_dir = tempfile.mkdtemp() saver.save(sess, model_dir) # Reset the graph. tf.reset_default_graph() x, prediction, output_class = self.buildModel(fw_lstm_layer, bw_lstm_layer, is_dynamic_rnn) new_sess = tf.Session(config=CONFIG) saver = tf.train.Saver() saver.restore(new_sess, model_dir) return x, prediction, output_class, new_sess
def testBasic(self): base_path = tf.test.test_src_dir_path( "contrib/session_bundle/example/half_plus_two/00000123") tf.reset_default_graph() sess, meta_graph_def = session_bundle.load_session_bundle_from_path( base_path, target="", config=tf.ConfigProto(device_count={"CPU": 2})) self.assertTrue(sess) asset_path = os.path.join(base_path, constants.ASSETS_DIRECTORY) with sess.as_default(): path1, path2 = sess.run(["filename1:0", "filename2:0"]) self.assertEqual( compat.as_bytes(os.path.join(asset_path, "hello1.txt")), path1) self.assertEqual( compat.as_bytes(os.path.join(asset_path, "hello2.txt")), path2) collection_def = meta_graph_def.collection_def signatures_any = collection_def[constants.SIGNATURES_KEY].any_list.value self.assertEquals(len(signatures_any), 1) signatures = manifest_pb2.Signatures() signatures_any[0].Unpack(signatures) self._checkRegressionSignature(signatures, sess) self._checkNamedSigantures(signatures, sess)
def build(self, configuration): tf.reset_default_graph() # --- specify input data self.inputs = tf.placeholder(tf.float32, [None, 28, 28, 1], name='x') self.labels = tf.placeholder(tf.float32, [None, 10], name='labels') # tf.summary.image('input', inputs, 3) # TODO add name scopes and summaries # --- specify layers of network # TODO try another strides for conv layer # TODO try to get rid of pooling layer conv1 = tf.layers.conv2d(inputs=self.inputs, filters=configuration[0], kernel_size=[5, 5], padding="same", activation=tf.nn.relu, name='conv1') pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2, name='pool1') conv2 = tf.layers.conv2d(inputs=pool1, filters=configuration[1], kernel_size=[5, 5], padding="same", activation=tf.nn.relu, name='conv2') pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2, name='pool2') flattened = tf.reshape(pool2, [-1, 7 * 7 * configuration[1]]) dense = tf.layers.dense(inputs=flattened, units=1024, activation=tf.nn.relu, name='fc') logits = tf.layers.dense(inputs=dense, units=10, name='output') # --- specify cost function and how training is performed with tf.name_scope("train"): cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=self.labels, logits=logits) self.train_step = tf.train.AdamOptimizer(0.015).minimize(cross_entropy) # --- specify function to calculate accuracy with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(self.labels, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar("accuracy", self.accuracy) self.summary = tf.summary.merge_all()
def setUp(self): tf.reset_default_graph() self.x = tf.placeholder(tf.float64) self.x_np = np.random.randn(10) self.session = tf.Session() self.transforms = [C() for C in GPflow.transforms.Transform.__subclasses__()] self.transforms.append(GPflow.transforms.Logistic(7.3, 19.4))
def trainfold_fine(conffile,curfold,curgpu,batch_size,confname='conf'): imp_mod = importlib.import_module(conffile) conf = imp_mod.__dict__[confname] if batch_size>0: conf.batch_size = batch_size ## ext = '_fold_{}'.format(curfold) conf.valdatafilename = conf.valdatafilename + ext conf.trainfilename = conf.trainfilename + ext conf.valfilename = conf.valfilename + ext conf.fulltrainfilename += ext conf.baseoutname = conf.baseoutname + ext conf.mrfoutname += ext conf.fineoutname += ext conf.baseckptname += ext conf.mrfckptname += ext conf.fineckptname += ext conf.basedataname += ext conf.finedataname += ext conf.mrfdataname += ext os.environ['CUDA_VISIBLE_DEVICES'] = curgpu tf.reset_default_graph() self = PoseTrain.PoseTrain(conf) self.fineTrain(restore=True,trainPhase=True,trainType=0)
def DetectionImage(self): image_xy = [self.label.x0, self.label.y0, self.label.x1, self.label.y1] #print(image_xy) id = self.imgName[-10:-4] id_i = [] fg = 0 for i in id: if i != '0' or fg: id_i.append(i) fg = 1 id_i = [str(a) for a in id_i] image_id = int(''.join(id_i)) print(self.imgName) img = cv2.imread(self.imgName) jpg = img[:, :, [2, 1, 0]] #im = jpg[self.label.y0:self.label.y1, self.label.x0:self.label.x1] #cv2.namedWindow("Image") #cv2.imshow("Image", im) #cv2.waitKey(0) #cv2.destroyAllWindows() im_orig = jpg.astype(np.float32, copy=True) im_orig -= np.array([[[102.9801, 115.9465, 122.7717]]]) im_shape = im_orig.shape #print(im_shape[0], im_shape[1]) im_orig = im_orig.reshape(1, im_shape[0], im_shape[1], 3) self.im_orig = im_orig self.im_shape = im_shape self.jpg = jpg #im_orig = jpg.astype(np.float32, copy=True) #im_orig -= np.array([[[102.9801, 115.9465, 122.7717]]]) #im_shape = im_orig.shape #print(im_shape[0], im_shape[1]) #im_orig = im_orig.reshape(1, im_shape[0], im_shape[1], 3) Test_RCNN = pickle.load(open( cfg.DATA_DIR + '/' + 'Test_Faster_RCNN_R-50-PFN_2x_VCOCO_with_pose.pkl', "rb"), encoding='latin1') prior_mask = pickle.load(open(cfg.DATA_DIR + '/' + 'prior_mask.pkl', "rb"), encoding='latin1') Action_dic = json.load(open(cfg.DATA_DIR + '/' + 'action_index.json')) Action_dic_inv = {y: x for x, y in Action_dic.items()} vcocoeval = VCOCOeval( cfg.DATA_DIR + '/' + 'v-coco/data/vcoco/vcoco_test.json', cfg.DATA_DIR + '/' + 'v-coco/data/instances_vcoco_all_2014.json', cfg.DATA_DIR + '/' + 'v-coco/data/splits/vcoco_test.ids') p_weight = 'E:/projects/Transferable-Interactiveness-Network/Weights/TIN_VCOCO/HOI_iter_6000.ckpt' p_output_file = 'E:/projects/Transferable-Interactiveness-Network/-Results/TIN_VCOCO_0.6_0.4_GUItest_naked.pkl' # 最佳预测试结果 # init session tf.reset_default_graph() tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True sess = tf.Session(config=tfconfig) net = ResNet50() net.create_architecture(False) saver = tf.train.Saver() saver.restore(sess, p_weight) p_detection = [] if self.f_HO: im_detect_GUI_H(sess, net, im_orig, im_shape, image_id, image_xy, Test_RCNN, prior_mask, Action_dic_inv, 0.4, 0.6, 3, p_detection) else: im_detect_GUI_O(sess, net, im_orig, im_shape, image_id, image_xy, Test_RCNN, prior_mask, Action_dic_inv, 0.4, 0.6, 3, p_detection) # print(detection) pickle.dump(p_detection, open(p_output_file, "wb")) sess.close() # 为了得到单个图像的模型预测,对之后的图像test的NIS进行参考 # weight = 'E:/projects/Transferable-Interactiveness-Network/Weights/TIN_VCOCO/HOI_iter_6000.ckpt' weight = 'E:/projects/Transferable-Interactiveness-Network/Weights/TIN_VCOCO_Mytest/HOI_iter_10.ckpt' # init session tf.reset_default_graph() tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True sess = tf.Session(config=tfconfig) net = ResNet50() net.create_architecture(False) saver = tf.train.Saver() saver.restore(sess, weight) detection = [] if self.f_HO: im_detect_GUI_H(sess, net, im_orig, im_shape, image_id, image_xy, Test_RCNN, prior_mask, Action_dic_inv, 0.4, 0.6, 3, detection) else: im_detect_GUI_O(sess, net, im_orig, im_shape, image_id, image_xy, Test_RCNN, prior_mask, Action_dic_inv, 0.4, 0.6, 3, detection) # print(detection) # pickle.dump(detection, open(output_file, "wb")) sess.close() #test_result = detection #input_D = cfg.ROOT_DIR + '/-Results/TIN_VCOCO_0.6_0.4_GUItest_naked.pkl'#模型预测,对NIS进行参考 #with open(input_D, 'rb') as f: # test_D = pickle.load(f, encoding='latin1') output_file = 'E:/projects/Transferable-Interactiveness-Network/-Results/p_nis_detection_TIN_VCOCO_0.6_0.4_GUItest_naked.pkl' generate_result = generate_pkl_GUI(p_detection, detection, prior_mask, Action_dic_inv, (6, 6, 7, 0), prior_flag=3) with open(output_file, 'wb') as f: pickle.dump(generate_result, f) cc = plt.get_cmap('hsv', lut=6) height, width, nbands = jpg.shape figsize = width / float(80), height / float(80) fig = plt.figure(figsize=figsize) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.imshow(jpg, interpolation='nearest') HO_dic = {} HO_set = set() count = 0 if self.f_HO: #框选human,标出object for ele in generate_result: if (ele['image_id'] == image_id): action_count = -1 for action_key, action_value in ele.items(): if (action_key.split('_')[ -1] != 'agent') and action_key != 'image_id' and action_key != 'person_box' \ and action_key != 'object_class' and action_key != 'object_box' \ and action_key != 'binary_score'and action_key != 'O_det' and action_key != 'H_det': # print(action_value[:4]) # if ele['object_box'] : if (not np.isnan(action_value[0])) and ( action_value[4] > 0.01): # print(action_value[:5]) O_box = action_value[:4] H_box = ele['person_box'] action_count += 1 if tuple(O_box) not in HO_set: HO_dic[tuple(O_box)] = count HO_set.add(tuple(O_box)) count += 1 if tuple(H_box) not in HO_set: HO_dic[tuple(H_box)] = count HO_set.add(tuple(H_box)) count += 1 '''ax.add_patch( plt.Rectangle((H_box[0], H_box[1] ), H_box[2] - H_box[0], H_box[3] - H_box[1], fill=False, edgecolor=cc(HO_dic[tuple(H_box)])[:3], linewidth=5) ) ax.add_patch( plt.Rectangle((image_xy[0], image_xy[1]), image_xy[2] - image_xy[0], image_xy[3] - image_xy[1], fill=False, edgecolor=cc(HO_dic[tuple(H_box)])[:3], linewidth=2) )''' text = action_key.split( '_')[0] + ', ' + "%.2f" % action_value[4] ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35, text, bbox=dict(facecolor=cc( HO_dic[tuple(O_box)])[:3], alpha=0.5), fontsize=16, color='white') ax.add_patch( plt.Rectangle( (O_box[0], O_box[1]), O_box[2] - O_box[0], O_box[3] - O_box[1], fill=False, edgecolor=cc(HO_dic[tuple(O_box)])[:3], linewidth=2)) ax.set(xlim=[0, width], ylim=[height, 0], aspect=1) else: #框选object,标出human for ele in generate_result: if (ele['image_id'] == image_id): action_count = -1 for action_key, action_value in ele.items(): if (action_key.split('_')[ -1] != 'agent') and action_key != 'image_id' and action_key != 'person_box' \ and action_key != 'object_class' and action_key != 'object_box' \ and action_key != 'binary_score'and action_key != 'O_det' and action_key != 'H_det': # print(action_value[:4]) # if ele['object_box'] : if (not np.isnan(action_value[0])) and ( action_value[4] > 0.01): # print(action_value[:5]) O_box = action_value[:4] H_box = ele['person_box'] action_count += 1 if tuple(O_box) not in HO_set: HO_dic[tuple(O_box)] = count HO_set.add(tuple(O_box)) count += 1 if tuple(H_box) not in HO_set: HO_dic[tuple(H_box)] = count HO_set.add(tuple(H_box)) count += 1 ax.add_patch( plt.Rectangle( (H_box[0], H_box[1]), H_box[2] - H_box[0], H_box[3] - H_box[1], fill=False, edgecolor=cc(HO_dic[tuple(H_box)])[:3], linewidth=3)) '''ax.add_patch( plt.Rectangle((image_xy[0], image_xy[1]), image_xy[2] - image_xy[0], image_xy[3] - image_xy[1], fill=False, edgecolor=cc(HO_dic[tuple(H_box)])[:3], linewidth=2) )''' text = action_key.split( '_')[0] + ', ' + "%.2f" % action_value[4] ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35, text, bbox=dict(facecolor=cc( HO_dic[tuple(O_box)])[:3], alpha=0.5), fontsize=16, color='white') '''ax.add_patch( plt.Rectangle((O_box[0], O_box[1]), O_box[2] - O_box[0], O_box[3] - O_box[1], fill=False, edgecolor=cc(HO_dic[tuple(O_box)])[:3], linewidth=2) )''' ax.set(xlim=[0, width], ylim=[height, 0], aspect=1) plt.show() plt.pause(40) plt.close()
def tearDown(self): tf.reset_default_graph()
def __graph__(): # placeholders tf.reset_default_graph() # encoder inputs : list of indices of length xseq_len self.enc_ip = [ tf.placeholder(shape=[ None, ], dtype=tf.int64, name='ei_{}'.format(t)) for t in range(xseq_len) ] # labels that represent the real outputs self.labels = [ tf.placeholder(shape=[ None, ], dtype=tf.int64, name='ei_{}'.format(t)) for t in range(yseq_len) ] # decoder inputs : 'GO' + [ y1, y2, ... y_t-1 ] self.dec_ip = [ tf.zeros_like(self.enc_ip[0], dtype=tf.int64, name='GO') ] + self.labels[:-1] # Basic LSTM cell wrapped in Dropout Wrapper self.keep_prob = tf.placeholder(tf.float32) # define the basic cell basic_cell = tf.contrib.rnn.DropoutWrapper( tf.contrib.rnn.BasicLSTMCell(emb_dim, state_is_tuple=True), output_keep_prob=self.keep_prob) # stack cells together : n layered model stacked_lstm = tf.contrib.rnn.MultiRNNCell([basic_cell] * num_layers, state_is_tuple=True) # for parameter sharing between training model # and testing model with tf.variable_scope('decoder') as scope: # build the seq2seq model # inputs : encoder, decoder inputs, LSTM cell type, vocabulary sizes, embedding dimensions self.decode_outputs, self.decode_states = tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq( self.enc_ip, self.dec_ip, stacked_lstm, xvocab_size, yvocab_size, emb_dim) # share parameters scope.reuse_variables() # testing model, where output of previous timestep is fed as input # to the next timestep self.decode_outputs_test, self.decode_states_test = tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq( self.enc_ip, self.dec_ip, stacked_lstm, xvocab_size, yvocab_size, emb_dim, feed_previous=True) # now, for training, # build loss function # weighted loss # TODO : add parameter hint loss_weights = [ tf.ones_like(label, dtype=tf.float32) for label in self.labels ] self.loss = tf.contrib.legacy_seq2seq.sequence_loss( self.decode_outputs, self.labels, loss_weights, yvocab_size) # train op to minimize the loss self.train_op = tf.train.AdamOptimizer(learning_rate=lr).minimize( self.loss)
of human presence and movement detection. We use a single-LSTM layer in this network. The classes of the classification problem are 1. Empty room 2. Stationary human present 3. Moving human present. Navod Suraweera, Macquarie University """ import tensorflow as tf import numpy as np from datetime import datetime import scipy.io as sio # tf.reset_default_graph() startTime = datetime.now() #The number of classes in the classifier num_classes = 3 #Data input as a .mat file mat_train = sio.loadmat('RNN_FFT_12ele_train.mat') mat_test = sio.loadmat('RNN_FFT_12ele_test.mat') """ The length of the sequence input into the RNN in-terms of the number of time steps (T) For each T value we generate test and train data batches, which are contained in the input data mat file. """ #The number of time steps (T) in the RNN. time_steps = int(mat_train['time_steps'])
def DRL_implementation(filename,globali): try: #filename = 'benchmark_reduced/test_benchmark_5.gr' # Filename corresponds to benchmark to route # filename = '3d.txt' # filename = '8by8small.gr' # 8-by-8 10 nets toy sample # filename = '8by8simplee.gr' # 8-by-8 10 nets toy sample # filename = 'adaptecSmall.gr' # filename = '4by4small.gr' # 3-by-3 nets toy sample # filename = '4by4simple.gr' # 3-by-3 nets toy sample # filename = 'adaptec1.capo70.2d.35.50.90.gr' # # Getting Net Info with open('myResults.txt', 'a') as myFile: myFile.write(filename) grid_info = init.read(filename) gridParameters = init.gridParameters(grid_info) # # GridGraph graphcase = graph.GridGraph(init.gridParameters(grid_info)) capacity = graphcase.generate_capacity() # print ('capacity before route: ',capacity.shape) gridX,gridY,gridZ = graphcase.generate_grid() # Real Router for Multiple Net # Note: pinCoord input as absolute length coordinates gridGraphSearch = twoPinASearch.AStarSearchGraph(gridParameters, capacity) # Sort net halfWireLength = init.VisualGraph(init.gridParameters(grid_info)).bounding_length() # print('Half Wire Length:',halfWireLength) sortedHalfWireLength = sorted(halfWireLength.items(),key=operator.itemgetter(1),reverse=True) # Large2Small # sortedHalfWireLength = sorted(halfWireLength.items(),key=operator.itemgetter(1),reverse=False) # Small2Large netSort = [] for i in range(gridParameters['numNet']): order = int(sortedHalfWireLength[i][0]) netSort.append(order) # random order the nets # print('netSort Before',netSort) # random.shuffle(netSort) # print('netSort After',netSort) routeListMerged = [] routeListNotMerged = [] #print('gridParameters',gridParameters) # Getting two pin list combo (For RL) twopinListCombo = [] twopinListComboCleared = [] for i in range(len(init.gridParameters(grid_info)['netInfo'])): netNum = i netPinList = []; netPinCoord = [] for j in range(0, gridParameters['netInfo'][netNum]['numPins']): pin = tuple([int((gridParameters['netInfo'][netNum][str(j+1)][0]-gridParameters['Origin'][0])/gridParameters['tileWidth']), int((gridParameters['netInfo'][netNum][str(j+1)][1]-gridParameters['Origin'][1])/gridParameters['tileHeight']), int(gridParameters['netInfo'][netNum][str(j+1)][2]), int(gridParameters['netInfo'][netNum][str(j+1)][0]), int(gridParameters['netInfo'][netNum][str(j+1)][1])]) if pin[0:3] in netPinCoord: continue else: netPinList.append(pin) netPinCoord.append(pin[0:3]) twoPinList = [] for i in range(len(netPinList)-1): pinStart = netPinList[i] pinEnd = netPinList[i+1] twoPinList.append([pinStart,pinEnd]) twoPinListVanilla = twoPinList # Insert Tree method to decompose two pin problems here twoPinList = tree.generateMST(twoPinList) # print('Two pin list after:', twoPinList, '\n') # Remove pin pairs that are in the same grid nullPairList = [] for i in range(len(twoPinListVanilla)): if twoPinListVanilla[i][0][:3] == twoPinListVanilla[i][1][:3]: nullPairList.append(twoPinListVanilla[i]) for i in range(len(nullPairList)): twoPinListVanilla.remove(nullPairList[i]) # Remove pin pairs that are in the same grid nullPairList = [] for i in range(len(twoPinList)): if twoPinList[i][0][:3] == twoPinList[i][1][:3]: nullPairList.append(twoPinList[i]) for i in range(len(nullPairList)): twoPinList.reomove(nullPairList[i]) # Key: use original sequence of two pin pairs twopinListComboCleared.append(twoPinListVanilla) # print('twopinListComboCleared',twopinListComboCleared) twoPinEachNetClear = [] for i in twopinListComboCleared: num = 0 for j in i: num = num + 1 twoPinEachNetClear.append(num) # print('twoPinEachNetClear',twoPinEachNetClear) for i in range(len(init.gridParameters(grid_info)['netInfo'])): netNum = int(sortedHalfWireLength[i][0]) # i # Sort the pins by a heuristic such as Min Spanning Tree or Rectilinear Steiner Tree netPinList = [] netPinCoord = [] for j in range(0, gridParameters['netInfo'][netNum]['numPins']): pin = tuple([int((gridParameters['netInfo'][netNum][str(j+1)][0]-gridParameters['Origin'][0])/gridParameters['tileWidth']), int((gridParameters['netInfo'][netNum][str(j+1)][1]-gridParameters['Origin'][1])/gridParameters['tileHeight']), int(gridParameters['netInfo'][netNum][str(j+1)][2]), int(gridParameters['netInfo'][netNum][str(j+1)][0]), int(gridParameters['netInfo'][netNum][str(j+1)][1])]) if pin[0:3] in netPinCoord: continue else: netPinList.append(pin) netPinCoord.append(pin[0:3]) twoPinList = [] for i in range(len(netPinList)-1): pinStart = netPinList[i] pinEnd = netPinList[i+1] twoPinList.append([pinStart,pinEnd]) # Insert Tree method to decompose two pin problems here twoPinList = tree.generateMST(twoPinList) # print('Two pin list after:', twoPinList, '\n') # Remove pin pairs that are in the same grid nullPairList = [] for i in range(len(twoPinList)): if twoPinList[i][0][:3] == twoPinList[i][1][:3]: nullPairList.append(twoPinList[i]) for i in range(len(nullPairList)): twoPinList.reomove(nullPairList[i]) # Key: Use MST sorted pin pair sequence under half wirelength sorted nets twopinListCombo.append(twoPinList) # print('twopinListCombo',twopinListCombo) ###################################################################################### # for i in range(1): for i in range(len(init.gridParameters(grid_info)['netInfo'])): # Determine nets to wire based on sorted nets (stored in list sortedHalfWireLength) # print('*********************') # print('Routing net No.',init.gridParameters(grid_info)['netInfo'][int(sortedHalfWireLength[i][0])]['netName']) # (above output is to get actual netName) # print('Routing net No.',sortedHalfWireLength[i][0]) netNum = int(sortedHalfWireLength[i][0]) # Sort the pins by a heuristic such as Min Spanning Tree or Rectilinear Steiner Tree netPinList = [] netPinCoord = [] for j in range(0, gridParameters['netInfo'][netNum]['numPins']): pin = tuple([int((gridParameters['netInfo'][netNum][str(j+1)][0]-gridParameters['Origin'][0])/gridParameters['tileWidth']), int((gridParameters['netInfo'][netNum][str(j+1)][1]-gridParameters['Origin'][1])/gridParameters['tileHeight']), int(gridParameters['netInfo'][netNum][str(j+1)][2]), int(gridParameters['netInfo'][netNum][str(j+1)][0]), int(gridParameters['netInfo'][netNum][str(j+1)][1])]) if pin[0:3] in netPinCoord: continue else: netPinList.append(pin) netPinCoord.append(pin[0:3]) twoPinList = [] for i in range(len(netPinList)-1): pinStart = netPinList[i] pinEnd = netPinList[i+1] twoPinList.append([pinStart,pinEnd]) # Insert Tree method to decompose two pin problems here twoPinList = tree.generateMST(twoPinList) # Remove pin pairs that are in the same grid nullPairList = [] for i in range(len(twoPinList)): if twoPinList[i][0][:3] == twoPinList[i][1][:3]: nullPairList.append(twoPinList[i]) for i in range(len(nullPairList)): twoPinList.reomove(nullPairList[i]) i = 1 routeListSingleNet = [] for twoPinPair in twoPinList: pinStart = twoPinPair[0]; pinEnd = twoPinPair[1] route, cost = twoPinASearch.AStarSearchRouter(pinStart, pinEnd, gridGraphSearch) routeListSingleNet.append(route) i += 1 mergedrouteListSingleNet = [] for list in routeListSingleNet: # if len(routeListSingleNet[0]) == 2: # mergedrouteListSingleNet.append(list[0]) # mergedrouteListSingleNet.append(list[1]) # else: for loc in list: if loc not in mergedrouteListSingleNet: mergedrouteListSingleNet.append(loc) routeListMerged.append(mergedrouteListSingleNet) routeListNotMerged.append(routeListSingleNet) # Update capacity and grid graph after routing one pin pair # # WARNING: there are some bugs in capacity update # # # print(route) # capacity = graph.updateCapacity(capacity, mergedrouteListSingleNet) # gridGraph = twoPinASearch.AStarSearchGraph(gridParameters, capacity) # print('\nRoute List Merged:',routeListMerged) ##########################################################################''' twopinlist_nonet = [] for net in twopinListCombo: # for net in twopinListComboCleared: for pinpair in net: twopinlist_nonet.append(pinpair) # Get two pin numbers twoPinNum = 0 twoPinNumEachNet = [] for i in range(len(init.gridParameters(grid_info)['netInfo'])): netNum = int(sortedHalfWireLength[i][0]) # i twoPinNum = twoPinNum + (init.gridParameters(grid_info)['netInfo'][netNum]['numPins'] - 1) twoPinNumEachNet.append(init.gridParameters(grid_info)['netInfo'][netNum]['numPins'] - 1) # print('twoPinNumEachNet debug1: ',twoPinNumEachNet) # print('twopinlist_nonet',twopinlist_nonet) # DRL Module from here graphcase.max_step = 50 #20 graphcase.twopin_combo = twopinlist_nonet # print('twopinlist_nonet',twopinlist_nonet) graphcase.net_pair = twoPinNumEachNet # Setting the session to allow growth, so it doesn't allocate all GPU memory. gpu_ops = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(gpu_options=gpu_ops) sess = tf.Session(config=config) # Setting this as the default tensorflow session. keras.backend.tensorflow_backend.set_session(sess) # You want to create an instance of the DQN_Agent class here, and then train / test it. model_path = '../model/' data_path = '../data/' environment_name = 'grid' if not os.path.exists(model_path): os.makedirs(model_path) if not os.path.exists(data_path): os.makedirs(data_path) agent = DQN_Implementation.DQN_Agent(environment_name, sess, graphcase) # Burn in with search # Get a list of (observation, action, reward, observation_next, is_terminal) # with Route List Merged (solution given with A*search plus tree) graphcaseBurnIn = graph.GridGraph(init.gridParameters(grid_info)) graphcaseBurnIn.max_step = 10000 observationCombo = []; actionCombo = []; rewardCombo = [] observation_nextCombo = []; is_terminalCombo = [] for enumerator in range(300): for i in range(gridParameters['numNet']): goal = routeListMerged[i][-1] graphcaseBurnIn.goal_state = (goal[3],goal[4],goal[2],goal[0],goal[1]) for j in range(len(routeListMerged[i])-1): position = routeListMerged[i][j] nextposition = routeListMerged[i][j+1] graphcaseBurnIn.current_state = (position[3],position[4], position[2],position[0],position[1]) # print(graphcaseBurnIn.state2obsv()) observationCombo.append(graphcaseBurnIn.state2obsv()) action = graph.get_action(position,nextposition) # print('action',action) actionCombo.append(action) graphcaseBurnIn.step(action) rewardCombo.append(graphcaseBurnIn.instantreward) # graphcaseBurnIn.current_state = (nextposition[3],nextposition[4], # nextposition[2],nextposition[0],nextposition[1]) observation_nextCombo.append(graphcaseBurnIn.state2obsv()) is_terminalCombo.append(False) is_terminalCombo[-1] = True # if testing using training function, comment burn_in agent.replay = DQN_Implementation.Replay_Memory() #Remove memeory of previous training agent.burn_in_memory_search(observationCombo,actionCombo,rewardCombo, observation_nextCombo,is_terminalCombo) twoPinNum = 0 twoPinNumEachNet = [] for i in range(len(init.gridParameters(grid_info)['netInfo'])): twoPinNum = twoPinNum + (init.gridParameters(grid_info)['netInfo'][i]['numPins'] - 1) twoPinNumEachNet.append(init.gridParameters(grid_info)['netInfo'][i]['numPins'] - 1) # print ("Two pin num: ",len(graphcase.twopin_combo)) twoPinNum = len(graphcase.twopin_combo) twoPinNumEachNet = graphcase.twopin_combo # Training DRL savepath = model_path #32by32simple_model_train" # model_file = "../32by32simple_model_train/model_24000.ckpt" # print ('twoPinNumEachNet debug2',twoPinNumEachNet) # graphcase.max_step = 50 #20 # graphcase.twopin_combo = twopinlist_nonet # graphcase.net_pair = twoPinNumEachNet # Reinitialze grid graph parameters before training on new benchmarks agent.gridParameters = gridParameters agent.gridgraph.max_step = 50 #100 agent.goal_state = None; agent.init_state = None agent.gridgraph.capacity = capacity agent.gridgraph.route = [] agent.gridgraph.twopin_combo = twopinlist_nonet agent.gridgraph.twopin_pt = 0 agent.gridgraph.twopin_rdn = None agent.gridgraph.reward = 0.0 agent.gridgraph.instantreward = 0.0 agent.gridgraph.best_reward = 0.0 agent.gridgraph.best_route = [] agent.gridgraph.route_combo = [] agent.gridgraph.net_pair = twoPinEachNetClear agent.gridgraph.instantrewardcombo = [] agent.gridgraph.net_ind = 0 agent.gridgraph.pair_ind = 0 agent.gridgraph.posTwoPinNum = 0 agent.gridgraph.passby = np.zeros_like(capacity) agent.previous_action = -1 # print('twopinlist_nonet',twopinlist_nonet) episodes = agent.max_episodes # print('twopinlist_nonet',twopinlist_nonet) solution_combo_filled,reward_plot_combo,reward_plot_combo_pure,solutionTwoPin,posTwoPinNum \ = agent.train(twoPinNum,twoPinEachNetClear,netSort,savepath,gridParameters,sortedHalfWireLength,globali,model_file=None) # pkl.dump(solution_combo_filled,fileObject) # fileObject.close() twoPinListPlotRavel = [] for i in range(len(twopinlist_nonet)): twoPinListPlotRavel.append(twopinlist_nonet[i][0]) twoPinListPlotRavel.append(twopinlist_nonet[i][1]) # Generate output file for DRL solver # print('posTwoPinNum',posTwoPinNum) # print('len(graphcase.twopin_combo)',len(graphcase.twopin_combo)) if posTwoPinNum >= len(graphcase.twopin_combo): #twoPinNum: # Plot reward and save reward data n = np.linspace(1,episodes,len(reward_plot_combo)) '''########################################################## plt.figure() plt.plot(n,reward_plot_combo) plt.xlabel('episodes') plt.ylabel('reward') plt.savefig('DRLRewardPlot/test_benchmark_{dumpBench}.DRLRewardPlot.jpg'.format(dumpBench=globali+1)) # plt.show() plt.close() n = np.linspace(1,episodes,len(reward_plot_combo_pure)) plt.figure() plt.plot(n,reward_plot_combo_pure) plt.xlabel('episodes') plt.ylabel('reward') plt.savefig('DRLRewardPlot/test_benchmark_{dumpBench}.DRLRewardPlotPure.jpg'.format(dumpBench=globali+1)) plt.close()''' #filenameplot = '%s.rewardData' % filename filenameplot = 'DRLRewardPlot/rewardData_test_benchmark_{dumpBench}.txt'.format(dumpBench=globali+1) np.save(filenameplot,reward_plot_combo) # dump solution of DRL f = open('solutionsDRL/test_benchmark_{dumpBench}.gr.DRLsolution'.format(dumpBench=globali+1), 'w+') # for i in range(1): twoPinSolutionPointer = 0 routeListMerged = solution_combo_filled # print('solution_combo_filled',solution_combo_filled) for i in range(gridParameters['numNet']): singleNetRouteCache = [] singleNetRouteCacheSmall = [] indicator = i netNum = int(sortedHalfWireLength[i][0]) # i i = netNum value = '{netName} {netID} {cost}\n'.format(netName=gridParameters['netInfo'][indicator]['netName'], netID = gridParameters['netInfo'][indicator]['netID'], cost = 0) #max(0,len(routeListMerged[indicator])-1)) f.write(value) for j in range(len(routeListMerged[indicator])): for k in range(len(routeListMerged[indicator][j])-1): a = routeListMerged[indicator][j][k] b = routeListMerged[indicator][j][k+1] if (a[3],a[4],a[2],b[3],b[4],b[2]) not in singleNetRouteCache: # and (b[3],b[4],b[2]) not in singleNetRouteCacheSmall: singleNetRouteCache.append((a[3],a[4],a[2],b[3],b[4],b[2])) singleNetRouteCache.append((b[3],b[4],b[2],a[3],a[4],a[2])) # singleNetRouteCacheSmall.append((a[3],a[4],a[2])) # singleNetRouteCacheSmall.append((b[3],b[4],b[2])) diff = [abs(a[2]-b[2]),abs(a[3]-b[3]),abs(a[4]-b[4])] if diff[1] > 2 or diff[2] > 2: continue elif diff[1] == 2 or diff[2] == 2: continue elif diff[0] == 0 and diff[1] == 0 and diff[2] == 0: continue elif diff[0] + diff[1] + diff[2] >= 2: continue else: value = '({},{},{})-({},{},{})\n'.format(int(a[0]),int(a[1]),a[2],int(b[0]),int(b[1]),b[2]) f.write(value) twoPinSolutionPointer = twoPinSolutionPointer + 1 f.write('!\n') f.close() '''########################################################################## # Plot of routing for multilple net (RL solution) 3d fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.set_zlim(0.75,2.25) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) x_meshP = np.linspace(0,gridParameters['gridSize'][0]-1,200) y_meshP = np.linspace(0,gridParameters['gridSize'][1]-1,200) z_meshP = np.linspace(1,2,200) x_mesh,y_mesh = np.meshgrid(x_meshP,y_meshP) z_mesh = np.ones_like(x_mesh) ax.plot_surface(x_mesh,y_mesh,z_mesh,alpha=0.3,color='r') ax.plot_surface(x_mesh,y_mesh,2*z_mesh,alpha=0.3,color='r') plt.axis('off') plt.close() # for i in twoPinListPlotRavel: # x = [coord[0] for coord in twoPinListPlotRavel] # y = [coord[1] for coord in twoPinListPlotRavel] # z = [coord[2] for coord in twoPinListPlotRavel] # ax.scatter(x,y,z,s=25,facecolors='none', edgecolors='k') # Visualize solution for twoPinRoute in solutionTwoPin: x = []; y = []; z = [] for i in range(len(twoPinRoute)): # print(routeList[i]) # diff = [abs(routeList[i][2]-routeList[i+1][2]), # abs(routeList[i][3]-routeList[i+1][3]), # abs(routeList[i][4]-routeList[i+1][4])] # if diff[1] > 2 or diff[2] > 2: # continue # elif diff[1] == 2 or diff[2] == 2: # # print('Alert') # continue # elif diff[0] == 0 and diff[1] == 0 and diff[2] == 0: # continue # elif diff[0] + diff[1] + diff[2] >= 2: # continue # else: x.append(twoPinRoute[i][3]) y.append(twoPinRoute[i][4]) z.append(twoPinRoute[i][2]) ax.plot(x,y,z,linewidth=2.5) plt.xlim([0, gridParameters['gridSize'][0]-1]) plt.ylim([0, gridParameters['gridSize'][1]-1]) plt.savefig('DRLRewardPlot/DRLRoutingVisualize_test_benchmark_{dumpBench}.png'.format(dumpBench=globali+1)) # plt.show() plt.close() # Visualize results on 2D fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # ax.set_zlim(-0.5,2.5) ax = fig.add_subplot(111) ##########################################################################''' # for routeList in routeListNotMerged: for route in routeList: num_points = len(route) for i in range(num_points-1): pair_x = [route[i][3], route[i+1][3]] pair_y = [route[i][4], route[i+1][4]] pair_z = [route[i][2], route[i+1][2]] '''if pair_z[0] ==pair_z[1] == 1: ax.plot(pair_x, pair_y, color='blue', linewidth=2.5) if pair_z[0] ==pair_z[1] == 2: ax.plot(pair_x, pair_y, color='red', linewidth=2.5)''' '''ax.axis('scaled') ax.invert_yaxis() plt.xlim([-0.1, gridParameters['gridSize'][0]-0.9]) plt.ylim([-0.1, gridParameters['gridSize'][1]-0.9]) plt.axis('off') # for i in twoPinListPlotRavel: # x = [coord[0] for coord in twoPinListPlotRavel] # y = [coord[1] for coord in twoPinListPlotRavel] # ax.scatter(x,y,s=40, facecolors='none', edgecolors='k') plt.savefig('DRLRewardPlot/DRLRoutingVisualize_test_benchmark2d_{dumpBench}.png'.format(dumpBench=globali+1)) plt.close()''' fileNAME = './solutionsDRL/test_benchmark_{dumpBench}.gr.DRLsolution'.format(dumpBench=globali + 1) command = "perl eval2008.pl test_benchmark_{dumpBench}.gr".format(dumpBench=globali + 1) + fileNAME os.system(command) else: print("DRL fails with existing max episodes! : (") except IndexError: print ("Invalid Benchmarks! ") with open('myResults.txt', 'a') as myFile: myFile.write('\n*********Invalid Benchmarks! **********\n') agent.sess.close() tf.reset_default_graph() # graphcase.posTwoPinNum = 0 return
output_row = out_empty[:] output_row[labels.index(docs_y[x])] = 1 training.append(bag) output.append(output_row) training = numpy.array(training) output = numpy.array(output) with open("data.pickle", "wb") as f: pickle.dump((words, labels, training, output), f) #interpreting model tensorflow.reset_default_graph() net = tflearn.input_data(shape=[None, len(training[0])]) net = tflearn.fully_connected(net, 8) net = tflearn.fully_connected(net, 8) net = tflearn.fully_connected(net, len(output[0]), activation="softmax") net = tflearn.regression(net) model = tflearn.DNN(net) try: model.load("model.tflearn") except: model.fit(training, output, n_epoch=2000, batch_size=8, show_metric=True) model.save("model.tflearn")
def learn_model_vel(params,trainX1,trainX2,trainY,testX1,testX2,testY): ''' learn_model(params,trainX1,trainX2,trainY,testX1,testX2,testY): Use stochastic gradient descent to learn parameters in model. Inputs: params: dictionary with: dt: time step reg: l2 regularization strength n_coefficients: number of fourier coefficients to use to represent coupling function learning_rate: learning rate for gradient descent n_epochs: number of epochs for training batch_size: batch size for training n_oscillators: number of oscillators trainX1,trainX2,trainY: training data testX1,testX2,testY: testing data Outputs: A: estimated adjacency matrix omega: estimated natural frequencies fout: estimated coupling function values evaluated at testX1 K: estimated coupling strength ''' learning_rate=params['learning_rate'] n_epochs=params['n_epochs'] batch_size=params['batch_size'] n_oscillators=params['n_oscillators'] method=params['prediction_method'] global_seed=params['global_seed'] # contruct model tf.reset_default_graph() if global_seed>0: tf.set_random_seed(global_seed) # remove this later # initialize placeholders for inputs X1 = tf.placeholder(dtype=tf.float32, shape=(None,n_oscillators,n_oscillators,1), name="X1") X2 = tf.placeholder(dtype=tf.float32, shape=(None,n_oscillators), name="X2") y = tf.placeholder(dtype=tf.float32, shape=(None,n_oscillators), name="y") ## initialize variable A (Adjacency matrix) that is symmetric with 0 entries on the diagonal. A_rand=tf.Variable(tf.random_normal((n_oscillators,n_oscillators), mean=0.5, stddev=1/n_oscillators), name='A_rand', dtype=tf.float32) A_upper = tf.matrix_band_part(A_rand, 0, -1) A = 0.5 * (A_upper + tf.transpose(A_upper))-tf.matrix_band_part(A_upper,0,0) ## initialize variable omega (natural frequencies) omega=tf.Variable(tf.random_normal((1,n_oscillators),mean=0,stddev=1/n_oscillators,dtype=tf.float32), name='omega',dtype=tf.float32) ## initialize variable K (coupling strength value) K=tf.Variable(tf.random_normal(shape=(1,),mean=1,stddev=1/n_oscillators,dtype=tf.float32),name='K') c=np.array([1.0,1.0]) # regularization parameters for A matrix ## compute phase velocities v,fout=get_vel(A,omega,K,X1,params) ## compute predicitions if method=='rk2': k1=v k2=get_vel(A,omega,K,get_diff_tensor(X2+params['dt']*k1/2.0,params),params)[0] # compute improved velocity prediction velpred=k2 elif method=='rk4': k1=v k2=get_vel(A,omega,K,get_diff_tensor(X2+params['dt']*k1/2.0,params),params)[0] k3=get_vel(A,omega,K,get_diff_tensor(X2+params['dt']*k2/2.0,params),params)[0] k4=get_vel(A,omega,K,get_diff_tensor(X2+params['dt']*k3,params),params)[0] velpred=1/6.0*k1+1/3.0*k2+1/3.0*k3+1/6.0*k4 elif method=='euler': velpred=v else: print('Invalid prediction method. Using default of Euler.') velpred=v ## compute regularization terms for neural network weights l2_loss = tf.losses.get_regularization_loss() ## loss function computation with tf.name_scope("loss"): loss=loss_sse(velpred,y,A,c)+l2_loss ## initialize optimizer (use Adam) with tf.name_scope("train"): #optimizer=tf.train.GradientDescentOptimizer(learning_rate=learning_rate) optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate,beta1=0.9,beta2=0.999) training_op=optimizer.minimize(loss) ## compute error to be displayed (currently ignores regularization terms) with tf.name_scope("eval"): error=loss_sse(velpred,y,A,np.array([0.0,0.0])) # no Aij error away from 0,1 init=tf.global_variables_initializer() ## initialize variables and optimize variables with tf.Session() as sess: init.run() ## loop for batch gradient descent for epoch in range(n_epochs): for X1_batch,X2_batch, y_batch in shuffle_batch(add_dim(trainX1), trainX2,trainY, batch_size): sess.run(training_op, feed_dict={X1: X1_batch, X2: X2_batch, y: y_batch}) error_batch = error.eval(feed_dict={X1: X1_batch, X2: X2_batch, y: y_batch}) error_val = error.eval(feed_dict={X1: add_dim(testX1), X2: testX2, y: testY}) ## display results every 20 epochs if epoch % 20==0: print('',end='\n') print("Epoch:",epoch, "Batch error:", error_batch, "Val error:", error_val,end='') else: print('.',end='') #print(tf.trainable_variables()) print('',end='\n') return(A.eval(), omega.eval(), fout.eval(feed_dict={X1: add_dim(np.angle(np.exp(1j*testX1))), X2: testX2, y: testY}), K.eval(),error_val)
def mnist_model(learning_rate, use_two_conv, use_two_fc, hparam): tf.reset_default_graph() sess = tf.Session() # Setup placeholders, and reshape the data x = tf.placeholder(tf.float32, shape=[None, 784], name="x") x_image = tf.reshape(x, [-1, 28, 28, 1]) tf.summary.image('input', x_image, 3) y = tf.placeholder(tf.float32, shape=[None, 10], name="labels") if use_two_conv: conv1 = conv_layer(x_image, 1, 32, "conv1") conv_out = conv_layer(conv1, 32, 64, "conv2") else: conv1 = conv_layer(x_image, 1, 64, "conv") conv_out = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") flattened = tf.reshape(conv_out, [-1, 7 * 7 * 64]) if use_two_fc: fc1 = fc_layer(flattened, 7 * 7 * 64, 1024, False, "fc1") embedding_input = fc1 embedding_size = 1024 #logits = fc_layer(fc1, 1024, 10, "fc2") logits = fc_layer(fc1, 1024, 10, True, "fc2") else: embedding_input = flattened embedding_size = 7*7*64 logits = fc_layer(flattened, 7*7*64, 10, True, "fc") with tf.name_scope("xent"): xent = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( logits=logits, labels=y), name="xent") tf.summary.scalar("xent", xent) with tf.name_scope("train"): train_step = tf.train.MomentumOptimizer(learning_rate,0.9).minimize(xent) with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar("accuracy", accuracy) summ = tf.summary.merge_all() embedding = tf.Variable(tf.zeros([1024, embedding_size]), name="test_embedding") assignment = embedding.assign(embedding_input) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter(LOGDIR + hparam) writer.add_graph(sess.graph) config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig() embedding_config = config.embeddings.add() embedding_config.tensor_name = embedding.name embedding_config.sprite.image_path = LOGDIR + 'sprite_1024.png' embedding_config.metadata_path = LOGDIR + 'labels_1024.tsv' # Specify the width and height of a single thumbnail. embedding_config.sprite.single_image_dim.extend([28, 28]) tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config) for i in range(2000): batch = mnist.train.next_batch(100) if i % 5 == 0: [train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]}) writer.add_summary(s, i) print("Training Acc : " + str(i) + ": " + str(train_accuracy) ) if i % 500 == 0: sess.run(assignment, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]}) saver.save(sess, os.path.join(LOGDIR, "model.ckpt"), i) sess.run(train_step, feed_dict={x: batch[0], y: batch[1]}) test_accuracy = sess.run(accuracy, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]}) print("Test accuracy is {}".format(test_accuracy))
def close(self): if self.session is not None: print('DELF: closing tf session') self.session.close() tf.reset_default_graph()
def RetrieveImage(self): id = self.imgName[-10:-4] id_i = [] fg = 0 for i in id: if i != '0' or fg: id_i.append(i) fg = 1 id_i = [str(a) for a in id_i] image_id = int(''.join(id_i)) print(self.imgName) img = cv2.imread(self.imgName) jpg = img[:, :, [2, 1, 0]] im_orig = jpg.astype(np.float32, copy=True) im_orig -= np.array([[[102.9801, 115.9465, 122.7717]]]) im_shape = im_orig.shape im_orig = im_orig.reshape(1, im_shape[0], im_shape[1], 3) self.im_orig = im_orig self.im_shape = im_shape self.jpg = jpg Test_RCNN = pickle.load(open( cfg.DATA_DIR + '/' + 'Test_Faster_RCNN_R-50-PFN_2x_VCOCO_with_pose.pkl', "rb"), encoding='latin1') prior_mask = pickle.load(open(cfg.DATA_DIR + '/' + 'prior_mask.pkl', "rb"), encoding='latin1') Action_dic = json.load(open(cfg.DATA_DIR + '/' + 'action_index.json')) Action_dic_inv = {y: x for x, y in Action_dic.items()} vcocoeval = VCOCOeval( cfg.DATA_DIR + '/' + 'v-coco/data/vcoco/vcoco_test.json', cfg.DATA_DIR + '/' + 'v-coco/data/instances_vcoco_all_2014.json', cfg.DATA_DIR + '/' + 'v-coco/data/splits/vcoco_test.ids') p_weight = 'E:/projects/Transferable-Interactiveness-Network/Weights/TIN_VCOCO/HOI_iter_6000.ckpt' p_output_file = 'E:/projects/Transferable-Interactiveness-Network/-Results/p_detection_TIN_VCOCO_0.6_0.4_GUItest_naked.pkl' # 最佳预测试结果 # init session tf.reset_default_graph() tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True sess = tf.Session(config=tfconfig) net = ResNet50() net.create_architecture(False) saver = tf.train.Saver() saver.restore(sess, p_weight) p_detection = [] im_detect_GUI(sess, net, im_orig, im_shape, image_id, Test_RCNN, prior_mask, Action_dic_inv, 0.4, 0.6, 3, p_detection) # print(detection) pickle.dump(p_detection, open(p_output_file, "wb")) sess.close() # 为了得到单个图像的模型预测,对之后的图像test的NIS进行参考 #weight = 'E:/projects/Transferable-Interactiveness-Network/Weights/TIN_VCOCO/HOI_iter_6000.ckpt' weight = 'E:/projects/Transferable-Interactiveness-Network/Weights/TIN_VCOCO_Mytest/HOI_iter_10.ckpt' # init session tf.reset_default_graph() tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True sess = tf.Session(config=tfconfig) net = ResNet50() net.create_architecture(False) saver = tf.train.Saver() saver.restore(sess, weight) detection = [] im_detect_GUI(sess, net, im_orig, im_shape, image_id, Test_RCNN, prior_mask, Action_dic_inv, 0.4, 0.6, 3, detection) # print(detection) #pickle.dump(detection, open(output_file, "wb")) sess.close() #test_result = detection #input_D = cfg.ROOT_DIR + '/-Results/TIN_VCOCO_0.6_0.4_GUItest_naked.pkl' # 模型预测,对NIS进行参考 #with open(input_D, 'rb') as f: # test_D = pickle.load(f, encoding='latin1') output_file = 'E:/projects/Transferable-Interactiveness-Network/-Results/p_nis_detection_TIN_VCOCO_0.6_0.4_GUItest_naked.pkl' generate_result = generate_pkl_GUI(p_detection, detection, prior_mask, Action_dic_inv, (6, 6, 7, 0), prior_flag=3) with open(output_file, 'wb') as f: pickle.dump(generate_result, f) max = 50 min = 0 action_name = [] action_score = [] action_HO_weight = [] for ele in generate_result: if (ele['image_id'] == image_id): for action_key, action_value in ele.items(): if (action_key.split('_')[-1] != 'agent') and action_key != 'image_id' and action_key != 'person_box' \ and action_key != 'object_class' and action_key != 'object_box' \ and action_key != 'binary_score'and action_key != 'O_det' and action_key != 'H_det': if (not np.isnan( action_value[0])) and (action_value[4] > 0.01): O_box = action_value[:4] H_box = ele['person_box'] HO_weight = ((O_box[2] - O_box[0]) * (O_box[3] - O_box[1])) / ( (H_box[2] - H_box[0]) * (H_box[3] - H_box[1])) action_name.append(action_key.split('_')[0]) action_score.append(action_value[4]) action_HO_weight.append(HO_weight) Image_action_MaxScore = np.max(action_score) normalization = (Image_action_MaxScore - min) / (max - min) Image_action_MaxScore_name = action_name[action_score.index( Image_action_MaxScore)] P_HO_weight = action_HO_weight[action_score.index( Image_action_MaxScore)] p_similar_score = normalization * 0.5 + 0.5 #print(action_score) #print(Image_action_MaxScore) #print(Image_action_MaxScore_name) Detection = pickle.load( open(cfg.ROOT_DIR + "/Results/CVPR_best_VCOCO_nis_best_lis.pkl", "rb")) image_ids = np.loadtxt( open(cfg.DATA_DIR + '/' + 'v-coco/data/splits/vcoco_test.ids', 'r')) D_action_image_id = [] D_action_image_score = [] D_action_image_verb_score = [] for ele in Detection: D_action_name = [] D_action_score = [] D_action_HO_weight = [] if ele['image_id'] in image_ids: # print(ele['image_id']) for action_key, action_value in ele.items(): if (action_key.split('_')[-1] != 'agent') and action_key != 'image_id' \ and action_key != 'person_box' and action_key != 'object_class' \ and action_key != 'object_box' and action_key != 'binary_score'\ and action_key != 'O_det' and action_key != 'H_det': if (not np.isnan( action_value[0])) and (action_value[4] > 0.01): O_box = action_value[:4] H_box = ele['person_box'] HO_weight = ((O_box[2] - O_box[0]) * (O_box[3] - O_box[1])) / ( (H_box[2] - H_box[0]) * (H_box[3] - H_box[1])) D_action_name.append(action_key.split('_')[0]) D_action_score.append(action_value[4]) D_action_HO_weight.append(HO_weight) if len(D_action_score) == 0: continue D_Image_action_MaxScore = np.max(D_action_score) #max_HO_weight = np.max(D_action_HO_weight) D_Image_action_MaxScore_name = D_action_name[D_action_score.index( D_Image_action_MaxScore)] D_Image_action_MaxScore_HO_weight = D_action_HO_weight[ D_action_score.index(D_Image_action_MaxScore)] if D_Image_action_MaxScore_name == Image_action_MaxScore_name: D_action_image_id.append(ele['image_id']) normalization = (D_Image_action_MaxScore - min) / (max - min) similar_score = normalization * 0.2 + ( (abs(P_HO_weight - D_Image_action_MaxScore_HO_weight) - 252)**2) / 63500 * 0.8 #归一化得分 D_action_image_score.append(similar_score) D_action_image_verb_score.append(D_Image_action_MaxScore) #All_Image_action_MaxScore = np.max(D_action_image_score) #All_Image_action_MaxScore_id = D_action_image_id[D_action_image_score.index(All_Image_action_MaxScore)] new_list_arr = np.array(D_action_image_score) list = np.argsort(-new_list_arr) All_Image_action_MaxScore_id = [] All_Image_action_MaxScore_sim = [] All_Image_action_MaxScore_ver = [] for i in list: im_file = str( 'E:/DATASETS/VCOCO/v-coco/coco/images/val2014/COCO_val2014_' + (str(D_action_image_id[i]).zfill(12) + '.jpg')) #self.textBrowser.append("<html><p><a href="+im_file+">Image:{0}<br>S_score:{1}<br>V_scroe:{2}</a></p></html>".format(str(D_action_image_id[i]), str('%.4f' % D_action_image_score[i]),str('%.4f' % D_action_image_verb_score[i]))) self.textBrowser.append( "Image:{0}\r\nS_score:{1}\r\nV_scroe:{2}\r\n".format( str(D_action_image_id[i]), str('%.4f' % D_action_image_score[i]), str('%.4f' % D_action_image_verb_score[i]))) self.cursor = self.textBrowser.textCursor() self.textBrowser.moveCursor(self.cursor.End) All_Image_action_MaxScore_id.append(D_action_image_id[i]) All_Image_action_MaxScore_sim.append(D_action_image_score[i]) All_Image_action_MaxScore_ver.append(D_action_image_verb_score[i]) self.childwin.show() self.childwin.image_show(list, All_Image_action_MaxScore_id, All_Image_action_MaxScore_sim, All_Image_action_MaxScore_ver)
def setUp(self): tf.reset_default_graph()
def load_pretrained_model(self, run_name = 'reddit_comment_generator', checkpoint_dir = './GPT2/checkpoint'): tf.reset_default_graph() self.tf_sess = gpt2.start_tf_sess() gpt2.load_gpt2(self.tf_sess, run_name = run_name, checkpoint_dir = checkpoint_dir)
def build_model(self): """ Implements the tensorflow computational graph for the child model to be trained """ tf.reset_default_graph() print("Building model...") with tf.name_scope('Conv1'): w_conv1 = tf.get_variable( "conv1_weights", shape=[5, 5, 3, 16], initializer=tf.contrib.layers.xavier_initializer_conv2d(), dtype=tf.float64) b_conv1 = tf.Variable(tf.constant(0.05, shape=[16], dtype=tf.float64), name='conv1_biases') conv1 = tf.nn.conv2d(input=self.x, filter=w_conv1, padding="SAME", strides=[1, 1, 1, 1]) conv1 = tf.nn.relu(conv1 + b_conv1) with tf.name_scope('Pool1'): pool1 = tf.nn.max_pool(value=conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") with tf.name_scope('Conv2'): w_conv2 = tf.get_variable( "conv2_weights", shape=[5, 5, 16, 36], initializer=tf.contrib.layers.xavier_initializer_conv2d(), dtype=tf.float64) b_conv2 = tf.Variable(tf.constant(0.05, shape=[36], dtype=tf.float64), name='conv2_biases') conv2 = tf.nn.conv2d(input=pool1, filter=w_conv2, padding="SAME", strides=[1, 1, 1, 1]) conv2 += b_conv2 with tf.name_scope('Pool2'): pool2 = tf.nn.max_pool(value=conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") with tf.name_scope('FullyConnected1'): w_fc1 = tf.get_variable( "fc1_weights", shape=[1764, 128], initializer=tf.contrib.layers.xavier_initializer_conv2d(), dtype=tf.float64) b_fc1 = tf.Variable(tf.constant(0.05, shape=[128], dtype=tf.float64), name='fc1_biases') flatten_input = tf.reshape(pool2, [-1, 1764]) fc1 = tf.nn.relu(tf.matmul(flatten_input, w_fc1) + b_fc1) with tf.name_scope('FullyConnected2'): w_fc2 = tf.get_variable( "fc2_weights", shape=[128, 10], initializer=tf.contrib.layers.xavier_initializer_conv2d(), dtype=tf.float64) b_fc2 = tf.Variable(tf.constant(0.05, shape=[10], dtype=tf.float64), name='fc2_biases') logits = tf.matmul(fc1, w_fc2) + b_fc2 probabilities = tf.nn.softmax(logits) predictions = tf.argmax(probabilities, axis=1) with tf.name_scope("cross_entropy"): self.loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=self.y), name="cross_entropy") with tf.name_scope("train"): self.train_step = tf.train.AdamOptimizer(self.lr).minimize( self.loss) with tf.name_scope("accuracy"): y_cls = tf.argmax(self.y, axis=1) correct_prediction = tf.equal(predictions, y_cls) self.accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float64))
def run(self): tf.reset_default_graph() print('') self.logger.info('===== building graph start =====') with tf.Graph().as_default() as graph: # datafeed self.logger.info('* datafeed') input_pipeline = InputPipeline( records=self._records_file, records_type=RecordsParser.RECORDS_UNLABELLED, shuffle_buffer_size=0, batch_size=self._batch_size, num_preprocessing_threads=self.NUM_PREPROCESSING_THREADS, num_repeat=1, preprocessing_fn=self._get_resize_function( self._image_height, self._image_width), preprocessing_kwargs={}, drop_remainder=True, compute_bpp=False, shuffle=False) images = input_pipeline.next_batch()[0] image_shape = images.get_shape().as_list() self.logger.info('image_shape: {}'.format(image_shape)) # compression op self.logger.info('* compression') images_per_compression = [] bpp_op_per_compression = [] for j, compression_level in enumerate(self.COMPRESSION_LEVELS): # compress batch with tf.name_scope( 'compression_jpeg_{}'.format(compression_level)): with tf.device(CPU_DEVICE): # -> jpeg compression on cpu img_batch_compressed, _bpp = TFJpeg.encode_decode_image_batch( image_batch=tf.cast(images, tf.uint8), quality=compression_level, image_shape=image_shape[1:]) images_per_compression.append( tf.cast(img_batch_compressed, tf.float32)) bpp_op_per_compression.append(_bpp) # compute distortions self.logger.info('* distortions') distortions_obj_per_compression = [ Distortions(reconstructed_images=c_img_batch, original_images=tf.cast(images, tf.float32), lambda_ms_ssim=1.0, lambda_psnr=1.0, lambda_feature_loss=1.0, data_format=self.DATA_FORMAT, loss_net_kwargs=None) for c_img_batch in images_per_compression ] distortions_ops_per_compression = [{ 'ms_ssim': d.compute_ms_ssim() } for d in distortions_obj_per_compression] graph.finalize() with tf.Session(config=get_sess_config(allow_growth=True), graph=graph) as sess: distortions_values_per_compression = [{ key: list() for key in self.DISTORTION_KEYS } for _ in self.COMPRESSION_LEVELS] bpp_values_per_compression = [ list() for _ in self.COMPRESSION_LEVELS ] n_images_processed = 0 n_images_processed_per_second = deque(10 * [0.0], 10) progress( n_images_processed, self._dataset.NUM_VAL, '{}/{} images processed'.format(n_images_processed, self._dataset.NUM_VAL)) try: while True: batch_start_time = time.time() # compute distortions and bpp batch_bpp_values_per_compression, batch_distortions_values_per_compression = sess.run( [ bpp_op_per_compression, distortions_ops_per_compression ]) # collect values for comp_level, (dist_comp, bpp_comp) in enumerate( zip(batch_distortions_values_per_compression, batch_bpp_values_per_compression)): bpp_values_per_compression[comp_level].extend(bpp_comp) for key in self.DISTORTION_KEYS: distortions_values_per_compression[comp_level][ key].append(dist_comp[key]) n_images_processed += len( batch_bpp_values_per_compression[0]) n_images_processed_per_second.append( len(batch_bpp_values_per_compression[0]) / (time.time() - batch_start_time)) progress(n_images_processed, self._dataset.NUM_VAL, status='{}/{} images processed ({} img/s)'.format( n_images_processed, self._dataset.NUM_VAL, np.mean([ t for t in n_images_processed_per_second ]))) except tf.errors.OutOfRangeError: self.logger.info( 'reached end of dataset; processed {} images'.format( n_images_processed)) except KeyboardInterrupt: self.logger.info( 'manual interrupt; processed {}/{} images'.format( n_images_processed, self._dataset.NUM_VAL)) return [(np.nan, np.nan) for _ in self.COMPRESSION_LEVELS] mean_bpp_values_per_compression = [ np.mean(bpp_vals) for bpp_vals in bpp_values_per_compression ] mean_dist_values_per_compression = [{ key: np.mean(arr) for key, arr in dist_dict.items() } for dist_dict in distortions_values_per_compression] self._save_results(mean_bpp_values_per_compression, mean_dist_values_per_compression, 'jpeg', self.COMPRESSION_LEVELS)
def reset_env(seed = 42): tf.reset_default_graph() tf.set_random_seed(seed) np.random.seed(seed)
else: train_data = create_train_data( 5) # attribute label = 4 (old/young column) # Build CNN from tensorflow and tflearn libraries # CNN with 6 convolution layers and one fully connected layer 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 print('Building model...') import tensorflow as tf tf.reset_default_graph() # resets graph if new model built cnn = input_data(shape=[None, img_size, img_size, 1], name='input') cnn = conv_2d(cnn, 32, 2, activation='tanh') cnn = max_pool_2d(cnn, 2) cnn = conv_2d(cnn, 64, 2, activation='tanh') cnn = max_pool_2d(cnn, 2) cnn = conv_2d(cnn, 32, 2, activation='tanh') cnn = max_pool_2d(cnn, 2) cnn = conv_2d(cnn, 64, 2, activation='tanh') cnn = max_pool_2d(cnn, 2)
def train_network(self): """ trains a network with preprocessed data """ tf.reset_default_graph() tf.set_random_seed(1) x = tf.placeholder(tf.float32, shape=[None] + list(self.frame_shape), name="input") y = tf.placeholder(tf.float32, shape=[None, 1], name="y") keep_prob = tf.placeholder(tf.float32, name="keep_prob") # Convolution # output = self.conv2d(x, 32, (3, 3), (1, 1), (2,2), (2,2)) # output = self.conv2d(output, 64, (3, 3), (1, 1), (2, 2), (2, 2)) output = self.conv2d(x, 5, (3, 3), (1, 1), (2, 2), (2, 2)) output = self.conv2d(output, 10, (3, 3), (2, 2), (2, 2), (2, 2)) output = tf.nn.dropout(output, keep_prob) output = tf.contrib.layers.flatten(output) # Fully Connected Layer output = self.fully_connected(output, 15) output = self.fully_connected(output, 1) output = tf.identity(output, name="output") # cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y)) error = tf.subtract(y, output) cost = tf.reduce_mean(tf.square(error)) optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(cost) # init = tf.global_variables_initializer() if not self.trained: with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print("Number of epochs: %s" % (self.epochs)) number_batches = int( len(self.frame_label_queue) / self.batch_size) print("Number of batches: %s" % (number_batches)) data_size = len(self.frame_label_queue) repeat_size = 3 for _ in range(repeat_size): batch_init = 0 batch_end = self.batch_size for batch in range(number_batches): # for train_frame, train_label in zip(train_frames, train_labels): if data_size - batch_init < self.batch_size: batch_end = data_size if batch_end - batch_init == 0: break print(len( self.frame_label_queue[batch_init:batch_end])) self.process( self.frame_label_queue[batch_init:batch_end]) print("----- Batch %s -----" % (batch + 1)) for epoch in range(self.epochs): sess.run(optimizer, feed_dict={ x: self.frames, y: self.labels, keep_prob: self.keep_prob, }) print("Epoch: %s Error: %s" % (epoch, sess.run(cost, feed_dict={ x: self.frames, y: self.labels, keep_prob: self.keep_prob, }))) batch_init += self.batch_size batch_end += self.batch_size # Save Model self.saver = tf.train.Saver() self.saver.save(sess, self.save_path) self.trained = True
def run(cfg, dataset_path, logs_dir, checkpoints_dir, checkpoints=True): with tf.Session() as sess: get_data = from_files.get_data_provider(dataset_path, cfg.batch_size) get_gen = from_files.get_genconst_provider(dataset_path, cfg.batch_size) if cfg.validation: get_valid_data = from_files.get_valid_provider( dataset_path, cfg.batch_size) get_valid_const = from_files.get_validconst_provider( dataset_path, cfg.batch_size) else: get_valid_data = None get_valid_const = None get_noise = cfg.noise_provider(**cfg.noise_provider_args) if cfg.fid_model is not None: fid = create_fid_func(cfg.fid_model, sess) else: fid = lambda x, y: 0 # Building models Z = tf.placeholder(tf.float32, ( None, cfg.zx, cfg.zx, cfg.nz, ), name="Z") X = tf.placeholder(tf.float32, ( None, cfg.npx, cfg.npx, cfg.channels, ), name="X") C = tf.placeholder(tf.float32, ( None, cfg.npx, cfg.npx, cfg.channels, ), name="C") Cf = tf.placeholder(tf.float32, ( None, cfg.npx, cfg.npx, cfg.channels, ), name="Cf") D = cfg.discriminator(X, C, **cfg.disc_args) G = cfg.generator(Z, Cf, **cfg.gen_args) D_out = D.output G_out = G.output with tf.name_scope("DG"): DG = D([G_out, Cf]) # Objectives with tf.name_scope("D_real_objective"): D_real_objective = cfg.loss_disc_real(D, G, Z, X, C, Cf) with tf.name_scope("D_fake_objective"): D_fake_objective = cfg.loss_disc_fake(D, G, Z, X, C, Cf) with tf.name_scope("D_objective"): D_objective = D_real_objective + D_fake_objective with tf.name_scope("MSE_cost"): mse = cfg.regularizer(D, G, Z, X, C, Cf) with tf.name_scope("G_objective"): G_real_objective = cfg.loss_gen(D, G, Z, X, C, Cf) G_objective = G_real_objective + (cfg.lmbda * mse) # Optimizers D_optimizer = cfg.disc_optimizer(**cfg.disc_optimizer_args) G_optimizer = cfg.gen_optimizer(**cfg.gen_optimizer_args) D_cost = D_optimizer.minimize(D_objective, var_list=D.trainable_weights) G_cost = G_optimizer.minimize(G_objective, var_list=G.trainable_weights) # Logging costs msesumm = tf.summary.scalar("MSE_train", mse) drealcostsumm = tf.summary.scalar("D_real_cost", D_real_objective) dfakecostsumm = tf.summary.scalar("D_fake_cost", D_fake_objective) gcostsumm = tf.summary.scalar("G_cost", G_real_objective) # Logging images constimgpl = tf.placeholder(tf.float32, shape=(1, cfg.npx, cfg.npx, 3)) consttrueimgpl = tf.placeholder(tf.float32, shape=(1, cfg.npx, cfg.npx, 3)) imgpl = tf.placeholder(tf.float32, shape=(1, cfg.npx, cfg.npx, cfg.channels)) trueimgpl = tf.placeholder(tf.float32, shape=(1, cfg.npx, cfg.npx, cfg.channels)) imgsummaries = [ tf.summary.image("Generated_const", constimgpl), tf.summary.image("Ground_truth_const", consttrueimgpl), tf.summary.image("Generated", imgpl), tf.summary.image("Ground_truth", trueimgpl) ] imgsumm = tf.summary.merge(imgsummaries) # Logging weights histograms weightsumms = [] for layer in D.layers: i = 0 for vect in layer.trainable_weights: weightsumms.append( tf.summary.histogram("D_" + layer.name + str(i), vect)) for layer in G.layers: i = 0 for vect in layer.trainable_weights: weightsumms.append( tf.summary.histogram("G_" + layer.name + str(i), vect)) weightsum = tf.summary.merge(weightsumms) # Setting up the training sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter(logs_dir + os.sep + cfg.name, tf.get_default_graph()) os.mkdir(checkpoints_dir) writer.flush() # Do the actual training for epoch in range(cfg.epochs): bar = progressbar.ProgressBar(maxvalue=cfg.epoch_iters, redirect_stdout=True) print( "----------------------------------------------------Epoch " + str(epoch) + "----------------------------------------------------") for it in bar(range(int(cfg.dataset_size / cfg.batch_size))): # Training D x_real, c_real = get_data() c_fake = get_gen() noise = get_noise(cfg.batch_size) _, drealout, dfakeout = sess.run( [D_cost, drealcostsumm, dfakecostsumm], feed_dict={ X: x_real, Z: noise, C: c_real, Cf: c_fake }) # Training G c_fake = get_gen() noise = get_noise(cfg.batch_size) _, mseout, gout = sess.run([G_cost, msesumm, gcostsumm], feed_dict={ Z: noise, Cf: c_fake }) # Logging losses t = int(cfg.dataset_size / cfg.batch_size) * epoch + it writer.add_summary(drealout, t) writer.add_summary(dfakeout, t) writer.add_summary(gout, t) writer.add_summary(mseout, t) # Epoch ended # Logging metrics on validation set curmets = {} generated = [] valid_data = [] valid_consts = [] bar = progressbar.ProgressBar(maxvalue=int(cfg.valid_size / cfg.batch_size), redirect_stdout=True) print("Generating on validation") for i in bar(range(int(cfg.valid_size / cfg.batch_size))): real_imgs = get_valid_data() consts = get_valid_const() if len(real_imgs) == cfg.batch_size: noise = get_noise(cfg.batch_size) generated.extend( list(sess.run(G_out, feed_dict={ Z: noise, Cf: consts }))) valid_data.extend(list(real_imgs)) valid_consts.extend(list(consts)) generated = np.asarray(generated) valid_data = np.asarray(valid_data) valid_consts = np.asarray(valid_consts) for m in cfg.metrics: if m not in curmets: curmets[m] = [] curmets[m].append(cfg.metrics[m](valid_data, generated)) metricslist = [ tf.Summary.Value(tag="MSE", simple_value=metrics.mse( generated, valid_consts)), tf.Summary.Value(tag="FID", simple_value=fid(valid_data, generated)) ] for m in curmets.keys(): metricslist.append( tf.Summary.Value(tag=m, simple_value=np.mean(curmets[m]))) metricsout = tf.Summary(value=metricslist) # Logging weights histograms weightout = sess.run(weightsum) # Logging images print("Logging images") true_img = np.expand_dims(x_real[0], axis=0) const = np.expand_dims(c_real[0], axis=0) noise = get_noise(1) img = sess.run(G_out, feed_dict={Z: noise, Cf: const}) imgout = sess.run(imgsumm, feed_dict={ trueimgpl: true_img, imgpl: img, constimgpl: log.constraints_image(img, const), consttrueimgpl: log.constraints_image(true_img, const) }) writer.flush() # Writing all logs as tensorboard writer.add_summary(metricsout, epoch) writer.add_summary(imgout, epoch) writer.add_summary(weightout, epoch) writer.flush() # Saving weights if checkpoints: G.save(checkpoints_dir + os.sep + "G_" + str(epoch) + ".hdf5", include_optimizer=False) D.save(checkpoints_dir + os.sep + "D_" + str(epoch) + ".hdf5", include_optimizer=False) # Run end writer.close() tf.reset_default_graph()
def Lr_finder(epochs = 10, min_lr = 1e-5, max_lr = 10, params = ['Lr_finder'], fin_wt_dec = 0, fin_dropout = 0, vanilla = False, wt_dec_gridsearch = False, dropout_gridsearch = False): """ Arguments: epochs: Number of epochs to run min_lr: Max_learning rate in one-cycle policy max_lr: Min_learning rate in one-cycle policy params: Grid Search parameters fin_wt_dec: Constant weight decay value to be used while training fin_dropout: Constant dropout value to be used while training vanilla: Boolean, whether to run vanilla LR_finder wt_dec_gridsearch: Boolean, whether to run grid search over wt_decay values dropout_gridsearch: Boolean, whether to run grid search over dropout values """ # Get the learning rate collection corresponding to the first part of the training g_lr_finder = tf.get_default_graph(); with g_lr_finder.as_default(): plt.figure(figsize = (20, 10)); epochs = epochs*2; tf.set_random_seed(42); with tf.Session(graph = g_lr_finder) as sess: # Initialize the model logits, loss, accuracy = initialize_model(); for ind, val in enumerate(params): # Run Grid_Search for wt_dec values if boolean wt_dec_gridsearch is true if wt_dec_gridsearch: print('Grid_Search statrted corresponding to wt_dec: ' + str(val)); train_step = training(loss, val); else: train_step = training(loss, fin_wt_dec); # Run Grid_Search for Dropout values if boolean dropout_gridsearch is true if dropout_gridsearch: print('Grid_Search statrted corresponding to dropout: ' + str(val)); drop_prob = val; else: drop_prob = fin_dropout; # Run Vanilla LR_finder if boolean vanilla is true if vanilla: print("Vanilla Lr_finder Started:"); # Get the iterators, LR collection and momentum collection handle, next_element, train_iter, train_handle, val_iter, val_handle, test_iter, test_handle = get_dataset_iterators(sess); learning_rate_collection = []; val_acc_collection = []; sess.run([tf.global_variables_initializer()]); sess.run(train_iter.initializer); lr_coll, mom_coll = lr_mom_calculator(epochs, max_lr, min_lr, False); for ptr in range(len(lr_coll)): curr_lr = lr_coll[ptr]; curr_beta1 = mom_coll[ptr]; try: X_train_batch, y_train_batch = sess.run(next_element, feed_dict = {handle: train_handle}); except tf.errors.OutOfRangeError: sess.run(train_iter.initializer) # Run the training step to update the parameters and collect loss and acc values _, train_loss, train_acc = sess.run([train_step, loss, accuracy], feed_dict = {X_ph: X_train_batch, y_ph: y_train_batch, lr_ph: curr_lr, train_mode: True, beta1_ph: curr_beta1, dropout: drop_prob}) # Evaluate the trained model on eval set after every certain number of iterations if ptr % 25 == 0 and ptr > num_train_iters: sess.run(val_iter.initializer); tot_val_loss = 0; tot_val_acc = 0; for val_ptr in range(num_val_iters + 1): # Grab a batch of Validation set and calculate running sum of some useful statistics try: X_val_batch, y_val_batch = sess.run(next_element, feed_dict = {handle: val_handle}); except tf.errors.OutOfRangeError: sess.run(val_iter.initializer); val_loss, val_acc = sess.run([loss, accuracy], feed_dict = {X_ph: X_val_batch, y_ph: y_val_batch, train_mode: False, dropout: 0}); tot_val_loss += val_loss; tot_val_acc += val_acc; val_acc_collection.append(tot_val_acc/(num_val_iters + 1)); learning_rate_collection.append(curr_lr); # Get a smoothed version of the accuracy plot!! val_acc_collection_smooth = smooth(val_acc_collection); plot_graph(learning_rate_collection, val_acc_collection_smooth, str(val), colors[ind]); # Terminate the Session""" sess.close(); # Reset the default graph in order to ensure that weights of the network at start are random for each Grid_search_param""" tf.reset_default_graph(); # Show the plot plt.show()
def reset(): global _PLACEHOLDER_CACHE global VARIABLES _PLACEHOLDER_CACHE = {} VARIABLES = {} tf.reset_default_graph()
def test_tf_faster_rcnn(art_warning, get_mnist_dataset): try: master_seed(seed=1234, set_tensorflow=True) # Only import if object detection module is available from art.estimators.object_detection.tensorflow_faster_rcnn import TensorFlowFasterRCNN # Define object detector images = tf.placeholder(tf.float32, shape=[1, 28, 28, 1]) obj_dec = TensorFlowFasterRCNN(images=images) # Get test data (_, _), (x_test_mnist, y_test_mnist) = get_mnist_dataset x_test_mnist = x_test_mnist[:1] # First test predict result = obj_dec.predict(x_test_mnist) assert list(result[0].keys()) == ["boxes", "labels", "scores"] assert result[0]["boxes"].shape == (300, 4) expected_detection_boxes = np.asarray([0.008862, 0.003788, 0.070454, 0.175931]) np.testing.assert_array_almost_equal(result[0]["boxes"][2, :], expected_detection_boxes, decimal=3) assert result[0]["scores"].shape == (300,) expected_detection_scores = np.asarray( [ 2.196349e-04, 7.968055e-05, 7.811916e-05, 7.334248e-05, 6.868376e-05, 6.861838e-05, 6.756858e-05, 6.331169e-05, 6.313509e-05, 6.222352e-05, ] ) np.testing.assert_array_almost_equal(result[0]["scores"][:10], expected_detection_scores, decimal=3) assert result[0]["labels"].shape == (300,) expected_detection_classes = np.asarray([37, 15, 15, 66, 15, 15, 15, 63, 2, 66]) np.testing.assert_array_almost_equal(result[0]["labels"][:10], expected_detection_classes) # Then test loss gradient # Create labels y = [{"boxes": result[0]["boxes"], "labels": result[0]["labels"], "scores": np.ones_like(result[0]["labels"])}] # Compute gradients grads = obj_dec.loss_gradient(x_test_mnist[:1], y) assert grads.shape == (1, 28, 28, 1) expected_gradients = np.asarray( [ [-0.00298723], [-0.0039893], [-0.00036253], [0.01038542], [0.01455704], [0.00995643], [0.00424966], [0.00470569], [0.00666382], [0.0028694], [0.00525351], [0.00889174], [0.0071413], [0.00618231], [0.00598106], [0.0072665], [0.00708815], [0.00286943], [0.00411595], [0.00788978], [0.00587319], [0.00808631], [0.01018151], [0.00867905], [0.00820272], [0.00124911], [-0.0042593], [0.02380728], ] ) np.testing.assert_array_almost_equal(grads[0, 0, :, :], expected_gradients, decimal=2) # Then test loss gradient with standard format # Create labels result_tf = obj_dec.predict(x_test_mnist, standardise_output=False) result = obj_dec.predict(x_test_mnist, standardise_output=True) from art.estimators.object_detection.utils import convert_tf_to_pt result_pt = convert_tf_to_pt(y=result_tf, height=x_test_mnist.shape[1], width=x_test_mnist.shape[2]) np.testing.assert_array_equal(result[0]["boxes"], result_pt[0]["boxes"]) np.testing.assert_array_equal(result[0]["labels"], result_pt[0]["labels"]) np.testing.assert_array_equal(result[0]["scores"], result_pt[0]["scores"]) y = [{"boxes": result[0]["boxes"], "labels": result[0]["labels"], "scores": np.ones_like(result[0]["labels"])}] # Compute gradients grads = obj_dec.loss_gradient(x_test_mnist[:1], y, standardise_output=True) assert grads.shape == (1, 28, 28, 1) expected_gradients = np.asarray( [ [-0.00095965], [-0.00265362], [-0.00031886], [0.01132964], [0.01674244], [0.01262039], [0.0063345], [0.00673249], [0.00618648], [0.00422678], [0.00542425], [0.00814896], [0.00919153], [0.01068758], [0.00929435], [0.00877143], [0.00747379], [0.0050377], [0.00656254], [0.00799547], [0.0051057], [0.00714598], [0.01090685], [0.00787637], [0.00709959], [0.00047201], [-0.00460457], [0.02629307], ] ) np.testing.assert_array_almost_equal(grads[0, 0, :, :], expected_gradients, decimal=2) obj_dec._sess.close() tf.reset_default_graph() except ARTTestException as e: art_warning(e)
def train(num_epochs): """ Arguments: epochs: Number of epochs to run Returns: None """ # Define a graph and set it to default one g_train = tf.get_default_graph(); with g_train.as_default(): tf.set_random_seed(42); # Initialize the model and the variables whose statistics are to be plotted logits, loss, acc = initialize_model(); tf.summary.scalar("Loss", loss); tf.summary.scalar("Accuracy", acc); # Define a Session with tf.Session(graph = g_train) as sess: # Define the training step and iterators over the dataset train_step = training(loss, 1e-4); handle, next_element, train_iter, train_handle, val_iter, val_handle, test_iter, test_handle = get_dataset_iterators(sess); lr_coll, mom_coll = lr_mom_calculator(num_epochs, 1, 5e-2, True); # Define the saver and writer which will be writing the logs at the specified location (again for Tensorboard) saver = tf.train.Saver(max_to_keep = 5); merged = tf.summary.merge_all(); train_writer = tf.summary.FileWriter(log_path + "train/", sess.graph); val_writer = tf.summary.FileWriter(log_path + "val/"); sess.run([tf.global_variables_initializer()]); print('Training statrted...'); for epoch in range(1, num_epochs + 1): # Initialize the training iterator sess.run(train_iter.initializer); tot_train_loss = 0; tot_train_acc = 0; for train_ptr in range(num_train_iters + 1): try: X_train_batch, y_train_batch = sess.run(next_element, feed_dict = {handle: train_handle}); except tf.errors.OutOfRangeError: sess.run(train_iter.initializer) # Run the training step to update the parameters _, train_loss, train_acc, summary = sess.run([train_step, loss, acc, merged], feed_dict = {X_ph: X_train_batch, y_ph: y_train_batch, train_mode: True, dropout: 0.15, lr_ph: lr_coll[(epoch-1)*num_train_iters + train_ptr], beta1_ph: mom_coll[(epoch-1)*num_train_iters + train_ptr]}) tot_train_loss += train_loss; tot_train_acc += train_acc; train_writer.add_summary(summary, epoch); # Evaluate the trained model on eval set if the condition is satisfied sess.run(val_iter.initializer); tot_val_loss = 0; tot_val_acc = 0; for val_ptr in range(num_val_iters + 1): try: X_val_batch, y_val_batch = sess.run(next_element, feed_dict = {handle: val_handle}) except tf.errors.OutOfRangeError: sess.run(val_iter.initializer) val_loss, val_acc, summary = sess.run([loss, acc, merged], feed_dict = {X_ph: X_val_batch, y_ph: y_val_batch, train_mode: False, dropout: 0}) tot_val_loss += val_loss; tot_val_acc += val_acc; val_writer.add_summary(summary, epoch); print(str(epoch) + ' Epochs:: Train_loss: ' + str(tot_train_loss/(num_train_iters + 1)) + ', Val_loss: ' + \ str(tot_val_loss/(num_val_iters + 1)) + ', Train_acc: ' + str((tot_train_acc/(num_train_iters + 1))*100) \ + '%, Val_acc: ' + str(tot_val_acc/(num_val_iters + 1)*100) + '%') # Evaluate the trained model on the test set after every 5 epochs # NOTE: This is not introducing any leakage of statistics from train/Val set into the test set by any means, just evaluating # it on the test set after every 5 epochs to have a rough idea of what's going on. if epoch % 5 == 0: sess.run(test_iter.initializer); tot_test_loss = 0; tot_test_acc = 0; for test_ptr in range(num_test_iters + 1): try: X_test_batch, y_test_batch = sess.run(next_element, feed_dict = {handle: test_handle}) except tf.errors.OutOfRangeError: sess.run(test_iter.initializer) test_loss, test_acc = sess.run([loss, acc], feed_dict = {X_ph: X_test_batch, y_ph: y_test_batch, train_mode: False, dropout: 0}); tot_test_loss += test_loss; tot_test_acc += test_acc; print('Test_loss: ' + str(tot_test_loss/(num_test_iters + 1)) + ', Test_acc: ' + str(tot_test_acc/(num_test_iters + 1))) # Save the checkpoints at the specified directory!!! # if(epoch % 5 == 0): saver.save(sess, log_path + 'Ckpts', global_step = epoch) sess.close(); """ Close the Session """ tf.reset_default_graph(); """ Reset the default graph """
def main(): tf.reset_default_graph() TEST = True input_placeholder = tf.placeholder( tf.float32, shape=[None, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNEL], name="input_placeholder") output_placeholder = tf.placeholder(tf.float32, shape=[None, 1], name="output_placeholder") layer_conv_1, weights_conv_1 = new_conv_layer( input=input_placeholder, num_input_channels=IMAGE_CHANNEL, filter_size=5, num_filters=64, pooling=2) layer_conv_2, weights_conv_2 = new_conv_layer(input=layer_conv_1, num_input_channels=64, filter_size=3, num_filters=64, pooling=2) layer_conv_3, weights_conv_3 = new_conv_layer(input=layer_conv_2, num_input_channels=64, filter_size=3, num_filters=128, pooling=2) layer_flat, num_features = flatten_layer(layer_conv_3) layer_fc_1 = new_fc_layer(input=layer_flat, num_inputs=num_features, num_outputs=512) layer_fc_1 = tf.nn.sigmoid(layer_fc_1) #if TEST is not True: #layer_fc_1 = tf.nn.dropout(layer_fc_1, 0.5) layer_output = new_fc_layer(layer_fc_1, num_inputs=512, num_outputs=1) layer_output = tf.nn.sigmoid(layer_output) cost = tf.reduce_sum( tf.squared_difference(layer_output, output_placeholder) / 2) optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(cost) correct_predictions = tf.equal(tf.round(layer_output), output_placeholder) accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32)) init_g = tf.global_variables_initializer() init_l = tf.local_variables_initializer() with tf.Session() as sess: sess.run(init_g) sess.run(init_l) saver = tf.train.Saver() if TEST == False: train_model(sess, input_placeholder, output_placeholder, accuracy, cost, optimizer) saver.save(sess, "./model.ckpt") test_model(sess, input_placeholder, output_placeholder, accuracy, cost) f = open("./data.json", "w") f.write( json.dumps( { "batch_costs": train_costs, "batch_accuracies": train_accuracies, "test_cost": test_cost, "test_accuracy": test_accuracy }, indent=4, sort_keys=True)) f.close() else: saver.restore(sess, "./model.ckpt") test_model(sess, input_placeholder, output_placeholder, accuracy, cost)
# Get a smoothed version of the accuracy plot!! val_acc_collection_smooth = smooth(val_acc_collection); plot_graph(learning_rate_collection, val_acc_collection_smooth, str(val), colors[ind]); # Terminate the Session""" sess.close(); # Reset the default graph in order to ensure that weights of the network at start are random for each Grid_search_param""" tf.reset_default_graph(); # Show the plot plt.show() # Run the Vanilla LR_finder tf.reset_default_graph(); Lr_finder(epochs = 10, max_lr = 1, min_lr = 5e-2, vanilla = True); # Run the Grid_Search_CV for wt_dec values tf.reset_default_graph(); wt_dec_list = [1e-5, 3.18e-5, 1e-4]; Lr_finder(epochs = 10, max_lr = 1, min_lr = 5e-2, params = wt_dec_list, wt_dec_gridsearch = True) # Run the Grid_Search_CV for Dropout values tf.reset_default_graph(); dropout_list = [0.15, 0.3, 0.45]; Lr_finder(epochs = 10, max_lr = 1, min_lr = 5e-2, params = dropout_list, fin_wt_dec = 1e-4, dropout_gridsearch = True) def train(num_epochs): """ Arguments: epochs: Number of epochs to run
def main(): data_dir = './data/cp.txt' text = load_data(data_dir) view_sentence_range = (0, 10) print('数据情况:') print('不重复单词(彩票开奖记录)的个数: {}'.format( len({word: None for word in text.split()}))) scenes = text.split('\n\n') sentence_count_scene = [scene.count('\n') for scene in scenes] print('开奖期数: {}期'.format(int(np.average(sentence_count_scene)))) sentences = [ sentence for scene in scenes for sentence in scene.split('\n') ] print('行数: {}'.format(len(sentences))) word_count_sentence = [len(sentence.split()) for sentence in sentences] print('平均每行单词数: {}'.format(np.ceil(np.average(word_count_sentence)))) print() print('开奖记录从 {} 到 {}:'.format(*view_sentence_range)) print('\n'.join( text.split('\n')[view_sentence_range[0]:view_sentence_range[1]])) # Preprocess Training, Validation, and Testing Data preprocess_and_save_data(data_dir, create_lookup_tables) int_text, vocab_to_int, int_to_vocab = load_preprocess() ''' num_epochs 设置训练几代。 batch_size 是批次大小。 rnn_size 是RNN的大小(隐藏节点的维度)。 embed_dim 是嵌入层的维度。 seq_length 是序列的长度,始终为1。 learning_rate 是学习率。 show_every_n_batches 是过多少batch以后打印训练信息。 ''' # Number of Epochs num_epochs = 25 # Batch Size batch_size = 32 # RNN Size rnn_size = 1000 # Embedding Dimension Size embed_dim = 1000 # Sequence Length seq_length = 1 # Learning Rate learning_rate = 0.01 # Show stats for every n number of batches show_every_n_batches = 10 save_dir = './save' tf.reset_default_graph() train_graph = tf.Graph() with train_graph.as_default(): vocab_size = len(int_to_vocab) input_text, targets, lr = get_inputs() input_data_shape = tf.shape(input_text) cell, initial_state = get_init_cell(input_data_shape[0], rnn_size) logits, final_state, embed_matrix = build_nn(cell, rnn_size, input_text, vocab_size, embed_dim) # Probabilities for generating words probs = tf.nn.softmax(logits, name='probs') # Loss function cost = seq2seq.sequence_loss( logits, targets, tf.ones([input_data_shape[0], input_data_shape[1]])) # cost = build_loss(logits, targets, vocab_size) # We use the cosine distance: norm = tf.sqrt(tf.reduce_sum(tf.square(embed_matrix), 1, keepdims=True)) normalized_embedding = embed_matrix / norm probs_embeddings = tf.nn.embedding_lookup( normalized_embedding, tf.squeeze(tf.argmax(probs, 2))) # np.squeeze(probs.argmax(2)) probs_similarity = tf.matmul(probs_embeddings, tf.transpose(normalized_embedding)) y_embeddings = tf.nn.embedding_lookup(normalized_embedding, tf.squeeze(targets)) y_similarity = tf.matmul(y_embeddings, tf.transpose(normalized_embedding)) # data_moments = tf.reduce_mean(y_similarity, axis=0) # sample_moments = tf.reduce_mean(probs_similarity, axis=0) similar_loss = tf.reduce_mean(tf.abs(y_similarity - probs_similarity)) total_loss = cost + similar_loss # Optimizer optimizer = tf.train.AdamOptimizer(lr) # Gradient Clipping gradients = optimizer.compute_gradients(total_loss) # cost capped_gradients = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gradients if grad is not None] # clip_by_norm train_op = optimizer.apply_gradients(capped_gradients) # Accuracy correct_pred = tf.equal(tf.argmax(probs, 2), tf.cast( targets, tf.int64)) # logits <--> probs tf.argmax(targets, 1) <--> targets accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy') batches = get_batches(int_text[:-(batch_size + 1)], batch_size, seq_length) test_batches = get_batches(int_text[-(batch_size + 1):], batch_size, seq_length) top_k = 10 topk_acc_list = [] topk_acc = 0 sim_topk_acc_list = [] sim_topk_acc = 0 range_k = 5 floating_median_idx = 0 floating_median_acc_range_k = 0 floating_median_acc_range_k_list = [] floating_median_sim_acc_range_k = 0 floating_median_sim_acc_range_k_list = [] losses = {'train': [], 'test': []} accuracies = { 'accuracy': [], 'topk': [], 'sim_topk': [], 'floating_median_acc_range_k': [], 'floating_median_sim_acc_range_k': [] } with tf.Session(graph=train_graph) as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() for epoch_i in range(num_epochs): state = sess.run(initial_state, {input_text: batches[0][0]}) # 训练的迭代,保存训练损失 for batch_i, (x, y) in enumerate(batches): feed = { input_text: x, targets: y, initial_state: state, lr: learning_rate } train_loss, state, _ = sess.run( [total_loss, final_state, train_op], feed) # cost losses['train'].append(train_loss) # Show every <show_every_n_batches> batches if (epoch_i * len(batches) + batch_i) % show_every_n_batches == 0: print('Epoch {:>3} Batch {:>4}/{} train_loss = {:.3f}'. format(epoch_i, batch_i, len(batches), train_loss)) # 使用测试数据的迭代 acc_list = [] prev_state = sess.run( initial_state, {input_text: np.array([[1]])}) # test_batches[0][0] for batch_i, (x, y) in enumerate(test_batches): # Get Prediction test_loss, acc, probabilities, prev_state = sess.run( [total_loss, accuracy, probs, final_state], { input_text: x, targets: y, initial_state: prev_state }) # cost # 保存测试损失和准确率 acc_list.append(acc) losses['test'].append(test_loss) accuracies['accuracy'].append(acc) print('Epoch {:>3} Batch {:>4}/{} test_loss = {:.3f}'.format( epoch_i, batch_i, len(test_batches), test_loss)) # 利用嵌入矩阵和生成的预测计算得到相似度矩阵sim valid_embedding = tf.nn.embedding_lookup( normalized_embedding, np.squeeze(probabilities.argmax(2))) similarity = tf.matmul(valid_embedding, tf.transpose(normalized_embedding)) sim = similarity.eval() # 保存预测结果的Top K准确率和与预测结果距离最近的Top K准确率 topk_acc = 0 sim_topk_acc = 0 for ii in range(len(probabilities)): nearest = (-sim[ii, :]).argsort()[0:top_k] if y[ii] in nearest: sim_topk_acc += 1 if y[ii] in (-probabilities[ii]).argsort()[0][0:top_k]: topk_acc += 1 topk_acc = topk_acc / len(y) topk_acc_list.append(topk_acc) accuracies['topk'].append(topk_acc) sim_topk_acc = sim_topk_acc / len(y) sim_topk_acc_list.append(sim_topk_acc) accuracies['sim_topk'].append(sim_topk_acc) # 计算真实值在预测值中的距离数据 realInSim_distance_list = [] realInPredict_distance_list = [] for ii in range(len(probabilities)): sim_nearest = (-sim[ii, :]).argsort() idx = list(sim_nearest).index(y[ii]) realInSim_distance_list.append(idx) nearest = (-probabilities[ii]).argsort()[0] idx = list(nearest).index(y[ii]) realInPredict_distance_list.append(idx) print('真实值在预测值中的距离数据:') print('max distance : {}'.format( max(realInPredict_distance_list))) print('min distance : {}'.format( min(realInPredict_distance_list))) print('平均距离 : {}'.format(np.mean(realInPredict_distance_list))) print('距离中位数 : {}'.format( np.median(realInPredict_distance_list))) print('距离标准差 : {}'.format(np.std(realInPredict_distance_list))) print('真实值在预测值相似向量中的距离数据:') print('max distance : {}'.format(max(realInSim_distance_list))) print('min distance : {}'.format(min(realInSim_distance_list))) print('平均距离 : {}'.format(np.mean(realInSim_distance_list))) print('距离中位数 : {}'.format(np.median(realInSim_distance_list))) print('距离标准差 : {}'.format(np.std(realInSim_distance_list))) # sns.distplot(realInPredict_distance_list, rug=True) #, hist=False # plt.hist(np.log(realInPredict_distance_list), bins=50, color='steelblue', normed=True ) # 计算以距离中位数为中心,范围K为半径的准确率 floating_median_sim_idx = int( np.median(realInSim_distance_list)) floating_median_sim_acc_range_k = 0 floating_median_idx = int( np.median(realInPredict_distance_list)) floating_median_acc_range_k = 0 for ii in range(len(probabilities)): nearest_floating_median = (-probabilities[ii]).argsort( )[0][floating_median_idx - range_k:floating_median_idx + range_k] if y[ii] in nearest_floating_median: floating_median_acc_range_k += 1 nearest_floating_median_sim = (-sim[ii, :]).argsort( )[floating_median_sim_idx - range_k:floating_median_sim_idx + range_k] if y[ii] in nearest_floating_median_sim: floating_median_sim_acc_range_k += 1 floating_median_acc_range_k = floating_median_acc_range_k / len( y) floating_median_acc_range_k_list.append( floating_median_acc_range_k) accuracies['floating_median_acc_range_k'].append( floating_median_acc_range_k) floating_median_sim_acc_range_k = floating_median_sim_acc_range_k / len( y) floating_median_sim_acc_range_k_list.append( floating_median_sim_acc_range_k) accuracies['floating_median_sim_acc_range_k'].append( floating_median_sim_acc_range_k) print( 'Epoch {:>3} floating median sim range k accuracy {} '.format( epoch_i, np.mean(floating_median_sim_acc_range_k_list))) #:.3f print('Epoch {:>3} floating median range k accuracy {} '.format( epoch_i, np.mean(floating_median_acc_range_k_list))) #:.3f print('Epoch {:>3} similar top k accuracy {} '.format( epoch_i, np.mean(sim_topk_acc_list))) #:.3f print('Epoch {:>3} top k accuracy {} '.format( epoch_i, np.mean(topk_acc_list))) #:.3f print('Epoch {:>3} accuracy {} '.format(epoch_i, np.mean(acc_list))) #:.3f # Save Model saver.save(sess, save_dir) # , global_step=epoch_i print('Model Trained and Saved') embed_mat = sess.run(normalized_embedding) sns.distplot(realInSim_distance_list, rug=True) sns.distplot(realInPredict_distance_list, rug=True) plt.plot(losses['train'], label='Training loss') plt.legend() _ = plt.ylim() plt.plot(losses['test'], label='Test loss') plt.legend() _ = plt.ylim() plt.plot(accuracies['accuracy'], label='Accuracy') plt.plot(accuracies['topk'], label='Top K') plt.plot(accuracies['sim_topk'], label='Similar Top K') plt.plot(accuracies['floating_median_acc_range_k'], label='Floating Median Range K Acc') plt.plot(accuracies['floating_median_sim_acc_range_k'], label='Floating Median Sim Range K Acc') plt.legend() _ = plt.ylim() for batch_i, (x, y) in enumerate(test_batches): plt.plot(y, label='Targets') plt.plot(np.squeeze(probabilities.argmax(2)), label='Prediction') plt.legend() _ = plt.ylim() # Save parameters for checkpoint save_params((seq_length, save_dir)) _, vocab_to_int, int_to_vocab = load_preprocess() seq_length, load_dir = load_params() with train_graph.as_default(): saver = tf.train.Saver() with tf.Session(graph=train_graph) as sess: # Load saved model loader = tf.train.import_meta_graph(load_dir + '.meta') loader.restore(sess, load_dir) # saver.restore(sess, tf.train.latest_checkpoint('checkpoints')) embed_mat = sess.run(embed_matrix) viz_words = 1000 tsne = TSNE() with train_graph.as_default(): embed_tsne = tsne.fit_transform(embed_mat[:viz_words, :]) fig, ax = plt.subplots(figsize=(24, 24)) for idx in range(viz_words): plt.scatter(*embed_tsne[idx, :], color='steelblue') plt.annotate(int_to_vocab[idx], (embed_tsne[idx, 0], embed_tsne[idx, 1]), alpha=0.7) gen_length = 17 prime_word = '202' loaded_graph = tf.Graph() # loaded_graph with tf.Session(graph=train_graph) as sess: # Load saved model loader = tf.train.import_meta_graph(load_dir + '.meta') loader.restore(sess, load_dir) # Get Tensors from loaded model input_text, initial_state, final_state, probs = get_tensors( train_graph) # loaded_graph # Sentences generation setup gen_sentences = [prime_word] prev_state = sess.run(initial_state, {input_text: np.array([[1]])}) # Generate sentences for n in range(gen_length): # Dynamic Input dyn_input = [[ vocab_to_int[word] for word in gen_sentences[-seq_length:] ]] dyn_seq_length = len(dyn_input[0]) # Get Prediction probabilities, prev_state = sess.run([probs, final_state], { input_text: dyn_input, initial_state: prev_state }) valid_embedding = tf.nn.embedding_lookup(normalized_embedding, probabilities.argmax()) valid_embedding = tf.reshape(valid_embedding, (1, len(int_to_vocab))) similarity = tf.matmul(valid_embedding, tf.transpose(normalized_embedding)) sim = similarity.eval() pred_word = pick_word(probabilities[dyn_seq_length - 1], sim, int_to_vocab, 5, 'median') gen_sentences.append(pred_word) cp_script = ' '.join(gen_sentences) cp_script = cp_script.replace('\n ', '\n') cp_script = cp_script.replace('( ', '(') print(cp_script) int_sentences = [int(words) for words in gen_sentences] int_sentences = int_sentences[1:] val_data = [[103], [883], [939], [36], [435], [173], [572], [828], [509], [723], [145], [621], [535], [385], [98], [321], [427]] plt.plot(int_sentences, label='History') plt.plot(val_data, label='val_data') plt.legend() _ = plt.ylim()
def train(): s_time = time.time() global n_episode try: tf.reset_default_graph() sess = tf.InteractiveSession() coord = tf.train.Coordinator() checkpoint_dir = "./breakout-a3c-max-step" save_path = os.path.join(checkpoint_dir, "model.ckpt") if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) print("Directory {} was created".format(checkpoint_dir)) n_threads = 8 h_size = 512 input_shape = [84, 84, 4] output_dim = 3 # {0, 1, 2} action_offset = 1 global_network = A3CNetwork(name="global", input_shape=input_shape, output_dim=output_dim, h_size=h_size) thread_list = [] env_list = [] for id in range(n_threads): env = gym.make("BreakoutDeterministic-v4") single_agent = Agent(env=env, session=sess, coord=coord, name="thread_{}".format(id), global_network=global_network, input_shape=input_shape, output_dim=output_dim, h_size=h_size, action_offset=action_offset) thread_list.append(single_agent) env_list.append(env) init = tf.global_variables_initializer() sess.run(init) if tf.train.get_checkpoint_state(os.path.dirname(save_path)): var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, "global") saver = tf.train.Saver(var_list=var_list) maybe_path = tf.train.latest_checkpoint(checkpoint_dir) saver.restore(sess, maybe_path) print("Model restored to global: ", maybe_path) n_episode = int( tf.train.latest_checkpoint(checkpoint_dir).split('-')[-1]) else: print("No model is found") for t in thread_list: t.start() # print("Ctrl + C to close") while True: time.sleep(60 * 30) var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, "global") saver = tf.train.Saver(var_list=var_list) saver.save(sess, save_path, global_step=n_episode) print('Checkpoint Saved to {}/{}. elapsed = {}'.format( save_path, n_episode, time.time() - s_time)) except KeyboardInterrupt: print("Closing threads") coord.request_stop() coord.join(thread_list) print("Closing environments") for env in env_list: env.close() sess.close()
def gamma_variance_test(x, alphas, alpha_prior, N_trials, base_dir=os.getcwd()): # get useful numbers K = x.shape[0] # compute optimal posterior parameters alpha_star = alpha_prior + x print('alpha max = {:.2f}'.format(np.max(alpha_star))) # initialize gradients gradients = np.zeros([len(alphas), N_trials, K]) # reset graph with new session tf.reset_default_graph() with tf.Session() as sess: # declare training variable alpha_ph = tf.placeholder(tf.float32, [1, K]) # declare noise variable epsilon_ph = tf.placeholder(tf.float32, [1, K]) # declare Gamma sampler sampler = NeuralInverseCDF(target=GammaCDF(base_dir=base_dir), trainable=False) # clamp alpha to supported value alpha_ph = tf.minimum(alpha_ph, sampler.target.theta_max) alpha_ph = tf.maximum(alpha_ph, sampler.target.theta_min) # route each alpha dimension through a shared neural gamma sampler num_vars = len(tf.global_variables()) pi = [] for k in range(K): pi.append( sampler.sample_operation(epsilon_ph[:, k], alpha_ph[:, k])) # normalize Gamma samples so they form a Dirichlet sample pi = tf.stack(pi, axis=-1) pi = pi / tf.reduce_sum(pi, axis=-1, keepdims=True) # compute the expected log likelihood ll = tf.reduce_sum(x * tf.log(pi)) # compute the ELBO elbo = ll - kl_dirichlet(alpha_ph, tf.constant(alpha_prior, dtype=tf.float32)) # compute gradient grad = tf.gradients(xs=[alpha_ph], ys=elbo) # save the list of gamma sampler variables that will need to be loaded as constants sampler_vars = tf.global_variables()[num_vars:] # restore variables (order of operations matter) sess.run(tf.global_variables_initializer()) sampler.restore(sess, sampler_vars) # loop over the alphas for i in range(len(alphas)): # set alpha for this test alpha = alpha_star * np.ones([1, K]) alpha[0, 0] = alphas[i] # compute the gradient over the specified number of trials for j in range(N_trials): gradients[i, j] = sess.run(grad, feed_dict={ alpha_ph: alpha, epsilon_ph: np.random.random(size=(1, K)) })[0] # print update a_per = 100 * (i + 1) / len(alphas) n_per = 100 * (j + 1) / N_trials update_str = 'Alphas done: {:.2f}%, Trials done: {:.2f}%'.format( a_per, n_per) print('\r' + update_str, end='') print('') # return the gradients return gradients
def main(flags): nn_utils.set_gpu(flags.GPU) for start_layer in flags.start_layer: if start_layer >= 10: suffix_base = 'aemo_newloss' else: suffix_base = 'aemo_newloss_up{}'.format(start_layer) if flags.from_scratch: suffix_base += '_scratch' for lr in flags.learn_rate: for run_id in range(4): suffix = '{}_{}'.format(suffix_base, run_id) tf.reset_default_graph() np.random.seed(run_id) tf.set_random_seed(run_id) # define network model = unet.UNet(flags.num_classes, flags.patch_size, suffix=suffix, learn_rate=lr, decay_step=flags.decay_step, decay_rate=flags.decay_rate, epochs=flags.epochs, batch_size=flags.batch_size) overlap = model.get_overlap() cm = collectionMaker.read_collection(raw_data_path=flags.data_dir, field_name='aus10,aus30,aus50', field_id='', rgb_ext='.*rgb', gt_ext='.*gt', file_ext='tif', force_run=False, clc_name=flags.ds_name) cm.print_meta_data() file_list_train = cm.load_files(field_name='aus10,aus30', field_id='', field_ext='.*rgb,.*gt') file_list_valid = cm.load_files(field_name='aus50', field_id='', field_ext='.*rgb,.*gt') patch_list_train = patchExtractor.PatchExtractor(flags.patch_size, flags.tile_size, flags.ds_name + '_train_hist', overlap, overlap // 2). \ run(file_list=file_list_train, file_exts=['jpg', 'png'], force_run=False).get_filelist() patch_list_valid = patchExtractor.PatchExtractor(flags.patch_size, flags.tile_size, flags.ds_name + '_valid_hist', overlap, overlap // 2). \ run(file_list=file_list_valid, file_exts=['jpg', 'png'], force_run=False).get_filelist() chan_mean = cm.meta_data['chan_mean'] train_init_op, valid_init_op, reader_op = \ dataReaderSegmentation.DataReaderSegmentationTrainValid( flags.patch_size, patch_list_train, patch_list_valid, batch_size=flags.batch_size, chan_mean=chan_mean, aug_func=[reader_utils.image_flipping, reader_utils.image_rotating], random=True, has_gt=True, gt_dim=1, include_gt=True, valid_mult=flags.val_mult).read_op() feature, label = reader_op model.create_graph(feature) if start_layer >= 10: model.compile(feature, label, flags.n_train, flags.n_valid, flags.patch_size, ersaPath.PATH['model'], par_dir=flags.par_dir, loss_type='xent') else: model.compile(feature, label, flags.n_train, flags.n_valid, flags.patch_size, ersaPath.PATH['model'], par_dir=flags.par_dir, loss_type='xent', train_var_filter=['layerup{}'.format(i) for i in range(start_layer, 10)]) train_hook = hook.ValueSummaryHook(flags.verb_step, [model.loss, model.lr_op], value_names=['train_loss', 'learning_rate'], print_val=[0]) model_save_hook = hook.ModelSaveHook(model.get_epoch_step() * flags.save_epoch, model.ckdir) valid_loss_hook = hook.ValueSummaryHookIters(model.get_epoch_step(), [model.loss_xent, model.loss_iou], value_names=['valid_loss', 'IoU'], log_time=True, run_time=model.n_valid) image_hook = hook.ImageValidSummaryHook(model.input_size, model.get_epoch_step(), feature, label, model.pred, nn_utils.image_summary, img_mean=chan_mean) start_time = time.time() if not flags.from_scratch: model.load(flags.model_dir) model.train(train_hooks=[train_hook, model_save_hook], valid_hooks=[valid_loss_hook, image_hook], train_init=train_init_op, valid_init=valid_init_op) print('Duration: {:.3f}'.format((time.time() - start_time) / 3600))
def test_restore_ema(self): # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3 x_data = np.random.rand(100).astype(np.float32) y_data = x_data * 0.1 + 0.3 # Try to find values for W and b that compute y_data = W * x_data + b # (We know that W should be 0.1 and b 0.3, but TensorFlow will # figure that out for us.) W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W') b = tf.Variable(tf.zeros([1]), name='b') y = W * x_data + b # Minimize the mean squared errors. loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) opt_op = optimizer.minimize(loss) # Track the moving averages of all trainable variables. ema = tf.train.ExponentialMovingAverage(decay=0.9999) averages_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([opt_op]): train_op = tf.group(averages_op) # Before starting, initialize the variables. We will 'run' this first. init = tf.initialize_all_variables() saver = tf.train.Saver(tf.trainable_variables()) # Launch the graph. sess = tf.Session() sess.run(init) # Fit the line. for _ in range(201): sess.run(train_op) w_reference = sess.run('W/ExponentialMovingAverage:0') b_reference = sess.run('b/ExponentialMovingAverage:0') saver.save(sess, os.path.join(self.tmp_dir, "model_ex1")) tf.reset_default_graph() tf.train.import_meta_graph(os.path.join(self.tmp_dir, "model_ex1.meta")) sess = tf.Session() print('------------------------------------------------------') for var in tf.all_variables(): print('all variables: ' + var.op.name) for var in tf.trainable_variables(): print('normal variable: ' + var.op.name) for var in tf.moving_average_variables(): print('ema variable: ' + var.op.name) print('------------------------------------------------------') mode = 1 restore_vars = {} if mode == 0: ema = tf.train.ExponentialMovingAverage(1.0) for var in tf.trainable_variables(): print('%s: %s' % (ema.average_name(var), var.op.name)) restore_vars[ema.average_name(var)] = var elif mode == 1: for var in tf.trainable_variables(): ema_name = var.op.name + '/ExponentialMovingAverage' print('%s: %s' % (ema_name, var.op.name)) restore_vars[ema_name] = var saver = tf.train.Saver(restore_vars, name='ema_restore') saver.restore(sess, os.path.join(self.tmp_dir, "model_ex1")) w_restored = sess.run('W:0') b_restored = sess.run('b:0') self.assertAlmostEqual( w_reference, w_restored, 'Restored model modes not use the EMA filtered weight') self.assertAlmostEqual( b_reference, b_restored, 'Restored model modes not use the EMA filtered bias')
def estimate_model(model, X_train, y_train, X_val, y_val): tf.reset_default_graph() X = tf.placeholder(tf.float32, [None, 32, 32, 3], name='X') y = tf.placeholder(tf.int64, [None], name='y') learning_rate = tf.placeholder(tf.float32, shape=[]) is_training = tf.placeholder(tf.bool) y_out, params_to_eval = model['model_builder'](X, y, is_training, **model) mean_loss = tf.reduce_mean( tf.losses.softmax_cross_entropy( onehot_labels=tf.one_hot(y, y_out.shape[1]), logits=y_out, )) # optimizer = tf.train.RMSPropOptimizer( optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, ) # batch normalization in tensorflow requires this extra dependency extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): train_step = optimizer.minimize(mean_loss) num_of_trainable_val = num_of_trainable() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print('Training') train_time = time.perf_counter() total_loss, total_correct, all_losses, all_correct = run_model( sess, X, y, is_training, y_out, mean_loss, X_train, y_train, model.get('num_of_epochs', 2), batch_size=64, print_every=100, training=train_step, plot_losses=True, learning_rate=learning_rate, learning_rate_value=model.get('learning_rate', 1e-3), part_of_dataset=model.get('part_of_dataset', 1.0), snapshot_name=f'{model["group"]}/{model["name"]}'.replace( ' ', '-').replace(':', '').lower(), ) train_time = time.perf_counter() - train_time print('training time (seconds)', train_time) training = { 'total_lost': total_loss, 'total_correct': total_correct, 'losses': all_losses, 'accuracy': all_correct, 'time': train_time, } print('Validation') validation_time = time.perf_counter() total_loss, total_correct, all_losses, all_total_correct = run_model( sess, X, y, is_training, y_out, mean_loss, X_val, y_val, epochs=1, batch_size=64, learning_rate=learning_rate, learning_rate_value=model.get('learning_rate', 1e-3), ) validation_time = time.perf_counter() - validation_time print('validation time (seconds)', validation_time) validation = { 'total_lost': total_loss, 'total_correct': total_correct, 'losses': all_losses, 'accuracy': all_correct, 'time': validation_time, } # estimate how much time it would get to prediction print('predict') validation_time = time.perf_counter() run_model( sess, X, y, is_training, y_out, mean_loss, X_val[:1], y_val[:1], epochs=1, batch_size=64, learning_rate=learning_rate, learning_rate_value=model.get('learning_rate', 1e-3), ) validation_time = time.perf_counter() - validation_time predict = { 'time': validation_time, } # eval params and store params = {} for p in params_to_eval: params[p] = sess.run(p, feed_dict={ X: X_val[:1], y: y_val[:1], is_training: False, }) return { 'params': params, 'hyper_params': model, 'res': { 'predict': predict, 'training': training, 'validation': validation, 'num_of_trainable': num_of_trainable_val, }, }