Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create img_recog #154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions img_recog
Original file line number Diff line number Diff line change
@@ -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)