import numpy as np
import pandas as pd
benign =  np.load('NPY/benign_data_no_preprocessing_128.npy', encoding = 'latin1') # MODEL  1
malware = np.load('NPY/malware_data_no_preprocessing_128.npy', encoding = 'latin1')

from sklearn.model_selection import train_test_split
A_benign,B_benign = train_test_split(benign,test_size=0.2,random_state=5)
A_malware,B_malware = train_test_split(malware,test_size=0.2,random_state=5)

training_data  = []
cnt_ben=0
cnt_mal=0
for i in A_benign:
    cnt_ben+=1
    training_data.append(i)
for i in A_malware:
    cnt_mal+=1
    training_data.append(i)

print(cnt_ben)
print(cnt_mal)
print(len(training_data))
cnt_ben=0
cnt_mal=0

test_data  = []
for i in B_benign:
    cnt_ben+=1
    test_data.append(i)
for i in B_malware:
    cnt_mal+=1
    test_data.append(i)

print(cnt_ben)
print(cnt_mal)

print(len(test_data))

import numpy as np
from keras.layers import *
from keras.models import Model
from  keras.models import Input
from keras import optimizers
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import ModelCheckpoint
from keras.layers.normalization import BatchNormalization
from keras.layers import Dropout
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import cohen_kappa_score
from keras import regularizers
import pandas as pd
import matplotlib.pyplot as plt
X_train = np.array([i[0] for i in training_data])
y_train = np.array([i[1] for i in training_data])
print(X_train[0].shape)
X_test = np.array([i[0] for i in test_data])
y_test = np.array([i[1] for i in test_data])
print(y_test)
import numpy as np
from keras.layers import *
from keras.models import Model
from  keras.models import Input
from keras import optimizers
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import ModelCheckpoint
from keras.layers.normalization import BatchNormalization
from keras.layers import Dropout
import matplotlib.pyplot as plt
from keras.layers import GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import cohen_kappa_score
from keras.layers import Concatenate
from keras.layers import add
from keras import regularizers
from sklearn.metrics import confusion_matrix
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from keras.applications import xception
from keras.applications import densenet
import os

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from  os import listdir,makedirs
from os.path import join,exists,expanduser
X_model = xception.Xception(weights = 'imagenet',  include_top = False, input_shape = (128,128,3))
#X_model = densenet.DenseNet169(weights = 'imagenet',  include_top = False, input_shape = (320,320,3))

for layer in X_model.layers[:-15]:
    layer.trainable = False

x = X_model.output
x = Flatten()(x)
x =  Dense(output_dim = 100, activation  = 'relu', init = 'glorot_uniform',kernel_regularizer=regularizers.l2(0.001))(x)
x = Dropout(0.1)(x)
x =  Dense(output_dim = 50, activation  = 'tanh', init = 'glorot_uniform' ,kernel_regularizer=regularizers.l2(0.01))(x)
x = Dropout(0.12)(x)
output = Dense(output_dim = 1, activation = 'sigmoid', init = 'lecun_uniform', kernel_regularizer=regularizers.l2(0.0001))(x)
model =  Model(inputs =  X_model.input , outputs = output)

model.load_weights('output2.hdf5')
adam = optimizers.Adam(lr=1e-3,beta_1=0.9,beta_2=0.999)
# sgd = optimizers.SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer = adam, loss = 'binary_crossentropy', metrics = ['accuracy'])

# checkpoint = ModelCheckpoint('output2.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# reducelr = ReduceLROnPlateau(monitor='val_loss', factor=0.8,minimum =0, patience=3, verbose= 1, mode = 'auto')
# history = model.fit(x=X_train,y=y_train, validation_data=(X_test, y_test),epochs=200,batch_size=32,callbacks = [reducelr,checkpoint], verbose=2)


y_pred = model.predict(X_test)
from sklearn.metrics import accuracy_score
score = accuracy_score(y_test,y_pred)
print(score)

import numpy as np
import pandas as pd
benign =  np.load('NPY/benign_data_no_preprocessing_128.npy', encoding = 'latin1') # MODEL  1
malware = np.load('NPY/malware_data_no_preprocessing_128.npy', encoding = 'latin1')

from sklearn.model_selection import train_test_split
A_benign,B_benign = train_test_split(benign,test_size=0.2,random_state=5)
A_malware,B_malware = train_test_split(malware,test_size=0.2,random_state=5)

training_data  = []
cnt_ben=0
cnt_mal=0
for i in A_benign:
    cnt_ben+=1
    training_data.append(i)
for i in A_malware:
    cnt_mal+=1
    training_data.append(i)

print(cnt_ben)
print(cnt_mal)
print(len(training_data))
cnt_ben=0
cnt_mal=0

test_data  = []
for i in B_benign:
    cnt_ben+=1
    test_data.append(i)
for i in B_malware:
    cnt_mal+=1
    test_data.append(i)

print(cnt_ben)
print(cnt_mal)

print(len(test_data))

import numpy as np
from keras.layers import *
from keras.models import Model
from  keras.models import Input
from keras import optimizers
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import ModelCheckpoint
from keras.layers.normalization import BatchNormalization
from keras.layers import Dropout
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import cohen_kappa_score
from keras import regularizers
import pandas as pd
import matplotlib.pyplot as plt
X_train = np.array([i[0] for i in training_data])
y_train = np.array([i[1] for i in training_data])
print(X_train[0].shape)
X_test = np.array([i[0] for i in test_data])
y_test = np.array([i[1] for i in test_data])
print(y_test)
import numpy as np
from keras.layers import *
from keras.models import Model
from  keras.models import Input
from keras import optimizers
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import ModelCheckpoint
from keras.layers.normalization import BatchNormalization
from keras.layers import Dropout
import matplotlib.pyplot as plt
from keras.layers import GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import cohen_kappa_score
from keras.layers import Concatenate
from keras.layers import add
from keras import regularizers
from sklearn.metrics import confusion_matrix
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from keras.applications import xception
from keras.applications import densenet
import os

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from  os import listdir,makedirs
from os.path import join,exists,expanduser
X_model = xception.Xception(weights = 'imagenet',  include_top = False, input_shape = (128,128,3))
#X_model = densenet.DenseNet169(weights = 'imagenet',  include_top = False, input_shape = (320,320,3))

for layer in X_model.layers[:-15]:
    layer.trainable = False

x = X_model.output
x = Flatten()(x)
x =  Dense(output_dim = 100, activation  = 'relu', init = 'glorot_uniform',kernel_regularizer=regularizers.l2(0.001))(x)
x = Dropout(0.1)(x)
x =  Dense(output_dim = 50, activation  = 'tanh', init = 'glorot_uniform' ,kernel_regularizer=regularizers.l2(0.01))(x)
x = Dropout(0.12)(x)
output = Dense(output_dim = 1, activation = 'sigmoid', init = 'lecun_uniform', kernel_regularizer=regularizers.l2(0.0001))(x)
model =  Model(inputs =  X_model.input , outputs = output)

model.load_weights('output2.hdf5')
adam = optimizers.Adam(lr=1e-3,beta_1=0.9,beta_2=0.999)
# sgd = optimizers.SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer = adam, loss = 'binary_crossentropy', metrics = ['accuracy'])

# checkpoint = ModelCheckpoint('output2.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# reducelr = ReduceLROnPlateau(monitor='val_loss', factor=0.8,minimum =0, patience=3, verbose= 1, mode = 'auto')
# history = model.fit(x=X_train,y=y_train, validation_data=(X_test, y_test),epochs=200,batch_size=32,callbacks = [reducelr,checkpoint], verbose=2)


y_pred = model.predict(X_test)
from sklearn.metrics import accuracy_score
score = accuracy_score(y_test,y_pred)
print(score)

# MODEL  1 # Importing the Keras libraries and packages #X_model = densenet.DenseNet169(weights = 'imagenet',  include_top = False, input_shape = (320,320,3)) # sgd = optimizers.SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) # checkpoint = ModelCheckpoint('output2.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max') # reducelr = ReduceLROnPlateau(monitor='val_loss', factor=0.8,minimum =0, patience=3, verbose= 1, mode = 'auto') # history = model.fit(x=X_train,y=y_train, validation_data=(X_test, y_test),epochs=200,batch_size=32,callbacks = [reducelr,checkpoint], verbose=2)