Spaces:
Sleeping
Sleeping
Upload 48 files
Browse files- finetuning.py +39 -0
- train/Bulbasaur/00000000.png +0 -0
- train/Bulbasaur/00000002.png +0 -0
- train/Bulbasaur/00000003.png +0 -0
- train/Bulbasaur/00000004.png +0 -0
- train/Bulbasaur/00000005.png +0 -0
- train/Bulbasaur/00000006.jpg +0 -0
- train/Charmander/00000000.png +0 -0
- train/Charmander/00000001.jpg +0 -0
- train/Charmander/00000001.png +0 -0
- train/Charmander/00000002.jpg +0 -0
- train/Charmander/00000002.png +0 -0
- train/Charmander/00000003.png +0 -0
- train/Charmander/00000004.png +0 -0
- train/Charmander/00000005.png +0 -0
- train/Charmander/00000006.png +0 -0
- train/Squirtle/00000000.png +0 -0
- train/Squirtle/00000001.jpg +0 -0
- train/Squirtle/00000002.jpg +0 -0
- train/Squirtle/00000002.png +0 -0
- train/Squirtle/00000003.png +0 -0
- train/Squirtle/00000004.png +0 -0
- train/Squirtle/00000005.png +0 -0
- train/Squirtle/00000006.jpg +0 -0
- validation/Bulbasaur/00000000.png +0 -0
- validation/Bulbasaur/00000002.png +0 -0
- validation/Bulbasaur/00000003.png +0 -0
- validation/Bulbasaur/00000004.png +0 -0
- validation/Bulbasaur/00000005.png +0 -0
- validation/Bulbasaur/00000006.jpg +0 -0
- validation/Charmander/00000000.png +0 -0
- validation/Charmander/00000001.jpg +0 -0
- validation/Charmander/00000001.png +0 -0
- validation/Charmander/00000002.jpg +0 -0
- validation/Charmander/00000002.png +0 -0
- validation/Charmander/00000003.png +0 -0
- validation/Charmander/00000004.png +0 -0
- validation/Charmander/00000005.png +0 -0
- validation/Charmander/00000006.png +0 -0
- validation/Squirtle/00000000.png +0 -0
- validation/Squirtle/00000001.jpg +0 -0
- validation/Squirtle/00000002.jpg +0 -0
- validation/Squirtle/00000002.png +0 -0
- validation/Squirtle/00000003.png +0 -0
- validation/Squirtle/00000004.png +0 -0
- validation/Squirtle/00000005.png +0 -0
- validation/Squirtle/00000006.jpg +0 -0
- visualize.py +40 -0
finetuning.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
|
3 |
+
|
4 |
+
# Trainiere das Modell
|
5 |
+
history = model.fit(
|
6 |
+
train_generator,
|
7 |
+
steps_per_epoch=train_generator.samples // train_generator.batch_size,
|
8 |
+
epochs=10,
|
9 |
+
validation_data=validation_generator,
|
10 |
+
validation_steps=validation_generator.samples // validation_generator.batch_size
|
11 |
+
)
|
12 |
+
|
13 |
+
# Speichern von Genauigkeit und Verlust während des Trainings
|
14 |
+
acc = history.history['accuracy']
|
15 |
+
val_acc = history.history['val_accuracy']
|
16 |
+
loss = history.history['loss']
|
17 |
+
val_loss = history.history['val_loss']
|
18 |
+
|
19 |
+
# Plot der Trainingshistorie
|
20 |
+
plt.figure(figsize=(8, 8))
|
21 |
+
|
22 |
+
# Subplot für Genauigkeit
|
23 |
+
plt.subplot(2, 1, 1)
|
24 |
+
plt.plot(acc, label='Training Accuracy')
|
25 |
+
plt.plot(val_acc, label='Validation Accuracy')
|
26 |
+
plt.ylim([0.4, 1]) # Setze die y-Achsen-Grenzen
|
27 |
+
plt.plot([initial_epochs - 1, initial_epochs - 1], plt.ylim(), label='Start Fine Tuning')
|
28 |
+
plt.legend(loc='lower right')
|
29 |
+
plt.title('Training and Validation Accuracy')
|
30 |
+
|
31 |
+
# Subplot für Verlust
|
32 |
+
plt.subplot(2, 1, 2)
|
33 |
+
plt.plot(loss, label='Training Loss')
|
34 |
+
plt.plot(val_loss, label='Validation Loss')
|
35 |
+
plt.plot([initial_epochs - 1, initial_epochs - 1], plt.ylim(), label='Start Fine Tuning')
|
36 |
+
plt.legend(loc='upper right')
|
37 |
+
plt.title('Training and Validation Loss')
|
38 |
+
plt.xlabel('Epoch')
|
39 |
+
plt.show()
|
train/Bulbasaur/00000000.png
ADDED
train/Bulbasaur/00000002.png
ADDED
train/Bulbasaur/00000003.png
ADDED
train/Bulbasaur/00000004.png
ADDED
train/Bulbasaur/00000005.png
ADDED
train/Bulbasaur/00000006.jpg
ADDED
train/Charmander/00000000.png
ADDED
train/Charmander/00000001.jpg
ADDED
train/Charmander/00000001.png
ADDED
train/Charmander/00000002.jpg
ADDED
train/Charmander/00000002.png
ADDED
train/Charmander/00000003.png
ADDED
train/Charmander/00000004.png
ADDED
train/Charmander/00000005.png
ADDED
train/Charmander/00000006.png
ADDED
train/Squirtle/00000000.png
ADDED
train/Squirtle/00000001.jpg
ADDED
train/Squirtle/00000002.jpg
ADDED
train/Squirtle/00000002.png
ADDED
train/Squirtle/00000003.png
ADDED
train/Squirtle/00000004.png
ADDED
train/Squirtle/00000005.png
ADDED
train/Squirtle/00000006.jpg
ADDED
validation/Bulbasaur/00000000.png
ADDED
validation/Bulbasaur/00000002.png
ADDED
validation/Bulbasaur/00000003.png
ADDED
validation/Bulbasaur/00000004.png
ADDED
validation/Bulbasaur/00000005.png
ADDED
validation/Bulbasaur/00000006.jpg
ADDED
validation/Charmander/00000000.png
ADDED
validation/Charmander/00000001.jpg
ADDED
validation/Charmander/00000001.png
ADDED
validation/Charmander/00000002.jpg
ADDED
validation/Charmander/00000002.png
ADDED
validation/Charmander/00000003.png
ADDED
validation/Charmander/00000004.png
ADDED
validation/Charmander/00000005.png
ADDED
validation/Charmander/00000006.png
ADDED
validation/Squirtle/00000000.png
ADDED
validation/Squirtle/00000001.jpg
ADDED
validation/Squirtle/00000002.jpg
ADDED
validation/Squirtle/00000002.png
ADDED
validation/Squirtle/00000003.png
ADDED
validation/Squirtle/00000004.png
ADDED
validation/Squirtle/00000005.png
ADDED
validation/Squirtle/00000006.jpg
ADDED
visualize.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
5 |
+
from tensorflow.keras.applications import ResNet50
|
6 |
+
from tensorflow.keras.models import Model
|
7 |
+
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
8 |
+
from tensorflow.keras.optimizers import Adam
|
9 |
+
|
10 |
+
# Laden der Validierungsdaten
|
11 |
+
validation_datagen = ImageDataGenerator(rescale=1./255)
|
12 |
+
validation_generator = validation_datagen.flow_from_directory(
|
13 |
+
r'C:\Coding\BlockImageClassification\validation',
|
14 |
+
target_size=(224, 224),
|
15 |
+
batch_size=8,
|
16 |
+
class_mode='categorical',
|
17 |
+
shuffle=True # Mischt die Daten vor jeder Epoche
|
18 |
+
)
|
19 |
+
|
20 |
+
# Laden des Modells
|
21 |
+
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
22 |
+
x = base_model.output
|
23 |
+
x = GlobalAveragePooling2D()(x)
|
24 |
+
x = Dense(1024, activation='relu')(x)
|
25 |
+
predictions = Dense(3, activation='softmax')(x)
|
26 |
+
model = Model(inputs=base_model.input, outputs=predictions)
|
27 |
+
|
28 |
+
# Vorhersagen auf den Validierungsdaten
|
29 |
+
predictions_in_percentage = model.predict(validation_generator)
|
30 |
+
predictions = np.argmax(predictions_in_percentage, axis=-1)
|
31 |
+
|
32 |
+
# Darstellen der Vorhersagen
|
33 |
+
class_names = ['Bulbasaur', 'Charmander', 'Squirtle'] # Aktualisierte Klassennamen
|
34 |
+
for i in range(len(predictions)):
|
35 |
+
image, label = validation_generator[i]
|
36 |
+
plt.imshow(image[0])
|
37 |
+
plt.title('pred. ' + class_names[predictions[i]] + ' war ' + class_names[np.argmax(label)] + ' ' + str(np.round(predictions_in_percentage[i], 2)), fontsize=8)
|
38 |
+
plt.axis("off")
|
39 |
+
plt.show()
|
40 |
+
|