-
Notifications
You must be signed in to change notification settings - Fork 0
/
autobilder.py
165 lines (136 loc) · 4.85 KB
/
autobilder.py
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
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 21 11:08:26 2021
@author: alex
"""
# Install if missing
# ! pip install tensorflow_hub
import os, shutil
import matplotlib.pylab as plt
from sklearn.metrics import confusion_matrix
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
print("TF version:", tf.__version__)
print("Hub version:", hub.__version__)
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")
# Download data from Kaggle or Git to local directory ('/test')
# https://github.com/nicolas-gervais/predicting-car-price-from-scraped-data/tree/master/picture-scraper
# subfolder erstellen
sourcefile = 'C:/Users/alex/CAS_ML_local/B_Deeplearning/03_Project/test'
'''
# Filename auslesen und Orderstruktur anlegen
for filename in os.listdir(sourcefile):
brand = filename.rsplit('_', 17)[0]
try:
os.mkdir(os.path.join(sourcefile, brand)) # Ordner erstellen wenn nötig...
except WindowsError:
pass # ...sonst weiter und Bild moven
shutil.move(os.path.join(sourcefile, filename), os.path.join(sourcefile, brand, filename))
# Anzahl Bilder zählen
def get_nr_files(sourcefile):
file_count = 0
for r, d, files in os.walk(sourcefile):
file_count += len(files)
return file_count
# shoud return 64467
'''
# Daten in TensorFlow laden und spliten
para_kwargs = dict(
directory=sourcefile,
labels='inferred',
label_mode='categorical', # for fitting = 'categorical' for print = 'int'
class_names=None,
color_mode='rgb',
batch_size=32,
image_size=(224, 224),
shuffle=False,
seed=None,
validation_split=0.2,
interpolation='bilinear',
follow_links=False)
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
**para_kwargs,
subset='training')
valid_ds = tf.keras.preprocessing.image_dataset_from_directory(
**para_kwargs,
subset='validation')
for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
# Visualisieren der eingelesenen Daten
class_names = train_ds.class_names
image_batch, label_batch = next(iter(train_ds))
image_batch / 255
plt.figure(figsize=(10, 10))
for i in range(16):
ax = plt.subplot(4, 4, i + 1)
plt.imshow(image_batch[i].numpy().astype("uint8"))
label = label_batch[i]
plt.title(class_names[label])
plt.axis("off")
# Pre-trained Model auswählen (aus TF-Hub)
model_name = "mobilenet_v2_100_224"
model_handle_map = {"mobilenet_v2_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4",}
model_handle = model_handle_map.get(model_name)
IMAGE_SIZE = (224, 224, 3)
BATCH_SIZE = 32
print(f"\nAusgewaehltes model: {model_name} : {model_handle}")
print(f"\nBild-Groesse {IMAGE_SIZE}")
# CNN zusammenstellen mit ANzahl Klassen wie im Datenset
print("\nModell erstellen mit", model_handle)
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(IMAGE_SIZE)),
hub.KerasLayer(model_handle, trainable=False),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(len(class_names),
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE)
model.summary()
# Trainieren des Modells
model.compile(
optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1),
metrics=['accuracy'])
history = model.fit(
train_ds,
epochs=25,
validation_data=valid_ds).history
# plot the development of the accuracy and loss during training
plt.figure(figsize=(12,4))
plt.subplot(1,2,(1))
plt.plot(history['accuracy'],linestyle='-.')
plt.plot(history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'valid'], loc='lower right')
plt.subplot(1,2,(2))
plt.plot(history['loss'],linestyle='-.')
plt.plot(history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'valid'], loc='upper right')
plt.show()
pred=model.predict(valid_ds)
'''
Wie machen wir Test datenset?
Wie zeigen wir confusionmatrix ?
Wie zeigen wir predicted foto?
Cool wäre:
eigenes modell
alles auf colab migrieren
print(confusion_matrix(np.argmax(valid_ds.image_batch,axis=1),np.argmax(pred,axis=1)))
acc_fc = np.sum(np.argmax(valid_ds,axis=1)==np.argmax(pred,axis=1))/len(pred)
print("Acc = " , acc_fc)
def show_batch(image_batch, label_batch):
plt.figure(figsize=(10,10))
for n in range(25):
ax = plt.subplot(5,5,n+1)
plt.imshow(image_batch[n])
plt.title(class_names[label_batch[n]==1][0].title())
plt.axis('off')
show_batch(train_ds, train_ds)
'''