Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1. IMPORT FILE
- import tensorflow as tf
- from tensorflow import keras
- from tensorflow.keras import Sequential
- from tensorflow.keras.layers import Conv2D, Dense, Dropout, MaxPooling2D, Flatten
- from tensorflow.keras.preprocessing import image
- import numpy as np
- #2. IMPORT IMAGE DATASET
- train_ds, val_ds = keras.utils.image_dataset_from_directory(
- "E:/Maroon/College/S9/DL/Fonts",
- color_mode="grayscale",
- image_size=(28, 28),
- validation_split=0.2,
- subset="both",
- seed=1337
- )
- #3. VARIABLE DECLARATION
- def process(image,label):
- image=tf.cast(image/255,tf.float32)
- return image,label;
- train_ds=train_ds.map(process)
- val_ds=val_ds.map(process)
- #OR WITHOUT FUNCTION (KARVU HOY TO KARJO)
- #train_ds = train_ds.map(lambda x, y: (tf.cast(x, tf.float32) / 255.0, y))
- #val_ds = val_ds.map(lambda x, y: (tf.cast(x, tf.float32) / 255.0, y))
- #4.MODEL CREATION (SUGGESTION AVSE)
- model = Sequential([
- Conv2D(32, (5, 5), activation="relu", padding="same", input_shape=(28, 28, 1)), #input_shape nu suggestion ni avse
- MaxPooling2D(padding="same"),
- Conv2D(64, (5, 5), activation="relu", padding="same"),
- MaxPooling2D(padding="same"),
- Flatten(),
- Dense(1024, activation="relu"),
- Dropout(0.2),
- Dense(26, activation="sigmoid") #CHANGE DENSE 26 ACCORDING TO THE NEED LIKE 10 FOR 0-9 NUMBERS
- ])
- #OPTIONAL
- #model.summary()
- #5. MODEL COMPILE AND TRAINING (SUGGESTION NI AVSE)
- model.compile(optimizer="adam",
- loss="sparse_categorical_crossentropy",
- metrics=["accuracy"])
- model.fit(train_ds, epochs=20, validation_data=val_ds)
- #6. IMAGE ARRAY
- img = image.load_img("E:/Maroon/College/S9/DL/Fonts/test1.png",
- target_size=(28, 28), color_mode="grayscale")
- img_array = np.expand_dims(image.img_to_array(img) / 255.0, axis=0)
- #6. MODEL PREDICTION
- pred = model.predict(img_array)
- pred_class = np.argmax(pred, axis=1)[0]
- #7.VALUE GENERATION
- class_labels = [chr(i) for i in range(65, 91)] #Auto-generate A–Z
- #class_labels=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] Predefined data
- #class_labels = [chr(i) for i in range(65, 91)] Auto-generate 0-9
- #class_labels = [str(i) for i in range(0, 101)] Auto-generate 0 to 100
- #8 PRINT
- print("Predicted label:", class_labels[pred_class])
Advertisement
Add Comment
Please, Sign In to add comment