Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
haverland committed Dec 22, 2024
1 parent 9e13d56 commit 8dbc3b1
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 915 deletions.
39 changes: 23 additions & 16 deletions b2n/augmentation/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from scipy.stats import norm
import cv2
import random
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
import tensorflow.python.keras.layers as layers
import random

def add_noise(img):
Expand Down Expand Up @@ -141,25 +143,30 @@ def invert_noise(img):

def augmentation(x, y, Batch_Size = 32, Width_Shift_Range = 3, Height_Shift_Range = 0,
Brightness_Range = [0.4,1.6],
Rotation_Angle = 5,
Rotation_Angle = 0.01,
ZoomRange_Out = 0.1,
ZoomRange_In = 0.1,
ShearRange= 3,
Channel_Shift=0.4,
preprocessing_function=preprocessing):
datagen = ImageDataGenerator(width_shift_range=Width_Shift_Range,
height_shift_range=Height_Shift_Range,
brightness_range=Brightness_Range,
#zoom_range=[1-ZoomRange_In, 1+ZoomRange_Out],
rotation_range=Rotation_Angle,
#channel_shift_range=Channel_Shift,
fill_mode='nearest',
shear_range=ShearRange
,preprocessing_function=preprocessing_function
,dtype=float)
#datagen.fit(x)
return datagen.flow(x, y, batch_size=Batch_Size)
# datagen = ImageDataGenerator(width_shift_range=Width_Shift_Range,
# height_shift_range=Height_Shift_Range,
# brightness_range=Brightness_Range,
# #zoom_range=[1-ZoomRange_In, 1+ZoomRange_Out],
# rotation_range=Rotation_Angle,
# #channel_shift_range=Channel_Shift,
# fill_mode='nearest',
# shear_range=ShearRange
# ,preprocessing_function=preprocessing_function
# ,dtype=float)

preprocessing = tf.keras.Sequential([
tf.keras.layers.RandomZoom([ZoomRange_In, ZoomRange_Out]),
tf.keras.layers.RandomRotation((-Rotation_Angle, Rotation_Angle))])

datagen = tf.data.Dataset.from_tensor_slices(x, y)
return datagen #datagen.map(preprocessing)


def no_augmentation(x, y, Batch_Size = 32):
datagen = ImageDataGenerator(dtype=float)
return datagen.flow(x, y, batch_size=Batch_Size)
return tf.data.Dataset.from_tensor_slices(x, y)
2 changes: 0 additions & 2 deletions b2n/data/ziffer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import shutil
from PIL import Image
from tensorflow import keras
import numpy as np


Expand Down
2 changes: 0 additions & 2 deletions b2n/encodings/class_encoding.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import tensorflow.keras as keras
import math
import numpy as np

def class_encoding(y_train, nb_classes):
Expand Down
18 changes: 18 additions & 0 deletions b2n/plotfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ def plot_dataset_it(data_iter, columns=9, rows=5, nb_classes=100, classdecoding=
plt.axhline(y=y,color='yellow')
plt.show()

def plot_dataset_ds(data_set, columns=9, rows=5, nb_classes=100, classdecoding=class_decoding):

fig = plt.figure(figsize=(18, 11))
data_iter = iter(data_set)

for i in range(1, columns*rows +1):
img, label = next(data_iter)
fig.add_subplot(rows, columns, i)
plt.xticks([0.2, 0.4, 0.6, 0.8])
plt.title(str(class_decoding(np.array(label), nb_classes).reshape(-1)[0] )) # set title
plt.imshow(np.array(img).astype(np.uint8), aspect='1.6', cmap='gray', extent=[0, 1, 0, 1], )
ax=plt.gca()
ax.get_xaxis().set_visible(False)
# yellow lines
for y in np.arange(0.2, 0.8, 0.2):
plt.axhline(y=y,color='yellow')
plt.show()

def plot_acc_loss(history, modelname="modelname"):
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle(modelname)
Expand Down
1,167 changes: 282 additions & 885 deletions dig-class100-s2.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dig-class100-s2_bw.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@
"\n",
"#annealer = LearningRateScheduler(lambda x: 1e-3 * 0.98 ** x, verbose=0)\n",
"earlystop = EarlyStopping(monitor='val_loss', mode='min' , patience=25, verbose=1, restore_best_weights=True)\n",
"mc = ModelCheckpoint(output_tmp_dir + model_filename + '_best.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True, initial_value_threshold=.9)\n",
"mc = ModelCheckpoint(output_tmp_dir + model_filename + '_best.keras', monitor='val_loss', mode='min', verbose=1, save_best_only=True, initial_value_threshold=.9)\n",
"reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.85, patience=3, min_lr=1e-5, verbose=1)\n",
"history = model.fit(train_iterator, \n",
" validation_data=validation_iterator, \n",
Expand All @@ -985,7 +985,7 @@
" verbose=1)\n",
"\n",
"\n",
"#model.save(model_filename + '.h5')\n",
"#model.save(model_filename + '.keras')\n",
"\n",
"plot_acc_loss(history, \"Trainingsresults\")"
]
Expand Down Expand Up @@ -1039,7 +1039,7 @@
}
],
"source": [
"model = keras.models.load_model(output_tmp_dir + model_filename + '_best.h5')\n",
"model = keras.models.load_model(output_tmp_dir + model_filename + '_best.keras')\n",
"false_predicted_files = predict_meter_digits(model, x_data=xz_data, y_data=yz_data, f_data=fz_data, \n",
" max_delta=0.11)\n",
"fpf = pd.DataFrame(false_predicted_files)\n",
Expand Down
6 changes: 3 additions & 3 deletions dig-class100_s0.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@
"\n",
"#annealer = LearningRateScheduler(lambda x: 1e-3 * 0.98 ** x, verbose=0)\n",
"earlystop = EarlyStopping(monitor='val_accuracy', mode='max' , patience=25, verbose=1, restore_best_weights=True)\n",
"mc = ModelCheckpoint(model_filename + '_best.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)\n",
"mc = ModelCheckpoint(model_filename + '_best.keras', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)\n",
"reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=5, min_lr=1e-5, verbose=1)\n",
"history = model.fit(train_iterator, \n",
" validation_data=validation_iterator, \n",
Expand All @@ -402,7 +402,7 @@
" verbose=1)\n",
"\n",
"\n",
"#model.save(model_filename + '.h5')\n",
"#model.save(model_filename + '.keras')\n",
"\n",
"plot_acc_loss(history, \"Trainingsresults\")"
]
Expand All @@ -424,7 +424,7 @@
"metadata": {},
"outputs": [],
"source": [
"model = keras.models.load_model(model_filename + '_best.h5')\n",
"model = keras.models.load_model(model_filename + '_best.keras')\n",
"xz_data, yz_data, fz_data = shuffle(xz_data, yz_data, fz_data)\n",
"false_predicted_files = predict_meter_digits(model, x_data=xz_data, y_data=yz_data, f_data=fz_data, \n",
" max_delta=0.)\n",
Expand Down
2 changes: 1 addition & 1 deletion dig_class100-s1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@
" verbose=1)\n",
"\n",
"\n",
"#model.save(model_filename + '.h5')\n",
"#model.save(model_filename + '.keras')\n",
"\n",
"plot_acc_loss(history, \"Trainingsresults\")"
]
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ lxml_html_clean
scipy
scikit-learn
pandas
tensorflow>=2.12.1; sys_platform != 'darwin'
tensorflow-macos>=2.12.1; sys_platform == 'darwin'
tensorflow-metal==0.8; sys_platform == 'darwin'
tensorflow<=2.17.0; sys_platform != 'darwin'
#tensorflow-macos>=2.12.1; sys_platform == 'darwin'
#tensorflow-metal==0.8; sys_platform == 'darwin'
tensorflow-model-optimization
opencv-python
notebook
Expand Down

0 comments on commit 8dbc3b1

Please sign in to comment.