touch ups

This commit is contained in:
Kamila Bobkowska 2020-05-10 16:27:46 +00:00
parent 1aeb9bf1fa
commit e8bc0b6722

View File

@ -1,114 +1,117 @@
import os import os
import numpy as np import numpy as np
import random import random
import shutil import shutil
from keras.models import Sequential from keras.models import Sequential
from keras.layers import Conv2D, Flatten, MaxPooling2D, Dense from keras.layers import Conv2D, Flatten, MaxPooling2D, Dense
from keras.preprocessing import image from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
#dataset from https://www.kaggle.com/asdasdasasdas/garbage-classification import matplotlib.pyplot as plt
import random
'''#sepperating the file into training and testing data, creation of folders by hand removal of 75 images from papers for a more even distribution
def sepperate(type): #dataset from https://www.kaggle.com/asdasdasasdas/garbage-classification
for i in type:
folder = "Garbage classification\\Garbage classification\\" + i '''#sepperating the file into training and testing data, creation of folders by hand removal of 75 images from papers for a more even distribution
destination = "Garbage classification\\testset\\" + i def sepperate(type):
howmany = len(os.listdir(folder)) for i in type:
for j in range(int(howmany*0.2)): folder = "Garbage classification\\Garbage classification\\" + i
move1 = random.choice(os.listdir(folder)) destination = "Garbage classification\\testset\\" + i
source = "Garbage classification\\Garbage classification\\" + i + "\\" + move1 howmany = len(os.listdir(folder))
d = shutil.move(source, destination, copy_function = shutil.copytree) for j in range(int(howmany*0.2)):
move1 = random.choice(os.listdir(folder))
source = "Garbage classification\\Garbage classification\\" + i + "\\" + move1
types = ["cardboard", "glass", "metal", "paper", "plastic"] d = shutil.move(source, destination, copy_function = shutil.copytree)
sepperate(types)
os.rename("Garbage classification\\Garbage classification", "Garbage classification\\trainset")
''' types = ["cardboard", "glass", "metal", "paper", "plastic"]
sepperate(types)
os.rename("Garbage classification\\Garbage classification", "Garbage classification\\trainset")
classifier = Sequential() '''
classifier.add(Conv2D(32, (3, 3), input_shape=(110, 110, 3), activation = "relu"))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(64, (3, 3), activation = "relu")) classifier = Sequential()
classifier.add(MaxPooling2D(pool_size=(2, 2))) classifier.add(Conv2D(32, (3, 3), input_shape=(110, 110, 3), activation = "relu"))
# this layer in ver 4 classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(32, (3, 3), activation = "relu")) classifier.add(Conv2D(64, (3, 3), activation = "relu"))
classifier.add(MaxPooling2D(pool_size=(2, 2))) classifier.add(MaxPooling2D(pool_size=(2, 2)))
# ----------------- # this layer in ver 4
classifier.add(Flatten()) classifier.add(Conv2D(32, (3, 3), activation = "relu"))
classifier.add(Dense(activation = "relu", units = 64 )) classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dense(activation = "softmax", units = 5)) # -----------------
classifier.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"]) classifier.add(Flatten())
classifier.add(Dense(activation = "relu", units = 64 ))
classifier.add(Dense(activation = "softmax", units = 5))
from keras.preprocessing.image import ImageDataGenerator classifier.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.1,
zoom_range=0.1, train_datagen = ImageDataGenerator(
width_shift_range=0.1, rescale=1./255,
height_shift_range=0.1, shear_range=0.1,
horizontal_flip=True, zoom_range=0.1,
vertical_flip=True, width_shift_range=0.1,
) height_shift_range=0.1,
horizontal_flip=True,
test_datagen = ImageDataGenerator( vertical_flip=True,
rescale=1./255, )
validation_split=0.1
) test_datagen = ImageDataGenerator(
rescale=1./255,
train_generator = train_datagen.flow_from_directory( validation_split=0.1
"Garbage classification\\trainset", )
target_size=(110, 110),
batch_size=16, train_generator = train_datagen.flow_from_directory(
class_mode='categorical', "Garbage classification\\trainset",
#seed=0 target_size=(110, 110),
) batch_size=16,
class_mode='categorical',
test_generator = test_datagen.flow_from_directory( #seed=0
"Garbage classification\\testset", )
target_size=(110, 110),
batch_size=16, test_generator = test_datagen.flow_from_directory(
class_mode='categorical', "Garbage classification\\testset",
) target_size=(110, 110),
batch_size=16,
class_mode='categorical',
#Teaching the classifier )
'''classifier.fit_generator( train_generator, steps_per_epoch = 150, epochs = 25, validation_data = test_generator )
classifier.save_weights('model_ver_4.h5')'''
#Teaching the classifier
import matplotlib.pyplot as plt '''classifier.fit_generator( train_generator, steps_per_epoch = 165, epochs = 32, validation_data = test_generator )
labels = (train_generator.class_indices) classifier.save_weights('model_ver_5.h5')'''
labels = dict((value,key) for key,value in labels.items())
labels = (train_generator.class_indices)
classifier.load_weights("model_ver_4.h5") labels = dict((value,key) for key,value in labels.items())
import random
classifier.load_weights("model_ver_5.h5")
def getTrashPhoto(x, type):
for i in range(x):
kind = random.choice(type)
path = "Garbage classification\\testset\\" + kind def getTrashPhoto(x, type):
file = random.choice(os.listdir(path)) for i in range(x):
path = "Garbage classification\\testset\\" + kind + "\\" + file kind = random.choice(type)
gz = image.load_img(path, target_size = (110,110)) path = "Garbage classification\\testset\\" + kind
ti = image.img_to_array(gz) file = random.choice(os.listdir(path))
ti=np.array(ti)/255.0 path = "Garbage classification\\testset\\" + kind + "\\" + file
ti = np.expand_dims(ti, axis = 0) var = image.load_img(path, target_size = (110,110))
prediction = classifier.predict(ti) ti = image.img_to_array(var)
plt.subplot(1, 3, i+1) ti=np.array(ti)/255.0
plt.imshow(gz) ti = np.expand_dims(ti, axis = 0)
plt.title("AI thinks:%s \nReality:\n %s" % (labels[np.argmax(prediction)], file)) prediction = classifier.predict(ti)
plt.show() plt.subplot(1, 3, i+1)
plt.imshow(var)
plt.title("AI thinks:%s \nReality:\n %s" % (labels[np.argmax(prediction)], file))
types = ["cardboard", "glass", "metal", "paper", "plastic"] plt.show()
type = ["metal"]
getTrashPhoto(3, types) types = ["cardboard", "glass", "metal", "paper", "plastic"]
type = ["metal"]
plt.show() getTrashPhoto(3, types)
plt.show()