This commit is contained in:
Lorenzo Iovino 2019-09-11 16:04:45 +02:00
parent ae477fc3df
commit 0109aab0f5
15 changed files with 918 additions and 586 deletions

View file

@ -8,15 +8,38 @@ import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import numpy as np
import os
import shutil
import random
img_width, img_height = 150, 150
train_data_dir = './UserflowPredictorSystem/predictor/datas/train'
validation_data_dir = './UserflowPredictorSystem/predictor/datas/test'
train_data_dir = './Predictor/PredictorNeuralNetwork/datas/userflows/train/'
validation_data_dir = './Predictor/PredictorNeuralNetwork/datas/userflows/test/'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 100
epochs = 20
batch_size = 16
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def populateDatas():
copytree('./Predictor/PredictorWebService/trainingImages/virtual', './Predictor/PredictorNeuralNetwork/datas/userflows/train')
offset = 0
for folder in os.listdir('./Predictor/PredictorNeuralNetwork/datas/userflows/train'):
numberOfFiles = len([name for name in os.listdir('./Predictor/PredictorNeuralNetwork/datas/userflows/train/' + folder)])
numberOfTestFiles = int(numberOfFiles / 10)
for idx in range(0,numberOfTestFiles):
file = os.listdir('./Predictor/PredictorNeuralNetwork/datas/userflows/train/' + folder)[random.randint(0,numberOfTestFiles)]
shutil.copy2("./Predictor/PredictorNeuralNetwork/datas/userflows/train/" + folder + "/" + file, './Predictor/PredictorNeuralNetwork/datas/userflows/test/' + str(idx + offset) + '.png')
shutil.copy2("./Predictor/PredictorNeuralNetwork/datas/userflows/train/" + folder + "/" + file, './Predictor/PredictorNeuralNetwork/datas/userflows/test/' + str(idx + offset) + '_SEPARATOR.png')
offset = numberOfTestFiles
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
@ -45,7 +68,7 @@ def create_model():
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model
@ -66,13 +89,13 @@ def train_model(model):
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
class_mode='categorical')
model.fit_generator(
train_generator,
@ -81,7 +104,7 @@ def train_model(model):
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save_weights('./UserflowPredictorSystem/first_try2.h5')
model.save_weights('./Predictor/PredictorNeuralNetwork/weights/userflows.h5')
return model
@ -93,29 +116,26 @@ def load_trained_model(weights_path):
def predict(number, model):
img = cv2.imread("./UserflowPredictorSystem/predictor/datas/test/" + str(number) + ".jpg")
im = mpimg.imread("./UserflowPredictorSystem/predictor/datas/test/" + str(number) + ".jpg")
img = cv2.imread("./Predictor/PredictorNeuralNetwork/datas/userflows/test/" + str(number) + ".png")
im = mpimg.imread("./Predictor/PredictorNeuralNetwork/datas/userflows/test/" + str(number) + ".png")
plt.imshow(im)
img = cv2.resize(img, (img_width,img_height))
img = img.reshape(1, img_width, img_height, 3)
res = model.predict(img)
if res == 1:
print('DOG')
else:
print('CAT')
print(res)
model = create_model()
populateDatas()
model = train_model(model)
import os
os.getcwd()
trained_model = load_trained_model("./UserflowPredictorSystem/first_try2.h5")
trained_model = load_trained_model("./Predictor/PredictorNeuralNetwork/weights/userflows.h5")
trained_model.summary()
import random
predict(random.randint(1,12500), trained_model)
predict('lolly', model)
num = random.randint(1,10)
print(num)
predict(num, trained_model)
print(np.argmax(loaded_model.predict(img)))
print(np.argmax(trained_model.predict(img)))

View file

@ -0,0 +1,119 @@
from keras.models import Sequential
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Conv2D, MaxPooling2D
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import numpy as np
img_width, img_height = 150, 150
train_data_dir = './Predictor/PredictorNeuralNetwork/datas/catVsDog/train/'
validation_data_dir = './Predictor/PredictorNeuralNetwork/datas/catVsDog/test/'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 100
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
def create_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model
def train_model(model):
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save_weights('./Predictor/PredictorNeuralNetwork/weights/catVsDog2.h5')
return model
def load_trained_model(weights_path):
model = create_model()
model.load_weights(weights_path)
return model
def predict(number, model):
img = cv2.imread("./Predictor/PredictorNeuralNetwork/datas/catVsDog/test/" + str(number) + ".jpg")
im = mpimg.imread("./Predictor/PredictorNeuralNetwork/datas/catVsDog/test/" + str(number) + ".jpg")
plt.imshow(im)
img = cv2.resize(img, (img_width,img_height))
img = img.reshape(1, img_width, img_height, 3)
res = model.predict(img)
if res == 1:
print('DOG')
else:
print('CAT')
model = create_model()
model = train_model(model)
import os
os.getcwd()
trained_model = load_trained_model("./Predictor/PredictorNeuralNetwork/weights/catVsDog.h5")
trained_model.summary()
import random
predict(random.randint(1,12500), trained_model)
print(np.argmax(trained_model.predict(img)))

Binary file not shown.

View file

@ -0,0 +1,78 @@
# Do not edit. File was generated by node-gyp's "configure" step
{
"target_defaults": {
"cflags": [],
"default_configuration": "Release",
"defines": [],
"include_dirs": [],
"libraries": [],
"msbuild_toolset": "v141",
"msvs_windows_target_platform_version": "10.0.17763.0"
},
"variables": {
"asan": 0,
"build_v8_with_gn": "false",
"coverage": "false",
"debug_nghttp2": "false",
"enable_lto": "false",
"enable_pgo_generate": "false",
"enable_pgo_use": "false",
"force_dynamic_crt": 0,
"host_arch": "x64",
"icu_data_in": "..\\..\\deps/icu-small\\source/data/in\\icudt64l.dat",
"icu_endianness": "l",
"icu_gyp_path": "tools/icu/icu-generic.gyp",
"icu_locales": "en,root",
"icu_path": "deps/icu-small",
"icu_small": "true",
"icu_ver_major": "64",
"nasm_version": "2.14",
"node_byteorder": "little",
"node_code_cache_path": "yes",
"node_debug_lib": "false",
"node_enable_d8": "false",
"node_enable_v8_vtunejit": "false",
"node_install_npm": "true",
"node_module_version": 72,
"node_no_browser_globals": "false",
"node_prefix": "/usr/local",
"node_release_urlbase": "https://nodejs.org/download/release/",
"node_report": "true",
"node_shared": "false",
"node_shared_cares": "false",
"node_shared_http_parser": "false",
"node_shared_libuv": "false",
"node_shared_nghttp2": "false",
"node_shared_openssl": "false",
"node_shared_zlib": "false",
"node_tag": "",
"node_target_type": "executable",
"node_use_bundled_v8": "true",
"node_use_dtrace": "false",
"node_use_etw": "true",
"node_use_large_pages": "false",
"node_use_node_snapshot": "false",
"node_use_openssl": "true",
"node_use_v8_platform": "true",
"node_with_ltcg": "true",
"node_without_node_options": "false",
"openssl_fips": "",
"openssl_is_fips": "false",
"shlib_suffix": "so.72",
"target_arch": "x64",
"v8_enable_gdbjit": 0,
"v8_enable_i18n_support": 1,
"v8_enable_inspector": 1,
"v8_no_strict_aliasing": 1,
"v8_optimized_debug": 1,
"v8_promise_internal_field_count": 1,
"v8_random_seed": 0,
"v8_trace_maps": 0,
"v8_use_siphash": 1,
"v8_use_snapshot": 1,
"want_separate_host_toolset": 0,
"nodedir": "C:\\Users\\Lorenzo\\AppData\\Local\\node-gyp\\Cache\\12.4.0",
"standalone_static_library": 1,
"msbuild_path": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\MSBuild.exe"
}
}

File diff suppressed because it is too large Load diff

View file

@ -39,7 +39,7 @@ export class ImageCreatorService {
});
//todo da cambiare le dimensioni e renderle dinamiche
const canvas = createCanvas(1000, 800);
const canvas = createCanvas(150, 150);
const ctx = canvas.getContext('2d');
this.printHTMLElements(htmlElements, ctx);
this.printMouseClick(mouseClicks, ctx);