-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
81 lines (55 loc) · 1.78 KB
/
train.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
import json
import math
import os
from PIL import Image
import numpy as np
from numpy import load
from keras import layers
from keras.applications.inception_v3 import InceptionV3
from keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
import matplotlib.pyplot as plt
import pandas as pd
import scipy
from tqdm import tqdm
import tensorflow as tf
from keras import backend as K
import gc
from functools import partial
from sklearn import metrics
from collections import Counter
import json
import itertools
from model import build_model
if __name__ == "__main__":
dict_data = load('data.npz')
X_train, y_train, X_test, y_test = (dict_data['arr_0'],dict_data['arr_1'],dict_data['arr_2'],dict_data['arr_3'])
K.clear_session()
gc.collect()
BATCH_SIZE = 16
# Using original generator
train_generator = ImageDataGenerator()
inceptionNet = InceptionV3(
weights='imagenet',
include_top=False,
input_shape=(256, 256, 3)
)
model = build_model(inceptionNet,lr = 1e-4)
print(model.summary())
# Learning Rate Reducer
learn_control = ReduceLROnPlateau(monitor='val_acc', patience=5,
verbose=1,factor=0.2, min_lr=1e-7)
# Checkpoint
filepath="./weights_best.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
history = model.fit_generator(
train_generator.flow(X_train, y_train, batch_size=BATCH_SIZE),
steps_per_epoch=X_train.shape[0] / BATCH_SIZE,
epochs=15,
validation_data=(X_test, y_test),
callbacks=[learn_control, checkpoint]
)
with open('history.json', 'w') as f:
json.dump(str(history.history), f)
model.save("./malware.h5")