forked from brannondorsey/PassGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
120 lines (91 loc) · 4.25 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import time
import pickle
import argparse
import tensorflow as tf
import numpy as np
import tflib as lib
import tflib.ops.linear
import tflib.ops.conv1d
import utils
import models
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--input-dir', '-i',
required=True,
dest='input_dir',
help='Trained model directory. The --output-dir value used for training.')
parser.add_argument('--checkpoint', '-c',
required=True,
dest='checkpoint',
help='Model checkpoint to use for sampling. Expects a .ckpt file.')
parser.add_argument('--output', '-o',
default='samples.txt',
help='File path to save generated samples to (default: samples.txt)')
parser.add_argument('--num-samples', '-n',
type=int,
default=1000000,
dest='num_samples',
help='The number of password samples to generate (default: 1000000)')
parser.add_argument('--batch-size', '-b',
type=int,
default=64,
dest='batch_size',
help='Batch size (default: 64).')
parser.add_argument('--seq-length', '-l',
type=int,
default=10,
dest='seq_length',
help='The maximum password length. Use the same value that you did for training. (default: 10)')
parser.add_argument('--layer-dim', '-d',
type=int,
default=128,
dest='layer_dim',
help='The hidden layer dimensionality for the generator. Use the same value that you did for training (default: 128)')
args = parser.parse_args()
if not os.path.isdir(args.input_dir):
parser.error('"{}" folder doesn\'t exist'.format(args.input_dir))
if not os.path.exists(args.checkpoint + '.meta'):
parser.error('"{}.meta" file doesn\'t exist'.format(args.checkpoint))
if not os.path.exists(os.path.join(args.input_dir, 'charmap.pickle')):
parser.error('charmap.pickle doesn\'t exist in {}, are you sure that directory is a trained model directory'.format(args.input_dir))
if not os.path.exists(os.path.join(args.input_dir, 'inv_charmap.pickle')):
parser.error('inv_charmap.pickle doesn\'t exist in {}, are you sure that directory is a trained model directory'.format(args.input_dir))
return args
args = parse_args()
with open(os.path.join(args.input_dir, 'charmap.pickle'), 'rb') as f:
charmap = pickle.load(f)
with open(os.path.join(args.input_dir, 'inv_charmap.pickle'), 'rb') as f:
inv_charmap = pickle.load(f)
fake_inputs = models.Generator(args.batch_size, args.seq_length, args.layer_dim, len(charmap))
with tf.compat.v1.Session() as session:
def generate_samples():
samples = session.run(fake_inputs)
samples = np.argmax(samples, axis=2)
decoded_samples = []
for i in range(len(samples)):
decoded = []
for j in range(len(samples[i])):
decoded.append(inv_charmap[samples[i][j]])
decoded_samples.append(tuple(decoded))
return decoded_samples
def save(samples):
with open(args.output, 'a') as f:
for s in samples:
s = "".join(s).replace('`', '')
f.write(s + "\n")
saver = tf.compat.v1.train.Saver()
saver.restore(session, args.checkpoint)
samples = []
then = time.time()
start = time.time()
for i in range(int(args.num_samples / args.batch_size)):
samples.extend(generate_samples())
# append to output file every 1000 batches
if i % 1000 == 0 and i > 0:
save(samples)
samples = [] # flush
print('wrote {} samples to {} in {:.2f} seconds. {} total.'.format(1000 * args.batch_size, args.output, time.time() - then, i * args.batch_size))
then = time.time()
save(samples)
print('finished in {:.2f} seconds'.format(time.time() - start))