Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
futami committed Nov 5, 2018
1 parent 0ade41c commit 8e40b9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dcgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, args):
os.makedirs('./model_images/')

""" build discriminator model """
self.d = model.discriminator_model(self.img_size)
self.d = model.discriminator_model(self.img_size, self.channels)
plot_model(self.d, to_file='./model_images/discriminator.png', show_shapes=True)

""" build generator model """
Expand Down
16 changes: 12 additions & 4 deletions load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
from keras.preprocessing.image import load_img, img_to_array
from keras.datasets import mnist

def load_image_data(dataset_path,img_size):
def load_image_data(dataset_path, test_img, img_size, mode):
X_train = []
X_test = []

print(dataset_path)
print("train image data loading...")
train_image_list = glob.glob(dataset_path+'*.png')
train_image_list = glob.glob(dataset_path+'*.jpg')
for img_path in train_image_list:
print(img_path)
img = load_img(img_path, target_size=(img_size, img_size))
imgarray = img_to_array(img)
X_train.append(imgarray)

X_train = np.array(X_train).astype(np.float32)
X_train = (X_train -127.5) / 127.5


return X_train
if mode == 'test':
test_img = load_img(test_img, target_size=(img_size, img_size))
test_imgarray = img_to_array(test_img)
X_test = np.array(test_imgarray).astype(np.float32)
X_test = (X_test -127.5) / 127.5

return X_train, X_test

def load_mnist_data():
""" load mnist data """
Expand Down
39 changes: 20 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

def anomaly_detection(test_img, args, g=None, d=None):
anogan_model = model.anomaly_detector(args, g=g, d=d)
ano_score, similar_img = model.compute_anomaly_score(args, anogan_model, test_img.reshape(1, 28, 28, 1), iterations=500, d=d)
ano_score, similar_img = model.compute_anomaly_score(args, anogan_model, test_img.reshape(1, args.imgsize, args.imgsize, args.channels), iterations=500, d=d)

# anomaly area, 255 normalization
np_residual = test_img.reshape(args.imgsize, args.imgsize, args.imgsize) - similar_img.reshape(28, 28, 1)
np_residual = test_img.reshape(args.imgsize, args.imgsize, args.channels) - similar_img.reshape(args.imgsize, args.imgsize, args.channels)
np_residual = (np_residual + 2)/4

np_residual = (255*np_residual).astype(np.uint8)
original_x = (test_img.reshape(28,28,1)*127.5+127.5).astype(np.uint8)
similar_x = (similar_img.reshape(28,28,1)*127.5+127.5).astype(np.uint8)
original_x = (test_img.reshape(args.imgsize,args.imgsize,args.channels)*127.5+127.5).astype(np.uint8)
similar_x = (similar_img.reshape(args.imgsize,args.imgsize,args.channels)*127.5+127.5).astype(np.uint8)

original_x_color = cv2.cvtColor(original_x, cv2.COLOR_GRAY2BGR)
original_x_color = cv2.cvtColor(original_x, cv2.COLOR_RGB2BGR)
residual_color = cv2.applyColorMap(np_residual, cv2.COLORMAP_JET)
show = cv2.addWeighted(original_x_color, 0.3, residual_color, 0.7, 0.)

Expand Down Expand Up @@ -68,7 +68,7 @@ def run(args):
#X_train, X_test, X_test_original, Y_test = load.load_mnist_data()

""" load image data """
X_train = load.load_image_data(args.dataset_path, args.img_size)
X_train, test_img = load.load_image_data(args.datapath, args.testpath, args.imgsize, args.mode)

""" init DCGAN """
print("initialize DCGAN ")
Expand Down Expand Up @@ -108,48 +108,49 @@ def run(args):
#test_img = X_test_original[Y_test==0][30]

# compute anomaly score - sample from strange image
img_idx = args.img_idx
label_idx = args.label_idx
test_img = X_test_original[Y_test==label_idx][img_idx]
#img_idx = args.img_idx
#label_idx = args.label_idx
#test_img = X_test_original[Y_test==label_idx][img_idx]
# test_img = np.random.uniform(-1, 1 (28, 28, 1))

start = cv2.getTickCount()
score, qurey, pred, diff = anomaly_detection(test_img, args)
time = (cv2.getTickCount() - start ) / cv2.getTickFrequency() * 1000
print ('%d label, %d : done ' %(label_idx, img_idx), '%.2f' %score, '%.2fms'%time)
#print ('%d label, %d : done ' %(label_idx, img_idx), '%.2f' %score, '%.2fms'%time)

""" matplot view """
plt.figure(1, figsize=(3, 3))
plt.title('query image')
plt.imshow(qurey.reshape(28, 28), cmap=plt.cm.gray)
plt.imshow(qurey.reshape(args.imgsize, args.imgsize, args.channels), cmap=plt.cm.gray)

print('anomaly score :', score)
plt.figure(2, figsize=(3,3))
plt.title('generated similar image')
plt.imshow(pred.reshape(28, 28), cmap=plt.cm.gray)
plt.imshow(pred.reshape(args.imgsize, args.imgsize, args.channels), cmap=plt.cm.gray)

plt.figure(3, figsize=(3,3))
plt.figure(3, figsize=(3,4))
plt.title('anomaly detection')
plt.imshow(cv2.cvtColor(diff, cv2.COLOR_BGR2RGB))
plt.show()

def main():
parser = argparse.ArgumentParser(description='train AnoGAN')
parser.add_argument('--epoch', '-e', default=30)
parser.add_argument('--batchsize', '-b', default=64)
parser.add_argument('--datapath', '-d', default='/media/futami/HDD1/BABA/data/train/BABA/')
parser.add_argument('--epoch', '-e', default=1000)
parser.add_argument('--batchsize', '-b', default=32)
parser.add_argument('--mode', '-m' , type=str, default='test',help='train, test')
parser.add_argument('--imgsize', type=int, default=28)
parser.add_argument('--imgsize', type=int, default=64)
parser.add_argument('--channels', type=int, default=3)
parser.add_argument('--zdims', type=int, default=10)
parser.add_argument('--testpath'type=str)
parser.add_argument('--zdims', type=int, default=100)
parser.add_argument('--testpath', '-p', type=str )

args = parser.parse_args()

run(args)

""" t-SNE embedding """
### generating anomaly image for test (random noise image)
tsne(args)
#tsne(args)

if __name__ == '__main__':
main()
10 changes: 5 additions & 5 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

""" build generator model """
def generator_model(z_dim, imgsize, channels):
col = imgsize / 4
col = int(imgsize / 4)
inputs = Input((z_dim, ))
fc1 = Dense(input_dim=z_dim, units=128*col*col)(inputs)
fc1 = BatchNormalization()(fc1)
Expand All @@ -41,8 +41,8 @@ def generator_model(z_dim, imgsize, channels):
return model

""" build discriminator model """
def discriminator_model(img_size):
inputs = Input((img_size, img_size, 1))
def discriminator_model(img_size, channels):
inputs = Input((img_size, img_size, channels))

conv1 = Conv2D(64, (5,5), padding='same')(inputs)
conv1 = LeakyReLU(0.2)(conv1)
Expand Down Expand Up @@ -74,7 +74,7 @@ def generator_containg_discriminator(g, d, z_dim):
""" discriminator intermediate ayer feature extraction """
def feature_extractor(args, d=None):
if d is None:
d = discriminator_model(args.img_size)
d = discriminator_model(args.imgsize, args.channels)
d.load_weights('./saved_model/discriminator.h5')

intermidiate_model = Model(inputs=d.layers[0].input, outputs=d.layers[-7].output)
Expand All @@ -90,7 +90,7 @@ def sum_of_residual(y_true, y_pred):
""" anomaly detection model """
def anomaly_detector(args, g=None, d=None ):
if g is None:
g = generator_model(args.zdims, args.imgsize, args.zdims)
g = generator_model(args.zdims, args.imgsize, args.channels)
g.load_weights('./saved_model/generator.h5')

intermidiate_model = feature_extractor(args, d)
Expand Down

0 comments on commit 8e40b9b

Please sign in to comment.