Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from keras.models import Sequential
- from keras.layers import Conv2D
- from keras.layers import Flatten, MaxPooling2D
- from keras.models import Sequential
- from keras.layers import Dense, Activation, Dropout
- from keras.models import load_model
- import numpy as np
- import random
- import cv2
- import imutils
- import glob
- def dawaj():
- y_train = []
- y_validate = []
- size = (200, 200)
- train_range = 200
- random.seed()
- for i in range(100):
- a = random.randrange(1, 11)
- b = random.randrange(0, 9)
- image = cv2.imread('im{}.jpg'.format(a))
- sword = cv2.imread('mieczalak/miecz{}.jpg'.format(b))
- alfa = random.randrange(0, 360)
- sword = imutils.rotate_bound(sword, alfa)
- size2 = (int(np.shape(sword)[1]/2), int(np.shape(sword)[0]/2))
- x = random.randrange(0, size[1] - size2[1])
- y = random.randrange(0, size[0] - size2[0])
- image = cv2.resize(image, size)
- sword = cv2.resize(sword, size2)
- for j in range(size2[0]):
- for k in range(size2[1]):
- if sword[k, j, 0] == 0 and sword[k, j, 1] == 0 and sword[k, j, 2] == 0:
- pass
- else:
- image[x + k, y + j, 0] = sword[k, j, 0]
- image[x + k, y + j, 1] = sword[k, j, 1]
- image[x + k, y + j, 2] = sword[k, j, 2]
- if i < 80:
- cv2.imwrite('dataset/train/im{}.jpg'.format(i), image)
- y_train.append([(y + size2[0]/2)/size[0], (x + size2[1]/2)/size[1]])
- else:
- cv2.imwrite('dataset/validate/im{}.jpg'.format(i), image)
- y_validate.append([(y + size2[0]/2)/size[0], (x+size2[1]/2)/size[1]])
- x_train = [cv2.imread(file) for file in glob.glob('./dataset/train/*.jpg')]
- x_validate = [cv2.imread(file) for file in glob.glob('./dataset/validate/*.jpg')]
- x_train = np.array(x_train)
- x_validate = np.array(x_validate)
- x_train = (x_train)/255
- x_validate = (x_validate)/255
- y_train = np.array(y_train)
- y_validate = np.array(y_validate)
- return x_train, y_train, x_validate, y_validate, size
- x_train, y_train, x_validate, y_validate, size = dawaj()
- classifier = Sequential([
- Conv2D(32, (5, 5), input_shape=(size[0], size[1], 3), activation='relu'),
- MaxPooling2D(pool_size=(2, 2)),
- Conv2D(32, (5, 5), activation='sigmoid'),
- MaxPooling2D(pool_size=(2, 2)),
- Flatten(),
- Dense(units=128, activation='sigmoid'),
- Dropout(rate=0.4),
- Dense(units=2, activation='sigmoid')
- ])
- classifier.compile('adadelta', 'mse', metrics=['accuracy'])
- # classifier.compile('adam', 'mse', metrics=['accuracy'])
- #classifier = load_model('model_lab3.h5')
- classifier.fit(x_train, y_train, epochs=40, validation_data=(x_validate, y_validate))
- classifier.save('model_lab3.h5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement