Neural networks learning
@ -1,13 +1,14 @@
|
|||||||
import matplotlib.pyplot as plt
|
|
||||||
import seaborn as sns
|
import seaborn as sns
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
import keras
|
|
||||||
from keras.models import Sequential
|
|
||||||
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
|
|
||||||
from keras.preprocessing.image import ImageDataGenerator
|
|
||||||
from keras.optimizers import Adam
|
|
||||||
|
|
||||||
from sklearn.metrics import classification_report,confusion_matrix
|
from tensorflow import keras
|
||||||
|
from tensorflow.keras.models import Sequential
|
||||||
|
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
|
||||||
|
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
||||||
|
from tensorflow.keras import layers
|
||||||
|
|
||||||
|
from sklearn.metrics import classification_report, confusion_matrix
|
||||||
|
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
|
|
||||||
@ -16,12 +17,13 @@ import os
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
from resources.Globals import *
|
||||||
|
|
||||||
def main():
|
labels = ['houses', 'other']
|
||||||
labels = ['house', 'other']
|
img_size = 400
|
||||||
img_size = 500
|
|
||||||
|
|
||||||
def get_data(data_dir):
|
|
||||||
|
def get_data(data_dir):
|
||||||
data = []
|
data = []
|
||||||
for label in labels:
|
for label in labels:
|
||||||
path = os.path.join(data_dir, label)
|
path = os.path.join(data_dir, label)
|
||||||
@ -33,7 +35,133 @@ def main():
|
|||||||
data.append([resized_arr, class_num])
|
data.append([resized_arr, class_num])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
return np.array(data)
|
return np.array(data, dtype="object")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
train = get_data('E:/Projects/Pycharm Projects/sapper/files/Neural_networks/Train')
|
||||||
|
val = get_data('E:/Projects/Pycharm Projects/sapper/files/Neural_networks/Test')
|
||||||
|
|
||||||
|
# Visualize the data
|
||||||
|
l = []
|
||||||
|
for i in train:
|
||||||
|
if i[1] != 0:
|
||||||
|
l.append("houses")
|
||||||
|
else:
|
||||||
|
l.append("other")
|
||||||
|
sns.set_style('darkgrid')
|
||||||
|
sns.countplot(l)
|
||||||
|
|
||||||
|
# House
|
||||||
|
plt.figure(figsize=(5, 5))
|
||||||
|
plt.imshow(train[1][0])
|
||||||
|
plt.title(labels[train[0][1]])
|
||||||
|
|
||||||
|
# Other
|
||||||
|
plt.figure(figsize=(5, 5))
|
||||||
|
plt.imshow(train[-1][0])
|
||||||
|
plt.title(labels[train[-1][1]])
|
||||||
|
|
||||||
|
# Data Preprocessing
|
||||||
|
x_train = []
|
||||||
|
y_train = []
|
||||||
|
x_val = []
|
||||||
|
y_val = []
|
||||||
|
|
||||||
|
for feature, label in train:
|
||||||
|
x_train.append(feature)
|
||||||
|
y_train.append(label)
|
||||||
|
|
||||||
|
for feature, label in val:
|
||||||
|
x_val.append(feature)
|
||||||
|
y_val.append(label)
|
||||||
|
|
||||||
|
# Normalize the data
|
||||||
|
x_train = np.array(x_train) / 255
|
||||||
|
x_val = np.array(x_val) / 255
|
||||||
|
|
||||||
|
x_train.reshape(-1, img_size, img_size, 1)
|
||||||
|
y_train = np.array(y_train)
|
||||||
|
|
||||||
|
x_val.reshape(-1, img_size, img_size, 1)
|
||||||
|
y_val = np.array(y_val)
|
||||||
|
|
||||||
|
# Data augmentation
|
||||||
|
datagen = ImageDataGenerator(
|
||||||
|
featurewise_center=False, # set input mean to 0 over the dataset
|
||||||
|
samplewise_center=False, # set each sample mean to 0
|
||||||
|
featurewise_std_normalization=False, # divide inputs by std of the dataset
|
||||||
|
samplewise_std_normalization=False, # divide each input by its std
|
||||||
|
zca_whitening=False, # apply ZCA whitening
|
||||||
|
rotation_range=30, # randomly rotate images in the range (degrees, 0 to 180)
|
||||||
|
zoom_range=0.2, # Randomly zoom image
|
||||||
|
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
|
||||||
|
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
|
||||||
|
horizontal_flip=True, # randomly flip images
|
||||||
|
vertical_flip=False) # randomly flip images
|
||||||
|
|
||||||
|
print('Pretrain')
|
||||||
|
|
||||||
|
datagen.fit(x_train)
|
||||||
|
|
||||||
|
print('After train')
|
||||||
|
|
||||||
|
# Define the Model
|
||||||
|
model = Sequential()
|
||||||
|
model.add(Conv2D(32, 3, padding="same", activation="relu", input_shape=(img_size, img_size, 3)))
|
||||||
|
model.add(MaxPool2D())
|
||||||
|
|
||||||
|
model.add(Conv2D(32, 3, padding="same", activation="relu"))
|
||||||
|
model.add(MaxPool2D())
|
||||||
|
|
||||||
|
model.add(Conv2D(64, 3, padding="same", activation="relu"))
|
||||||
|
model.add(MaxPool2D())
|
||||||
|
model.add(Dropout(0.4))
|
||||||
|
|
||||||
|
model.add(Flatten())
|
||||||
|
model.add(Dense(128, activation="relu"))
|
||||||
|
model.add(Dense(2, activation="softmax"))
|
||||||
|
|
||||||
|
model.summary()
|
||||||
|
|
||||||
|
# Compile the model
|
||||||
|
opt = keras.optimizers.Adam(lr=0.000001) # Adam as optimizer and SparseCategoricalCrossentropy as the loss function
|
||||||
|
model.compile(optimizer=opt, loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
||||||
|
metrics=['accuracy'])
|
||||||
|
|
||||||
|
print('Pretrain #2')
|
||||||
|
# Train model
|
||||||
|
history = model.fit(x_train, y_train, epochs=AMOUNT_OF_EPOCHS, validation_data=(x_val, y_val))
|
||||||
|
print('After train #2')
|
||||||
|
|
||||||
|
# Evaluating the result
|
||||||
|
acc = history.history['accuracy']
|
||||||
|
val_acc = history.history['val_accuracy']
|
||||||
|
loss = history.history['loss']
|
||||||
|
val_loss = history.history['val_loss']
|
||||||
|
|
||||||
|
epochs_range = range(1, AMOUNT_OF_EPOCHS + 1)
|
||||||
|
|
||||||
|
plt.figure(figsize=(15, 15))
|
||||||
|
plt.subplot(2, 2, 1)
|
||||||
|
plt.plot(epochs_range, acc, label='Training Accuracy')
|
||||||
|
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
|
||||||
|
plt.legend(loc='lower right')
|
||||||
|
plt.title('Training and Validation Accuracy')
|
||||||
|
|
||||||
|
plt.subplot(2, 2, 2)
|
||||||
|
plt.plot(epochs_range, loss, label='Training Loss')
|
||||||
|
plt.plot(epochs_range, val_loss, label='Validation Loss')
|
||||||
|
plt.legend(loc='upper right')
|
||||||
|
plt.title('Training and Validation Loss')
|
||||||
|
|
||||||
|
# Precision and accuracy report
|
||||||
|
predictions = model.predict_classes(x_val)
|
||||||
|
predictions = predictions.reshape(1, -1)[0]
|
||||||
|
print(classification_report(y_val, predictions, target_names=['Rugby (Class 0)', 'Soccer (Class 1)']))
|
||||||
|
|
||||||
|
# Show all plots
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -1,15 +1,2 @@
|
|||||||
import pandas as pd
|
t = range(10)
|
||||||
|
print(t)
|
||||||
|
|
||||||
def main():
|
|
||||||
my_dict = {'known': [1], 'power': [1], 'new': [1], 'location': 0, 'stable': [1], 'chain_reaction': [1]}
|
|
||||||
|
|
||||||
s = pd.DataFrame.from_dict(my_dict)
|
|
||||||
|
|
||||||
print(s)
|
|
||||||
|
|
||||||
# data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
|
|
||||||
# print(pd.DataFrame.from_dict(data))
|
|
||||||
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
BIN
files/Neural_networks/Test/houses/114_frontal.jpg
Normal file
After Width: | Height: | Size: 113 KiB |
BIN
files/Neural_networks/Test/houses/116_frontal.jpg
Normal file
After Width: | Height: | Size: 146 KiB |
BIN
files/Neural_networks/Test/houses/12_frontal.jpg
Normal file
After Width: | Height: | Size: 128 KiB |
BIN
files/Neural_networks/Test/houses/141_frontal.jpg
Normal file
After Width: | Height: | Size: 106 KiB |
BIN
files/Neural_networks/Test/houses/143_frontal.jpg
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
files/Neural_networks/Test/houses/146_frontal.jpg
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
files/Neural_networks/Test/houses/178_frontal.jpg
Normal file
After Width: | Height: | Size: 164 KiB |
BIN
files/Neural_networks/Test/houses/194_frontal.jpg
Normal file
After Width: | Height: | Size: 110 KiB |
BIN
files/Neural_networks/Test/houses/197_frontal.jpg
Normal file
After Width: | Height: | Size: 100 KiB |
BIN
files/Neural_networks/Test/houses/204_frontal.jpg
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
files/Neural_networks/Test/houses/207_frontal.jpg
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
files/Neural_networks/Test/houses/220_frontal.jpg
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
files/Neural_networks/Test/houses/224_frontal.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
files/Neural_networks/Test/houses/226_frontal.jpg
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
files/Neural_networks/Test/houses/233_frontal.jpg
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
files/Neural_networks/Test/houses/236_frontal.jpg
Normal file
After Width: | Height: | Size: 68 KiB |
BIN
files/Neural_networks/Test/houses/238_frontal.jpg
Normal file
After Width: | Height: | Size: 82 KiB |
BIN
files/Neural_networks/Test/houses/266_frontal.jpg
Normal file
After Width: | Height: | Size: 53 KiB |
BIN
files/Neural_networks/Test/houses/272_frontal.jpg
Normal file
After Width: | Height: | Size: 105 KiB |
BIN
files/Neural_networks/Test/houses/279_frontal.jpg
Normal file
After Width: | Height: | Size: 134 KiB |
BIN
files/Neural_networks/Test/houses/283_frontal.jpg
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
files/Neural_networks/Test/houses/289_frontal.jpg
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
files/Neural_networks/Test/houses/291_frontal.jpg
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
files/Neural_networks/Test/houses/292_frontal.jpg
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
files/Neural_networks/Test/houses/308_frontal.jpg
Normal file
After Width: | Height: | Size: 98 KiB |
BIN
files/Neural_networks/Test/houses/321_frontal.jpg
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
files/Neural_networks/Test/houses/323_frontal.jpg
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
files/Neural_networks/Test/houses/325_frontal.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
files/Neural_networks/Test/houses/35_frontal.jpg
Normal file
After Width: | Height: | Size: 101 KiB |
BIN
files/Neural_networks/Test/houses/361_frontal.jpg
Normal file
After Width: | Height: | Size: 68 KiB |
BIN
files/Neural_networks/Test/houses/363_frontal.jpg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
files/Neural_networks/Test/houses/364_frontal.jpg
Normal file
After Width: | Height: | Size: 152 KiB |
BIN
files/Neural_networks/Test/houses/366_frontal.jpg
Normal file
After Width: | Height: | Size: 67 KiB |
BIN
files/Neural_networks/Test/houses/377_frontal.jpg
Normal file
After Width: | Height: | Size: 104 KiB |
BIN
files/Neural_networks/Test/houses/391_frontal.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
files/Neural_networks/Test/houses/39_frontal.jpg
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
files/Neural_networks/Test/houses/417_frontal.jpg
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
files/Neural_networks/Test/houses/434_frontal.jpg
Normal file
After Width: | Height: | Size: 83 KiB |
BIN
files/Neural_networks/Test/houses/435_frontal.jpg
Normal file
After Width: | Height: | Size: 125 KiB |
BIN
files/Neural_networks/Test/houses/445_frontal.jpg
Normal file
After Width: | Height: | Size: 137 KiB |
BIN
files/Neural_networks/Test/houses/449_frontal.jpg
Normal file
After Width: | Height: | Size: 53 KiB |
BIN
files/Neural_networks/Test/houses/452_frontal.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
files/Neural_networks/Test/houses/460_frontal.jpg
Normal file
After Width: | Height: | Size: 115 KiB |
BIN
files/Neural_networks/Test/houses/465_frontal.jpg
Normal file
After Width: | Height: | Size: 109 KiB |
BIN
files/Neural_networks/Test/houses/46_frontal.jpg
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
files/Neural_networks/Test/houses/474_frontal.jpg
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
files/Neural_networks/Test/houses/481_frontal.jpg
Normal file
After Width: | Height: | Size: 116 KiB |
BIN
files/Neural_networks/Test/houses/487_frontal.jpg
Normal file
After Width: | Height: | Size: 145 KiB |
BIN
files/Neural_networks/Test/houses/498_frontal.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
files/Neural_networks/Test/houses/500_frontal.jpg
Normal file
After Width: | Height: | Size: 131 KiB |
BIN
files/Neural_networks/Test/houses/508_frontal.jpg
Normal file
After Width: | Height: | Size: 122 KiB |
BIN
files/Neural_networks/Test/houses/509_frontal.jpg
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
files/Neural_networks/Test/houses/525_frontal.jpg
Normal file
After Width: | Height: | Size: 113 KiB |
BIN
files/Neural_networks/Test/houses/531_frontal.jpg
Normal file
After Width: | Height: | Size: 118 KiB |
BIN
files/Neural_networks/Test/houses/5_frontal.jpg
Normal file
After Width: | Height: | Size: 58 KiB |
BIN
files/Neural_networks/Test/houses/61_frontal.jpg
Normal file
After Width: | Height: | Size: 66 KiB |
BIN
files/Neural_networks/Test/houses/65_frontal.jpg
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
files/Neural_networks/Test/houses/67_frontal.jpg
Normal file
After Width: | Height: | Size: 75 KiB |
BIN
files/Neural_networks/Test/houses/72_frontal.jpg
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
files/Neural_networks/Test/houses/73_frontal.jpg
Normal file
After Width: | Height: | Size: 121 KiB |
BIN
files/Neural_networks/Test/houses/80_frontal.jpg
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2008.JPG
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2010.JPG
Normal file
After Width: | Height: | Size: 75 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2021(1).jpg
Normal file
After Width: | Height: | Size: 70 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2036.jpg
Normal file
After Width: | Height: | Size: 98 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2041.jpg
Normal file
After Width: | Height: | Size: 105 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2046.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2048.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2055.jpg
Normal file
After Width: | Height: | Size: 121 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2059.jpg
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2062.jpg
Normal file
After Width: | Height: | Size: 105 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2074.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2078.jpg
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2080.jpg
Normal file
After Width: | Height: | Size: 98 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2081.jpg
Normal file
After Width: | Height: | Size: 111 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2089.jpg
Normal file
After Width: | Height: | Size: 97 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2091.jpg
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2093.jpg
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2122.jpg
Normal file
After Width: | Height: | Size: 117 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2145.jpg
Normal file
After Width: | Height: | Size: 116 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2160.jpg
Normal file
After Width: | Height: | Size: 99 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2176.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2201.jpg
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2206.jpg
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2332.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2348.jpg
Normal file
After Width: | Height: | Size: 100 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2369.jpg
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2371.jpg
Normal file
After Width: | Height: | Size: 88 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2390.jpg
Normal file
After Width: | Height: | Size: 106 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2431.JPG
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2439.JPG
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2447.JPG
Normal file
After Width: | Height: | Size: 88 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2457.JPG
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2460.JPG
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2475.JPG
Normal file
After Width: | Height: | Size: 72 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2479.JPG
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2481.JPG
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
files/Neural_networks/Test/houses/IMG_2497.JPG
Normal file
After Width: | Height: | Size: 99 KiB |