def save_boxcar(path, x, rate= 8000, hop= 512, window= 'boxcar'): """undoes `load`.""" assert 2 == x.ndim if np.isrealobj(x): x = r2c(x) x = x.T x = np.concatenate((x, np.zeros_like(x[:1]))) wav = librosa.istft(x, hop, window= window) librosa.output.write_wav(path, wav, rate)
def infer5(src_idx, len_src, t=256, bos=2): emb_src = sess.run(m.emb_src, {m.src_idx: src_idx, m.len_src: len_src}) x = np.full((1, len_src.size), bos, np.int32) s, x = sess.run((m.state_in, m.fire2), {m.fire: x}) idxs = [] for _ in range(t): s, x = sess.run((m.state_ex, m.prob), { m.state_in: s, m.fire2: x, m.emb_src: emb_src, m.len_src: len_src }) idxs.append(np.argmax(x, axis=-1)) return np.concatenate(idxs).T
def infer(src_idx, len_src, t=256, bos=2): emb_src = sess.run(m.emb_src, {m.src_idx: src_idx, m.len_src: len_src}) x = np.full((1, len_src.size), bos, np.int32) s = sess.run(m.state_in, {m.fire: x}) idxs = [] for _ in range(t): s, x = sess.run((m.state_ex, m.pidx), { m.state_in: s, m.fire: x, m.emb_src: emb_src, m.len_src: len_src }) idxs.append(x) return np.concatenate(idxs).T
def log(step, wtr=tf.summary.FileWriter("../log/{}".format(trial)), log=tf.summary.merge((tf.summary.scalar('step_xid', dummy[0]), tf.summary.scalar('step_err', dummy[1]))), fet=(valid.xid, valid.err), inp=(valid.src_idx, valid.len_src, valid.tgt_idx, valid.len_tgt), src=src_valid, tgt=tgt_valid, bat=256): stats = [ sess.run(fet, dict(zip(inp, feed(src[i:j], tgt[i:j])))) for i, j in partition(len(tgt), bat) ] stats = [np.mean(np.concatenate(stat)) for stat in zip(*stats)] wtr.add_summary(sess.run(log, dict(zip(dummy, stats))), step) wtr.flush()
def sinusoid(dim, time, freq= 1e-4, array= False): """returns a rank-2 tensor of shape `dim, time`, where each column corresponds to a time step and each row a sinusoid, with frequencies in a geometric progression from 1 to `freq`. """ assert not dim % 2 if array: a = (freq ** ((2 / dim) * np.arange(dim // 2))).reshape(-1, 1) @ (1 + np.arange(time).reshape(1, -1)) return np.concatenate((np.sin(a), np.cos(a)), -1).reshape(dim, time) else: assert False # figure out a better way to do this a = tf.reshape( freq ** ((2 / dim) * tf.range(dim // 2, dtype= tf.float32)) , (-1, 1)) @ tf.reshape( 1 + tf.range(tf.to_float(time), dtype= tf.float32) , (1, -1)) return tf.reshape(tf.concat((tf.sin(a), tf.cos(a)), axis= -1), (dim, time))
def decode(sess, vae, z, steps=256): """decodes latent states -> array i32 (b, t) z : array f32 (b, dim_rep) t <= steps """ x = np.full((1, len(z)), vae.bos, dtype=np.int32) s = vae.state_in.eval({vae.z: z}) y = [] for _ in range(steps): x, s = sess.run((vae.pred, vae.state_ex), { vae.lead: x, vae.state_in: s }) if np.all(x == vae.eos): break y.append(x) return np.concatenate(y).T
model = vAe('infer') # Restore the session sess = tf.InteractiveSession() tf.train.Saver().restore(sess, path_ckpt) ################################ # deterministic representation # ################################ # encode text with sentence piece model data = list(map(partial(sp.encode_capped, vocab), text)) data = vpack(data, (len(data), max(map(len, data))), vocab.eos_id(), np.int32) # calculate z for the test data in batches inpt = [model.z.eval({model.src: data[i:j]}) for i, j in partition(len(data), 128)] inpt = np.concatenate(inpt, axis=0) np.save(path_emb, inpt) ####################################################### # averaged representation with sentencepiece sampling # ####################################################### def infer_avg(sent, samples=128): bat = [sp.encode_capped_sample(vocab, sent) for _ in range(samples)] bat = vpack(bat, (len(bat), max(map(len, bat))), vocab.eos_id(), np.int32) z = model.z.eval({model.src: bat}) return np.mean(z, axis=0) from tqdm import tqdm inpt = np.stack(list(map(infer_avg, tqdm(text))), axis=0)
def train(anomaly_class=8): #set gpu os.environ["CUDA_VISIBLE_DEVICES"] = "0" #load data (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data() inlier = train_images[train_labels != anomaly_class] x_train = np.reshape(inlier, (len(inlier), 28 * 28)) / 255 #y_train = train_labels[train_labels!=anomaly_class] y_train = np.zeros(len(x_train), dtype=np.int8) # dummy outlier = train_images[train_labels == anomaly_class] x_test = np.reshape(np.concatenate([outlier, test_images]), (len(outlier) + len(test_images), 28 * 28)) / 255 y_test = np.concatenate( [train_labels[train_labels == anomaly_class], test_labels]) y_test = [0 if y != anomaly_class else 1 for y in y_test] x_test, y_test = unison_shfl(x_test, np.array(y_test)) path_log = "/cache/tensorboard-logdir/ae" path_ckpt = "/project/outlier_detection/ckpt" epochs = 400 batch_size = 700 dim_btlnk = 32 mult = 20 lr_max = 1e-4 trial = f"dae{anomaly_class}_b{batch_size}_btlnk{dim_btlnk}_lr_{lr_max}m{mult}" #trial="test1" dim_x = len(x_train[0]) #reset graphs and fix seeds tf.reset_default_graph() if 'sess' in globals(): sess.close() rand = RandomState(0) tf.set_random_seed(0) # data pipeline batch_fn = lambda: batch2(x_train, y_train, batch_size) x, y = pipe(batch_fn, (tf.float32, tf.float32), prefetch=4) #z = tf.random_normal((batch_size, z_dim)) # load graph dae = DAE.new(dim_x, dim_btlnk) model = DAE.build(dae, x, y, lr_max, mult) # start session, initialize variables sess = tf.InteractiveSession() saver = tf.train.Saver() wrtr = tf.summary.FileWriter(pform(path_log, trial)) #wrtr.add_graph(sess.graph) ### if load pretrained model # pretrain = "modelname" #saver.restore(sess, pform(path_ckpt, pretrain)) ### else: auc_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope='AUC') init = tf.group(tf.global_variables_initializer(), tf.variables_initializer(var_list=auc_vars)) sess.run(init) def log(step, wrtr=wrtr, log=tf.summary.merge([ tf.summary.scalar('g_loss', model.g_loss), tf.summary.scalar('d_loss', model.d_loss), tf.summary.image('gx400', spread_image(model.gx[:400], 20, 20, 28, 28)), tf.summary.image('dgx400', spread_image(model.dgx[:400], 20, 20, 28, 28)), tf.summary.image('dx400', spread_image(model.dx[:400], 20, 20, 28, 28)), tf.summary.scalar("AUC_dgx", model.auc_dgx), tf.summary.scalar("AUC_dx", model.auc_dx), tf.summary.scalar("AUC_gx", model.auc_gx) ]), y=y_test, x=x_test): wrtr.add_summary(sess.run(log, {model.x: x, model.y: y}), step) wrtr.flush() steps_per_epoch = len(x_train) // batch_size for epoch in tqdm(range(epochs)): for i in range(steps_per_epoch): sess.run(model.d_step) sess.run(model.g_step) # tensorboard writer log(sess.run(model["step"]) // steps_per_epoch) saver.save(sess, pform(path_ckpt, trial), write_meta_graph=False)
def train(anomaly_class, loss_type): #set gpu os.environ["CUDA_VISIBLE_DEVICES"] = "1" #load data (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data() inlier = train_images[train_labels != anomaly_class] x_train = np.reshape(inlier, (len(inlier), 28 * 28)) / 255 #y_train = train_labels[train_labels!=anomaly_class] y_train = np.zeros(len(x_train), dtype=np.int8) # dummy outlier = train_images[train_labels == anomaly_class] x_test = np.reshape(np.concatenate([outlier, test_images]), (len(outlier) + len(test_images), 28 * 28)) / 255 y_test = np.concatenate( [train_labels[train_labels == anomaly_class], test_labels]) y_test = [0 if y != anomaly_class else 1 for y in y_test] x_test, y_test = unison_shfl(x_test, np.array(y_test)) path_log = "/cache/tensorboard-logdir/ae" path_ckpt = "/project/outlier_detection/ckpt" epochs = 400 batch_size = 700 dim_btlnk = 32 dim_z = dim_btlnk dim_dense = 32 accelerate = 1e-5 context_weight = 1 trial = f"vaegan_{loss_type}_{anomaly_class}_b{batch_size}_btlnk{dim_btlnk}_d{dim_dense}_n{dim_z}_a{accelerate}" dim_x = len(x_train[0]) #reset graphs and fix seeds tf.reset_default_graph() if 'sess' in globals(): sess.close() rand = RandomState(0) tf.set_random_seed(0) # data pipeline batch_fn = lambda: batch2(x_train, y_train, batch_size, dim_z) x, y, z = pipe(batch_fn, (tf.float32, tf.float32, tf.float32), prefetch=4) # load graph aegan = VAEGAN.new(dim_x, dim_btlnk, dim_dense, dim_z, accelerate) model = VAEGAN.build(aegan, x, y, z, loss_type) # start session, initialize variables sess = tf.InteractiveSession() saver = tf.train.Saver() wrtr = tf.summary.FileWriter(pform(path_log, trial)) #wrtr.add_graph(sess.graph) ### if load pretrained model # pretrain = "modelname" #saver.restore(sess, pform(path_ckpt, pretrain)) ### else: auc_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope='AUC') init = tf.group(tf.global_variables_initializer(), tf.variables_initializer(var_list=auc_vars)) sess.run(init) def log( step, wrtr=wrtr, log=tf.summary. merge([ tf.summary.scalar('g_loss', model.g_loss), tf.summary.scalar('d_loss', model.d_loss), tf.summary.scalar('mu', model.m), tf.summary.scalar('lv', model.l), tf.summary.image('gzx400', spread_image(model.gzx[:400], 20, 20, 28, 28)) #, tf.summary.image('gz400', spread_image(model.gz[:400], 20,20,28,28)) , tf.summary.scalar("AUC_gzx", model.auc_gzx), tf.summary.scalar("AUC_dgzx", model.auc_dgzx), tf.summary.scalar("AUC_dx", model.auc_dx) #, tf.summary.scalar("gz_loss",model.gz_loss) , tf.summary.scalar("gzx_loss", model.gzx_loss), tf.summary.scalar("ftr_loss", model.ftr_loss), tf.summary.scalar("kl_loss", model.kl_loss), tf.summary.scalar("dx_loss", model.dx_loss) #, tf.summary.scalar("dgz_loss",model.dgz_loss) , tf.summary.scalar("dgzx_loss", model.dgzx_loss) ]), y=y_test, x=x_test): mu = sess.run(model.mu, {model.x: x}) wrtr.add_summary(sess.run(log, { model.zx: mu, model.x: x, model.y: y }), step) wrtr.flush() steps_per_epoch = len(x_train) // batch_size for epoch in tqdm(range(epochs)): for i in range(steps_per_epoch): sess.run(model.g_step) sess.run(model.d_step) # tensorboard writer #log(sess.run(model["step"])//steps_per_epoch) log(sess.run(model["step"] // steps_per_epoch)) saver.save(sess, pform(path_ckpt, trial), write_meta_graph=False)
def train(anomaly_class=8, dataset="cifar", n_dis=1, epochs=25, dim_btlnk=32, batch_size=64, loss="mean", context_weight=1, dim_d=64, dim_g=64, extra_layers=0, gpu="0"): #set gpu os.environ["CUDA_VISIBLE_DEVICES"] = gpu path_log = f"/cache/tensorboard-logdir/{dataset}" path_ckpt = "/project/multi-discriminator-gan/ckpt" path_data = "/project/multi-discriminator-gan/data" #reset graphs and fix seeds tf.reset_default_graph() if 'sess' in globals(): sess.close() rand = RandomState(0) tf.set_random_seed(0) #load data if dataset == "ucsd1": x_train = np.load("./data/ucsd1_train_x.npz")["arr_0"] / 255 y_train = np.load("./data/ucsd1_train_y.npz")["arr_0"] x_test = np.load("./data/ucsd1_test_x.npz")["arr_0"] / 255 y_test = np.load("./data/ucsd1_test_y.npz")["arr_0"] elif dataset == "uscd2": x_train = np.load("./data/ucsd2_train_x.npz")["arr_0"] y_train = np.load("./data/ucsd2_train_y.npz")["arr_0"] x_test = np.load("./data/ucsd2_test_x.npz")["arr_0"] y_test = np.load("./data/ucsd2_test_y.npz")["arr_0"] else: if dataset == "mnist": (train_images, train_labels), ( test_images, test_labels) = tf.keras.datasets.mnist.load_data() train_images = resize_images(train_images) test_images = resize_images(test_images) else: (train_images, train_labels), ( test_images, test_labels) = tf.keras.datasets.cifar10.load_data() train_labels = np.reshape(train_labels, len(train_labels)) test_labels = np.reshape(test_labels, len(test_labels)) inlier = train_images[train_labels != anomaly_class] #data_size = prod(inlier[0].sha x_train = inlier / 255 #x_train = np.reshape(inlier, (len(inlier), data_size))/255 #y_train = train_labels[train_labels!=anomaly_class] y_train = np.zeros(len(x_train), dtype=np.int8) # dummy outlier = train_images[train_labels == anomaly_class] x_test = np.concatenate([outlier, test_images]) / 255 #x_test = np.reshape(np.concatenate([outlier, test_images]) # ,(len(outlier)+len(test_images), data_size))/255 y_test = np.concatenate( [train_labels[train_labels == anomaly_class], test_labels]) y_test = [0 if y != anomaly_class else 1 for y in y_test] x_test, y_test = unison_shfl(x_test, np.array(y_test)) img_size_x = x_train[0].shape[0] img_size_y = x_train[0].shape[1] channel = x_train[0].shape[-1] trial = f"{dataset}_{loss}_dis{n_dis}_{anomaly_class}_w{context_weight}_btlnk{dim_btlnk}_d{dim_d}_g{dim_g}e{extra_layers}" # data pipeline batch_fn = lambda: batch2(x_train, y_train, batch_size) x, y = pipe(batch_fn, (tf.float32, tf.float32), prefetch=4) #z = tf.random_normal((batch_size, z_dim)) # load graph mg_gan = MG_GAN.new(img_size_x, channel, dim_btlnk, dim_d, dim_g, n_dis, extra_layers=0) model = MG_GAN.build(mg_gan, x, y, context_weight, loss) # start session, initialize variables sess = tf.InteractiveSession() saver = tf.train.Saver() wrtr = tf.summary.FileWriter(pform(path_log, trial)) wrtr.add_graph(sess.graph) ### if load pretrained model # pretrain = "modelname" #saver.restore(sess, pform(path_ckpt, pretrain)) ### else: auc_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope='AUC') init = tf.group(tf.global_variables_initializer(), tf.variables_initializer(var_list=auc_vars)) sess.run(init) #if "ucsd" in dataset: summary_test = tf.summary.merge([ tf.summary.scalar('g_loss', model.g_loss), tf.summary.scalar("lambda", model.lam), tf.summary.scalar("gl_rec", model.gl_rec), tf.summary.scalar("gl_adv", model.gl_adv), tf.summary.scalar("gl_lam", model.gl_lam), tf.summary.scalar('d_loss_mean', model.d_loss_mean), tf.summary.scalar('d_max', model.d_max) #, tf.summary.scalar('d_loss', model.d_loss) , tf.summary.scalar("AUC_gx", model.auc_gx) ]) if dataset == "ucsd1": summary_images = tf.summary.merge( (tf.summary.image("gx", model.gx, max_outputs=8), tf.summary.image("x", model.x, max_outputs=8), tf.summary.image( 'gx400', spread_image(tf.concat([model.gx, model.x], axis=1), 8, 2, img_size_x, img_size_y, channel)))) else: summary_images = tf.summary.merge( (tf.summary.image("gx", model.gx, max_outputs=8), tf.summary.image( 'gx400', spread_image(model.gx[:400], 20, 20, img_size_x, img_size_y, channel)), tf.summary.image("x", model.x, max_outputs=8))) if n_dis > 1: d_wrtr = { i: tf.summary.FileWriter(pform(path_log, trial + f"d{i}")) for i in range(n_dis) } summary_discr = { i: tf.summary.scalar('d_loss_multi', model.d_loss[i]) for i in range(n_dis) } def summ(step): fetches = model.g_loss, model.lam, model.d_loss_mean, model.auc_gx results = map( np.mean, zip(*(sess.run(fetches, { model['x']: x_test[i:j], model['y']: y_test[i:j] }) for i, j in partition(len(x_test), batch_size, discard=False)))) results = list(results) wrtr.add_summary(sess.run(summary_test, dict(zip(fetches, results))), step) if dataset == "ucsd1": # bike, skateboard, grasswalk, shopping cart, car, normal, normal, grass wrtr.add_summary( sess.run( summary_images, { model.x: x_test[[990, 1851, 2140, 2500, 2780, 2880, 3380, 3580]] }), step) else: wrtr.add_summary(sess.run(summary_images, {model.x: x_test}), step) wrtr.flush() def summ_discr(step): fetches = model.d_loss results = map( np.mean, zip(*(sess.run(fetches, { model['x']: x_test[i:j], model['y']: y_test[i:j] }) for i, j in partition(len(x_test), batch_size, discard=False)))) results = list(results) if n_dis > 1: # put all losses of the discriminators in one plot for i in range(n_dis): d_wrtr[i].add_summary( sess.run(summary_discr[i], dict(zip(fetches, results))), step) #d_wrtr[i].add_summary(sess.run(summary_discr[i], dict([(fetches[i], results[i])])), step) d_wrtr[i].flush() #def log(step # , wrtr= wrtr # , log = tf.summary.merge([tf.summary.scalar('g_loss', model.g_loss) # , tf.summary.scalar('d_loss', tf.reduce_mean(model.d_loss)) # , tf.summary.scalar("lambda", model.lam) # , tf.summary.image("gx", model.gx, max_outputs=5) # , tf.summary.image('gx400', spread_image(model.gx[:400], 20,20, img_size, img_size, channel)) # #, tf.summary.scalar("AUC_dgx", model.auc_dgx) # #, tf.summary.scalar("AUC_dx", model.auc_dx) # , tf.summary.scalar("AUC_gx", model.auc_gx)]) # , y= y_test # , x= x_test): # wrtr.add_summary(sess.run(log, {model["x"]:x # , model["y"]:y}) # , step) # wrtr.flush() steps_per_epoch = len(x_train) // batch_size - 1 for epoch in tqdm(range(epochs)): for i in range(steps_per_epoch): #sess.run(model["train_step"]) sess.run(model['d_step']) sess.run(model['g_step']) # tensorboard writer #if "ucsd" in dataset: summ(sess.run(model["step"]) // steps_per_epoch) #else: # log(sess.run(model["step"])//steps_per_epoch) if n_dis > 1: summ_discr(sess.run(model["step"]) // steps_per_epoch) saver.save(sess, pform(path_ckpt, trial), write_meta_graph=False)