From 7b5c3f457b7292d7322efaf3bc52be13dffc87d5 Mon Sep 17 00:00:00 2001 From: devank21 <132289193+devank21@users.noreply.github.com> Date: Mon, 23 Oct 2023 17:41:15 +0530 Subject: [PATCH] Create img_recog --- img_recog | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 img_recog diff --git a/img_recog b/img_recog new file mode 100644 index 0000000..215385f --- /dev/null +++ b/img_recog @@ -0,0 +1,63 @@ +from keras.preprocessing import image +import numpy as np +import tensorflow as tf +from keras.preprocessing.image import ImageDataGenerator +# print(tf.__version__) +train_datagen = ImageDataGenerator( + rescale=1./255, + shear_range=0.4, + zoom_range=0.3, + horizontal_flip=True) + +training_set = train_datagen.flow_from_directory( + r"C:\Users\devan_jmbn6cf\Downloads\archive (3)\train", + target_size=(64, 64), + batch_size=16, + class_mode='sparse') + +test_datagen = ImageDataGenerator(rescale=1./255) +test_set = test_datagen.flow_from_directory( + r"C:\Users\devan_jmbn6cf\Downloads\archive (3)\test", + target_size=(64, 64), + batch_size=16, + class_mode='sparse') +cnn = tf.keras.models.Sequential() # artifical neural network +# convolution +cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, + activation='relu', input_shape=[64, 64, 3])) +# pooling +cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) +# 2nd convolution layer +cnn.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu')) +cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) +# flattening +cnn.add(tf.keras.layers.Flatten()) +# full connection +cnn.add(tf.keras.layers.Dense(units=128, activation='relu')) +# output layer +cnn.add(tf.keras.layers.Dense(units=23, activation='softmax')) + +# training the cnn +cnn.compile(optimizer='adam', loss='sparse_categorical_crossentropy', + metrics=['accuracy']) + + +cnn.fit(x=training_set, validation_data=test_set, epochs=25) + + +test_image = image.load_img( + r'C:\Users\devan_jmbn6cf\Downloads\archive (3)\test\Eczema Photos\03DermatitisLids1.jpg', target_size=(64, 64)) +test_image = image.img_to_array(test_image) +test_image = np.expand_dims(test_image, axis=0) + +result = cnn.predict(test_image) + +print(test_set.class_indices) + +predicted_class = np.argmax(result, axis=1) +predicted_class = predicted_class +print("ans:", predicted_class[0]) + +# input_arr = tf.keras.utils.img_to_array(image) +# input_arr = np.array([input_arr]) # Convert single image to a batch. +# predictions = cnn.predict(input_arr)