File size: 4,869 Bytes
40f30f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential

import pathlib

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

data_dir = "C:/Users/jilek/Downloads/AAT+"
data_dir = pathlib.Path(data_dir).with_suffix('')

data_dir_test = "C:/Users/jilek/Downloads/AAT+_TEST"
data_dir_test = pathlib.Path(data_dir_test).with_suffix('')

image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)

batch_size = 1
img_height = 1024
img_width = 1024

train_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    validation_split=0.0,
    #subset="training",
    seed=123,
    labels='inferred',
    label_mode='categorical',
    class_names=["C100", "C095", "C090", "C085", "C080", "C070", "C060", "C040", "C020"],
    color_mode="grayscale", #grayscale
    shuffle=True,
    image_size=(img_height, img_width),
    batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir_test,
    validation_split=0.0,
    #subset="validation",
    seed=123,
    labels='inferred',
    label_mode='categorical',
    class_names=["C100", "C095", "C090", "C085", "C080", "C070", "C060", "C040", "C020"],
    color_mode="grayscale",
    image_size=(img_height, img_width),
    batch_size=batch_size)

class_names = train_ds.class_names
print(class_names)

for image_batch, labels_batch in train_ds:
    print(image_batch.shape)
    print(labels_batch.shape)
    break

AUTOTUNE = tf.data.AUTOTUNE

data_augmentation = keras.Sequential(
    [
        layers.RandomFlip("horizontal_and_vertical",
                          input_shape=(img_height,
                                       img_width,
                                       1)), #rgb
        #layers.RandomRotation(0.5),
        #layers.RandomZoom(0.5),
    ]
)

train_ds = train_ds.shuffle(buffer_size=900).prefetch(buffer_size=AUTOTUNE) #.cache()
val_ds = val_ds.prefetch(buffer_size=AUTOTUNE) #.cache()


num_classes = len(class_names)
print(str(num_classes))

model = Sequential([
  layers.Rescaling(1.0/255, input_shape=(img_height, img_width, 1)), #rgb
  #layers.Dropout(0.0),
  #layers.MaxPooling2D(pool_size=(8, 8)),
  layers.Conv2D(4, (4, 4), strides=(2, 2), padding='valid', dilation_rate=(1, 1), groups=1, input_shape=(1024, 1024, 1), activation='relu'),
  layers.Conv2D(8, (4, 4), strides=(2, 2), padding='valid', dilation_rate=(1, 1), groups=1, input_shape=(512, 512, 4), activation='relu'),
  layers.Conv2D(16, (4, 4), strides=(4, 4), padding='valid', dilation_rate=(1, 1), groups=1, input_shape=(256, 256, 8), activation='relu'),
  layers.Conv2D(32, (4, 4), strides=(4, 4), padding='valid', dilation_rate=(1, 1), groups=1, input_shape=(64, 64, 16), activation='relu'),
  layers.Conv2D(64, (4, 4), strides=(4, 4), padding='valid', dilation_rate=(1, 1), groups=1, input_shape=(16, 16, 32), activation='relu'),
  #layers.Conv2D(128, (4, 4), strides=(1, 1), padding='valid', dilation_rate=(1, 1), groups=1, input_shape=(8, 8, 64), activation='relu'),
  #layers.Dropout(0.1),
  layers.Flatten(),
  layers.Dense(32, activation='relu'),
  layers.Dense(num_classes, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=['accuracy'])

model.summary()
model.save("./model/AAT+")

epochs = 130
history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

test_dir = "C:/Users/jilek/Downloads/AAT_T/"
for file_name in os.listdir(test_dir):
    file_path = os.path.join(test_dir, file_name)
    img = tf.keras.utils.load_img(
        file_path, target_size=(img_height, img_width), color_mode="grayscale" #grayscale
    )
    img_array = tf.keras.utils.img_to_array(img)
    img_array = tf.expand_dims(img_array, 0)  # Create a batch

    predictions = model.predict(img_array)
    score = tf.nn.softmax(predictions[0])

    print(file_name)
    print(
        "This image most likely belongs to {} with a {:.2f} percent confidence."
        .format(class_names[np.argmax(score)], 100 * np.max(score))
    )