this is my code: import matplotlib.pyplot as plt import numpy as np import PIL import…
- User
this is my code:
import matplotlib.pyplot as plt import numpy as np import PIL import tensorflow as tf import pathlib import os from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.models import Sequential
dataset_folder_path = r"D://parser_selenium//2ch wiper//captcha_solver//Dataset//" model_path = f'{dataset_folder_path}model_before_augmentation.h5'
dataset_dir = pathlib.Path(dataset_folder_path)
batch_size = 32 img_width = 20 img_height = 20
train_ds = tf.keras.utils.image_dataset_from_directory( dataset_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory( dataset_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size)
class_names = train_ds.class_names
print(f"Class names: {class_names}")
# create model
num_classes = len(class_names) model = Sequential([ # т.к. у нас версия TF 2.6 локально layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
code# аугментация layers.experimental.preprocessing.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)), layers.experimental.preprocessing.RandomRotation(0.1), layers.experimental.preprocessing.RandomZoom(0.1), layers.experimental.preprocessing.RandomContrast(0.2), # дальше везде одинаково layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(32, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, padding='same', activation='relu'), layers.MaxPooling2D(), # регуляризация layers.Dropout(0.2), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(num_classes)])
compile the model
model.compile( optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
# print model summary
model.summary()
model.load_weights(model_path)
loss,acc = model.evaluate(train_ds, verbose=2)
def Detect(img_path):
- Assistant
If you want to not print the messages such as "ms/step" when making predictions (classifying images) with your TensorFlow/Keras model, you can set the argument
verboseto 0 in thepredictmethod.For example, in the
Detectfunction of your code, modify the line where you callmodel.predictlike this:pythonpredictions = model.predict(img_array, verbose=0)So, the updated
Detectfunction will look like this:pythondef Detect(img_path): img = tf.keras.utils.load_img( img_path, target_size=(img_height, img_width)) img_array = tf.keras.utils.img_to_array(img) img_array = tf.expand_dims(img_array, 0) predictions = model.predict(img_array, verbose=0) # Set verbose to 0 score = tf.nn.softmax(predictions[0]) return(class_names[np.argmax(score)])