-
Notifications
You must be signed in to change notification settings - Fork 142
/
img_recog
63 lines (52 loc) · 2.03 KB
/
img_recog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)