You are on page 1of 2

from tensorflow.keras.

models import Sequential


from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
import numpy as np
import os
import cv2
import pickle

def load_data(directory):
X = []
y = []
for img in os.listdir(directory):
label = img.split('_')[0]
if label == 'circle':
y.append([1, 0])
else:
y.append([0, 1])
path = os.path.join(directory, img)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (100, 100))
X.append(img)
X = np.array(X).reshape(-1, 100, 100, 1)
y = np.array(y)
return X, y

def create_model(input_shape, output_shape):


model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=input_shape, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(output_shape, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
return model

def save_weights(model, filename):


model.save(filename)

if __name__ == '__main__':
data_dir = 'images'
weights_file = 'gewichte'

X, y = load_data(data_dir)
model = create_model(X.shape[1:], y.shape[1])
model.fit(X, y, epochs=50, validation_split=0.1)

save_weights(model, weights_file)
)
reste_button = Button(
root,
bg='white',
bd=0,
image=download_icon2,
command=reste
)

download_button.pack(
ipadx=5,
ipady=5,
expand=True
)
reste_button.pack(
ipadx=5,
ipady=5,
expand=True
)

root.mainloop()

You might also like