Plik z algorytmem i uporządkowanie datasetu.
135
algorytm_biedronki.py
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
#%%
|
||||||
|
|
||||||
|
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten,Dropout
|
||||||
|
from tensorflow.keras.models import Model
|
||||||
|
from tensorflow.keras.applications.vgg19 import VGG19
|
||||||
|
from tensorflow.keras.applications.vgg19 import preprocess_input
|
||||||
|
from tensorflow.keras.preprocessing import image
|
||||||
|
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
||||||
|
from tensorflow.keras.models import Sequential
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import os
|
||||||
|
import cv2
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
# %%
|
||||||
|
os.chdir(r"C:\Users\pittb\Documents\biedronkapp\BiedronkApp\biedronki_dataset")
|
||||||
|
train_path="train"
|
||||||
|
test_path="test"
|
||||||
|
# %%
|
||||||
|
x_train=[]
|
||||||
|
|
||||||
|
for folder in os.listdir(train_path):
|
||||||
|
|
||||||
|
sub_path=train_path+"/"+folder
|
||||||
|
|
||||||
|
for img in os.listdir(sub_path):
|
||||||
|
image_path=sub_path+"/"+img
|
||||||
|
|
||||||
|
img_arr=cv2.imread(image_path)
|
||||||
|
|
||||||
|
img_arr=cv2.resize(img_arr,(224,224))
|
||||||
|
|
||||||
|
x_train.append(img_arr)
|
||||||
|
|
||||||
|
x_test=[]
|
||||||
|
|
||||||
|
for folder in os.listdir(test_path):
|
||||||
|
|
||||||
|
sub_path=test_path+"/"+folder
|
||||||
|
|
||||||
|
for img in os.listdir(sub_path):
|
||||||
|
|
||||||
|
image_path=sub_path+"/"+img
|
||||||
|
|
||||||
|
img_arr=cv2.imread(image_path)
|
||||||
|
|
||||||
|
img_arr=cv2.resize(img_arr,(224,224))
|
||||||
|
|
||||||
|
x_test.append(img_arr)
|
||||||
|
# %%
|
||||||
|
train_x=np.array(x_train)
|
||||||
|
test_x=np.array(x_test)
|
||||||
|
|
||||||
|
train_x=train_x/255.0
|
||||||
|
test_x=test_x/255.0
|
||||||
|
# %%
|
||||||
|
train_datagen = ImageDataGenerator(rescale = 1./255)
|
||||||
|
test_datagen = ImageDataGenerator(rescale = 1./255)
|
||||||
|
# %%
|
||||||
|
training_set = train_datagen.flow_from_directory(train_path,
|
||||||
|
target_size = (224, 224),
|
||||||
|
batch_size = 32,
|
||||||
|
class_mode = 'sparse')
|
||||||
|
test_set = test_datagen.flow_from_directory(test_path,
|
||||||
|
target_size = (224, 224),
|
||||||
|
batch_size = 32,
|
||||||
|
class_mode = 'sparse')
|
||||||
|
# %%
|
||||||
|
train_y=training_set.classes
|
||||||
|
test_y=test_set.classes
|
||||||
|
|
||||||
|
training_set.class_indices
|
||||||
|
|
||||||
|
train_y.shape,test_y.shape
|
||||||
|
# %%
|
||||||
|
IMAGE_SIZE = [224, 224]
|
||||||
|
|
||||||
|
vgg = VGG19(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
|
||||||
|
|
||||||
|
for layer in vgg.layers:
|
||||||
|
layer.trainable = False
|
||||||
|
|
||||||
|
# %%
|
||||||
|
# our layers - you can add more if you want
|
||||||
|
x = Flatten()(vgg.output)
|
||||||
|
|
||||||
|
prediction = Dense(2, activation='softmax')(x)
|
||||||
|
# %%
|
||||||
|
# create a model object
|
||||||
|
model = Model(inputs=vgg.input, outputs=prediction)
|
||||||
|
|
||||||
|
# view the structure of the model
|
||||||
|
model.summary()
|
||||||
|
# %%
|
||||||
|
model.compile(
|
||||||
|
loss='sparse_categorical_crossentropy',
|
||||||
|
optimizer="adam",
|
||||||
|
metrics=['accuracy']
|
||||||
|
)
|
||||||
|
# %%
|
||||||
|
from tensorflow.keras.callbacks import EarlyStopping
|
||||||
|
early_stop=EarlyStopping(monitor='val_loss',mode='min',verbose=1,patience=5)
|
||||||
|
# %%
|
||||||
|
history = model.fit(
|
||||||
|
train_x,
|
||||||
|
train_y,
|
||||||
|
epochs=10,
|
||||||
|
callbacks=[early_stop],
|
||||||
|
batch_size=32,shuffle=True)
|
||||||
|
# %%
|
||||||
|
plt.plot(history.history['loss'], label='train loss')
|
||||||
|
plt.legend()
|
||||||
|
|
||||||
|
plt.savefig('vgg-loss-rps-1.png')
|
||||||
|
plt.show()
|
||||||
|
# %%
|
||||||
|
# accuracies
|
||||||
|
plt.plot(history.history['accuracy'], label='train acc')
|
||||||
|
plt.legend()
|
||||||
|
|
||||||
|
plt.savefig('vgg-acc-rps-1.png')
|
||||||
|
plt.show()
|
||||||
|
# %%
|
||||||
|
model.evaluate(test_x,test_y,batch_size=6)
|
||||||
|
# %%
|
||||||
|
from sklearn.metrics import accuracy_score,classification_report,confusion_matrix
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
y_pred=model.predict(test_x)
|
||||||
|
y_pred=np.argmax(y_pred,axis=1)
|
||||||
|
|
||||||
|
accuracy_score(y_pred,test_y)
|
||||||
|
|
||||||
|
print(classification_report(y_pred,test_y))
|
||||||
|
# %%
|
Before Width: | Height: | Size: 1.0 MiB |
Before Width: | Height: | Size: 982 KiB |
Before Width: | Height: | Size: 904 KiB |
Before Width: | Height: | Size: 755 KiB |
Before Width: | Height: | Size: 252 KiB |
Before Width: | Height: | Size: 120 KiB |
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 8.1 KiB After Width: | Height: | Size: 8.1 KiB |
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 98 KiB After Width: | Height: | Size: 98 KiB |
Before Width: | Height: | Size: 189 KiB After Width: | Height: | Size: 189 KiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 5.6 MiB After Width: | Height: | Size: 5.6 MiB |
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 925 KiB After Width: | Height: | Size: 925 KiB |
Before Width: | Height: | Size: 630 KiB After Width: | Height: | Size: 630 KiB |
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 1.2 MiB |
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 1.2 MiB |
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 53 KiB |
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
Before Width: | Height: | Size: 103 KiB After Width: | Height: | Size: 103 KiB |
Before Width: | Height: | Size: 158 KiB After Width: | Height: | Size: 158 KiB |
Before Width: | Height: | Size: 464 KiB After Width: | Height: | Size: 464 KiB |
Before Width: | Height: | Size: 254 KiB After Width: | Height: | Size: 254 KiB |
Before Width: | Height: | Size: 378 KiB After Width: | Height: | Size: 378 KiB |
Before Width: | Height: | Size: 346 KiB After Width: | Height: | Size: 346 KiB |
Before Width: | Height: | Size: 192 KiB After Width: | Height: | Size: 192 KiB |
Before Width: | Height: | Size: 161 KiB After Width: | Height: | Size: 161 KiB |
Before Width: | Height: | Size: 323 KiB After Width: | Height: | Size: 323 KiB |
Before Width: | Height: | Size: 358 KiB After Width: | Height: | Size: 358 KiB |
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 122 KiB |
Before Width: | Height: | Size: 108 KiB After Width: | Height: | Size: 108 KiB |
Before Width: | Height: | Size: 91 KiB After Width: | Height: | Size: 91 KiB |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
Before Width: | Height: | Size: 361 KiB After Width: | Height: | Size: 361 KiB |
Before Width: | Height: | Size: 2.0 MiB After Width: | Height: | Size: 2.0 MiB |
Before Width: | Height: | Size: 1.8 MiB After Width: | Height: | Size: 1.8 MiB |
Before Width: | Height: | Size: 456 KiB After Width: | Height: | Size: 456 KiB |
Before Width: | Height: | Size: 850 KiB After Width: | Height: | Size: 850 KiB |
Before Width: | Height: | Size: 310 KiB After Width: | Height: | Size: 310 KiB |
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 274 KiB |
Before Width: | Height: | Size: 146 KiB After Width: | Height: | Size: 146 KiB |
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 130 KiB |
After Width: | Height: | Size: 156 KiB |
After Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 187 KiB After Width: | Height: | Size: 187 KiB |
Before Width: | Height: | Size: 304 KiB After Width: | Height: | Size: 304 KiB |
Before Width: | Height: | Size: 368 KiB After Width: | Height: | Size: 368 KiB |
Before Width: | Height: | Size: 242 KiB After Width: | Height: | Size: 242 KiB |
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 274 KiB |
Before Width: | Height: | Size: 220 KiB After Width: | Height: | Size: 220 KiB |
Before Width: | Height: | Size: 234 KiB After Width: | Height: | Size: 234 KiB |
After Width: | Height: | Size: 112 KiB |
After Width: | Height: | Size: 186 KiB |
Before Width: | Height: | Size: 387 KiB After Width: | Height: | Size: 387 KiB |
After Width: | Height: | Size: 109 KiB |
Before Width: | Height: | Size: 1.8 MiB After Width: | Height: | Size: 1.8 MiB |
Before Width: | Height: | Size: 703 KiB After Width: | Height: | Size: 703 KiB |
Before Width: | Height: | Size: 158 KiB After Width: | Height: | Size: 158 KiB |
After Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 121 KiB |